Repository: mitchellh/mapstructure (language: go)

Issue:
Empty key name when decoding map from struct with only omitempty flag
If a structtag only contains `,omitempty` when decoding a map from a struct, the keyName is overwritten by an empty string. I can't find whether this is defined behavior. If not, I have a PR ready to fix this.

Example:
```go
type Struct struct {
	Username string `mapstructure:",omitempty"`
}

s := Struct{
	Username: "Joe",
}
var m map[string]interface{}
mapstructure.Decode(s, &m)

// Output: map[:Joe]
// Expected output: map[Username:Joe]
fmt.Printf("%+v", m)
```


Discussion / hints:

Fix patch:
diff --git a/mapstructure.go b/mapstructure.go
index 3643901f..36d591fe 100644
--- a/mapstructure.go
+++ b/mapstructure.go
@@ -927,7 +927,9 @@ func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val re
 					return fmt.Errorf("cannot squash non-struct type '%s'", v.Type())
 				}
 			}
-			keyName = tagValue[:index]
+			if keyNameTagValue := tagValue[:index]; keyNameTagValue != "" {
+				keyName = keyNameTagValue
+			}
 		} else if len(tagValue) > 0 {
 			if tagValue == "-" {
 				continue


Test patch:
diff --git a/mapstructure_bugs_test.go b/mapstructure_bugs_test.go
index bcfc5054..7477c740 100644
--- a/mapstructure_bugs_test.go
+++ b/mapstructure_bugs_test.go
@@ -1,6 +1,7 @@
 package mapstructure
 
 import (
+	"fmt"
 	"reflect"
 	"testing"
 	"time"
@@ -601,3 +602,25 @@ func TestMapSquash(t *testing.T) {
 		t.Fatal("expected false")
 	}
 }
+
+// GH-238: Empty key name when decoding map from struct with only omitempty flag
+func TestMapOmitEmptyWithEmptyFieldnameInTag(t *testing.T) {
+	type Struct struct {
+		Username string `mapstructure:",omitempty"`
+		Age      int    `mapstructure:",omitempty"`
+	}
+
+	s := Struct{
+		Username: "Joe",
+	}
+	var m map[string]interface{}
+
+	if err := Decode(s, &m); err != nil {
+		t.Fatal(err)
+	}
+
+	expect := "map[Username:Joe]"
+	if got := fmt.Sprintf("%+v", m); expect != got {
+		t.Fatalf("expect %q, got: %s", expect, got)
+	}
+}