fix reflect from struct with multiple shadow keys (#254)

Co-authored-by: jmluang <350216438@qq.com>
Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
This commit is contained in:
orqzsf1
2020-08-16 19:20:02 +08:00
committed by GitHub
co-authored by jmluang ᴜɴᴋɴᴡᴏɴ
parent f1d9ab1fbe
commit 8e98c6523e
2 changed files with 24 additions and 1 deletions
+1 -1
View File
@@ -479,7 +479,7 @@ func reflectSliceWithProperType(key *Key, field reflect.Value, delim string, all
_ = keyWithShadows.AddShadow(val)
}
}
key = keyWithShadows
*key = *keyWithShadows
return nil
}
+23
View File
@@ -685,6 +685,29 @@ path = /tmp/gpm-profiles/test5.profile
path = /tmp/gpm-profiles/test1.profile
`)
Convey("Reflect from struct with shadows", func() {
cfg := ini.Empty(ini.LoadOptions{
AllowShadows: true,
})
type ShadowStruct struct {
StringArray []string `ini:"sa,,allowshadow"`
EmptyStringArrat []string `ini:"empty,omitempty,allowshadow"`
Allowshadow []string `ini:"allowshadow,,allowshadow"`
}
So(ini.ReflectFrom(cfg, &ShadowStruct{StringArray: []string{"s1", "s2"}, Allowshadow: []string{"s3", "s4"}}), ShouldBeNil)
var buf bytes.Buffer
_, err := cfg.WriteTo(&buf)
So(err, ShouldBeNil)
So(buf.String(), ShouldEqual, `sa = s1
sa = s2
allowshadow = s3
allowshadow = s4
`)
})
})
}