├── error.go ├── CONTRIBUTORS ├── mapstructure_bugs_test.go ├── mapstructure_benchmark_test.go ├── doc.go ├── mapstructure_examples_test.go ├── LICENSE ├── README.md ├── mapstructure.go └── mapstructure_test.go /error.go: -------------------------------------------------------------------------------- 1 | package mapstructure 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | // Error implements the error interface and can represents multiple 9 | // errors that occur in the course of a single decode. 10 | type Error struct { 11 | Errors []string 12 | } 13 | 14 | // Error is implementing the error interface. 15 | func (e *Error) Error() string { 16 | points := make([]string, len(e.Errors)) 17 | for i, err := range e.Errors { 18 | points[i] = fmt.Sprintf("* %s", err) 19 | } 20 | 21 | return fmt.Sprintf( 22 | "%d error(s) decoding:\n\n%s", 23 | len(e.Errors), strings.Join(points, "\n")) 24 | } 25 | 26 | func appendErrors(errors []string, err error) []string { 27 | switch e := err.(type) { 28 | case *Error: 29 | return append(errors, e.Errors...) 30 | default: 31 | return append(errors, e.Error()) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | # This is the official list of people who can contribute 2 | # (and typically have contributed) code to the gotraining repository. 3 | # 4 | # Names should be added to this file only after verifying that 5 | # the individual or the individual's organization has agreed to 6 | # the appropriate Contributor License Agreement, found here: 7 | # 8 | # http://code.google.com/legal/individual-cla-v1.0.html 9 | # http://code.google.com/legal/corporate-cla-v1.0.html 10 | # 11 | # The agreement for individuals can be filled out on the web. 12 | 13 | # Names should be added to this file like so: 14 | # Name 15 | # 16 | # An entry with two email addresses specifies that the 17 | # first address should be used in the submit logs and 18 | # that the second address should be recognized as the 19 | # same person when interacting with Rietveld. 20 | 21 | # Please keep the list sorted. 22 | 23 | William Kennedy -------------------------------------------------------------------------------- /mapstructure_bugs_test.go: -------------------------------------------------------------------------------- 1 | package mapstructure 2 | 3 | import "testing" 4 | 5 | // GH-1 6 | func TestDecode_NilValue(t *testing.T) { 7 | input := map[string]interface{}{ 8 | "vfoo": nil, 9 | "vother": nil, 10 | } 11 | 12 | var result Map 13 | err := Decode(input, &result) 14 | if err != nil { 15 | t.Fatalf("should not error: %s", err) 16 | } 17 | 18 | if result.Vfoo != "" { 19 | t.Fatalf("value should be default: %s", result.Vfoo) 20 | } 21 | 22 | if result.Vother != nil { 23 | t.Fatalf("Vother should be nil: %s", result.Vother) 24 | } 25 | } 26 | 27 | // GH-10 28 | func TestDecode_mapInterfaceInterface(t *testing.T) { 29 | input := map[interface{}]interface{}{ 30 | "vfoo": nil, 31 | "vother": nil, 32 | } 33 | 34 | var result Map 35 | err := Decode(input, &result) 36 | if err != nil { 37 | t.Fatalf("should not error: %s", err) 38 | } 39 | 40 | if result.Vfoo != "" { 41 | t.Fatalf("value should be default: %s", result.Vfoo) 42 | } 43 | 44 | if result.Vother != nil { 45 | t.Fatalf("Vother should be nil: %s", result.Vother) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /mapstructure_benchmark_test.go: -------------------------------------------------------------------------------- 1 | package mapstructure 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func Benchmark_Decode(b *testing.B) { 8 | type Person struct { 9 | Name string 10 | Age int 11 | Emails []string 12 | Extra map[string]string 13 | } 14 | 15 | input := map[string]interface{}{ 16 | "name": "Mitchell", 17 | "age": 91, 18 | "emails": []string{"one", "two", "three"}, 19 | "extra": map[string]string{ 20 | "twitter": "mitchellh", 21 | }, 22 | } 23 | 24 | var result Person 25 | for i := 0; i < b.N; i++ { 26 | Decode(input, &result) 27 | } 28 | } 29 | 30 | func Benchmark_DecodeBasic(b *testing.B) { 31 | input := map[string]interface{}{ 32 | "vstring": "foo", 33 | "vint": 42, 34 | "Vuint": 42, 35 | "vbool": true, 36 | "Vfloat": 42.42, 37 | "vsilent": true, 38 | "vdata": 42, 39 | } 40 | 41 | var result Basic 42 | for i := 0; i < b.N; i++ { 43 | Decode(input, &result) 44 | } 45 | } 46 | 47 | func Benchmark_DecodeEmbedded(b *testing.B) { 48 | input := map[string]interface{}{ 49 | "vstring": "foo", 50 | "Basic": map[string]interface{}{ 51 | "vstring": "innerfoo", 52 | }, 53 | "vunique": "bar", 54 | } 55 | 56 | var result Embedded 57 | for i := 0; i < b.N; i++ { 58 | Decode(input, &result) 59 | } 60 | } 61 | 62 | func Benchmark_DecodeTypeConversion(b *testing.B) { 63 | input := map[string]interface{}{ 64 | "IntToFloat": 42, 65 | "IntToUint": 42, 66 | "IntToBool": 1, 67 | "IntToString": 42, 68 | "UintToInt": 42, 69 | "UintToFloat": 42, 70 | "UintToBool": 42, 71 | "UintToString": 42, 72 | "BoolToInt": true, 73 | "BoolToUint": true, 74 | "BoolToFloat": true, 75 | "BoolToString": true, 76 | "FloatToInt": 42.42, 77 | "FloatToUint": 42.42, 78 | "FloatToBool": 42.42, 79 | "FloatToString": 42.42, 80 | "StringToInt": "42", 81 | "StringToUint": "42", 82 | "StringToBool": "1", 83 | "StringToFloat": "42.42", 84 | "SliceToMap": []interface{}{}, 85 | "MapToSlice": map[string]interface{}{}, 86 | } 87 | 88 | var resultStrict TypeConversionResult 89 | for i := 0; i < b.N; i++ { 90 | Decode(input, &resultStrict) 91 | } 92 | } 93 | 94 | func Benchmark_DecodeMap(b *testing.B) { 95 | input := map[string]interface{}{ 96 | "vfoo": "foo", 97 | "vother": map[interface{}]interface{}{ 98 | "foo": "foo", 99 | "bar": "bar", 100 | }, 101 | } 102 | 103 | var result Map 104 | for i := 0; i < b.N; i++ { 105 | Decode(input, &result) 106 | } 107 | } 108 | 109 | func Benchmark_DecodeMapOfStruct(b *testing.B) { 110 | input := map[string]interface{}{ 111 | "value": map[string]interface{}{ 112 | "foo": map[string]string{"vstring": "one"}, 113 | "bar": map[string]string{"vstring": "two"}, 114 | }, 115 | } 116 | 117 | var result MapOfStruct 118 | for i := 0; i < b.N; i++ { 119 | Decode(input, &result) 120 | } 121 | } 122 | 123 | func Benchmark_DecodeSlice(b *testing.B) { 124 | input := map[string]interface{}{ 125 | "vfoo": "foo", 126 | "vbar": []string{"foo", "bar", "baz"}, 127 | } 128 | 129 | var result Slice 130 | for i := 0; i < b.N; i++ { 131 | Decode(input, &result) 132 | } 133 | } 134 | 135 | func Benchmark_DecodeSliceOfStruct(b *testing.B) { 136 | input := map[string]interface{}{ 137 | "value": []map[string]interface{}{ 138 | {"vstring": "one"}, 139 | {"vstring": "two"}, 140 | }, 141 | } 142 | 143 | var result SliceOfStruct 144 | for i := 0; i < b.N; i++ { 145 | Decode(input, &result) 146 | } 147 | } 148 | 149 | func Benchmark_DecodeWeaklyTypedInput(b *testing.B) { 150 | type Person struct { 151 | Name string 152 | Age int 153 | Emails []string 154 | } 155 | 156 | // This input can come from anywhere, but typically comes from 157 | // something like decoding JSON, generated by a weakly typed language 158 | // such as PHP. 159 | input := map[string]interface{}{ 160 | "name": 123, // number => string 161 | "age": "42", // string => number 162 | "emails": map[string]interface{}{}, // empty map => empty array 163 | } 164 | 165 | var result Person 166 | config := &DecoderConfig{ 167 | WeaklyTypedInput: true, 168 | Result: &result, 169 | } 170 | 171 | decoder, err := NewDecoder(config) 172 | if err != nil { 173 | panic(err) 174 | } 175 | 176 | for i := 0; i < b.N; i++ { 177 | decoder.Decode(input) 178 | } 179 | } 180 | 181 | func Benchmark_DecodeMetadata(b *testing.B) { 182 | type Person struct { 183 | Name string 184 | Age int 185 | } 186 | 187 | input := map[string]interface{}{ 188 | "name": "Mitchell", 189 | "age": 91, 190 | "email": "foo@bar.com", 191 | } 192 | 193 | var md Metadata 194 | var result Person 195 | config := &DecoderConfig{ 196 | Metadata: &md, 197 | Result: &result, 198 | } 199 | 200 | decoder, err := NewDecoder(config) 201 | if err != nil { 202 | panic(err) 203 | } 204 | 205 | for i := 0; i < b.N; i++ { 206 | decoder.Decode(input) 207 | } 208 | } 209 | 210 | func Benchmark_DecodeMetadataEmbedded(b *testing.B) { 211 | input := map[string]interface{}{ 212 | "vstring": "foo", 213 | "vunique": "bar", 214 | } 215 | 216 | var md Metadata 217 | var result EmbeddedSquash 218 | config := &DecoderConfig{ 219 | Metadata: &md, 220 | Result: &result, 221 | } 222 | 223 | decoder, err := NewDecoder(config) 224 | if err != nil { 225 | b.Fatalf("err: %s", err) 226 | } 227 | 228 | for i := 0; i < b.N; i++ { 229 | decoder.Decode(input) 230 | } 231 | } 232 | 233 | func Benchmark_DecodeTagged(b *testing.B) { 234 | input := map[string]interface{}{ 235 | "foo": "bar", 236 | "bar": "value", 237 | } 238 | 239 | var result Tagged 240 | for i := 0; i < b.N; i++ { 241 | Decode(input, &result) 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | // Package mapstructure is a Go library for decoding generic map values to structures 2 | // and vice versa, while providing helpful error handling. 3 | // 4 | // Base code comes from: 5 | // 6 | // https://github.com/mitchellh/mapstructure 7 | // 8 | // This package has added higher level support for flattening out JSON documents. 9 | // 10 | // This library is most useful when decoding values from some data stream (JSON, 11 | // Gob, etc.) where you don't _quite_ know the structure of the underlying data 12 | // until you read a part of it. You can therefore read a `map[string]interface{}` 13 | // and use this library to decode it into the proper underlying native Go 14 | // structure. 15 | // 16 | // Usage & Example 17 | // 18 | // For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/mapstructure). 19 | // 20 | // The `Decode`, `DecodePath` and `DecodeSlicePath` functions have examples associated with it there. 21 | // 22 | // But Why?! 23 | // 24 | // Go offers fantastic standard libraries for decoding formats such as JSON. 25 | // The standard method is to have a struct pre-created, and populate that struct 26 | // from the bytes of the encoded format. This is great, but the problem is if 27 | // you have configuration or an encoding that changes slightly depending on 28 | // specific fields. For example, consider this JSON: 29 | // 30 | // json 31 | // { 32 | // "type": "person", 33 | // "name": "Mitchell" 34 | // } 35 | // 36 | // Perhaps we can't populate a specific structure without first reading 37 | // the "type" field from the JSON. We could always do two passes over the 38 | // decoding of the JSON (reading the "type" first, and the rest later). 39 | // However, it is much simpler to just decode this into a `map[string]interface{}` 40 | // structure, read the "type" key, then use something like this library 41 | // to decode it into the proper structure. 42 | // 43 | // DecodePath 44 | // 45 | // Sometimes you have a large and complex JSON document where you only need to decode 46 | // a small part. 47 | // 48 | // { 49 | // "userContext": { 50 | // "conversationCredentials": { 51 | // "sessionToken": "06142010_1:75bf6a413327dd71ebe8f3f30c5a4210a9b11e93c028d6e11abfca7ff" 52 | // }, 53 | // "valid": true, 54 | // "isPasswordExpired": false, 55 | // "cobrandId": 10000004, 56 | // "channelId": -1, 57 | // "locale": "en_US", 58 | // "tncVersion": 2, 59 | // "applicationId": "17CBE222A42161A3FF450E47CF4C1A00", 60 | // "cobrandConversationCredentials": { 61 | // "sessionToken": "06142010_1:b8d011fefbab8bf1753391b074ffedf9578612d676ed2b7f073b5785b" 62 | // }, 63 | // "preferenceInfo": { 64 | // "currencyCode": "USD", 65 | // "timeZone": "PST", 66 | // "dateFormat": "MM/dd/yyyy", 67 | // "currencyNotationType": { 68 | // "currencyNotationType": "SYMBOL" 69 | // }, 70 | // "numberFormat": { 71 | // "decimalSeparator": ".", 72 | // "groupingSeparator": ",", 73 | // "groupPattern": "###,##0.##" 74 | // } 75 | // } 76 | // }, 77 | // "lastLoginTime": 1375686841, 78 | // "loginCount": 299, 79 | // "passwordRecovered": false, 80 | // "emailAddress": "johndoe@email.com", 81 | // "loginName": "sptest1", 82 | // "userId": 10483860, 83 | // "userType": 84 | // { 85 | // "userTypeId": 1, 86 | // "userTypeName": "normal_user" 87 | // } 88 | // } 89 | // 90 | // It is nice to be able to define and pull the documents and fields you need without 91 | // having to map the entire JSON structure. 92 | // 93 | // type UserType struct { 94 | // UserTypeId int 95 | // UserTypeName string 96 | // } 97 | // 98 | // type NumberFormat struct { 99 | // DecimalSeparator string `jpath:"userContext.preferenceInfo.numberFormat.decimalSeparator"` 100 | // GroupingSeparator string `jpath:"userContext.preferenceInfo.numberFormat.groupingSeparator"` 101 | // GroupPattern string `jpath:"userContext.preferenceInfo.numberFormat.groupPattern"` 102 | // } 103 | // 104 | // type User struct { 105 | // Session string `jpath:"userContext.cobrandConversationCredentials.sessionToken"` 106 | // CobrandId int `jpath:"userContext.cobrandId"` 107 | // UserType UserType `jpath:"userType"` 108 | // LoginName string `jpath:"loginName"` 109 | // NumberFormat // This can also be a pointer to the struct (*NumberFormat) 110 | // } 111 | // 112 | // docScript := []byte(document) 113 | // var docMap map[string]interface{} 114 | // json.Unmarshal(docScript, &docMap) 115 | // 116 | // var user User 117 | // mapstructure.DecodePath(docMap, &user) 118 | // 119 | // DecodeSlicePath 120 | // 121 | // Sometimes you have a slice of documents that you need to decode into a slice of structures 122 | // 123 | // [ 124 | // {"name":"bill"}, 125 | // {"name":"lisa"} 126 | // ] 127 | // 128 | // Just Unmarshal your document into a slice of maps and decode the slice 129 | // 130 | // type NameDoc struct { 131 | // Name string `jpath:"name"` 132 | // } 133 | // 134 | // sliceScript := []byte(document) 135 | // var sliceMap []map[string]interface{} 136 | // json.Unmarshal(sliceScript, &sliceMap) 137 | // 138 | // var myslice []NameDoc 139 | // err := DecodeSlicePath(sliceMap, &myslice) 140 | // 141 | // var myslice []*NameDoc 142 | // err := DecodeSlicePath(sliceMap, &myslice) 143 | // 144 | // Decode Structs With Embedded Slices 145 | // 146 | // Sometimes you have a document with arrays 147 | // 148 | // { 149 | // "cobrandId": 10010352, 150 | // "channelId": -1, 151 | // "locale": "en_US", 152 | // "tncVersion": 2, 153 | // "people": [ 154 | // { 155 | // "name": "jack", 156 | // "age": { 157 | // "birth":10, 158 | // "year":2000, 159 | // "animals": [ 160 | // { 161 | // "barks":"yes", 162 | // "tail":"yes" 163 | // }, 164 | // { 165 | // "barks":"no", 166 | // "tail":"yes" 167 | // } 168 | // ] 169 | // } 170 | // }, 171 | // { 172 | // "name": "jill", 173 | // "age": { 174 | // "birth":11, 175 | // "year":2001 176 | // } 177 | // } 178 | // ] 179 | // } 180 | // 181 | // You can decode within those arrays 182 | // 183 | // type Animal struct { 184 | // Barks string `jpath:"barks"` 185 | // } 186 | // 187 | // type People struct { 188 | // Age int `jpath:"age.birth"` // jpath is relative to the array 189 | // Animals []Animal `jpath:"age.animals"` 190 | // } 191 | // 192 | // type Items struct { 193 | // Categories []string `jpath:"categories"` 194 | // Peoples []People `jpath:"people"` // Specify the location of the array 195 | // } 196 | // 197 | // docScript := []byte(document) 198 | // var docMap map[string]interface{} 199 | // json.Unmarshal(docScript, &docMap) 200 | // 201 | // var items Items 202 | // DecodePath(docMap, &items) 203 | package mapstructure 204 | -------------------------------------------------------------------------------- /mapstructure_examples_test.go: -------------------------------------------------------------------------------- 1 | package mapstructure 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | ) 7 | 8 | func ExampleDecode() { 9 | type Person struct { 10 | Name string 11 | Age int 12 | Emails []string 13 | Extra map[string]string 14 | } 15 | 16 | // This input can come from anywhere, but typically comes from 17 | // something like decoding JSON where we're not quite sure of the 18 | // struct initially. 19 | input := map[string]interface{}{ 20 | "name": "Mitchell", 21 | "age": 91, 22 | "emails": []string{"one", "two", "three"}, 23 | "extra": map[string]string{ 24 | "twitter": "mitchellh", 25 | }, 26 | } 27 | 28 | var result Person 29 | err := Decode(input, &result) 30 | if err != nil { 31 | panic(err) 32 | } 33 | 34 | fmt.Printf("%#v", result) 35 | // Output: 36 | // mapstructure.Person{Name:"Mitchell", Age:91, Emails:[]string{"one", "two", "three"}, Extra:map[string]string{"twitter":"mitchellh"}} 37 | } 38 | 39 | func ExampleDecode_errors() { 40 | type Person struct { 41 | Name string 42 | Age int 43 | Emails []string 44 | Extra map[string]string 45 | } 46 | 47 | // This input can come from anywhere, but typically comes from 48 | // something like decoding JSON where we're not quite sure of the 49 | // struct initially. 50 | input := map[string]interface{}{ 51 | "name": 123, 52 | "age": "bad value", 53 | "emails": []int{1, 2, 3}, 54 | } 55 | 56 | var result Person 57 | err := Decode(input, &result) 58 | if err == nil { 59 | panic("should have an error") 60 | } 61 | 62 | fmt.Println(err.Error()) 63 | // MAPS are Random - Output: 64 | // 5 error(s) decoding: 65 | // 66 | // * 'Name' expected type 'string', got unconvertible type 'int' 67 | // * 'Age' expected type 'int', got unconvertible type 'string' 68 | // * 'Emails[0]' expected type 'string', got unconvertible type 'int' 69 | // * 'Emails[1]' expected type 'string', got unconvertible type 'int' 70 | // * 'Emails[2]' expected type 'string', got unconvertible type 'int' 71 | } 72 | 73 | func ExampleDecode_metadata() { 74 | type Person struct { 75 | Name string 76 | Age int 77 | } 78 | 79 | // This input can come from anywhere, but typically comes from 80 | // something like decoding JSON where we're not quite sure of the 81 | // struct initially. 82 | input := map[string]interface{}{ 83 | "name": "Mitchell", 84 | "age": 91, 85 | "email": "foo@bar.com", 86 | } 87 | 88 | // For metadata, we make a more advanced DecoderConfig so we can 89 | // more finely configure the decoder that is used. In this case, we 90 | // just tell the decoder we want to track metadata. 91 | var md Metadata 92 | var result Person 93 | config := &DecoderConfig{ 94 | Metadata: &md, 95 | Result: &result, 96 | } 97 | 98 | decoder, err := NewDecoder(config) 99 | if err != nil { 100 | panic(err) 101 | } 102 | 103 | if err := decoder.Decode(input); err != nil { 104 | panic(err) 105 | } 106 | 107 | fmt.Printf("Unused keys: %#v", md.Unused) 108 | // Output: 109 | // Unused keys: []string{"email"} 110 | } 111 | 112 | func ExampleDecode_weaklyTypedInput() { 113 | type Person struct { 114 | Name string 115 | Age int 116 | Emails []string 117 | } 118 | 119 | // This input can come from anywhere, but typically comes from 120 | // something like decoding JSON, generated by a weakly typed language 121 | // such as PHP. 122 | input := map[string]interface{}{ 123 | "name": 123, // number => string 124 | "age": "42", // string => number 125 | "emails": map[string]interface{}{}, // empty map => empty array 126 | } 127 | 128 | var result Person 129 | config := &DecoderConfig{ 130 | WeaklyTypedInput: true, 131 | Result: &result, 132 | } 133 | 134 | decoder, err := NewDecoder(config) 135 | if err != nil { 136 | panic(err) 137 | } 138 | 139 | err = decoder.Decode(input) 140 | if err != nil { 141 | panic(err) 142 | } 143 | 144 | fmt.Printf("%#v", result) 145 | // Output: mapstructure.Person{Name:"123", Age:42, Emails:[]string{}} 146 | } 147 | 148 | func ExampleDecodePath() { 149 | document := `{ 150 | "userContext": { 151 | "conversationCredentials": { 152 | "sessionToken": "06142010_1:75bf6a413327dd71ebe8f3f30c5a4210a9b11e93c028d6e11abfca7ff" 153 | }, 154 | "valid": true, 155 | "isPasswordExpired": false, 156 | "cobrandId": 10000004, 157 | "channelId": -1, 158 | "locale": "en_US", 159 | "tncVersion": 2, 160 | "applicationId": "17CBE222A42161A3FF450E47CF4C1A00", 161 | "cobrandConversationCredentials": { 162 | "sessionToken": "06142010_1:b8d011fefbab8bf1753391b074ffedf9578612d676ed2b7f073b5785b" 163 | }, 164 | "preferenceInfo": { 165 | "currencyCode": "USD", 166 | "timeZone": "PST", 167 | "dateFormat": "MM/dd/yyyy", 168 | "currencyNotationType": { 169 | "currencyNotationType": "SYMBOL" 170 | }, 171 | "numberFormat": { 172 | "decimalSeparator": ".", 173 | "groupingSeparator": ",", 174 | "groupPattern": "###,##0.##" 175 | } 176 | } 177 | }, 178 | "lastLoginTime": 1375686841, 179 | "loginCount": 299, 180 | "passwordRecovered": false, 181 | "emailAddress": "johndoe@email.com", 182 | "loginName": "sptest1", 183 | "userId": 10483860, 184 | "userType": 185 | { 186 | "userTypeId": 1, 187 | "userTypeName": "normal_user" 188 | } 189 | }` 190 | 191 | type UserType struct { 192 | UserTypeID int 193 | UserTypeName string 194 | } 195 | 196 | type NumberFormat struct { 197 | DecimalSeparator string `jpath:"userContext.preferenceInfo.numberFormat.decimalSeparator"` 198 | GroupingSeparator string `jpath:"userContext.preferenceInfo.numberFormat.groupingSeparator"` 199 | GroupPattern string `jpath:"userContext.preferenceInfo.numberFormat.groupPattern"` 200 | } 201 | 202 | type User struct { 203 | Session string `jpath:"userContext.cobrandConversationCredentials.sessionToken"` 204 | CobrandID int `jpath:"userContext.cobrandId"` 205 | UserType UserType `jpath:"userType"` 206 | LoginName string `jpath:"loginName"` 207 | NumberFormat // This can also be a pointer to the struct (*NumberFormat) 208 | } 209 | 210 | docScript := []byte(document) 211 | var docMap map[string]interface{} 212 | json.Unmarshal(docScript, &docMap) 213 | 214 | var user User 215 | DecodePath(docMap, &user) 216 | 217 | fmt.Printf("%v", user) 218 | // Output: 219 | // {06142010_1:b8d011fefbab8bf1753391b074ffedf9578612d676ed2b7f073b5785b 10000004 {1 normal_user} sptest1 {. , ###,##0.##}} 220 | } 221 | 222 | func ExampleDecodeSlicePath() { 223 | var document = `[{"name":"bill"},{"name":"lisa"}]` 224 | 225 | type NameDoc struct { 226 | Name string `jpath:"name"` 227 | } 228 | 229 | sliceScript := []byte(document) 230 | var sliceMap []map[string]interface{} 231 | json.Unmarshal(sliceScript, &sliceMap) 232 | 233 | var myslice []NameDoc 234 | DecodeSlicePath(sliceMap, &myslice) 235 | 236 | fmt.Printf("%#v", myslice) 237 | // Output: 238 | // []mapstructure.NameDoc{mapstructure.NameDoc{Name:"bill"}, mapstructure.NameDoc{Name:"lisa"}} 239 | } 240 | 241 | func ExampleDecode_embeddedSlice() { 242 | document := `{ 243 | "cobrandId": 10010352, 244 | "channelId": -1, 245 | "locale": "en_US", 246 | "tncVersion": 2, 247 | "categories":["rabbit","bunny","frog"], 248 | "people": [ 249 | { 250 | "name": "jack", 251 | "age": { 252 | "birth":10, 253 | "year":2000, 254 | "animals": [ 255 | { 256 | "barks":"yes", 257 | "tail":"yes" 258 | }, 259 | { 260 | "barks":"no", 261 | "tail":"yes" 262 | } 263 | ] 264 | } 265 | }, 266 | { 267 | "name": "jill", 268 | "age": { 269 | "birth":11, 270 | "year":2001 271 | } 272 | } 273 | ] 274 | }` 275 | 276 | type Animal struct { 277 | Barks string `jpath:"barks"` 278 | } 279 | 280 | type People struct { 281 | Age int `jpath:"age.birth"` // jpath is relative to the array 282 | Animals []Animal `jpath:"age.animals"` 283 | } 284 | 285 | type Items struct { 286 | Categories []string `jpath:"categories"` 287 | Peoples []People `jpath:"people"` // Specify the location of the array 288 | } 289 | 290 | docScript := []byte(document) 291 | var docMap map[string]interface{} 292 | json.Unmarshal(docScript, &docMap) 293 | 294 | var items Items 295 | DecodePath(docMap, &items) 296 | 297 | fmt.Printf("%#v", items) 298 | // Output: 299 | // mapstructure.Items{Categories:[]string{"rabbit", "bunny", "frog"}, Peoples:[]mapstructure.People{mapstructure.People{Age:10, Animals:[]mapstructure.Animal{mapstructure.Animal{Barks:"yes"}, mapstructure.Animal{Barks:"no"}}}, mapstructure.People{Age:11, Animals:[]mapstructure.Animal(nil)}}} 300 | } 301 | 302 | func ExampleDecode_abstractField() { 303 | var document = `{"Error":[{"errorDetail":"Invalid Cobrand Credentials"}]}` 304 | 305 | type YodleeError struct { 306 | Error []map[string]interface{} `jpath:"Error"` 307 | } 308 | 309 | type CobrandContext struct { 310 | YodleeError 311 | } 312 | 313 | docScript := []byte(document) 314 | var docMap map[string]interface{} 315 | json.Unmarshal(docScript, &docMap) 316 | 317 | var cobrandContext CobrandContext 318 | DecodePath(docMap, &cobrandContext) 319 | 320 | fmt.Printf("%#v", cobrandContext) 321 | // Output: 322 | // mapstructure.CobrandContext{YodleeError:mapstructure.YodleeError{Error:[]map[string]interface {}{map[string]interface {}{"errorDetail":"Invalid Cobrand Credentials"}}}} 323 | } 324 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # mapstructure 4 | `import "github.com/ardanlabs/mapstructure"` 5 | 6 | * [Overview](#pkg-overview) 7 | * [Index](#pkg-index) 8 | * [Examples](#pkg-examples) 9 | 10 | ## Overview 11 | Package mapstructure is a Go library for decoding generic map values to structures 12 | and vice versa, while providing helpful error handling. 13 | 14 | Base code comes from: 15 | 16 | https://github.com/mitchellh/mapstructure 17 | 18 | This package has added higher level support for flattening out JSON documents. 19 | 20 | This library is most useful when decoding values from some data stream (JSON, 21 | Gob, etc.) where you don't _quite_ know the structure of the underlying data 22 | until you read a part of it. You can therefore read a `map[string]interface{}` 23 | and use this library to decode it into the proper underlying native Go 24 | structure. 25 | 26 | Usage & Example 27 | 28 | For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/mapstructure). 29 | 30 | The `Decode`, `DecodePath` and `DecodeSlicePath` functions have examples associated with it there. 31 | 32 | But Why?! 33 | 34 | Go offers fantastic standard libraries for decoding formats such as JSON. 35 | The standard method is to have a struct pre-created, and populate that struct 36 | from the bytes of the encoded format. This is great, but the problem is if 37 | you have configuration or an encoding that changes slightly depending on 38 | specific fields. For example, consider this JSON: 39 | 40 | 41 | json 42 | { 43 | "type": "person", 44 | "name": "Mitchell" 45 | } 46 | 47 | Perhaps we can't populate a specific structure without first reading 48 | the "type" field from the JSON. We could always do two passes over the 49 | decoding of the JSON (reading the "type" first, and the rest later). 50 | However, it is much simpler to just decode this into a `map[string]interface{}` 51 | structure, read the "type" key, then use something like this library 52 | to decode it into the proper structure. 53 | 54 | ### DecodePath 55 | Sometimes you have a large and complex JSON document where you only need to decode 56 | a small part. 57 | 58 | 59 | { 60 | "userContext": { 61 | "conversationCredentials": { 62 | "sessionToken": "06142010_1:75bf6a413327dd71ebe8f3f30c5a4210a9b11e93c028d6e11abfca7ff" 63 | }, 64 | "valid": true, 65 | "isPasswordExpired": false, 66 | "cobrandId": 10000004, 67 | "channelId": -1, 68 | "locale": "en_US", 69 | "tncVersion": 2, 70 | "applicationId": "17CBE222A42161A3FF450E47CF4C1A00", 71 | "cobrandConversationCredentials": { 72 | "sessionToken": "06142010_1:b8d011fefbab8bf1753391b074ffedf9578612d676ed2b7f073b5785b" 73 | }, 74 | "preferenceInfo": { 75 | "currencyCode": "USD", 76 | "timeZone": "PST", 77 | "dateFormat": "MM/dd/yyyy", 78 | "currencyNotationType": { 79 | "currencyNotationType": "SYMBOL" 80 | }, 81 | "numberFormat": { 82 | "decimalSeparator": ".", 83 | "groupingSeparator": ",", 84 | "groupPattern": "###,##0.##" 85 | } 86 | } 87 | }, 88 | "lastLoginTime": 1375686841, 89 | "loginCount": 299, 90 | "passwordRecovered": false, 91 | "emailAddress": "johndoe@email.com", 92 | "loginName": "sptest1", 93 | "userId": 10483860, 94 | "userType": 95 | { 96 | "userTypeId": 1, 97 | "userTypeName": "normal_user" 98 | } 99 | } 100 | 101 | It is nice to be able to define and pull the documents and fields you need without 102 | having to map the entire JSON structure. 103 | 104 | 105 | type UserType struct { 106 | UserTypeId int 107 | UserTypeName string 108 | } 109 | 110 | type NumberFormat struct { 111 | DecimalSeparator string `jpath:"userContext.preferenceInfo.numberFormat.decimalSeparator"` 112 | GroupingSeparator string `jpath:"userContext.preferenceInfo.numberFormat.groupingSeparator"` 113 | GroupPattern string `jpath:"userContext.preferenceInfo.numberFormat.groupPattern"` 114 | } 115 | 116 | type User struct { 117 | Session string `jpath:"userContext.cobrandConversationCredentials.sessionToken"` 118 | CobrandId int `jpath:"userContext.cobrandId"` 119 | UserType UserType `jpath:"userType"` 120 | LoginName string `jpath:"loginName"` 121 | NumberFormat // This can also be a pointer to the struct (*NumberFormat) 122 | } 123 | 124 | docScript := []byte(document) 125 | var docMap map[string]interface{} 126 | json.Unmarshal(docScript, &docMap) 127 | 128 | var user User 129 | mapstructure.DecodePath(docMap, &user) 130 | 131 | ### DecodeSlicePath 132 | Sometimes you have a slice of documents that you need to decode into a slice of structures 133 | 134 | 135 | [ 136 | {"name":"bill"}, 137 | {"name":"lisa"} 138 | ] 139 | 140 | Just Unmarshal your document into a slice of maps and decode the slice 141 | 142 | 143 | type NameDoc struct { 144 | Name string `jpath:"name"` 145 | } 146 | 147 | sliceScript := []byte(document) 148 | var sliceMap []map[string]interface{} 149 | json.Unmarshal(sliceScript, &sliceMap) 150 | 151 | var myslice []NameDoc 152 | err := DecodeSlicePath(sliceMap, &myslice) 153 | 154 | var myslice []*NameDoc 155 | err := DecodeSlicePath(sliceMap, &myslice) 156 | 157 | ### Decode Structs With Embedded Slices 158 | Sometimes you have a document with arrays 159 | 160 | 161 | { 162 | "cobrandId": 10010352, 163 | "channelId": -1, 164 | "locale": "en_US", 165 | "tncVersion": 2, 166 | "people": [ 167 | { 168 | "name": "jack", 169 | "age": { 170 | "birth":10, 171 | "year":2000, 172 | "animals": [ 173 | { 174 | "barks":"yes", 175 | "tail":"yes" 176 | }, 177 | { 178 | "barks":"no", 179 | "tail":"yes" 180 | } 181 | ] 182 | } 183 | }, 184 | { 185 | "name": "jill", 186 | "age": { 187 | "birth":11, 188 | "year":2001 189 | } 190 | } 191 | ] 192 | } 193 | 194 | You can decode within those arrays 195 | 196 | 197 | type Animal struct { 198 | Barks string `jpath:"barks"` 199 | } 200 | 201 | type People struct { 202 | Age int `jpath:"age.birth"` // jpath is relative to the array 203 | Animals []Animal `jpath:"age.animals"` 204 | } 205 | 206 | type Items struct { 207 | Categories []string `jpath:"categories"` 208 | Peoples []People `jpath:"people"` // Specify the location of the array 209 | } 210 | 211 | docScript := []byte(document) 212 | var docMap map[string]interface{} 213 | json.Unmarshal(docScript, &docMap) 214 | 215 | var items Items 216 | DecodePath(docMap, &items) 217 | 218 | 219 | 220 | 221 | ## Index 222 | * [func Decode(m interface{}, rawVal interface{}) error](#Decode) 223 | * [func DecodePath(m map[string]interface{}, rawVal interface{}) error](#DecodePath) 224 | * [func DecodeSlicePath(ms []map[string]interface{}, rawSlice interface{}) error](#DecodeSlicePath) 225 | * [type DecodeHookFunc](#DecodeHookFunc) 226 | * [type Decoder](#Decoder) 227 | * [func NewDecoder(config *DecoderConfig) (*Decoder, error)](#NewDecoder) 228 | * [func NewPathDecoder(config *DecoderConfig) (*Decoder, error)](#NewPathDecoder) 229 | * [func (d *Decoder) Decode(raw interface{}) error](#Decoder.Decode) 230 | * [func (d *Decoder) DecodePath(m map[string]interface{}, rawVal interface{}) (bool, error)](#Decoder.DecodePath) 231 | * [type DecoderConfig](#DecoderConfig) 232 | * [type Error](#Error) 233 | * [func (e *Error) Error() string](#Error.Error) 234 | * [type Metadata](#Metadata) 235 | 236 | #### Examples 237 | * [Decode](#example_Decode) 238 | * [DecodePath](#example_DecodePath) 239 | * [DecodeSlicePath](#example_DecodeSlicePath) 240 | * [Decode (AbstractField)](#example_Decode_abstractField) 241 | * [Decode (EmbeddedSlice)](#example_Decode_embeddedSlice) 242 | * [Decode (Errors)](#example_Decode_errors) 243 | * [Decode (Metadata)](#example_Decode_metadata) 244 | * [Decode (WeaklyTypedInput)](#example_Decode_weaklyTypedInput) 245 | 246 | #### Package files 247 | [doc.go](/src/github.com/ardanlabs/mapstructure/doc.go) [error.go](/src/github.com/ardanlabs/mapstructure/error.go) [mapstructure.go](/src/github.com/ardanlabs/mapstructure/mapstructure.go) 248 | 249 | 250 | 251 | 252 | 253 | ## func [Decode](/src/target/mapstructure.go?s=2737:2789#L71) 254 | ``` go 255 | func Decode(m interface{}, rawVal interface{}) error 256 | ``` 257 | Decode takes a map and uses reflection to convert it into the 258 | given Go native structure. val must be a pointer to a struct. 259 | 260 | 261 | 262 | ## func [DecodePath](/src/target/mapstructure.go?s=3138:3205#L88) 263 | ``` go 264 | func DecodePath(m map[string]interface{}, rawVal interface{}) error 265 | ``` 266 | DecodePath takes a map and uses reflection to convert it into the 267 | given Go native structure. Tags are used to specify the mapping 268 | between fields in the map and structure 269 | 270 | 271 | 272 | ## func [DecodeSlicePath](/src/target/mapstructure.go?s=3506:3583#L105) 273 | ``` go 274 | func DecodeSlicePath(ms []map[string]interface{}, rawSlice interface{}) error 275 | ``` 276 | DecodeSlicePath decodes a slice of maps against a slice of structures that 277 | contain specified tags 278 | 279 | 280 | 281 | 282 | ## type [DecodeHookFunc](/src/target/mapstructure.go?s=153:239#L3) 283 | ``` go 284 | type DecodeHookFunc func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error) 285 | ``` 286 | DecodeHookFunc declares a function to help with decoding. 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | ## type [Decoder](/src/target/mapstructure.go?s=2174:2220#L54) 298 | ``` go 299 | type Decoder struct { 300 | // contains filtered or unexported fields 301 | } 302 | ``` 303 | A Decoder takes a raw interface value and turns it into structured 304 | data, keeping track of rich error information along the way in case 305 | anything goes wrong. Unlike the basic top-level Decode method, you can 306 | more finely control how the Decoder behaves using the DecoderConfig 307 | structure. The top-level Decode method is just a convenience that sets 308 | up the most basic Decoder. 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | ### func [NewDecoder](/src/target/mapstructure.go?s=4979:5035#L154) 317 | ``` go 318 | func NewDecoder(config *DecoderConfig) (*Decoder, error) 319 | ``` 320 | NewDecoder returns a new decoder for the given configuration. Once 321 | a decoder has been returned, the same configuration must not be used 322 | again. 323 | 324 | 325 | ### func [NewPathDecoder](/src/target/mapstructure.go?s=5731:5791#L188) 326 | ``` go 327 | func NewPathDecoder(config *DecoderConfig) (*Decoder, error) 328 | ``` 329 | NewPathDecoder returns a new decoder for the given configuration. 330 | This is used to decode path specific structures 331 | 332 | 333 | 334 | 335 | 336 | ### func (\*Decoder) [Decode](/src/target/mapstructure.go?s=6228:6275#L212) 337 | ``` go 338 | func (d *Decoder) Decode(raw interface{}) error 339 | ``` 340 | Decode decodes the given raw interface to the target pointer specified 341 | by the configuration. 342 | 343 | 344 | 345 | 346 | ### func (\*Decoder) [DecodePath](/src/target/mapstructure.go?s=6435:6523#L218) 347 | ``` go 348 | func (d *Decoder) DecodePath(m map[string]interface{}, rawVal interface{}) (bool, error) 349 | ``` 350 | DecodePath decodes the raw interface against the map based on the 351 | specified tags 352 | 353 | 354 | 355 | 356 | ## type [DecoderConfig](/src/target/mapstructure.go?s=376:1782#L7) 357 | ``` go 358 | type DecoderConfig struct { 359 | // DecodeHook, if set, will be called before any decoding and any 360 | // type conversion (if WeaklyTypedInput is on). This lets you modify 361 | // the values before they're set down onto the resulting struct. 362 | // 363 | // If an error is returned, the entire decode will fail with that 364 | // error. 365 | DecodeHook DecodeHookFunc 366 | 367 | // If ErrorUnused is true, then it is an error for there to exist 368 | // keys in the original map that were unused in the decoding process 369 | // (extra keys). 370 | ErrorUnused bool 371 | 372 | // If WeaklyTypedInput is true, the decoder will make the following 373 | // "weak" conversions: 374 | // 375 | // - bools to string (true = "1", false = "0") 376 | // - numbers to string (base 10) 377 | // - bools to int/uint (true = 1, false = 0) 378 | // - strings to int/uint (base implied by prefix) 379 | // - int to bool (true if value != 0) 380 | // - string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F, 381 | // FALSE, false, False. Anything else is an error) 382 | // - empty array = empty map and vice versa 383 | // 384 | WeaklyTypedInput bool 385 | 386 | // Metadata is the struct that will contain extra metadata about 387 | // the decoding. If this is nil, then no metadata will be tracked. 388 | Metadata *Metadata 389 | 390 | // Result is a pointer to the struct that will contain the decoded 391 | // value. 392 | Result interface{} 393 | 394 | // The tag name that mapstructure reads for field names. This 395 | // defaults to "mapstructure" 396 | TagName string 397 | } 398 | ``` 399 | DecoderConfig is the configuration that is used to create a new decoder 400 | and allows customization of various aspects of decoding. 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | ## type [Error](/src/target/error.go?s=175:213#L1) 412 | ``` go 413 | type Error struct { 414 | Errors []string 415 | } 416 | ``` 417 | Error implements the error interface and can represents multiple 418 | errors that occur in the course of a single decode. 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | ### func (\*Error) [Error](/src/target/error.go?s=261:291#L5) 430 | ``` go 431 | func (e *Error) Error() string 432 | ``` 433 | Error is implementing the error interface. 434 | 435 | 436 | 437 | 438 | ## type [Metadata](/src/target/mapstructure.go?s=2332:2605#L60) 439 | ``` go 440 | type Metadata struct { 441 | // Keys are the keys of the structure which were successfully decoded 442 | Keys []string 443 | 444 | // Unused is a slice of keys that were found in the raw value but 445 | // weren't decoded since there was no matching field in the result interface 446 | Unused []string 447 | } 448 | ``` 449 | Metadata contains information about decoding a structure that 450 | is tedious or difficult to get otherwise. 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | - - - 466 | Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md) 467 | -------------------------------------------------------------------------------- /mapstructure.go: -------------------------------------------------------------------------------- 1 | package mapstructure 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "reflect" 7 | "sort" 8 | "strconv" 9 | "strings" 10 | ) 11 | 12 | // DecodeHookFunc declares a function to help with decoding. 13 | type DecodeHookFunc func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error) 14 | 15 | // DecoderConfig is the configuration that is used to create a new decoder 16 | // and allows customization of various aspects of decoding. 17 | type DecoderConfig struct { 18 | // DecodeHook, if set, will be called before any decoding and any 19 | // type conversion (if WeaklyTypedInput is on). This lets you modify 20 | // the values before they're set down onto the resulting struct. 21 | // 22 | // If an error is returned, the entire decode will fail with that 23 | // error. 24 | DecodeHook DecodeHookFunc 25 | 26 | // If ErrorUnused is true, then it is an error for there to exist 27 | // keys in the original map that were unused in the decoding process 28 | // (extra keys). 29 | ErrorUnused bool 30 | 31 | // If WeaklyTypedInput is true, the decoder will make the following 32 | // "weak" conversions: 33 | // 34 | // - bools to string (true = "1", false = "0") 35 | // - numbers to string (base 10) 36 | // - bools to int/uint (true = 1, false = 0) 37 | // - strings to int/uint (base implied by prefix) 38 | // - int to bool (true if value != 0) 39 | // - string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F, 40 | // FALSE, false, False. Anything else is an error) 41 | // - empty array = empty map and vice versa 42 | // 43 | WeaklyTypedInput bool 44 | 45 | // Metadata is the struct that will contain extra metadata about 46 | // the decoding. If this is nil, then no metadata will be tracked. 47 | Metadata *Metadata 48 | 49 | // Result is a pointer to the struct that will contain the decoded 50 | // value. 51 | Result interface{} 52 | 53 | // The tag name that mapstructure reads for field names. This 54 | // defaults to "mapstructure" 55 | TagName string 56 | } 57 | 58 | // A Decoder takes a raw interface value and turns it into structured 59 | // data, keeping track of rich error information along the way in case 60 | // anything goes wrong. Unlike the basic top-level Decode method, you can 61 | // more finely control how the Decoder behaves using the DecoderConfig 62 | // structure. The top-level Decode method is just a convenience that sets 63 | // up the most basic Decoder. 64 | type Decoder struct { 65 | config *DecoderConfig 66 | } 67 | 68 | // Metadata contains information about decoding a structure that 69 | // is tedious or difficult to get otherwise. 70 | type Metadata struct { 71 | // Keys are the keys of the structure which were successfully decoded 72 | Keys []string 73 | 74 | // Unused is a slice of keys that were found in the raw value but 75 | // weren't decoded since there was no matching field in the result interface 76 | Unused []string 77 | } 78 | 79 | // Decode takes a map and uses reflection to convert it into the 80 | // given Go native structure. val must be a pointer to a struct. 81 | func Decode(m interface{}, rawVal interface{}) error { 82 | config := &DecoderConfig{ 83 | Metadata: nil, 84 | Result: rawVal, 85 | } 86 | 87 | decoder, err := NewDecoder(config) 88 | if err != nil { 89 | return err 90 | } 91 | 92 | return decoder.Decode(m) 93 | } 94 | 95 | // DecodePath takes a map and uses reflection to convert it into the 96 | // given Go native structure. Tags are used to specify the mapping 97 | // between fields in the map and structure 98 | func DecodePath(m map[string]interface{}, rawVal interface{}) error { 99 | config := &DecoderConfig{ 100 | Metadata: nil, 101 | Result: nil, 102 | } 103 | 104 | decoder, err := NewPathDecoder(config) 105 | if err != nil { 106 | return err 107 | } 108 | 109 | _, err = decoder.DecodePath(m, rawVal) 110 | return err 111 | } 112 | 113 | // DecodeSlicePath decodes a slice of maps against a slice of structures that 114 | // contain specified tags 115 | func DecodeSlicePath(ms []map[string]interface{}, rawSlice interface{}) error { 116 | reflectRawSlice := reflect.TypeOf(rawSlice) 117 | rawKind := reflectRawSlice.Kind() 118 | rawElement := reflectRawSlice.Elem() 119 | 120 | if (rawKind == reflect.Ptr && rawElement.Kind() != reflect.Slice) || 121 | (rawKind != reflect.Ptr && rawKind != reflect.Slice) { 122 | return fmt.Errorf("Incompatible Value, Looking For Slice : %v : %v", rawKind, rawElement.Kind()) 123 | } 124 | 125 | config := &DecoderConfig{ 126 | Metadata: nil, 127 | Result: nil, 128 | } 129 | 130 | decoder, err := NewPathDecoder(config) 131 | if err != nil { 132 | return err 133 | } 134 | 135 | // Create a slice large enough to decode all the values 136 | valSlice := reflect.MakeSlice(rawElement, len(ms), len(ms)) 137 | 138 | // Iterate over the maps and decode each one 139 | for index, m := range ms { 140 | sliceElementType := rawElement.Elem() 141 | if sliceElementType.Kind() != reflect.Ptr { 142 | // A slice of objects 143 | obj := reflect.New(rawElement.Elem()) 144 | decoder.DecodePath(m, reflect.Indirect(obj)) 145 | indexVal := valSlice.Index(index) 146 | indexVal.Set(reflect.Indirect(obj)) 147 | } else { 148 | // A slice of pointers 149 | obj := reflect.New(rawElement.Elem().Elem()) 150 | decoder.DecodePath(m, reflect.Indirect(obj)) 151 | indexVal := valSlice.Index(index) 152 | indexVal.Set(obj) 153 | } 154 | } 155 | 156 | // Set the new slice 157 | reflect.ValueOf(rawSlice).Elem().Set(valSlice) 158 | return nil 159 | } 160 | 161 | // NewDecoder returns a new decoder for the given configuration. Once 162 | // a decoder has been returned, the same configuration must not be used 163 | // again. 164 | func NewDecoder(config *DecoderConfig) (*Decoder, error) { 165 | val := reflect.ValueOf(config.Result) 166 | if val.Kind() != reflect.Ptr { 167 | return nil, errors.New("result must be a pointer") 168 | } 169 | 170 | val = val.Elem() 171 | if !val.CanAddr() { 172 | return nil, errors.New("result must be addressable (a pointer)") 173 | } 174 | 175 | if config.Metadata != nil { 176 | if config.Metadata.Keys == nil { 177 | config.Metadata.Keys = make([]string, 0) 178 | } 179 | 180 | if config.Metadata.Unused == nil { 181 | config.Metadata.Unused = make([]string, 0) 182 | } 183 | } 184 | 185 | if config.TagName == "" { 186 | config.TagName = "mapstructure" 187 | } 188 | 189 | result := &Decoder{ 190 | config: config, 191 | } 192 | 193 | return result, nil 194 | } 195 | 196 | // NewPathDecoder returns a new decoder for the given configuration. 197 | // This is used to decode path specific structures 198 | func NewPathDecoder(config *DecoderConfig) (*Decoder, error) { 199 | if config.Metadata != nil { 200 | if config.Metadata.Keys == nil { 201 | config.Metadata.Keys = make([]string, 0) 202 | } 203 | 204 | if config.Metadata.Unused == nil { 205 | config.Metadata.Unused = make([]string, 0) 206 | } 207 | } 208 | 209 | if config.TagName == "" { 210 | config.TagName = "mapstructure" 211 | } 212 | 213 | result := &Decoder{ 214 | config: config, 215 | } 216 | 217 | return result, nil 218 | } 219 | 220 | // Decode decodes the given raw interface to the target pointer specified 221 | // by the configuration. 222 | func (d *Decoder) Decode(raw interface{}) error { 223 | return d.decode("", raw, reflect.ValueOf(d.config.Result).Elem()) 224 | } 225 | 226 | // DecodePath decodes the raw interface against the map based on the 227 | // specified tags 228 | func (d *Decoder) DecodePath(m map[string]interface{}, rawVal interface{}) (bool, error) { 229 | decoded := false 230 | 231 | var val reflect.Value 232 | reflectRawValue := reflect.ValueOf(rawVal) 233 | kind := reflectRawValue.Kind() 234 | 235 | // Looking for structs and pointers to structs 236 | switch kind { 237 | case reflect.Ptr: 238 | val = reflectRawValue.Elem() 239 | if val.Kind() != reflect.Struct { 240 | return decoded, fmt.Errorf("Incompatible Type : %v : Looking For Struct", kind) 241 | } 242 | case reflect.Struct: 243 | var ok bool 244 | val, ok = rawVal.(reflect.Value) 245 | if ok == false { 246 | return decoded, fmt.Errorf("Incompatible Type : %v : Looking For reflect.Value", kind) 247 | } 248 | default: 249 | return decoded, fmt.Errorf("Incompatible Type : %v", kind) 250 | } 251 | 252 | // Iterate over the fields in the struct 253 | for i := 0; i < val.NumField(); i++ { 254 | valueField := val.Field(i) 255 | typeField := val.Type().Field(i) 256 | tag := typeField.Tag 257 | tagValue := tag.Get("jpath") 258 | 259 | // Is this a field without a tag 260 | if tagValue == "" { 261 | if valueField.Kind() == reflect.Struct { 262 | // We have a struct that may have indivdual tags. Process separately 263 | d.DecodePath(m, valueField) 264 | continue 265 | } else if valueField.Kind() == reflect.Ptr && reflect.TypeOf(valueField).Kind() == reflect.Struct { 266 | // We have a pointer to a struct 267 | if valueField.IsNil() { 268 | // Create the object since it doesn't exist 269 | valueField.Set(reflect.New(valueField.Type().Elem())) 270 | decoded, _ = d.DecodePath(m, valueField.Elem()) 271 | if decoded == false { 272 | // If nothing was decoded for this object return the pointer to nil 273 | valueField.Set(reflect.NewAt(valueField.Type().Elem(), nil)) 274 | } 275 | continue 276 | } 277 | 278 | d.DecodePath(m, valueField.Elem()) 279 | continue 280 | } 281 | } 282 | 283 | // Use mapstructure to populate the fields 284 | keys := strings.Split(tagValue, ".") 285 | data := d.findData(m, keys) 286 | if data != nil { 287 | if valueField.Kind() == reflect.Slice { 288 | // Ignore a slice of maps - This sucks but not sure how to check 289 | if strings.Contains(valueField.Type().String(), "map[") { 290 | goto normal_decode 291 | } 292 | 293 | // We have a slice 294 | mapSlice := data.([]interface{}) 295 | if len(mapSlice) > 0 { 296 | // Test if this is a slice of more maps 297 | _, ok := mapSlice[0].(map[string]interface{}) 298 | if ok == false { 299 | goto normal_decode 300 | } 301 | 302 | // Extract the maps out and run it through DecodeSlicePath 303 | ms := make([]map[string]interface{}, len(mapSlice)) 304 | for index, m2 := range mapSlice { 305 | ms[index] = m2.(map[string]interface{}) 306 | } 307 | 308 | DecodeSlicePath(ms, valueField.Addr().Interface()) 309 | continue 310 | } 311 | } 312 | normal_decode: 313 | decoded = true 314 | err := d.decode("", data, valueField) 315 | if err != nil { 316 | return false, err 317 | } 318 | } 319 | } 320 | 321 | return decoded, nil 322 | } 323 | 324 | // Decodes an unknown data type into a specific reflection value. 325 | func (d *Decoder) decode(name string, data interface{}, val reflect.Value) error { 326 | if data == nil { 327 | // If the data is nil, then we don't set anything. 328 | return nil 329 | } 330 | 331 | dataVal := reflect.ValueOf(data) 332 | if !dataVal.IsValid() { 333 | // If the data value is invalid, then we just set the value 334 | // to be the zero value. 335 | val.Set(reflect.Zero(val.Type())) 336 | return nil 337 | } 338 | 339 | if d.config.DecodeHook != nil { 340 | // We have a DecodeHook, so let's pre-process the data. 341 | var err error 342 | data, err = d.config.DecodeHook(d.getKind(dataVal), d.getKind(val), data) 343 | if err != nil { 344 | return err 345 | } 346 | } 347 | 348 | var err error 349 | dataKind := d.getKind(val) 350 | switch dataKind { 351 | case reflect.Bool: 352 | err = d.decodeBool(name, data, val) 353 | case reflect.Interface: 354 | err = d.decodeBasic(name, data, val) 355 | case reflect.String: 356 | err = d.decodeString(name, data, val) 357 | case reflect.Int: 358 | err = d.decodeInt(name, data, val) 359 | case reflect.Uint: 360 | err = d.decodeUint(name, data, val) 361 | case reflect.Float32: 362 | err = d.decodeFloat(name, data, val) 363 | case reflect.Struct: 364 | err = d.decodeStruct(name, data, val) 365 | case reflect.Map: 366 | err = d.decodeMap(name, data, val) 367 | case reflect.Slice: 368 | err = d.decodeSlice(name, data, val) 369 | default: 370 | // If we reached this point then we weren't able to decode it 371 | return fmt.Errorf("%s: unsupported type: %s", name, dataKind) 372 | } 373 | 374 | // If we reached here, then we successfully decoded SOMETHING, so 375 | // mark the key as used if we're tracking metadata. 376 | if d.config.Metadata != nil && name != "" { 377 | d.config.Metadata.Keys = append(d.config.Metadata.Keys, name) 378 | } 379 | 380 | return err 381 | } 382 | 383 | // findData locates the data by walking the keys down the map 384 | func (d *Decoder) findData(m map[string]interface{}, keys []string) interface{} { 385 | if len(keys) == 1 { 386 | if value, ok := m[keys[0]]; ok == true { 387 | return value 388 | } 389 | return nil 390 | } 391 | 392 | if value, ok := m[keys[0]]; ok == true { 393 | if m, ok := value.(map[string]interface{}); ok == true { 394 | return d.findData(m, keys[1:]) 395 | } 396 | } 397 | 398 | return nil 399 | } 400 | 401 | func (d *Decoder) getKind(val reflect.Value) reflect.Kind { 402 | kind := val.Kind() 403 | 404 | switch { 405 | case kind >= reflect.Int && kind <= reflect.Int64: 406 | return reflect.Int 407 | case kind >= reflect.Uint && kind <= reflect.Uint64: 408 | return reflect.Uint 409 | case kind >= reflect.Float32 && kind <= reflect.Float64: 410 | return reflect.Float32 411 | default: 412 | return kind 413 | } 414 | } 415 | 416 | // This decodes a basic type (bool, int, string, etc.) and sets the 417 | // value to "data" of that type. 418 | func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error { 419 | dataVal := reflect.ValueOf(data) 420 | dataValType := dataVal.Type() 421 | if !dataValType.AssignableTo(val.Type()) { 422 | return fmt.Errorf( 423 | "'%s' expected type '%s', got '%s'", 424 | name, val.Type(), dataValType) 425 | } 426 | 427 | val.Set(dataVal) 428 | return nil 429 | } 430 | 431 | func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error { 432 | dataVal := reflect.ValueOf(data) 433 | dataKind := d.getKind(dataVal) 434 | 435 | switch { 436 | case dataKind == reflect.String: 437 | val.SetString(dataVal.String()) 438 | case dataKind == reflect.Bool && d.config.WeaklyTypedInput: 439 | if dataVal.Bool() { 440 | val.SetString("1") 441 | } else { 442 | val.SetString("0") 443 | } 444 | case dataKind == reflect.Int && d.config.WeaklyTypedInput: 445 | val.SetString(strconv.FormatInt(dataVal.Int(), 10)) 446 | case dataKind == reflect.Uint && d.config.WeaklyTypedInput: 447 | val.SetString(strconv.FormatUint(dataVal.Uint(), 10)) 448 | case dataKind == reflect.Float32 && d.config.WeaklyTypedInput: 449 | val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64)) 450 | default: 451 | return fmt.Errorf( 452 | "'%s' expected type '%s', got unconvertible type '%s'", 453 | name, val.Type(), dataVal.Type()) 454 | } 455 | 456 | return nil 457 | } 458 | 459 | func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error { 460 | dataVal := reflect.ValueOf(data) 461 | dataKind := d.getKind(dataVal) 462 | 463 | switch { 464 | case dataKind == reflect.Int: 465 | val.SetInt(dataVal.Int()) 466 | case dataKind == reflect.Uint: 467 | val.SetInt(int64(dataVal.Uint())) 468 | case dataKind == reflect.Float32: 469 | val.SetInt(int64(dataVal.Float())) 470 | case dataKind == reflect.Bool && d.config.WeaklyTypedInput: 471 | if dataVal.Bool() { 472 | val.SetInt(1) 473 | } else { 474 | val.SetInt(0) 475 | } 476 | case dataKind == reflect.String && d.config.WeaklyTypedInput: 477 | i, err := strconv.ParseInt(dataVal.String(), 0, val.Type().Bits()) 478 | if err == nil { 479 | val.SetInt(i) 480 | } else { 481 | return fmt.Errorf("cannot parse '%s' as int: %s", name, err) 482 | } 483 | default: 484 | return fmt.Errorf( 485 | "'%s' expected type '%s', got unconvertible type '%s'", 486 | name, val.Type(), dataVal.Type()) 487 | } 488 | 489 | return nil 490 | } 491 | 492 | func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error { 493 | dataVal := reflect.ValueOf(data) 494 | dataKind := d.getKind(dataVal) 495 | 496 | switch { 497 | case dataKind == reflect.Int: 498 | val.SetUint(uint64(dataVal.Int())) 499 | case dataKind == reflect.Uint: 500 | val.SetUint(dataVal.Uint()) 501 | case dataKind == reflect.Float32: 502 | val.SetUint(uint64(dataVal.Float())) 503 | case dataKind == reflect.Bool && d.config.WeaklyTypedInput: 504 | if dataVal.Bool() { 505 | val.SetUint(1) 506 | } else { 507 | val.SetUint(0) 508 | } 509 | case dataKind == reflect.String && d.config.WeaklyTypedInput: 510 | i, err := strconv.ParseUint(dataVal.String(), 0, val.Type().Bits()) 511 | if err == nil { 512 | val.SetUint(i) 513 | } else { 514 | return fmt.Errorf("cannot parse '%s' as uint: %s", name, err) 515 | } 516 | default: 517 | return fmt.Errorf( 518 | "'%s' expected type '%s', got unconvertible type '%s'", 519 | name, val.Type(), dataVal.Type()) 520 | } 521 | 522 | return nil 523 | } 524 | 525 | func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error { 526 | dataVal := reflect.ValueOf(data) 527 | dataKind := d.getKind(dataVal) 528 | 529 | switch { 530 | case dataKind == reflect.Bool: 531 | val.SetBool(dataVal.Bool()) 532 | case dataKind == reflect.Int && d.config.WeaklyTypedInput: 533 | val.SetBool(dataVal.Int() != 0) 534 | case dataKind == reflect.Uint && d.config.WeaklyTypedInput: 535 | val.SetBool(dataVal.Uint() != 0) 536 | case dataKind == reflect.Float32 && d.config.WeaklyTypedInput: 537 | val.SetBool(dataVal.Float() != 0) 538 | case dataKind == reflect.String && d.config.WeaklyTypedInput: 539 | b, err := strconv.ParseBool(dataVal.String()) 540 | if err == nil { 541 | val.SetBool(b) 542 | } else if dataVal.String() == "" { 543 | val.SetBool(false) 544 | } else { 545 | return fmt.Errorf("cannot parse '%s' as bool: %s", name, err) 546 | } 547 | default: 548 | return fmt.Errorf( 549 | "'%s' expected type '%s', got unconvertible type '%s'", 550 | name, val.Type(), dataVal.Type()) 551 | } 552 | 553 | return nil 554 | } 555 | 556 | func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error { 557 | dataVal := reflect.ValueOf(data) 558 | dataKind := d.getKind(dataVal) 559 | 560 | switch { 561 | case dataKind == reflect.Int: 562 | val.SetFloat(float64(dataVal.Int())) 563 | case dataKind == reflect.Uint: 564 | val.SetFloat(float64(dataVal.Uint())) 565 | case dataKind == reflect.Float32: 566 | val.SetFloat(float64(dataVal.Float())) 567 | case dataKind == reflect.Bool && d.config.WeaklyTypedInput: 568 | if dataVal.Bool() { 569 | val.SetFloat(1) 570 | } else { 571 | val.SetFloat(0) 572 | } 573 | case dataKind == reflect.String && d.config.WeaklyTypedInput: 574 | f, err := strconv.ParseFloat(dataVal.String(), val.Type().Bits()) 575 | if err == nil { 576 | val.SetFloat(f) 577 | } else { 578 | return fmt.Errorf("cannot parse '%s' as float: %s", name, err) 579 | } 580 | default: 581 | return fmt.Errorf( 582 | "'%s' expected type '%s', got unconvertible type '%s'", 583 | name, val.Type(), dataVal.Type()) 584 | } 585 | 586 | return nil 587 | } 588 | 589 | func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) error { 590 | valType := val.Type() 591 | valKeyType := valType.Key() 592 | valElemType := valType.Elem() 593 | 594 | // Make a new map to hold our result 595 | mapType := reflect.MapOf(valKeyType, valElemType) 596 | valMap := reflect.MakeMap(mapType) 597 | 598 | // Check input type 599 | dataVal := reflect.Indirect(reflect.ValueOf(data)) 600 | if dataVal.Kind() != reflect.Map { 601 | // Accept empty array/slice instead of an empty map in weakly typed mode 602 | if d.config.WeaklyTypedInput && 603 | (dataVal.Kind() == reflect.Slice || dataVal.Kind() == reflect.Array) && 604 | dataVal.Len() == 0 { 605 | val.Set(valMap) 606 | return nil 607 | } 608 | 609 | return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind()) 610 | } 611 | 612 | // Accumulate errors 613 | var errors []string 614 | 615 | for _, k := range dataVal.MapKeys() { 616 | fieldName := fmt.Sprintf("%s[%s]", name, k) 617 | 618 | // First decode the key into the proper type 619 | currentKey := reflect.Indirect(reflect.New(valKeyType)) 620 | if err := d.decode(fieldName, k.Interface(), currentKey); err != nil { 621 | errors = appendErrors(errors, err) 622 | continue 623 | } 624 | 625 | // Next decode the data into the proper type 626 | v := dataVal.MapIndex(k).Interface() 627 | currentVal := reflect.Indirect(reflect.New(valElemType)) 628 | if err := d.decode(fieldName, v, currentVal); err != nil { 629 | errors = appendErrors(errors, err) 630 | continue 631 | } 632 | 633 | valMap.SetMapIndex(currentKey, currentVal) 634 | } 635 | 636 | // Set the built up map to the value 637 | val.Set(valMap) 638 | 639 | // If we had errors, return those 640 | if len(errors) > 0 { 641 | return &Error{errors} 642 | } 643 | 644 | return nil 645 | } 646 | 647 | func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value) error { 648 | dataVal := reflect.Indirect(reflect.ValueOf(data)) 649 | dataValKind := dataVal.Kind() 650 | valType := val.Type() 651 | valElemType := valType.Elem() 652 | 653 | // Make a new slice to hold our result, same size as the original data. 654 | sliceType := reflect.SliceOf(valElemType) 655 | valSlice := reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len()) 656 | 657 | // Check input type 658 | if dataValKind != reflect.Array && dataValKind != reflect.Slice { 659 | // Accept empty map instead of array/slice in weakly typed mode 660 | if d.config.WeaklyTypedInput && dataVal.Kind() == reflect.Map && dataVal.Len() == 0 { 661 | val.Set(valSlice) 662 | return nil 663 | } 664 | 665 | return fmt.Errorf("'%s': source data must be an array or slice, got %s", name, dataValKind) 666 | } 667 | 668 | // Accumulate any errors 669 | var errors []string 670 | 671 | for i := 0; i < dataVal.Len(); i++ { 672 | currentData := dataVal.Index(i).Interface() 673 | currentField := valSlice.Index(i) 674 | 675 | fieldName := fmt.Sprintf("%s[%d]", name, i) 676 | if err := d.decode(fieldName, currentData, currentField); err != nil { 677 | errors = appendErrors(errors, err) 678 | } 679 | } 680 | 681 | // Finally, set the value to the slice we built up 682 | val.Set(valSlice) 683 | 684 | // If there were errors, we return those 685 | if len(errors) > 0 { 686 | return &Error{errors} 687 | } 688 | 689 | return nil 690 | } 691 | 692 | func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value) error { 693 | dataVal := reflect.Indirect(reflect.ValueOf(data)) 694 | dataValKind := dataVal.Kind() 695 | if dataValKind != reflect.Map { 696 | return fmt.Errorf("'%s' expected a map, got '%s'", name, dataValKind) 697 | } 698 | 699 | dataValType := dataVal.Type() 700 | if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface { 701 | return fmt.Errorf( 702 | "'%s' needs a map with string keys, has '%s' keys", 703 | name, dataValType.Key().Kind()) 704 | } 705 | 706 | dataValKeys := make(map[reflect.Value]struct{}) 707 | dataValKeysUnused := make(map[interface{}]struct{}) 708 | for _, dataValKey := range dataVal.MapKeys() { 709 | dataValKeys[dataValKey] = struct{}{} 710 | dataValKeysUnused[dataValKey.Interface()] = struct{}{} 711 | } 712 | 713 | var errors []string 714 | 715 | // This slice will keep track of all the structs we'll be decoding. 716 | // There can be more than one struct if there are embedded structs 717 | // that are squashed. 718 | structs := make([]reflect.Value, 1, 5) 719 | structs[0] = val 720 | 721 | // Compile the list of all the fields that we're going to be decoding 722 | // from all the structs. 723 | fields := make(map[*reflect.StructField]reflect.Value) 724 | for len(structs) > 0 { 725 | structVal := structs[0] 726 | structs = structs[1:] 727 | 728 | structType := structVal.Type() 729 | for i := 0; i < structType.NumField(); i++ { 730 | fieldType := structType.Field(i) 731 | 732 | if fieldType.Anonymous { 733 | fieldKind := fieldType.Type.Kind() 734 | if fieldKind != reflect.Struct { 735 | errors = appendErrors(errors, 736 | fmt.Errorf("%s: unsupported type: %s", fieldType.Name, fieldKind)) 737 | continue 738 | } 739 | 740 | // We have an embedded field. We "squash" the fields down 741 | // if specified in the tag. 742 | squash := false 743 | tagParts := strings.Split(fieldType.Tag.Get(d.config.TagName), ",") 744 | for _, tag := range tagParts[1:] { 745 | if tag == "squash" { 746 | squash = true 747 | break 748 | } 749 | } 750 | 751 | if squash { 752 | structs = append(structs, val.FieldByName(fieldType.Name)) 753 | continue 754 | } 755 | } 756 | 757 | // Normal struct field, store it away 758 | fields[&fieldType] = structVal.Field(i) 759 | } 760 | } 761 | 762 | for fieldType, field := range fields { 763 | fieldName := fieldType.Name 764 | 765 | tagValue := fieldType.Tag.Get(d.config.TagName) 766 | tagValue = strings.SplitN(tagValue, ",", 2)[0] 767 | if tagValue != "" { 768 | fieldName = tagValue 769 | } 770 | 771 | rawMapKey := reflect.ValueOf(fieldName) 772 | rawMapVal := dataVal.MapIndex(rawMapKey) 773 | if !rawMapVal.IsValid() { 774 | // Do a slower search by iterating over each key and 775 | // doing case-insensitive search. 776 | for dataValKey := range dataValKeys { 777 | mK, ok := dataValKey.Interface().(string) 778 | if !ok { 779 | // Not a string key 780 | continue 781 | } 782 | 783 | if strings.EqualFold(mK, fieldName) { 784 | rawMapKey = dataValKey 785 | rawMapVal = dataVal.MapIndex(dataValKey) 786 | break 787 | } 788 | } 789 | 790 | if !rawMapVal.IsValid() { 791 | // There was no matching key in the map for the value in 792 | // the struct. Just ignore. 793 | continue 794 | } 795 | } 796 | 797 | // Delete the key we're using from the unused map so we stop tracking 798 | delete(dataValKeysUnused, rawMapKey.Interface()) 799 | 800 | if !field.IsValid() { 801 | // This should never happen 802 | panic("field is not valid") 803 | } 804 | 805 | // If we can't set the field, then it is unexported or something, 806 | // and we just continue onwards. 807 | if !field.CanSet() { 808 | continue 809 | } 810 | 811 | // If the name is empty string, then we're at the root, and we 812 | // don't dot-join the fields. 813 | if name != "" { 814 | fieldName = fmt.Sprintf("%s.%s", name, fieldName) 815 | } 816 | 817 | if err := d.decode(fieldName, rawMapVal.Interface(), field); err != nil { 818 | errors = appendErrors(errors, err) 819 | } 820 | } 821 | 822 | if d.config.ErrorUnused && len(dataValKeysUnused) > 0 { 823 | keys := make([]string, 0, len(dataValKeysUnused)) 824 | for rawKey := range dataValKeysUnused { 825 | keys = append(keys, rawKey.(string)) 826 | } 827 | sort.Strings(keys) 828 | 829 | err := fmt.Errorf("'%s' has invalid keys: %s", name, strings.Join(keys, ", ")) 830 | errors = appendErrors(errors, err) 831 | } 832 | 833 | if len(errors) > 0 { 834 | return &Error{errors} 835 | } 836 | 837 | // Add the unused keys to the list of unused keys if we're tracking metadata 838 | if d.config.Metadata != nil { 839 | for rawKey := range dataValKeysUnused { 840 | key := rawKey.(string) 841 | if name != "" { 842 | key = fmt.Sprintf("%s.%s", name, key) 843 | } 844 | 845 | d.config.Metadata.Unused = append(d.config.Metadata.Unused, key) 846 | } 847 | } 848 | 849 | return nil 850 | } 851 | -------------------------------------------------------------------------------- /mapstructure_test.go: -------------------------------------------------------------------------------- 1 | package mapstructure 2 | 3 | import ( 4 | "encoding/json" 5 | "reflect" 6 | "sort" 7 | "testing" 8 | ) 9 | 10 | type Basic struct { 11 | Vstring string 12 | Vint int 13 | Vuint uint 14 | Vbool bool 15 | Vfloat float64 16 | Vextra string 17 | vsilent bool 18 | Vdata interface{} 19 | } 20 | 21 | type Embedded struct { 22 | Basic 23 | Vunique string 24 | } 25 | 26 | type EmbeddedPointer struct { 27 | *Basic 28 | Vunique string 29 | } 30 | 31 | type EmbeddedSquash struct { 32 | Basic `mapstructure:",squash"` 33 | Vunique string 34 | } 35 | 36 | type Map struct { 37 | Vfoo string 38 | Vother map[string]string 39 | } 40 | 41 | type MapOfStruct struct { 42 | Value map[string]Basic 43 | } 44 | 45 | type Nested struct { 46 | Vfoo string 47 | Vbar Basic 48 | } 49 | 50 | type Slice struct { 51 | Vfoo string 52 | Vbar []string 53 | } 54 | 55 | type SliceOfStruct struct { 56 | Value []Basic 57 | } 58 | 59 | type Tagged struct { 60 | Extra string `mapstructure:"bar,what,what"` 61 | Value string `mapstructure:"foo"` 62 | } 63 | 64 | type TypeConversionResult struct { 65 | IntToFloat float32 66 | IntToUint uint 67 | IntToBool bool 68 | IntToString string 69 | UintToInt int 70 | UintToFloat float32 71 | UintToBool bool 72 | UintToString string 73 | BoolToInt int 74 | BoolToUint uint 75 | BoolToFloat float32 76 | BoolToString string 77 | FloatToInt int 78 | FloatToUint uint 79 | FloatToBool bool 80 | FloatToString string 81 | StringToInt int 82 | StringToUint uint 83 | StringToBool bool 84 | StringToFloat float32 85 | SliceToMap map[string]interface{} 86 | MapToSlice []interface{} 87 | } 88 | 89 | func TestBasicTypes(t *testing.T) { 90 | t.Parallel() 91 | 92 | input := map[string]interface{}{ 93 | "vstring": "foo", 94 | "vint": 42, 95 | "Vuint": 42, 96 | "vbool": true, 97 | "Vfloat": 42.42, 98 | "vsilent": true, 99 | "vdata": 42, 100 | } 101 | 102 | var result Basic 103 | err := Decode(input, &result) 104 | if err != nil { 105 | t.Errorf("got an err: %s", err.Error()) 106 | t.FailNow() 107 | } 108 | 109 | if result.Vstring != "foo" { 110 | t.Errorf("vstring value should be 'foo': %#v", result.Vstring) 111 | } 112 | 113 | if result.Vint != 42 { 114 | t.Errorf("vint value should be 42: %#v", result.Vint) 115 | } 116 | 117 | if result.Vuint != 42 { 118 | t.Errorf("vuint value should be 42: %#v", result.Vuint) 119 | } 120 | 121 | if result.Vbool != true { 122 | t.Errorf("vbool value should be true: %#v", result.Vbool) 123 | } 124 | 125 | if result.Vfloat != 42.42 { 126 | t.Errorf("vfloat value should be 42.42: %#v", result.Vfloat) 127 | } 128 | 129 | if result.Vextra != "" { 130 | t.Errorf("vextra value should be empty: %#v", result.Vextra) 131 | } 132 | 133 | if result.vsilent != false { 134 | t.Error("vsilent should not be set, it is unexported") 135 | } 136 | 137 | if result.Vdata != 42 { 138 | t.Error("vdata should be valid") 139 | } 140 | } 141 | 142 | func TestBasic_IntWithFloat(t *testing.T) { 143 | t.Parallel() 144 | 145 | input := map[string]interface{}{ 146 | "vint": float64(42), 147 | } 148 | 149 | var result Basic 150 | err := Decode(input, &result) 151 | if err != nil { 152 | t.Fatalf("got an err: %s", err) 153 | } 154 | } 155 | 156 | func TestDecode_Embedded(t *testing.T) { 157 | t.Parallel() 158 | 159 | input := map[string]interface{}{ 160 | "vstring": "foo", 161 | "Basic": map[string]interface{}{ 162 | "vstring": "innerfoo", 163 | }, 164 | "vunique": "bar", 165 | } 166 | 167 | var result Embedded 168 | err := Decode(input, &result) 169 | if err != nil { 170 | t.Fatalf("got an err: %s", err.Error()) 171 | } 172 | 173 | if result.Vstring != "innerfoo" { 174 | t.Errorf("vstring value should be 'innerfoo': %#v", result.Vstring) 175 | } 176 | 177 | if result.Vunique != "bar" { 178 | t.Errorf("vunique value should be 'bar': %#v", result.Vunique) 179 | } 180 | } 181 | 182 | func TestDecode_EmbeddedPointer(t *testing.T) { 183 | t.Parallel() 184 | 185 | input := map[string]interface{}{ 186 | "vstring": "foo", 187 | "Basic": map[string]interface{}{ 188 | "vstring": "innerfoo", 189 | }, 190 | "vunique": "bar", 191 | } 192 | 193 | var result EmbeddedPointer 194 | err := Decode(input, &result) 195 | if err == nil { 196 | t.Fatal("should get error") 197 | } 198 | } 199 | 200 | func TestDecode_EmbeddedSquash(t *testing.T) { 201 | t.Parallel() 202 | 203 | input := map[string]interface{}{ 204 | "vstring": "foo", 205 | "vunique": "bar", 206 | } 207 | 208 | var result EmbeddedSquash 209 | err := Decode(input, &result) 210 | if err != nil { 211 | t.Fatalf("got an err: %s", err.Error()) 212 | } 213 | 214 | if result.Vstring != "foo" { 215 | t.Errorf("vstring value should be 'foo': %#v", result.Vstring) 216 | } 217 | 218 | if result.Vunique != "bar" { 219 | t.Errorf("vunique value should be 'bar': %#v", result.Vunique) 220 | } 221 | } 222 | 223 | func TestDecode_DecodeHook(t *testing.T) { 224 | t.Parallel() 225 | 226 | input := map[string]interface{}{ 227 | "vint": "WHAT", 228 | } 229 | 230 | decodeHook := func(from reflect.Kind, to reflect.Kind, v interface{}) (interface{}, error) { 231 | if from == reflect.String && to != reflect.String { 232 | return 5, nil 233 | } 234 | 235 | return v, nil 236 | } 237 | 238 | var result Basic 239 | config := &DecoderConfig{ 240 | DecodeHook: decodeHook, 241 | Result: &result, 242 | } 243 | 244 | decoder, err := NewDecoder(config) 245 | if err != nil { 246 | t.Fatalf("err: %s", err) 247 | } 248 | 249 | err = decoder.Decode(input) 250 | if err != nil { 251 | t.Fatalf("got an err: %s", err) 252 | } 253 | 254 | if result.Vint != 5 { 255 | t.Errorf("vint should be 5: %#v", result.Vint) 256 | } 257 | } 258 | 259 | func TestDecode_Nil(t *testing.T) { 260 | t.Parallel() 261 | 262 | var input interface{} 263 | result := Basic{ 264 | Vstring: "foo", 265 | } 266 | 267 | err := Decode(input, &result) 268 | if err != nil { 269 | t.Fatalf("err: %s", err) 270 | } 271 | 272 | if result.Vstring != "foo" { 273 | t.Fatalf("bad: %#v", result.Vstring) 274 | } 275 | } 276 | 277 | func TestDecode_NonStruct(t *testing.T) { 278 | t.Parallel() 279 | 280 | input := map[string]interface{}{ 281 | "foo": "bar", 282 | "bar": "baz", 283 | } 284 | 285 | var result map[string]string 286 | err := Decode(input, &result) 287 | if err != nil { 288 | t.Fatalf("err: %s", err) 289 | } 290 | 291 | if result["foo"] != "bar" { 292 | t.Fatal("foo is not bar") 293 | } 294 | } 295 | 296 | func TestDecode_TypeConversion(t *testing.T) { 297 | input := map[string]interface{}{ 298 | "IntToFloat": 42, 299 | "IntToUint": 42, 300 | "IntToBool": 1, 301 | "IntToString": 42, 302 | "UintToInt": 42, 303 | "UintToFloat": 42, 304 | "UintToBool": 42, 305 | "UintToString": 42, 306 | "BoolToInt": true, 307 | "BoolToUint": true, 308 | "BoolToFloat": true, 309 | "BoolToString": true, 310 | "FloatToInt": 42.42, 311 | "FloatToUint": 42.42, 312 | "FloatToBool": 42.42, 313 | "FloatToString": 42.42, 314 | "StringToInt": "42", 315 | "StringToUint": "42", 316 | "StringToBool": "1", 317 | "StringToFloat": "42.42", 318 | "SliceToMap": []interface{}{}, 319 | "MapToSlice": map[string]interface{}{}, 320 | } 321 | 322 | expectedResultStrict := TypeConversionResult{ 323 | IntToFloat: 42.0, 324 | IntToUint: 42, 325 | UintToInt: 42, 326 | UintToFloat: 42, 327 | BoolToInt: 0, 328 | BoolToUint: 0, 329 | BoolToFloat: 0, 330 | FloatToInt: 42, 331 | FloatToUint: 42, 332 | } 333 | 334 | expectedResultWeak := TypeConversionResult{ 335 | IntToFloat: 42.0, 336 | IntToUint: 42, 337 | IntToBool: true, 338 | IntToString: "42", 339 | UintToInt: 42, 340 | UintToFloat: 42, 341 | UintToBool: true, 342 | UintToString: "42", 343 | BoolToInt: 1, 344 | BoolToUint: 1, 345 | BoolToFloat: 1, 346 | BoolToString: "1", 347 | FloatToInt: 42, 348 | FloatToUint: 42, 349 | FloatToBool: true, 350 | FloatToString: "42.42", 351 | StringToInt: 42, 352 | StringToUint: 42, 353 | StringToBool: true, 354 | StringToFloat: 42.42, 355 | SliceToMap: map[string]interface{}{}, 356 | MapToSlice: []interface{}{}, 357 | } 358 | 359 | // Test strict type conversion 360 | var resultStrict TypeConversionResult 361 | err := Decode(input, &resultStrict) 362 | if err == nil { 363 | t.Errorf("should return an error") 364 | } 365 | if !reflect.DeepEqual(resultStrict, expectedResultStrict) { 366 | t.Errorf("expected %v, got: %v", expectedResultStrict, resultStrict) 367 | } 368 | 369 | // Test weak type conversion 370 | var decoder *Decoder 371 | var resultWeak TypeConversionResult 372 | 373 | config := &DecoderConfig{ 374 | WeaklyTypedInput: true, 375 | Result: &resultWeak, 376 | } 377 | 378 | decoder, err = NewDecoder(config) 379 | if err != nil { 380 | t.Fatalf("err: %s", err) 381 | } 382 | 383 | err = decoder.Decode(input) 384 | if err != nil { 385 | t.Fatalf("got an err: %s", err) 386 | } 387 | 388 | if !reflect.DeepEqual(resultWeak, expectedResultWeak) { 389 | t.Errorf("expected \n%#v, got: \n%#v", expectedResultWeak, resultWeak) 390 | } 391 | } 392 | 393 | func TestDecoder_ErrorUnused(t *testing.T) { 394 | t.Parallel() 395 | 396 | input := map[string]interface{}{ 397 | "vstring": "hello", 398 | "foo": "bar", 399 | } 400 | 401 | var result Basic 402 | config := &DecoderConfig{ 403 | ErrorUnused: true, 404 | Result: &result, 405 | } 406 | 407 | decoder, err := NewDecoder(config) 408 | if err != nil { 409 | t.Fatalf("err: %s", err) 410 | } 411 | 412 | err = decoder.Decode(input) 413 | if err == nil { 414 | t.Fatal("expected error") 415 | } 416 | } 417 | 418 | func TestMap(t *testing.T) { 419 | t.Parallel() 420 | 421 | input := map[string]interface{}{ 422 | "vfoo": "foo", 423 | "vother": map[interface{}]interface{}{ 424 | "foo": "foo", 425 | "bar": "bar", 426 | }, 427 | } 428 | 429 | var result Map 430 | err := Decode(input, &result) 431 | if err != nil { 432 | t.Fatalf("got an error: %s", err) 433 | } 434 | 435 | if result.Vfoo != "foo" { 436 | t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo) 437 | } 438 | 439 | if result.Vother == nil { 440 | t.Fatal("vother should not be nil") 441 | } 442 | 443 | if len(result.Vother) != 2 { 444 | t.Error("vother should have two items") 445 | } 446 | 447 | if result.Vother["foo"] != "foo" { 448 | t.Errorf("'foo' key should be foo, got: %#v", result.Vother["foo"]) 449 | } 450 | 451 | if result.Vother["bar"] != "bar" { 452 | t.Errorf("'bar' key should be bar, got: %#v", result.Vother["bar"]) 453 | } 454 | } 455 | 456 | func TestMapOfStruct(t *testing.T) { 457 | t.Parallel() 458 | 459 | input := map[string]interface{}{ 460 | "value": map[string]interface{}{ 461 | "foo": map[string]string{"vstring": "one"}, 462 | "bar": map[string]string{"vstring": "two"}, 463 | }, 464 | } 465 | 466 | var result MapOfStruct 467 | err := Decode(input, &result) 468 | if err != nil { 469 | t.Fatalf("got an err: %s", err) 470 | } 471 | 472 | if result.Value == nil { 473 | t.Fatal("value should not be nil") 474 | } 475 | 476 | if len(result.Value) != 2 { 477 | t.Error("value should have two items") 478 | } 479 | 480 | if result.Value["foo"].Vstring != "one" { 481 | t.Errorf("foo value should be 'one', got: %s", result.Value["foo"].Vstring) 482 | } 483 | 484 | if result.Value["bar"].Vstring != "two" { 485 | t.Errorf("bar value should be 'two', got: %s", result.Value["bar"].Vstring) 486 | } 487 | } 488 | 489 | func TestNestedType(t *testing.T) { 490 | t.Parallel() 491 | 492 | input := map[string]interface{}{ 493 | "vfoo": "foo", 494 | "vbar": map[string]interface{}{ 495 | "vstring": "foo", 496 | "vint": 42, 497 | "vbool": true, 498 | }, 499 | } 500 | 501 | var result Nested 502 | err := Decode(input, &result) 503 | if err != nil { 504 | t.Fatalf("got an err: %s", err.Error()) 505 | } 506 | 507 | if result.Vfoo != "foo" { 508 | t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo) 509 | } 510 | 511 | if result.Vbar.Vstring != "foo" { 512 | t.Errorf("vstring value should be 'foo': %#v", result.Vbar.Vstring) 513 | } 514 | 515 | if result.Vbar.Vint != 42 { 516 | t.Errorf("vint value should be 42: %#v", result.Vbar.Vint) 517 | } 518 | 519 | if result.Vbar.Vbool != true { 520 | t.Errorf("vbool value should be true: %#v", result.Vbar.Vbool) 521 | } 522 | 523 | if result.Vbar.Vextra != "" { 524 | t.Errorf("vextra value should be empty: %#v", result.Vbar.Vextra) 525 | } 526 | } 527 | 528 | func TestNestedTypePointer(t *testing.T) { 529 | t.Parallel() 530 | 531 | input := map[string]interface{}{ 532 | "vfoo": "foo", 533 | "vbar": &map[string]interface{}{ 534 | "vstring": "foo", 535 | "vint": 42, 536 | "vbool": true, 537 | }, 538 | } 539 | 540 | var result Nested 541 | err := Decode(input, &result) 542 | if err != nil { 543 | t.Fatalf("got an err: %s", err.Error()) 544 | } 545 | 546 | if result.Vfoo != "foo" { 547 | t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo) 548 | } 549 | 550 | if result.Vbar.Vstring != "foo" { 551 | t.Errorf("vstring value should be 'foo': %#v", result.Vbar.Vstring) 552 | } 553 | 554 | if result.Vbar.Vint != 42 { 555 | t.Errorf("vint value should be 42: %#v", result.Vbar.Vint) 556 | } 557 | 558 | if result.Vbar.Vbool != true { 559 | t.Errorf("vbool value should be true: %#v", result.Vbar.Vbool) 560 | } 561 | 562 | if result.Vbar.Vextra != "" { 563 | t.Errorf("vextra value should be empty: %#v", result.Vbar.Vextra) 564 | } 565 | } 566 | 567 | func TestSlice(t *testing.T) { 568 | t.Parallel() 569 | 570 | inputStringSlice := map[string]interface{}{ 571 | "vfoo": "foo", 572 | "vbar": []string{"foo", "bar", "baz"}, 573 | } 574 | 575 | inputStringSlicePointer := map[string]interface{}{ 576 | "vfoo": "foo", 577 | "vbar": &[]string{"foo", "bar", "baz"}, 578 | } 579 | 580 | outputStringSlice := &Slice{ 581 | "foo", 582 | []string{"foo", "bar", "baz"}, 583 | } 584 | 585 | testSliceInput(t, inputStringSlice, outputStringSlice) 586 | testSliceInput(t, inputStringSlicePointer, outputStringSlice) 587 | } 588 | 589 | func TestSliceOfStruct(t *testing.T) { 590 | t.Parallel() 591 | 592 | input := map[string]interface{}{ 593 | "value": []map[string]interface{}{ 594 | {"vstring": "one"}, 595 | {"vstring": "two"}, 596 | }, 597 | } 598 | 599 | var result SliceOfStruct 600 | err := Decode(input, &result) 601 | if err != nil { 602 | t.Fatalf("got unexpected error: %s", err) 603 | } 604 | 605 | if len(result.Value) != 2 { 606 | t.Fatalf("expected two values, got %d", len(result.Value)) 607 | } 608 | 609 | if result.Value[0].Vstring != "one" { 610 | t.Errorf("first value should be 'one', got: %s", result.Value[0].Vstring) 611 | } 612 | 613 | if result.Value[1].Vstring != "two" { 614 | t.Errorf("second value should be 'two', got: %s", result.Value[1].Vstring) 615 | } 616 | } 617 | 618 | func TestInvalidType(t *testing.T) { 619 | t.Parallel() 620 | 621 | input := map[string]interface{}{ 622 | "vstring": 42, 623 | } 624 | 625 | var result Basic 626 | err := Decode(input, &result) 627 | if err == nil { 628 | t.Fatal("error should exist") 629 | } 630 | 631 | derr, ok := err.(*Error) 632 | if !ok { 633 | t.Fatalf("error should be kind of Error, instead: %#v", err) 634 | } 635 | 636 | if derr.Errors[0] != "'Vstring' expected type 'string', got unconvertible type 'int'" { 637 | t.Errorf("got unexpected error: %s", err) 638 | } 639 | } 640 | 641 | func TestMetadata(t *testing.T) { 642 | t.Parallel() 643 | 644 | input := map[string]interface{}{ 645 | "vfoo": "foo", 646 | "vbar": map[string]interface{}{ 647 | "vstring": "foo", 648 | "Vuint": 42, 649 | "foo": "bar", 650 | }, 651 | "bar": "nil", 652 | } 653 | 654 | var md Metadata 655 | var result Nested 656 | config := &DecoderConfig{ 657 | Metadata: &md, 658 | Result: &result, 659 | } 660 | 661 | decoder, err := NewDecoder(config) 662 | if err != nil { 663 | t.Fatalf("err: %s", err) 664 | } 665 | 666 | err = decoder.Decode(input) 667 | if err != nil { 668 | t.Fatalf("err: %s", err.Error()) 669 | } 670 | 671 | expectedKeys := []string{"Vfoo", "Vbar.Vstring", "Vbar.Vuint", "Vbar"} 672 | 673 | // We sort these keys here to ensure ethat the reflect.DeepEqual will act as 674 | // expected. 675 | sort.Sort(sort.StringSlice(md.Keys)) 676 | sort.Sort(sort.StringSlice(expectedKeys)) 677 | 678 | if !reflect.DeepEqual(md.Keys, expectedKeys) { 679 | t.Fatalf("bad keys: %#v %#v", md.Keys, expectedKeys) 680 | } 681 | 682 | expectedUnused := []string{"Vbar.foo", "bar"} 683 | 684 | // We sort these keys here to ensure ethat the reflect.DeepEqual will act as 685 | // expected. 686 | sort.Sort(sort.StringSlice(md.Unused)) 687 | sort.Sort(sort.StringSlice(expectedUnused)) 688 | 689 | if !reflect.DeepEqual(md.Unused, expectedUnused) { 690 | t.Fatalf("bad unused: %#v", md.Unused) 691 | } 692 | } 693 | 694 | func TestMetadata_Embedded(t *testing.T) { 695 | t.Parallel() 696 | 697 | input := map[string]interface{}{ 698 | "vstring": "foo", 699 | "vunique": "bar", 700 | } 701 | 702 | var md Metadata 703 | var result EmbeddedSquash 704 | config := &DecoderConfig{ 705 | Metadata: &md, 706 | Result: &result, 707 | } 708 | 709 | decoder, err := NewDecoder(config) 710 | if err != nil { 711 | t.Fatalf("err: %s", err) 712 | } 713 | 714 | err = decoder.Decode(input) 715 | if err != nil { 716 | t.Fatalf("err: %s", err.Error()) 717 | } 718 | 719 | expectedKeys := []string{"Vstring", "Vunique"} 720 | 721 | sort.Strings(md.Keys) 722 | if !reflect.DeepEqual(md.Keys, expectedKeys) { 723 | t.Fatalf("bad keys: %#v", md.Keys) 724 | } 725 | 726 | expectedUnused := []string{} 727 | if !reflect.DeepEqual(md.Unused, expectedUnused) { 728 | t.Fatalf("bad unused: %#v", md.Unused) 729 | } 730 | } 731 | 732 | func TestNonPtrValue(t *testing.T) { 733 | t.Parallel() 734 | 735 | err := Decode(map[string]interface{}{}, Basic{}) 736 | if err == nil { 737 | t.Fatal("error should exist") 738 | } 739 | 740 | if err.Error() != "result must be a pointer" { 741 | t.Errorf("got unexpected error: %s", err) 742 | } 743 | } 744 | 745 | func TestTagged(t *testing.T) { 746 | t.Parallel() 747 | 748 | input := map[string]interface{}{ 749 | "foo": "bar", 750 | "bar": "value", 751 | } 752 | 753 | var result Tagged 754 | err := Decode(input, &result) 755 | if err != nil { 756 | t.Fatalf("unexpected error: %s", err) 757 | } 758 | 759 | if result.Value != "bar" { 760 | t.Errorf("value should be 'bar', got: %#v", result.Value) 761 | } 762 | 763 | if result.Extra != "value" { 764 | t.Errorf("extra should be 'value', got: %#v", result.Extra) 765 | } 766 | } 767 | 768 | func testSliceInput(t *testing.T, input map[string]interface{}, expected *Slice) { 769 | var result Slice 770 | err := Decode(input, &result) 771 | if err != nil { 772 | t.Fatalf("got error: %s", err) 773 | } 774 | 775 | if result.Vfoo != expected.Vfoo { 776 | t.Errorf("Vfoo expected '%s', got '%s'", expected.Vfoo, result.Vfoo) 777 | } 778 | 779 | if result.Vbar == nil { 780 | t.Fatalf("Vbar a slice, got '%#v'", result.Vbar) 781 | } 782 | 783 | if len(result.Vbar) != len(expected.Vbar) { 784 | t.Errorf("Vbar length should be %d, got %d", len(expected.Vbar), len(result.Vbar)) 785 | } 786 | 787 | for i, v := range result.Vbar { 788 | if v != expected.Vbar[i] { 789 | t.Errorf( 790 | "Vbar[%d] should be '%#v', got '%#v'", 791 | i, expected.Vbar[i], v) 792 | } 793 | } 794 | } 795 | 796 | func TestDecodePath(t *testing.T) { 797 | document := `{ 798 | "userContext": { 799 | "cobrandId": 10000004, 800 | "channelId": -1, 801 | "locale": "en_US", 802 | "tncVersion": 2, 803 | "applicationId": "17CBE222A42161A3FF450E47CF4C1A00", 804 | "cobrandConversationCredentials": { 805 | "sessionToken": "06142010_1:b8d011fefbab8bf1753391b074ffedf9578612d676ed2b7f073b5785b" 806 | }, 807 | "preferenceInfo": { 808 | "currencyCode": "USD", 809 | "timeZone": "PST", 810 | "dateFormat": "MM/dd/yyyy", 811 | "currencyNotationType": { 812 | "currencyNotationType": "SYMBOL" 813 | }, 814 | "numberFormat": { 815 | "decimalSeparator": ".", 816 | "groupingSeparator": ",", 817 | "groupPattern": "###,##0.##" 818 | } 819 | } 820 | }, 821 | "loginName": "sptest1", 822 | "userId": 10483860, 823 | "userType": 824 | { 825 | "userTypeId": 1, 826 | "userTypeName": "normal_user" 827 | } 828 | }` 829 | 830 | type UserType struct { 831 | UserTypeID int 832 | UserTypeName string 833 | } 834 | 835 | type NumberFormat struct { 836 | DecimalSeparator string `jpath:"userContext.preferenceInfo.numberFormat.decimalSeparator"` 837 | GroupingSeparator string `jpath:"userContext.preferenceInfo.numberFormat.groupingSeparator"` 838 | GroupPattern string `jpath:"userContext.preferenceInfo.numberFormat.groupPattern"` 839 | } 840 | 841 | type User struct { 842 | Session string `jpath:"userContext.cobrandConversationCredentials.sessionToken"` 843 | CobrandID int `jpath:"userContext.cobrandId"` 844 | UserType UserType `jpath:"userType"` 845 | LoginName string `jpath:"loginName"` 846 | *NumberFormat 847 | } 848 | 849 | docScript := []byte(document) 850 | var docMap map[string]interface{} 851 | err := json.Unmarshal(docScript, &docMap) 852 | if err != nil { 853 | t.Fatalf("Unable To Unmarshal Test Document, %s", err) 854 | } 855 | 856 | var user User 857 | DecodePath(docMap, &user) 858 | 859 | session := "06142010_1:b8d011fefbab8bf1753391b074ffedf9578612d676ed2b7f073b5785b" 860 | if user.Session != session { 861 | t.Errorf("user.Session should be '%s', we got '%s'", session, user.Session) 862 | } 863 | 864 | cobrandID := 10000004 865 | if user.CobrandID != cobrandID { 866 | t.Errorf("user.CobrandId should be '%d', we got '%d'", cobrandID, user.CobrandID) 867 | } 868 | 869 | loginName := "sptest1" 870 | if user.LoginName != loginName { 871 | t.Errorf("user.LoginName should be '%s', we got '%s'", loginName, user.LoginName) 872 | } 873 | 874 | userTypeID := 1 875 | if user.UserType.UserTypeID != userTypeID { 876 | t.Errorf("user.UserType.UserTypeId should be '%d', we got '%d'", userTypeID, user.UserType.UserTypeID) 877 | } 878 | 879 | userTypeName := "normal_user" 880 | if user.UserType.UserTypeName != userTypeName { 881 | t.Errorf("user.UserType.UserTypeName should be '%s', we got '%s'", userTypeName, user.UserType.UserTypeName) 882 | } 883 | 884 | decimalSeparator := "." 885 | if user.NumberFormat.DecimalSeparator != decimalSeparator { 886 | t.Errorf("user.NumberFormat.DecimalSeparator should be '%s', we got '%s'", decimalSeparator, user.NumberFormat.DecimalSeparator) 887 | } 888 | 889 | groupingSeparator := "," 890 | if user.NumberFormat.GroupingSeparator != groupingSeparator { 891 | t.Errorf("user.NumberFormat.GroupingSeparator should be '%s', we got '%s'", groupingSeparator, user.NumberFormat.GroupingSeparator) 892 | } 893 | 894 | groupPattern := "###,##0.##" 895 | if user.NumberFormat.GroupPattern != groupPattern { 896 | t.Errorf("user.NumberFormat.GroupPattern should be '%s', we got '%s'", groupPattern, user.NumberFormat.GroupPattern) 897 | } 898 | } 899 | 900 | func TestDecodeSlicePath(t *testing.T) { 901 | var document = `[{"name":"bill"},{"name":"lisa"}]` 902 | 903 | type NameDoc struct { 904 | Name string `jpath:"name"` 905 | } 906 | 907 | sliceScript := []byte(document) 908 | var sliceMap []map[string]interface{} 909 | json.Unmarshal(sliceScript, &sliceMap) 910 | 911 | var myslice1 []NameDoc 912 | err1 := DecodeSlicePath(sliceMap, &myslice1) 913 | 914 | var myslice2 []*NameDoc 915 | err2 := DecodeSlicePath(sliceMap, &myslice2) 916 | 917 | name1 := "bill" 918 | name2 := "lisa" 919 | 920 | if err1 != nil { 921 | t.Fatal(err1) 922 | } 923 | 924 | if err2 != nil { 925 | t.Fatal(err1) 926 | } 927 | 928 | if myslice1[0].Name != name1 { 929 | t.Errorf("myslice1[0].Name should be '%s', we got '%s'", name1, myslice1[0].Name) 930 | } 931 | 932 | if myslice1[1].Name != name2 { 933 | t.Errorf("myslice1[1].Name should be '%s', we got '%s'", name1, myslice1[1].Name) 934 | } 935 | 936 | if myslice2[0].Name != name1 { 937 | t.Errorf("myslice2[0].Name should be '%s', we got '%s'", name1, myslice1[0].Name) 938 | } 939 | 940 | if myslice2[1].Name != name2 { 941 | t.Errorf("myslice1[1].Name should be '%s', we got '%s'", name2, myslice2[1].Name) 942 | } 943 | } 944 | 945 | func TestDecodeWithEmbeddedSlice(t *testing.T) { 946 | document := `{ 947 | "cobrandId": 10010352, 948 | "channelId": -1, 949 | "locale": "en_US", 950 | "tncVersion": 2, 951 | "categories":["rabbit","bunny","frog"], 952 | "people": [ 953 | { 954 | "name": "jack", 955 | "age": { 956 | "birth":10, 957 | "year":2000, 958 | "animals": [ 959 | { 960 | "barks":"yes", 961 | "tail":"yes" 962 | }, 963 | { 964 | "barks":"no", 965 | "tail":"yes" 966 | } 967 | ] 968 | } 969 | }, 970 | { 971 | "name": "jill", 972 | "age": { 973 | "birth":11, 974 | "year":2001 975 | } 976 | } 977 | ] 978 | }` 979 | 980 | type Animal struct { 981 | Barks string `jpath:"barks"` 982 | } 983 | 984 | type People struct { 985 | Age int `jpath:"age.birth"` 986 | Animals []Animal `jpath:"age.animals"` 987 | } 988 | 989 | type Items struct { 990 | Categories []string `jpath:"categories"` 991 | Peoples []People `jpath:"people"` 992 | } 993 | 994 | docScript := []byte(document) 995 | var docMap map[string]interface{} 996 | json.Unmarshal(docScript, &docMap) 997 | 998 | items := Items{} 999 | DecodePath(docMap, &items) 1000 | 1001 | if len(items.Categories) != 3 { 1002 | t.Error("items.Categories did not decode") 1003 | return 1004 | } 1005 | 1006 | if len(items.Peoples) != 2 { 1007 | t.Error("items.Peoples did not decode") 1008 | return 1009 | } 1010 | 1011 | if len(items.Peoples[0].Animals) != 2 { 1012 | t.Error("items.Peoples[0].Animals did not decode") 1013 | return 1014 | } 1015 | 1016 | age := 10 1017 | if items.Peoples[0].Age != 10 { 1018 | t.Errorf("items.Peoples[0].Age should be '%d', we got '%d'", age, items.Peoples[0].Age) 1019 | } 1020 | 1021 | barks := "yes" 1022 | if items.Peoples[0].Animals[0].Barks != barks { 1023 | t.Errorf("items.Peoples[0].Animals[0].Barks should be '%s', we got '%s'", barks, items.Peoples[0].Animals[0].Barks) 1024 | } 1025 | } 1026 | 1027 | func TestDecodeWithAbstractField(t *testing.T) { 1028 | var document = `{"Error":[{"errorDetail":"Invalid Cobrand Credentials"}]}` 1029 | 1030 | type AnError struct { 1031 | Error []map[string]interface{} `jpath:"Error"` 1032 | } 1033 | 1034 | type Context struct { 1035 | *AnError 1036 | } 1037 | 1038 | docScript := []byte(document) 1039 | var docMap map[string]interface{} 1040 | json.Unmarshal(docScript, &docMap) 1041 | 1042 | context := Context{} 1043 | DecodePath(docMap, &context) 1044 | 1045 | errorDetail := "Invalid Cobrand Credentials" 1046 | if context.Error[0]["errorDetail"].(string) != errorDetail { 1047 | t.Errorf("context.Error[0][\"errorDetail\"] should be '%s', we got '%s'", errorDetail, context.Error[0]["errorDetail"].(string)) 1048 | } 1049 | } 1050 | 1051 | func TestDecodePointerToPointer(t *testing.T) { 1052 | var document = `{"Error":[{"errorDetail":"Invalid Cobrand Credentials"}]}` 1053 | 1054 | type AnError struct { 1055 | Error []map[string]interface{} `jpath:"Error"` 1056 | } 1057 | 1058 | type APointerError struct { 1059 | *AnError 1060 | } 1061 | 1062 | type Context struct { 1063 | *APointerError 1064 | } 1065 | 1066 | docScript := []byte(document) 1067 | var docMap map[string]interface{} 1068 | json.Unmarshal(docScript, &docMap) 1069 | 1070 | var context Context 1071 | DecodePath(docMap, &context) 1072 | 1073 | errorDetail := "Invalid Cobrand Credentials" 1074 | if context.Error != nil { 1075 | if context.Error[0]["errorDetail"].(string) != errorDetail { 1076 | t.Errorf("context.Error[0][\"errorDetail\"] should be '%s', we got '%s'", errorDetail, context.Error[0]["errorDetail"].(string)) 1077 | } 1078 | } 1079 | } 1080 | --------------------------------------------------------------------------------