Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mapstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,13 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
case reflect.Ptr:
if fieldVal.Type().Elem().Kind() == reflect.Struct {
if fieldVal.IsNil() {
if !fieldVal.CanSet() {
errs = append(errs, newDecodeError(
name+"."+fieldType.Name,
fmt.Errorf("unsupported type for squash: %s", fieldVal.Kind()),
))
continue
}
fieldVal.Set(reflect.New(fieldVal.Type().Elem()))
}
structs = append(structs, fieldVal.Elem())
Expand Down
27 changes: 27 additions & 0 deletions mapstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,33 @@ func TestDecode_EmbeddedPointerSquash_WithoutPreInitializedStructs_FromMapToStru
}
}

type EmbeddedUnexportedPointerSquash struct {
*embeddedUnexported `mapstructure:",squash"`
Vunique string
}

type embeddedUnexported struct {
Vstring string
}

func TestDecode_EmbeddedUnexportedPointerSquash_FromMapToStruct(t *testing.T) {
t.Parallel()

input := map[string]any{
"Vstring": "foo",
"Vunique": "bar",
}

result := EmbeddedUnexportedPointerSquash{}
err := Decode(input, &result)
if err == nil {
t.Fatal("expected an error decoding into an unexported embedded pointer squash, got nil")
}
if !strings.Contains(err.Error(), "unsupported type for squash") {
t.Fatalf("unexpected error message: %s", err)
}
}

func TestDecode_EmbeddedPointerSquashWithNestedMapstructure_FromStructToMap(t *testing.T) {
t.Parallel()

Expand Down