Skip to content

Commit

Permalink
feat: [CN-41] reuse and embed formatter/parser config in censor.Config (
Browse files Browse the repository at this point in the history
  • Loading branch information
vpakhuchyi committed Feb 3, 2024
1 parent f98f7a0 commit 73ce691
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 76 deletions.
40 changes: 8 additions & 32 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"os"

"gopkg.in/yaml.v3"

"github.com/vpakhuchyi/censor/internal/formatter"
"github.com/vpakhuchyi/censor/internal/parser"
)

// DefaultMaskValue is used to mask struct fields by default.
const DefaultMaskValue = "[CENSORED]"

// Config describes the available parser.Parser and formatter.Formatter configuration.
type Config struct {
General General `yaml:"general"`
Parser ParserConfig `yaml:"parser"`
Formatter FormatterConfig `yaml:"formatter"`
General General `yaml:"general"`
Parser parser.Config `yaml:"parser"`
Formatter formatter.Config `yaml:"formatter"`
}

// General describes general configuration settings.
Expand All @@ -25,40 +28,13 @@ type General struct {
PrintConfigOnInit bool `yaml:"print-config-on-init"`
}

// ParserConfig describes parser.Parser configuration.
type ParserConfig struct {
// UseJSONTagName sets whether to use the `json` tag to get the name of the struct field.
// If no `json` tag is present, the name of the struct field is used.
UseJSONTagName bool `yaml:"use-json-tag-name"`
}

// FormatterConfig describes formatter.Formatter configuration.
type FormatterConfig struct {
// MaskValue is used to mask struct fields with sensitive data.
// The default value is stored in DefaultMaskValue constant.
MaskValue string `yaml:"mask-value"`
// DisplayPointerSymbol is used to display '&' (pointer symbol) in the output.
// The default value is false.
DisplayPointerSymbol bool `yaml:"display-pointer-symbol"`
// DisplayStructName is used to display struct name in the output.
// A struct name includes the last part of the package path.
// The default value is false.
DisplayStructName bool `yaml:"display-struct-name"`
// DisplayMapType is used to display map type in the output.
// The default value is false.
DisplayMapType bool `yaml:"display-map-type"`
// ExcludePatterns contains regexp patterns that are used for the selection
// of strings that must be masked.
ExcludePatterns []string `yaml:"exclude-patterns"`
}

// Default returns a default configuration.
func Default() Config {
return Config{
Parser: ParserConfig{
Parser: parser.Config{
UseJSONTagName: false,
},
Formatter: FormatterConfig{
Formatter: formatter.Config{
MaskValue: DefaultMaskValue,
DisplayPointerSymbol: false,
DisplayStructName: false,
Expand Down
15 changes: 9 additions & 6 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

"github.com/vpakhuchyi/censor/internal/formatter"
"github.com/vpakhuchyi/censor/internal/parser"
)

func TestDefault(t *testing.T) {
Expand All @@ -14,10 +17,10 @@ func TestDefault(t *testing.T) {
General: General{
PrintConfigOnInit: true,
},
Parser: ParserConfig{
Parser: parser.Config{
UseJSONTagName: false,
},
Formatter: FormatterConfig{
Formatter: formatter.Config{
MaskValue: DefaultMaskValue,
DisplayPointerSymbol: false,
DisplayStructName: false,
Expand Down Expand Up @@ -46,10 +49,10 @@ func TestFromFile(t *testing.T) {
General: General{
PrintConfigOnInit: true,
},
Parser: ParserConfig{
Parser: parser.Config{
UseJSONTagName: true,
},
Formatter: FormatterConfig{
Formatter: formatter.Config{
MaskValue: "[CENSORED]",
DisplayPointerSymbol: true,
DisplayStructName: true,
Expand Down Expand Up @@ -121,10 +124,10 @@ func TestYAMLMarshal(t *testing.T) {
General: General{
PrintConfigOnInit: true,
},
Parser: ParserConfig{
Parser: parser.Config{
UseJSONTagName: true,
},
Formatter: FormatterConfig{
Formatter: formatter.Config{
MaskValue: "[CENSORED]",
DisplayPointerSymbol: true,
DisplayStructName: true,
Expand Down
15 changes: 9 additions & 6 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"time"

"github.com/stretchr/testify/require"

"github.com/vpakhuchyi/censor/internal/formatter"
"github.com/vpakhuchyi/censor/internal/parser"
)

func Test_InstanceFormatPrimitives(t *testing.T) {
Expand Down Expand Up @@ -41,13 +44,13 @@ func Test_InstanceConfiguration(t *testing.T) {

t.Run("with_provided_configuration", func(t *testing.T) {
c := Config{
Formatter: FormatterConfig{
Formatter: formatter.Config{
MaskValue: "[redacted]",
DisplayStructName: true,
DisplayMapType: false,
ExcludePatterns: nil,
},
Parser: ParserConfig{
Parser: parser.Config{
UseJSONTagName: true,
},
}
Expand Down Expand Up @@ -93,13 +96,13 @@ func Test_GlobalInstanceConfiguration(t *testing.T) {
t.Cleanup(func() { SetGlobalInstance(New()) })

c := Config{
Formatter: FormatterConfig{
Formatter: formatter.Config{
MaskValue: "[redacted]",
DisplayStructName: true,
DisplayMapType: false,
ExcludePatterns: nil,
},
Parser: ParserConfig{
Parser: parser.Config{
UseJSONTagName: true,
},
}
Expand All @@ -126,7 +129,7 @@ func Test_SetGlobalInstance(t *testing.T) {
t.Cleanup(func() { SetGlobalInstance(New()) })

p := NewWithConfig(Config{
Formatter: FormatterConfig{
Formatter: formatter.Config{
MaskValue: "[censored]",
},
})
Expand All @@ -146,7 +149,7 @@ func TestExcludePatterns(t *testing.T) {
t.Cleanup(func() { SetGlobalInstance(New()) })

p := NewWithConfig(Config{
Formatter: FormatterConfig{
Formatter: formatter.Config{
MaskValue: "[CENSORED]",
ExcludePatterns: []string{`\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b`},
},
Expand Down
32 changes: 4 additions & 28 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,9 @@ var globalInstance = New()
func New() *Processor {
c := Default()

fConfig := formatter.Config{
MaskValue: c.Formatter.MaskValue,
DisplayPointerSymbol: c.Formatter.DisplayPointerSymbol,
DisplayStructName: c.Formatter.DisplayStructName,
DisplayMapType: c.Formatter.DisplayMapType,
ExcludePatterns: c.Formatter.ExcludePatterns,
}

pConfig := parser.Config{
UseJSONTagName: c.Parser.UseJSONTagName,
}

p := Processor{
formatter: formatter.New(fConfig),
parser: parser.New(pConfig),
formatter: formatter.New(c.Formatter),
parser: parser.New(c.Parser),
cfg: c,
}

Expand All @@ -53,21 +41,9 @@ func New() *Processor {

// NewWithConfig returns a new instance of Processor with given configuration.
func NewWithConfig(c Config) *Processor {
fConfig := formatter.Config{
MaskValue: c.Formatter.MaskValue,
DisplayPointerSymbol: c.Formatter.DisplayPointerSymbol,
DisplayStructName: c.Formatter.DisplayStructName,
DisplayMapType: c.Formatter.DisplayMapType,
ExcludePatterns: c.Formatter.ExcludePatterns,
}

pConfig := parser.Config{
UseJSONTagName: c.Parser.UseJSONTagName,
}

p := Processor{
formatter: formatter.New(fConfig),
parser: parser.New(pConfig),
formatter: formatter.New(c.Formatter),
parser: parser.New(c.Parser),
cfg: c,
}

Expand Down
8 changes: 4 additions & 4 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,10 @@ func TestNewWithConfig(t *testing.T) {
General: General{
PrintConfigOnInit: true,
},
Parser: ParserConfig{
Parser: parser.Config{
UseJSONTagName: false,
},
Formatter: FormatterConfig{
Formatter: formatter.Config{
MaskValue: "####",
DisplayPointerSymbol: false,
DisplayStructName: false,
Expand Down Expand Up @@ -808,10 +808,10 @@ func TestProcessor_PrintConfig(t *testing.T) {
General: General{
PrintConfigOnInit: true,
},
Parser: ParserConfig{
Parser: parser.Config{
UseJSONTagName: false,
},
Formatter: FormatterConfig{
Formatter: formatter.Config{
MaskValue: DefaultMaskValue,
DisplayPointerSymbol: true,
DisplayStructName: true,
Expand Down

0 comments on commit 73ce691

Please sign in to comment.