From fcc6b58b019d49a8f3336554e03c318bd7ad216e Mon Sep 17 00:00:00 2001 From: samber Date: Tue, 9 Jul 2024 15:08:49 +0000 Subject: [PATCH] bump v0.17.1 --- attributes_test.go | 399 --------------------------------------------- go.mod | 14 +- go.sum | 23 --- main_test.go | 11 -- 4 files changed, 2 insertions(+), 445 deletions(-) delete mode 100644 attributes_test.go delete mode 100644 main_test.go diff --git a/attributes_test.go b/attributes_test.go deleted file mode 100644 index 949a8e8..0000000 --- a/attributes_test.go +++ /dev/null @@ -1,399 +0,0 @@ -package slogcommon - -import ( - "log/slog" - "testing" - - "github.com/stretchr/testify/assert" -) - -type testLogValuer struct { - name string - pass string -} - -func (t testLogValuer) LogValue() slog.Value { - return slog.GroupValue( - slog.String("name", t.name), - slog.String("password", "********"), - ) -} - -var stubLogValuer = testLogValuer{"userName", "password"} - -func TestReplaceAttrs(t *testing.T) { - t.Parallel() - is := assert.New(t) - - // no ReplaceAttr func - is.Equal( - []slog.Attr{slog.Bool("bool", true), slog.Int("int", 42)}, - ReplaceAttrs( - nil, - []string{"foobar"}, - slog.Bool("bool", true), slog.Int("int", 42), - ), - ) - - // no ReplaceAttr func, but convert struct with interface slog.LogValue in slog.Group - is.Equal( - []slog.Attr{slog.Group("user", slog.String("name", stubLogValuer.name), slog.String("password", "********"))}, - ReplaceAttrs( - nil, - []string{"foobar"}, - slog.Any("user", stubLogValuer), - ), - ) - - // ReplaceAttr func, but convert struct with interface slog.LogValue in slog.Group - is.Equal( - []slog.Attr{slog.Group("user", slog.String("name", stubLogValuer.name), slog.String("password", "********"))}, - ReplaceAttrs( - func(groups []string, a slog.Attr) slog.Attr { - is.Equal([]string{"foobar", "user"}, groups) - return a - }, - []string{"foobar"}, - slog.Any("user", stubLogValuer), - ), - ) - - // ReplaceAttr func, but returns the same attributes - is.Equal( - []slog.Attr{slog.Bool("bool", true), slog.Int("int", 42)}, - ReplaceAttrs( - func(groups []string, a slog.Attr) slog.Attr { - is.Equal("foobar", groups[0]) - return a - }, - []string{"foobar"}, - slog.Bool("bool", true), slog.Int("int", 42), - ), - ) - - // Replace int and divide by 2 - is.Equal( - []slog.Attr{slog.Bool("bool", true), slog.Int("int", 21)}, - ReplaceAttrs( - func(groups []string, a slog.Attr) slog.Attr { - is.Equal("foobar", groups[0]) - if a.Value.Kind() == slog.KindInt64 { - a.Value = slog.Int64Value(a.Value.Int64() / 2) - } - return a - }, - []string{"foobar"}, - slog.Bool("bool", true), slog.Int("int", 42), - ), - ) - - // Remove int attr - is.Equal( - []slog.Attr{slog.Bool("bool", true), slog.Any("int", nil)}, - ReplaceAttrs( - func(groups []string, a slog.Attr) slog.Attr { - is.Equal("foobar", groups[0]) - if a.Value.Kind() == slog.KindInt64 { - return slog.Any("int", nil) - } - return a - }, - []string{"foobar"}, - slog.Bool("bool", true), slog.Int("int", 42), - ), - ) - - // Rename int attr - is.Equal( - []slog.Attr{slog.Bool("bool", true), slog.Int("int2", 21)}, - ReplaceAttrs( - func(groups []string, a slog.Attr) slog.Attr { - is.Equal("foobar", groups[0]) - if a.Value.Kind() == slog.KindInt64 { - return slog.Int("int2", 21) - } - return a - }, - []string{"foobar"}, - slog.Bool("bool", true), slog.Int("int", 42), - ), - ) - - // Rename attr in groups - is.Equal( - []slog.Attr{slog.Bool("bool", true), slog.Group("group1", slog.Group("group2", slog.Int("int", 21)))}, - ReplaceAttrs( - func(groups []string, a slog.Attr) slog.Attr { - is.Equal("foobar", groups[0]) - if len(groups) > 1 { - is.Equal([]string{"foobar", "group1", "group2"}, groups) - return slog.Int("int", 21) - } - return a - }, - []string{"foobar"}, - slog.Bool("bool", true), slog.Group("group1", slog.Group("group2", slog.String("string", "foobar"))), - ), - ) -} - -func TestAttrsToMap(t *testing.T) { - t.Parallel() - is := assert.New(t) - - // simple - is.EqualValues( - map[string]any{"key": "value"}, - AttrsToMap(slog.Any("key", "value")), - ) - - // nested - is.EqualValues( - map[string]any{"key": "value", "key1": map[string]any{"key2": "value2"}}, - AttrsToMap(slog.Any("key", "value"), slog.Group("key1", slog.Any("key2", "value2"))), - ) - - // merge - is.EqualValues( - map[string]any{"key": "value", "key1": map[string]any{"key2": "value2", "key3": "value3"}}, - AttrsToMap( - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.Group("key1", slog.Any("key3", "value3")), - ), - ) - -} - -func TestFindAttribute(t *testing.T) { - t.Parallel() - is := assert.New(t) - - // simple - attr, ok := FindAttribute( - []slog.Attr{ - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - }, - []string{}, - "key1", - ) - is.True(ok) - is.EqualValues(slog.Group("key1", slog.Any("key2", "value2")), attr) - - // return first - attr, ok = FindAttribute( - []slog.Attr{ - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.Group("key1", slog.Any("key3", "value3")), - }, - []string{}, - "key1", - ) - is.True(ok) - is.EqualValues(slog.Group("key1", slog.Any("key2", "value2")), attr) - - // not found - attr, ok = FindAttribute( - []slog.Attr{ - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.Group("key1", slog.Any("key3", "value3")), - }, - []string{}, - "key2", - ) - is.False(ok) - is.EqualValues(slog.Attr{}, attr) - - // nested - attr, ok = FindAttribute( - []slog.Attr{ - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.Group("key1", slog.Any("key3", "value3")), - }, - []string{"key1"}, - "key2", - ) - is.True(ok) - is.EqualValues(slog.Any("key2", "value2"), attr) - - // nested second - attr, ok = FindAttribute( - []slog.Attr{ - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.Group("key1", slog.Any("key3", "value3")), - }, - []string{"key1"}, - "key3", - ) - is.True(ok) - is.EqualValues(slog.Any("key3", "value3"), attr) - - // too nested - attr, ok = FindAttribute( - []slog.Attr{ - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.Group("key1", slog.Any("key3", "value3")), - }, - []string{"key1", "key2"}, - "key3", - ) - is.False(ok) - is.EqualValues(slog.Attr{}, attr) - - // nested not found - attr, ok = FindAttribute( - []slog.Attr{ - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.Group("key1", slog.Any("key3", "value3")), - }, - []string{"key4"}, - "key1", - ) - is.False(ok) - is.EqualValues(slog.Attr{}, attr) -} - -func TestExtractError(t *testing.T) { - t.Parallel() - is := assert.New(t) - - // not found - attrs, err := ExtractError( - []slog.Attr{ - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.String("foo", "bar"), - }, - "kerrorey1", - ) - is.Len(attrs, 3) - is.Nil(err) - - // found key but wrong type - attrs, err = ExtractError( - []slog.Attr{ - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.String("error", "bar"), - }, - "error", - ) - is.Len(attrs, 3) - is.Nil(err) - - // found start first key - attrs, err = ExtractError( - []slog.Attr{ - slog.Any("error", assert.AnError), - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.String("foo", "bar"), - }, - "error", - "err", - ) - is.Len(attrs, 3) - is.EqualError(err, assert.AnError.Error()) - - // found start second key - attrs, err = ExtractError( - []slog.Attr{ - slog.Any("err", assert.AnError), - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.String("foo", "bar"), - }, - "error", - "err", - ) - is.Len(attrs, 3) - is.EqualError(err, assert.AnError.Error()) - - // found middle - attrs, err = ExtractError( - []slog.Attr{ - slog.Any("key", "value"), - slog.Any("error", assert.AnError), - slog.Group("key1", slog.Any("key2", "value2")), - slog.String("foo", "bar"), - }, - "error", - ) - is.Len(attrs, 3) - is.EqualError(err, assert.AnError.Error()) - - // found end - attrs, err = ExtractError( - []slog.Attr{ - slog.Any("key", "value"), - slog.Group("key1", slog.Any("key2", "value2")), - slog.String("foo", "bar"), - slog.Any("error", assert.AnError), - }, - "error", - ) - is.Len(attrs, 3) - is.EqualError(err, assert.AnError.Error()) -} - -func TestRemoveEmptyAttrs(t *testing.T) { - t.Parallel() - is := assert.New(t) - - // do not remove anything - is.Equal( - []slog.Attr{slog.Bool("bool", true), slog.Int("int", 42)}, - RemoveEmptyAttrs( - []slog.Attr{slog.Bool("bool", true), slog.Int("int", 42)}, - ), - ) - is.Equal( - []slog.Attr{slog.Bool("bool", false), slog.Int("int", 42)}, - RemoveEmptyAttrs( - []slog.Attr{slog.Bool("bool", false), slog.Int("int", 42)}, - ), - ) - - // remove if missing keys - is.Equal( - []slog.Attr{slog.Int("int", 42)}, - RemoveEmptyAttrs( - []slog.Attr{slog.Bool("", true), slog.Int("int", 42)}, - ), - ) - - // remove if missing value - is.Equal( - []slog.Attr{slog.Int("int", 42)}, - RemoveEmptyAttrs( - []slog.Attr{slog.Any("test", nil), slog.Int("int", 42)}, - ), - ) - is.Equal( - []slog.Attr{slog.Int("int", 42)}, - RemoveEmptyAttrs( - []slog.Attr{slog.Group("test"), slog.Int("int", 42)}, - ), - ) - - // remove nested - is.Equal( - []slog.Attr{slog.Int("int", 42)}, - RemoveEmptyAttrs( - []slog.Attr{slog.Any("test", nil), slog.Int("int", 42)}, - ), - ) - is.Equal( - []slog.Attr{slog.Int("int", 42)}, - RemoveEmptyAttrs( - []slog.Attr{slog.Group("test", slog.Any("foobar", nil)), slog.Int("int", 42)}, - ), - ) -} diff --git a/go.mod b/go.mod index 3aa26a9..e4fbe72 100644 --- a/go.mod +++ b/go.mod @@ -2,16 +2,6 @@ module github.com/samber/slog-common go 1.21 -require ( - github.com/samber/lo v1.44.0 - github.com/stretchr/testify v1.8.0 - go.uber.org/goleak v1.2.1 -) +require github.com/samber/lo v1.44.0 -require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/text v0.16.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect -) +require golang.org/x/text v0.16.0 // indirect diff --git a/go.sum b/go.sum index 25491fd..d282ac7 100644 --- a/go.sum +++ b/go.sum @@ -1,27 +1,4 @@ -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/samber/lo v1.44.0 h1:5il56KxRE+GHsm1IR+sZ/6J42NODigFiqCWpSc2dybA= github.com/samber/lo v1.44.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= -go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main_test.go b/main_test.go deleted file mode 100644 index aeeff97..0000000 --- a/main_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package slogcommon - -import ( - "testing" - - "go.uber.org/goleak" -) - -func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) -}