Add examples and fix some issues with error strings
diff --git a/mapstructure.go b/mapstructure.go index 72b6841..8729e96 100644 --- a/mapstructure.go +++ b/mapstructure.go
@@ -21,7 +21,7 @@ } func (e *Error) Error() string { - points := make([]string, 0, len(e.Errors)) + points := make([]string, len(e.Errors)) for i, err := range e.Errors { points[i] = fmt.Sprintf("* %s", err) }
diff --git a/mapstructure_examples_test.go b/mapstructure_examples_test.go new file mode 100644 index 0000000..847074a --- /dev/null +++ b/mapstructure_examples_test.go
@@ -0,0 +1,63 @@ +package mapstructure + +import ( + "fmt" +) + +type Person struct { + Name string + Age int + Emails []string + Extra map[string]string +} + +func ExampleDecode() { + // This input can come from anywhere, but typically comes from + // something like decoding JSON where we're not quite sure of the + // struct initially. + input := map[string]interface{}{ + "name": "Mitchell", + "age": 91, + "emails": []string{"one", "two", "three"}, + "extra": map[string]string{ + "twitter": "mitchellh", + }, + } + + var result Person + err := Decode(input, &result) + if err != nil { + panic(err) + } + + fmt.Printf("%#v", result) + // Output: + // mapstructure.Person{Name:"Mitchell", Age:91, Emails:[]string{"one", "two", "three"}, Extra:map[string]string{"twitter":"mitchellh"}} +} + +func ExampleDecode_errors() { + // This input can come from anywhere, but typically comes from + // something like decoding JSON where we're not quite sure of the + // struct initially. + input := map[string]interface{}{ + "name": 123, + "age": "bad value", + "emails": []int{1, 2, 3}, + } + + var result Person + err := Decode(input, &result) + if err == nil { + panic("should have an error") + } + + fmt.Println(err.Error()) + // 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' +}