Skip to content

Commit 74e6883

Browse files
authored
fix: panic when reading a config with a function type (#37)
1 parent 15980f4 commit 74e6883

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

go.mod

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@ module github.com/zencoder/go-remote-config
22

33
go 1.13
44

5-
require github.com/stretchr/testify v0.0.0-20151102014159-c478a808a1b3
5+
require (
6+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
7+
github.com/kr/text v0.2.0 // indirect
8+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
9+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
10+
github.com/stretchr/testify v1.8.4
11+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
12+
)

go.sum

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1-
github.com/stretchr/testify v0.0.0-20151102014159-c478a808a1b3 h1:jU+Ex9dS6B1VdPZssLnmiZDt/VzsutuuvLNKq8Wl9U0=
2-
github.com/stretchr/testify v0.0.0-20151102014159-c478a808a1b3/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
5+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
7+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
8+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
9+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
10+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
11+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
14+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
16+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
17+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
18+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
19+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
20+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
21+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
22+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
23+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
24+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
25+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
27+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

remoteconfig.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func ReadJSONValidate(cfgReader io.Reader, configStruct interface{}) error {
5353

5454
func isNilFixed(v reflect.Value) bool {
5555
switch v.Kind() {
56-
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
57-
//use of IsNil method
56+
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice, reflect.Func:
57+
// use of IsNil method
5858
return v.IsNil()
5959
}
6060
return false

remoteconfig_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1415
"github.com/stretchr/testify/suite"
1516
)
1617

@@ -644,6 +645,34 @@ func (s *RemoteConfigSuite) TestReadJSONValidate() {
644645
assert.Nil(s.T(), err)
645646
}
646647

648+
func (s *RemoteConfigSuite) TestReadJSONValidateWithFunctionType() {
649+
type ConfigWithFunc struct {
650+
MaxEntries *int `json:"max_entries" remoteconfig:"optional"`
651+
OnEvicted func(interface{}) `json:"_" remoteconfig:"optional"`
652+
}
653+
654+
i := 5
655+
tests := []struct {
656+
name string
657+
in string
658+
out *ConfigWithFunc
659+
}{
660+
{name: "no config", in: `{}`},
661+
{name: "empty config", in: `{"cfg": {}}`, out: &ConfigWithFunc{}},
662+
{name: "values in config", in: `{"cfg": {"max_entries": 5}}`, out: &ConfigWithFunc{MaxEntries: &i}},
663+
}
664+
665+
for _, test := range tests {
666+
s.T().Run(test.name, func(t *testing.T) {
667+
var found struct {
668+
Cfg *ConfigWithFunc `json:"cfg" remoteconfig:"optional"`
669+
}
670+
require.NoError(t, ReadJSONValidate(strings.NewReader(test.in), &found))
671+
assert.Equal(t, test.out, found.Cfg)
672+
})
673+
}
674+
}
675+
647676
func (s *RemoteConfigSuite) TestReadJSONParseEmbeddedStruct() {
648677
cfgBuffer := bytes.NewBufferString(validConfigJSON)
649678

0 commit comments

Comments
 (0)