Skip to content

Commit

Permalink
transform: test flatten mangler with nil subfields
Browse files Browse the repository at this point in the history
I suspect that we're not handling nested pointer-types correctly in the
FlattenMangler. This adds a test that fails because unmangle's creating
empty intermediate structs.
  • Loading branch information
dfinkel committed May 16, 2024
1 parent 6d7d1f9 commit 23d1281
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions transform/flatten_mangler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,65 @@ func TestFlattenMangler(t *testing.T) {
assert.Equal(t, st, i)
},
},
{
name: "nil nested struct",
testStruct: b,
modify: func(t testing.TB, val reflect.Value) {

expectedDialsTags := []string{
"config_field_Name",
"config_field_Foobar_Location",
"config_field_Foobar_Coordinates",
"config_field_Foobar_some_time",
"config_field_AnotherField",
}

expectedFieldTags := []string{
"ConfigField,Name",
"ConfigField,Foobar,Location",
"ConfigField,Foobar,Coordinates",
"ConfigField,Foobar,SomeTime",
"ConfigField,AnotherField",
}

for i := 0; i < val.Type().NumField(); i++ {
assert.EqualValues(t, expectedDialsTags[i], val.Type().Field(i).Tag.Get(common.DialsTagName))
assert.EqualValues(t, expectedFieldTags[i], val.Type().Field(i).Tag.Get(dialsFieldPathTag))
if val.Type().Kind() != reflect.Pointer {
t.Errorf("field %d has kind %s, not %s", i, val.Type().Kind(), reflect.Pointer)
}
}

s1 := "test"
i2 := 42

val.Field(0).Set(reflect.ValueOf(&s1))
val.Field(1).Set(reflect.Zero(reflect.TypeOf((*string)(nil))))
val.Field(2).Set(reflect.Zero(reflect.TypeOf((*int)(nil))))
val.Field(3).Set(reflect.Zero(reflect.TypeOf((*time.Duration)(nil))))
val.Field(4).Set(reflect.ValueOf(&i2))
},
assertion: func(t testing.TB, i interface{}) {
// all the fields are pointerified because of call to Pointerify
s1 := "test"
i2 := 42
b := struct {
Name *string `dials:"Name"`
Foobar *struct {
Location *string `dials:"Location"`
Coordinates *int `dials:"Coordinates"`
SomeTime *time.Duration
} `dials:"Foobar"`
AnotherField *int `dials:"AnotherField"`
}{
Name: &s1,
Foobar: nil,
AnotherField: &i2,
}

assert.EqualValues(t, &b, i)
},
},
{
name: "multilevel nested struct",
testStruct: b,
Expand Down

0 comments on commit 23d1281

Please sign in to comment.