Skip to content

Commit 8e98c65

Browse files
jmluangjmluangunknwon
authored
fix reflect from struct with multiple shadow keys (#254)
Co-authored-by: jmluang <[email protected]> Co-authored-by: ᴜɴᴋɴᴡᴏɴ <[email protected]>
1 parent f1d9ab1 commit 8e98c65

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

struct.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ func reflectSliceWithProperType(key *Key, field reflect.Value, delim string, all
479479
_ = keyWithShadows.AddShadow(val)
480480
}
481481
}
482-
key = keyWithShadows
482+
*key = *keyWithShadows
483483
return nil
484484
}
485485

struct_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,29 @@ path = /tmp/gpm-profiles/test5.profile
685685
path = /tmp/gpm-profiles/test1.profile
686686
687687
`)
688+
689+
Convey("Reflect from struct with shadows", func() {
690+
cfg := ini.Empty(ini.LoadOptions{
691+
AllowShadows: true,
692+
})
693+
type ShadowStruct struct {
694+
StringArray []string `ini:"sa,,allowshadow"`
695+
EmptyStringArrat []string `ini:"empty,omitempty,allowshadow"`
696+
Allowshadow []string `ini:"allowshadow,,allowshadow"`
697+
}
698+
699+
So(ini.ReflectFrom(cfg, &ShadowStruct{StringArray: []string{"s1", "s2"}, Allowshadow: []string{"s3", "s4"}}), ShouldBeNil)
700+
701+
var buf bytes.Buffer
702+
_, err := cfg.WriteTo(&buf)
703+
So(err, ShouldBeNil)
704+
So(buf.String(), ShouldEqual, `sa = s1
705+
sa = s2
706+
allowshadow = s3
707+
allowshadow = s4
708+
709+
`)
710+
})
688711
})
689712
}
690713

0 commit comments

Comments
 (0)