Codebase list golang-github-mitchellh-mapstructure / 5a2eb61
Merge pull request #281 from semrekkers/issue-238 Fix empty keyName when decoding struct -> map with omitempty Mitchell Hashimoto authored 2 years ago GitHub committed 2 years ago
2 changed file(s) with 26 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
956956 return fmt.Errorf("cannot squash non-struct type '%s'", v.Type())
957957 }
958958 }
959 keyName = tagValue[:index]
959 if keyNameTagValue := tagValue[:index]; keyNameTagValue != "" {
960 keyName = keyNameTagValue
961 }
960962 } else if len(tagValue) > 0 {
961963 if tagValue == "-" {
962964 continue
00 package mapstructure
11
22 import (
3 "fmt"
34 "reflect"
45 "testing"
56 "time"
600601 t.Fatal("expected false")
601602 }
602603 }
604
605 // GH-238: Empty key name when decoding map from struct with only omitempty flag
606 func TestMapOmitEmptyWithEmptyFieldnameInTag(t *testing.T) {
607 type Struct struct {
608 Username string `mapstructure:",omitempty"`
609 Age int `mapstructure:",omitempty"`
610 }
611
612 s := Struct{
613 Username: "Joe",
614 }
615 var m map[string]interface{}
616
617 if err := Decode(s, &m); err != nil {
618 t.Fatal(err)
619 }
620
621 expect := "map[Username:Joe]"
622 if got := fmt.Sprintf("%+v", m); expect != got {
623 t.Fatalf("expect %q, got: %s", expect, got)
624 }
625 }