diff --git a/go.mod b/go.mod index b480a4e..7292dc5 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,11 @@ module github.com/zencoder/go-remote-config go 1.13 -require github.com/stretchr/testify v0.0.0-20151102014159-c478a808a1b3 +require ( + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/kr/text v0.2.0 // indirect + github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + github.com/stretchr/testify v1.8.4 + gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect +) diff --git a/go.sum b/go.sum index dd9c668..86c8eaa 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,27 @@ -github.com/stretchr/testify v0.0.0-20151102014159-c478a808a1b3 h1:jU+Ex9dS6B1VdPZssLnmiZDt/VzsutuuvLNKq8Wl9U0= -github.com/stretchr/testify v0.0.0-20151102014159-c478a808a1b3/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +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/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/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/remoteconfig.go b/remoteconfig.go index 22374ed..b632eec 100644 --- a/remoteconfig.go +++ b/remoteconfig.go @@ -53,8 +53,8 @@ func ReadJSONValidate(cfgReader io.Reader, configStruct interface{}) error { func isNilFixed(v reflect.Value) bool { switch v.Kind() { - case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice: - //use of IsNil method + case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice, reflect.Func: + // use of IsNil method return v.IsNil() } return false diff --git a/remoteconfig_test.go b/remoteconfig_test.go index b768661..714b4af 100644 --- a/remoteconfig_test.go +++ b/remoteconfig_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -644,6 +645,34 @@ func (s *RemoteConfigSuite) TestReadJSONValidate() { assert.Nil(s.T(), err) } +func (s *RemoteConfigSuite) TestReadJSONValidateWithFunctionType() { + type ConfigWithFunc struct { + MaxEntries *int `json:"max_entries" remoteconfig:"optional"` + OnEvicted func(interface{}) `json:"_" remoteconfig:"optional"` + } + + i := 5 + tests := []struct { + name string + in string + out *ConfigWithFunc + }{ + {name: "no config", in: `{}`}, + {name: "empty config", in: `{"cfg": {}}`, out: &ConfigWithFunc{}}, + {name: "values in config", in: `{"cfg": {"max_entries": 5}}`, out: &ConfigWithFunc{MaxEntries: &i}}, + } + + for _, test := range tests { + s.T().Run(test.name, func(t *testing.T) { + var found struct { + Cfg *ConfigWithFunc `json:"cfg" remoteconfig:"optional"` + } + require.NoError(t, ReadJSONValidate(strings.NewReader(test.in), &found)) + assert.Equal(t, test.out, found.Cfg) + }) + } +} + func (s *RemoteConfigSuite) TestReadJSONParseEmbeddedStruct() { cfgBuffer := bytes.NewBufferString(validConfigJSON)