@@ -3900,6 +3900,58 @@ func TestDecode_structArrayDeepMap(t *testing.T) {
39003900 }
39013901}
39023902
3903+ func TestDecoder_RootName (t * testing.T ) {
3904+ t .Parallel ()
3905+
3906+ input := map [string ]any {
3907+ "surname" : "green" ,
3908+ "relation" : map [string ]any {
3909+ "surname" : "black" ,
3910+ },
3911+ }
3912+
3913+ var result struct {
3914+ Name string `mapstructure:"name"`
3915+ Relation struct {
3916+ Name string `mapstructure:"name"`
3917+ } `mapstructure:"relation"`
3918+ }
3919+
3920+ decoder , err := NewDecoder (& DecoderConfig {
3921+ ErrorUnset : true ,
3922+ ErrorUnused : true ,
3923+ Result : & result ,
3924+ RootName : "root" ,
3925+ })
3926+ if err != nil {
3927+ t .Fatalf ("err: %s" , err )
3928+ }
3929+
3930+ err = decoder .Decode (input )
3931+ if err == nil {
3932+ t .Fatal ("expected error" )
3933+ }
3934+
3935+ expectedErrors := []string {
3936+ `'root.relation' has invalid keys: surname` ,
3937+ `'root.relation' has unset fields: name` ,
3938+ `'root' has invalid keys: surname` ,
3939+ `'root' has unset fields: name` ,
3940+ }
3941+
3942+ failed := false
3943+
3944+ for _ , expectedErr := range expectedErrors {
3945+ if ! strings .Contains (err .Error (), expectedErr ) {
3946+ failed = true
3947+ }
3948+ }
3949+
3950+ if failed {
3951+ t .Errorf ("unexpected error message, got: %s" , err .Error ())
3952+ }
3953+ }
3954+
39033955func stringPtr (v string ) * string { return & v }
39043956func intPtr (v int ) * int { return & v }
39053957func uintPtr (v uint ) * uint { return & v }
0 commit comments