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 @@ -1363,6 +1363,13 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
fieldName = tagValue
}

if tagValue == "-" {
continue
}
if tagValue == "" && d.config.IgnoreUntaggedFields {
continue
}

rawMapKey := reflect.ValueOf(fieldName)
rawMapVal := dataVal.MapIndex(rawMapKey)
if !rawMapVal.IsValid() {
Expand Down
81 changes: 81 additions & 0 deletions mapstructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2789,6 +2789,87 @@ func testArrayInput(t *testing.T, input map[string]interface{}, expected *Array)
}

func stringPtr(v string) *string { return &v }

type BasicIgnored struct {
Vstring string `mapstructure:"-"`
Vint int
}

func TestDecoder_ErrorUnset_IgnoredField(t *testing.T) {
input := map[string]interface{}{
"Vint": 1,
}

var result BasicIgnored
err := Decode(&input, &result)
if err != nil {
t.Fatalf("got an error: %s", err)
}

if result.Vint != 1 {
t.Errorf("Vint should be 1: %d", result.Vint)
}
}

func TestDecoder_ErrorUnset_IgnoredFieldWithConfig(t *testing.T) {
input := map[string]interface{}{
"Vint": 1,
}

var result BasicIgnored
config := &DecoderConfig{
ErrorUnset: true,
Result: &result,
DecodeHook: DecodeHookFunc(func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
return data, nil
}),
}
decoder, err := NewDecoder(config)
if err != nil {
t.Fatalf("err: %s", err)
}
err = decoder.Decode(input)
if err != nil {
t.Fatalf("got an error: %s", err)
}

if result.Vint != 1 {
t.Errorf("Vint should be 1: %d", result.Vint)
}
}

type BasicIgnoredUntagged struct {
Vstring string
Vint int
}

func TestDecoder_ErrorUnset_IgnoredUntaggedField(t *testing.T) {
input := map[string]interface{}{
"Vint": 1,
}

var result BasicIgnoredUntagged
config := &DecoderConfig{
ErrorUnset: true,
Result: &result,
IgnoreUntaggedFields: true,
}
decoder, err := NewDecoder(config)
if err != nil {
t.Fatalf("err: %s", err)
}
err = decoder.Decode(input)
if err != nil {
t.Fatalf("got an error: %s", err)
}

if result.Vstring != "" {
t.Errorf("Vstring should be empty: %s", result.Vstring)
}
}
func intPtr(v int) *int { return &v }
func uintPtr(v uint) *uint { return &v }
func boolPtr(v bool) *bool { return &v }
Expand Down