diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 4240c3d..1f45566 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -80,9 +80,9 @@ func Keys[M ~map[K]V, K comparable, V any](m M) []K { return r } -func DedupStringSlice[T string](strings []T) []T { - keys := make(map[T]bool) - var list []T +func DedupStringSlice(strings []string) []string { + keys := make(map[string]bool) + var list []string for _, item := range strings { if _, value := keys[item]; !value { keys[item] = true diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index 768d954..b6491c3 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -15,3 +15,13 @@ func TestObfuscate(t *testing.T) { assert.Equal(t, "****-text", Obfuscate("test-text", 5)) assert.Equal(t, "****", Obfuscate("test", 6)) } + +func TestDedupStringSlice(t *testing.T) { + assert.Equal(t, []string{"a", "b"}, DedupStringSlice([]string{"a", "b", "b", "a"})) + assert.Equal(t, []string{"a", "b"}, DedupStringSlice([]string{"a", "b"})) +} + +func TestKeys(t *testing.T) { + assert.Contains(t, Keys(map[string]int{"a": 1, "b": 2}), "a") + assert.Contains(t, Keys(map[string]int{"a": 1, "b": 2}), "b") +} diff --git a/log/logger.go b/log/logger.go index e5361ed..5cb6b18 100644 --- a/log/logger.go +++ b/log/logger.go @@ -10,7 +10,7 @@ import ( type Level int type Logger interface { - GetLevel() configcat.LogLevel // for the SDK + GetConfigCatLevel() configcat.LogLevel // for the SDK Level() Level @@ -82,7 +82,7 @@ func (l *logger) WithPrefix(prefix string) Logger { } } -func (l *logger) GetLevel() configcat.LogLevel { +func (l *logger) GetConfigCatLevel() configcat.LogLevel { switch l.level { case Debug: return configcat.LogLevelDebug @@ -92,6 +92,8 @@ func (l *logger) GetLevel() configcat.LogLevel { return configcat.LogLevelWarn case Error: return configcat.LogLevelError + case None: + return configcat.LogLevelNone default: return configcat.LogLevelWarn } diff --git a/log/logger_test.go b/log/logger_test.go index 75d8540..e5a1389 100644 --- a/log/logger_test.go +++ b/log/logger_test.go @@ -2,6 +2,8 @@ package log import ( "bytes" + "fmt" + configcat "github.com/configcat/go-sdk/v9" "github.com/stretchr/testify/assert" "testing" ) @@ -98,4 +100,29 @@ func TestLogger(t *testing.T) { l.Errorf("error") l.Reportf("rep") }) + t.Run("debug logger", func(t *testing.T) { + l := NewDebugLogger() + assert.Equal(t, Debug, l.Level()) + }) + t.Run("sdk loglevel", func(t *testing.T) { + tests := []struct { + level Level + configCatLevel configcat.LogLevel + }{ + {Error, configcat.LogLevelError}, + {Warn, configcat.LogLevelWarn}, + {Info, configcat.LogLevelInfo}, + {Debug, configcat.LogLevelDebug}, + {None, configcat.LogLevelNone}, + {500, configcat.LogLevelWarn}, + } + + for _, test := range tests { + t.Run(fmt.Sprintf("%v == %v", test.level, test.configCatLevel), func(t *testing.T) { + var out, err bytes.Buffer + l := NewLogger(&err, &out, test.level) + assert.Equal(t, test.configCatLevel, l.GetConfigCatLevel()) + }) + } + }) } diff --git a/sdk/sdk.go b/sdk/sdk.go index 61665dc..6b579d6 100644 --- a/sdk/sdk.go +++ b/sdk/sdk.go @@ -108,7 +108,7 @@ func NewClient(sdkCtx *Context, log log.Logger) Client { SDKKey: key, DataGovernance: configcat.Global, Logger: sdkLog, - LogLevel: sdkLog.GetLevel(), + LogLevel: sdkLog.GetConfigCatLevel(), Transport: OverrideUserAgent(transport), Hooks: &configcat.Hooks{}, }