Slightly nicer error messages by omitting "root"
diff --git a/mapstructure.go b/mapstructure.go
index 8729e96..f953a8b 100644
--- a/mapstructure.go
+++ b/mapstructure.go
@@ -48,7 +48,7 @@
return errors.New("val must be an addressable struct")
}
- return decode("root", m, val)
+ return decode("", m, val)
}
// Decodes an unknown data type into a specific reflection value.
@@ -236,7 +236,12 @@
panic("field is not valid")
}
- fieldName = fmt.Sprintf("%s.%s", name, fieldName)
+ // If the name is empty string, then we're at the root, and we
+ // don't dot-join the fields.
+ if name != "" {
+ fieldName = fmt.Sprintf("%s.%s", name, fieldName)
+ }
+
if err := decode(fieldName, rawMapVal.Interface(), field); err != nil {
errors = appendErrors(errors, err)
}
diff --git a/mapstructure_examples_test.go b/mapstructure_examples_test.go
index 847074a..e7449c5 100644
--- a/mapstructure_examples_test.go
+++ b/mapstructure_examples_test.go
@@ -55,9 +55,9 @@
// Output:
// 5 error(s) decoding:
//
- // * 'root.Name' expected type 'string', got 'int'
- // * 'root.Age' expected type 'int', got 'string'
- // * 'root.Emails[0]' expected type 'string', got 'int'
- // * 'root.Emails[1]' expected type 'string', got 'int'
- // * 'root.Emails[2]' expected type 'string', got 'int'
+ // * 'Name' expected type 'string', got 'int'
+ // * 'Age' expected type 'int', got 'string'
+ // * 'Emails[0]' expected type 'string', got 'int'
+ // * 'Emails[1]' expected type 'string', got 'int'
+ // * 'Emails[2]' expected type 'string', got 'int'
}
diff --git a/mapstructure_test.go b/mapstructure_test.go
index 713e621..c417762 100644
--- a/mapstructure_test.go
+++ b/mapstructure_test.go
@@ -293,7 +293,7 @@
t.FailNow()
}
- if derr.Errors[0] != "'root.Vstring' expected type 'string', got 'int'" {
+ if derr.Errors[0] != "'Vstring' expected type 'string', got 'int'" {
t.Errorf("got unexpected error: %s", err)
}
}