From c9eba1a5bb737c9b2b5d793f6e5ceda9b976d9b1 Mon Sep 17 00:00:00 2001 From: Gladkov Alexey Date: Tue, 14 Nov 2017 15:16:02 +0100 Subject: [PATCH 1/2] Fix the pointer initialization If the overwriteStruct() finds an uninitialized pointer, it tries to initialize it, but does it incorrectly. It tries to assign a pointer to pointer, instead of pointer. Signed-off-by: Gladkov Alexey --- configuration/parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configuration/parser.go b/configuration/parser.go index b46f7326..3e9d587f 100644 --- a/configuration/parser.go +++ b/configuration/parser.go @@ -220,7 +220,7 @@ func (p *Parser) overwriteStruct(v reflect.Value, fullpath string, path []string } case reflect.Ptr: if field.IsNil() { - field.Set(reflect.New(sf.Type)) + field.Set(reflect.New(field.Type().Elem())) } } From e69837454adaffbcadfb11bb795db496a0cbb4ad Mon Sep 17 00:00:00 2001 From: Gladkov Alexey Date: Wed, 15 Nov 2017 16:37:43 +0100 Subject: [PATCH 2/2] Add tests for configuration parser Signed-off-by: Gladkov Alexey --- configuration/parser_test.go | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 configuration/parser_test.go diff --git a/configuration/parser_test.go b/configuration/parser_test.go new file mode 100644 index 00000000..344b2ee4 --- /dev/null +++ b/configuration/parser_test.go @@ -0,0 +1,70 @@ +package configuration + +import ( + "os" + "reflect" + + . "gopkg.in/check.v1" +) + +type localConfiguration struct { + Version Version `yaml:"version"` + Log *Log `yaml:"log"` +} + +type Log struct { + Formatter string `yaml:"formatter,omitempty"` +} + +var expectedConfig = localConfiguration{ + Version: "0.1", + Log: &Log{ + Formatter: "json", + }, +} + +type ParserSuite struct{} + +var _ = Suite(new(ParserSuite)) + +func (suite *ParserSuite) TestParserOverwriteIninitializedPoiner(c *C) { + config := localConfiguration{} + + os.Setenv("REGISTRY_LOG_FORMATTER", "json") + defer os.Unsetenv("REGISTRY_LOG_FORMATTER") + + p := NewParser("registry", []VersionedParseInfo{ + { + Version: "0.1", + ParseAs: reflect.TypeOf(config), + ConversionFunc: func(c interface{}) (interface{}, error) { + return c, nil + }, + }, + }) + + err := p.Parse([]byte(`{version: "0.1", log: {formatter: "text"}}`), &config) + c.Assert(err, IsNil) + c.Assert(config, DeepEquals, expectedConfig) +} + +func (suite *ParserSuite) TestParseOverwriteUnininitializedPoiner(c *C) { + config := localConfiguration{} + + os.Setenv("REGISTRY_LOG_FORMATTER", "json") + defer os.Unsetenv("REGISTRY_LOG_FORMATTER") + + p := NewParser("registry", []VersionedParseInfo{ + { + Version: "0.1", + ParseAs: reflect.TypeOf(config), + ConversionFunc: func(c interface{}) (interface{}, error) { + return c, nil + }, + }, + }) + + err := p.Parse([]byte(`{version: "0.1"}`), &config) + c.Assert(err, IsNil) + c.Assert(config, DeepEquals, expectedConfig) +}