From 448edf8970e3fbcacd2c386a99a22d6832ff5c1c Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Wed, 18 Sep 2019 14:53:58 +0500 Subject: [PATCH] Proper interface value obtaining and more tests. --- compose_tree.go | 10 +++++++--- parse_env_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/compose_tree.go b/compose_tree.go index 7b19500..19307a0 100644 --- a/compose_tree.go +++ b/compose_tree.go @@ -25,9 +25,13 @@ func composeTree(value reflect.Value, prefix string) { // If currently processed field - interface, then we should // get underlying value. - //if fieldToProcess.Kind() == reflect.Interface { - // fieldToProcess = fieldToProcess.Elem() - //} + if fieldToProcess.Kind() == reflect.Interface { + if fieldToProcess.Elem().Kind() == reflect.Ptr { + fieldToProcess = fieldToProcess.Elem().Elem() + } else if fieldToProcess.Elem().Kind() == reflect.Struct { + fieldToProcess = fieldToProcess.Elem() + } + } // In 99% of cases we will get uninitialized things we should // initialize. diff --git a/parse_env_test.go b/parse_env_test.go index fe4c49b..659d80e 100644 --- a/parse_env_test.go +++ b/parse_env_test.go @@ -766,3 +766,30 @@ func TestParseStructWithInterfaceFields(t *testing.T) { os.Unsetenv("DATA") os.Unsetenv(debugFlagEnvName) } + +func TestParseStructWitStructAsInterface(t *testing.T) { + type testStruct struct { + Data interface{} + Int int + } + type testUnderlyingStruct struct { + Data string + } + + os.Setenv(debugFlagEnvName, "true") + os.Setenv("INT", "64") + os.Setenv("DATA_DATA", "Test data") + + testCase := &testStruct{} + testUnderlyingCase := &testUnderlyingStruct{} + testCase.Data = testUnderlyingCase + err := Parse(testCase, nil) + + require.Nil(t, err) + require.Equal(t, testCase.Int, 64) + require.Equal(t, testCase.Data.(*testUnderlyingStruct).Data, "Test data") + + os.Unsetenv("INT") + os.Unsetenv("DATA_DATA") + os.Unsetenv(debugFlagEnvName) +}