Skip to content

Commit

Permalink
Merge pull request #42 from leonidasdeim/usage-improvements
Browse files Browse the repository at this point in the history
Change of masked function signature
  • Loading branch information
leodeim authored Dec 22, 2023
2 parents c7cc35a + e628239 commit 4d46eda
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
8 changes: 4 additions & 4 deletions cog.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type Subscriber[T any] func(T) error
type Callback[T any] func(T)
type MaskFn[T any] func(T) T
type MaskFn[T any] func(*T)

type C[T any] struct {
lock sync.Mutex
Expand Down Expand Up @@ -148,11 +148,11 @@ func (cog *C[T]) Config() T {
return cog.config
}

func (cog *C[T]) String(mask ...MaskFn[T]) (string, error) {
func (cog *C[T]) String(masks ...MaskFn[T]) (string, error) {
data := cog.Config()

if len(mask) > 0 {
data = mask[0](data)
for _, mask := range masks {
mask(&data)
}

b, err := json.MarshalIndent(data, "", " ")
Expand Down
18 changes: 14 additions & 4 deletions cog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,18 +471,17 @@ func (s *testSuite) TestFileHandlerUpdateFail() {
assert.ErrorContainsf(s.T(), err, "filehandler error", "not a filehandler error")
}

func (s *testSuite) TestStringMask() {
func (s *testSuite) TestString() {
c, err := setup(s.T(), fmt.Sprintf(defaultConfig, string(s.testCase.Type)), "", s.testCase.Type, s.testCase.TestString)
require.NoErrorf(s.T(), err, testSetupErrorMsg)

got := c.Config()
assert.Equalf(s.T(), testData, got, expectedResultErrorMsg)

str, err := c.String(func(tc testConfig) testConfig {
str, err := c.String(func(tc *testConfig) {
tc.Name = "[masked]"
return tc
})
require.NoErrorf(s.T(), err, "filehandler should not return error")
require.NoErrorf(s.T(), err, "string method should not return error")

strExpected := `{
"Name": "[masked]",
Expand All @@ -491,4 +490,15 @@ func (s *testSuite) TestStringMask() {
}`

assert.Equal(s.T(), strExpected, str)

str, err = c.String()
require.NoErrorf(s.T(), err, "string method should not return error")

strExpected = `{
"Name": "config_test",
"Version": 123,
"IsPrefork": true
}`

assert.Equal(s.T(), strExpected, str)
}

0 comments on commit 4d46eda

Please sign in to comment.