Rename the main function to Decode
diff --git a/mapstructure.go b/mapstructure.go index 49362eb..eda04fd 100644 --- a/mapstructure.go +++ b/mapstructure.go
@@ -7,9 +7,9 @@ "strings" ) -// MapToStruct takes a map and uses reflection to convert it into the +// Decode takes a map and uses reflection to convert it into the // given Go native structure. val must be a pointer to a struct. -func MapToStruct(m map[string]interface{}, rawVal interface{}) error { +func Decode(m map[string]interface{}, rawVal interface{}) error { val := reflect.ValueOf(rawVal) if val.Kind() != reflect.Ptr { return errors.New("val must be a pointer") @@ -90,7 +90,9 @@ } // At this point we know that data is a map with string keys, so - // we can properly cast it here. + // we can properly cast it here. We use the "Interface()" value because + // this gets us the proper interface whether or not data is a pointer + // or not. m, ok := dataVal.Interface().(map[string]interface{}) if !ok { panic("data could not be cast as map[string]interface{}")
diff --git a/mapstructure_test.go b/mapstructure_test.go index 82ee210..f420fc5 100644 --- a/mapstructure_test.go +++ b/mapstructure_test.go
@@ -24,7 +24,7 @@ } var result Basic - err := MapToStruct(input, &result) + err := Decode(input, &result) if err != nil { t.Errorf("got an err: %s", err.Error()) t.FailNow() @@ -60,7 +60,7 @@ } var result Nested - err := MapToStruct(input, &result) + err := Decode(input, &result) if err != nil { t.Errorf("got an err: %s", err.Error()) t.FailNow() @@ -100,7 +100,7 @@ } var result Nested - err := MapToStruct(input, &result) + err := Decode(input, &result) if err != nil { t.Errorf("got an err: %s", err.Error()) t.FailNow() @@ -135,7 +135,7 @@ } var result Basic - err := MapToStruct(input, &result) + err := Decode(input, &result) if err == nil { t.Error("error should exist") t.FailNow() @@ -149,7 +149,7 @@ func TestNonPtrValue(t *testing.T) { t.Parallel() - err := MapToStruct(map[string]interface{}{}, Basic{}) + err := Decode(map[string]interface{}{}, Basic{}) if err == nil { t.Error("error should exist") t.FailNow() @@ -164,7 +164,7 @@ t.Parallel() result := 42 - err := MapToStruct(map[string]interface{}{}, &result) + err := Decode(map[string]interface{}{}, &result) if err == nil { t.Error("error should exist") t.FailNow()