Skip to content

Commit daba868

Browse files
committed
fix: panic when reading a config with a function type
1 parent 15980f4 commit daba868

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

go.mod

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@ 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/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
8+
github.com/kr/text v0.2.0 // indirect
9+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
10+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
11+
github.com/stretchr/testify v1.8.4
12+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
13+
)

go.sum

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
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/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
7+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
8+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
9+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
10+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
11+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
12+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
13+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
14+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
16+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
19+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
20+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
21+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
22+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
23+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
24+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
25+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
26+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
27+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
28+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
29+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

remoteconfig.go

Lines changed: 2 additions & 2 deletions
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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"github.com/golang/groupcache/lru"
1314
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
1416
"github.com/stretchr/testify/suite"
1517
)
1618

@@ -644,6 +646,34 @@ func (s *RemoteConfigSuite) TestReadJSONValidate() {
644646
assert.Nil(s.T(), err)
645647
}
646648

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

0 commit comments

Comments
 (0)