222 return d.decode("", input, reflect.ValueOf(d.config.Result).Elem()) |
222 return d.decode("", input, reflect.ValueOf(d.config.Result).Elem()) |
223 } |
223 } |
224 |
224 |
225 // Decodes an unknown data type into a specific reflection value. |
225 // Decodes an unknown data type into a specific reflection value. |
226 func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) error { |
226 func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) error { |
|
227 var inputVal reflect.Value |
|
228 if input != nil { |
|
229 inputVal = reflect.ValueOf(input) |
|
230 |
|
231 // We need to check here if input is a typed nil. Typed nils won't |
|
232 // match the "input == nil" below so we check that here. |
|
233 if inputVal.Kind() == reflect.Ptr && inputVal.IsNil() { |
|
234 input = nil |
|
235 } |
|
236 } |
|
237 |
227 if input == nil { |
238 if input == nil { |
228 // If the data is nil, then we don't set anything, unless ZeroFields is set |
239 // If the data is nil, then we don't set anything, unless ZeroFields is set |
229 // to true. |
240 // to true. |
230 if d.config.ZeroFields { |
241 if d.config.ZeroFields { |
231 outVal.Set(reflect.Zero(outVal.Type())) |
242 outVal.Set(reflect.Zero(outVal.Type())) |
258 return fmt.Errorf("error decoding '%s': %s", name, err) |
268 return fmt.Errorf("error decoding '%s': %s", name, err) |
259 } |
269 } |
260 } |
270 } |
261 |
271 |
262 var err error |
272 var err error |
263 inputKind := getKind(outVal) |
273 outputKind := getKind(outVal) |
264 switch inputKind { |
274 switch outputKind { |
265 case reflect.Bool: |
275 case reflect.Bool: |
266 err = d.decodeBool(name, input, outVal) |
276 err = d.decodeBool(name, input, outVal) |
267 case reflect.Interface: |
277 case reflect.Interface: |
268 err = d.decodeBasic(name, input, outVal) |
278 err = d.decodeBasic(name, input, outVal) |
269 case reflect.String: |
279 case reflect.String: |
286 err = d.decodeArray(name, input, outVal) |
296 err = d.decodeArray(name, input, outVal) |
287 case reflect.Func: |
297 case reflect.Func: |
288 err = d.decodeFunc(name, input, outVal) |
298 err = d.decodeFunc(name, input, outVal) |
289 default: |
299 default: |
290 // If we reached this point then we weren't able to decode it |
300 // If we reached this point then we weren't able to decode it |
291 return fmt.Errorf("%s: unsupported type: %s", name, inputKind) |
301 return fmt.Errorf("%s: unsupported type: %s", name, outputKind) |
292 } |
302 } |
293 |
303 |
294 // If we reached here, then we successfully decoded SOMETHING, so |
304 // If we reached here, then we successfully decoded SOMETHING, so |
295 // mark the key as used if we're tracking metainput. |
305 // mark the key as used if we're tracking metainput. |
296 if d.config.Metadata != nil && name != "" { |
306 if d.config.Metadata != nil && name != "" { |
304 // value to "data" of that type. |
314 // value to "data" of that type. |
305 func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error { |
315 func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error { |
306 if val.IsValid() && val.Elem().IsValid() { |
316 if val.IsValid() && val.Elem().IsValid() { |
307 return d.decode(name, data, val.Elem()) |
317 return d.decode(name, data, val.Elem()) |
308 } |
318 } |
|
319 |
309 dataVal := reflect.ValueOf(data) |
320 dataVal := reflect.ValueOf(data) |
|
321 |
|
322 // If the input data is a pointer, and the assigned type is the dereference |
|
323 // of that exact pointer, then indirect it so that we can assign it. |
|
324 // Example: *string to string |
|
325 if dataVal.Kind() == reflect.Ptr && dataVal.Type().Elem() == val.Type() { |
|
326 dataVal = reflect.Indirect(dataVal) |
|
327 } |
|
328 |
310 if !dataVal.IsValid() { |
329 if !dataVal.IsValid() { |
311 dataVal = reflect.Zero(val.Type()) |
330 dataVal = reflect.Zero(val.Type()) |
312 } |
331 } |
313 |
332 |
314 dataValType := dataVal.Type() |
333 dataValType := dataVal.Type() |
593 valElemType := valType.Elem() |
612 valElemType := valType.Elem() |
594 |
613 |
595 // Accumulate errors |
614 // Accumulate errors |
596 errors := make([]string, 0) |
615 errors := make([]string, 0) |
597 |
616 |
|
617 // If the input data is empty, then we just match what the input data is. |
|
618 if dataVal.Len() == 0 { |
|
619 if dataVal.IsNil() { |
|
620 if !val.IsNil() { |
|
621 val.Set(dataVal) |
|
622 } |
|
623 } else { |
|
624 // Set to empty allocated value |
|
625 val.Set(valMap) |
|
626 } |
|
627 |
|
628 return nil |
|
629 } |
|
630 |
598 for _, k := range dataVal.MapKeys() { |
631 for _, k := range dataVal.MapKeys() { |
599 fieldName := fmt.Sprintf("%s[%s]", name, k) |
632 fieldName := fmt.Sprintf("%s[%s]", name, k) |
600 |
633 |
601 // First decode the key into the proper type |
634 // First decode the key into the proper type |
602 currentKey := reflect.Indirect(reflect.New(valKeyType)) |
635 currentKey := reflect.Indirect(reflect.New(valKeyType)) |
704 |
737 |
705 return nil |
738 return nil |
706 } |
739 } |
707 |
740 |
708 func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) error { |
741 func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) error { |
|
742 // If the input data is nil, then we want to just set the output |
|
743 // pointer to be nil as well. |
|
744 isNil := data == nil |
|
745 if !isNil { |
|
746 switch v := reflect.Indirect(reflect.ValueOf(data)); v.Kind() { |
|
747 case reflect.Chan, |
|
748 reflect.Func, |
|
749 reflect.Interface, |
|
750 reflect.Map, |
|
751 reflect.Ptr, |
|
752 reflect.Slice: |
|
753 isNil = v.IsNil() |
|
754 } |
|
755 } |
|
756 if isNil { |
|
757 if !val.IsNil() && val.CanSet() { |
|
758 nilValue := reflect.New(val.Type()).Elem() |
|
759 val.Set(nilValue) |
|
760 } |
|
761 |
|
762 return nil |
|
763 } |
|
764 |
709 // Create an element of the concrete (non pointer) type and decode |
765 // Create an element of the concrete (non pointer) type and decode |
710 // into that. Then set the value of the pointer to this type. |
766 // into that. Then set the value of the pointer to this type. |
711 valType := val.Type() |
767 valType := val.Type() |
712 valElemType := valType.Elem() |
768 valElemType := valType.Elem() |
713 |
|
714 if val.CanSet() { |
769 if val.CanSet() { |
715 realVal := val |
770 realVal := val |
716 if realVal.IsNil() || d.config.ZeroFields { |
771 if realVal.IsNil() || d.config.ZeroFields { |
717 realVal = reflect.New(valElemType) |
772 realVal = reflect.New(valElemType) |
718 } |
773 } |
750 valElemType := valType.Elem() |
805 valElemType := valType.Elem() |
751 sliceType := reflect.SliceOf(valElemType) |
806 sliceType := reflect.SliceOf(valElemType) |
752 |
807 |
753 valSlice := val |
808 valSlice := val |
754 if valSlice.IsNil() || d.config.ZeroFields { |
809 if valSlice.IsNil() || d.config.ZeroFields { |
|
810 if d.config.WeaklyTypedInput { |
|
811 switch { |
|
812 // Slice and array we use the normal logic |
|
813 case dataValKind == reflect.Slice, dataValKind == reflect.Array: |
|
814 break |
|
815 |
|
816 // Empty maps turn into empty slices |
|
817 case dataValKind == reflect.Map: |
|
818 if dataVal.Len() == 0 { |
|
819 val.Set(reflect.MakeSlice(sliceType, 0, 0)) |
|
820 return nil |
|
821 } |
|
822 // Create slice of maps of other sizes |
|
823 return d.decodeSlice(name, []interface{}{data}, val) |
|
824 |
|
825 case dataValKind == reflect.String && valElemType.Kind() == reflect.Uint8: |
|
826 return d.decodeSlice(name, []byte(dataVal.String()), val) |
|
827 |
|
828 // All other types we try to convert to the slice type |
|
829 // and "lift" it into it. i.e. a string becomes a string slice. |
|
830 default: |
|
831 // Just re-try this function with data as a slice. |
|
832 return d.decodeSlice(name, []interface{}{data}, val) |
|
833 } |
|
834 } |
|
835 |
755 // Check input type |
836 // Check input type |
756 if dataValKind != reflect.Array && dataValKind != reflect.Slice { |
837 if dataValKind != reflect.Array && dataValKind != reflect.Slice { |
757 if d.config.WeaklyTypedInput { |
|
758 switch { |
|
759 // Empty maps turn into empty slices |
|
760 case dataValKind == reflect.Map: |
|
761 if dataVal.Len() == 0 { |
|
762 val.Set(reflect.MakeSlice(sliceType, 0, 0)) |
|
763 return nil |
|
764 } |
|
765 // Create slice of maps of other sizes |
|
766 return d.decodeSlice(name, []interface{}{data}, val) |
|
767 |
|
768 case dataValKind == reflect.String && valElemType.Kind() == reflect.Uint8: |
|
769 return d.decodeSlice(name, []byte(dataVal.String()), val) |
|
770 // All other types we try to convert to the slice type |
|
771 // and "lift" it into it. i.e. a string becomes a string slice. |
|
772 default: |
|
773 // Just re-try this function with data as a slice. |
|
774 return d.decodeSlice(name, []interface{}{data}, val) |
|
775 } |
|
776 } |
|
777 return fmt.Errorf( |
838 return fmt.Errorf( |
778 "'%s': source data must be an array or slice, got %s", name, dataValKind) |
839 "'%s': source data must be an array or slice, got %s", name, dataValKind) |
779 |
840 |
|
841 } |
|
842 |
|
843 // If the input value is empty, then don't allocate since non-nil != nil |
|
844 if dataVal.Len() == 0 { |
|
845 return nil |
780 } |
846 } |
781 |
847 |
782 // Make a new slice to hold our result, same size as the original data. |
848 // Make a new slice to hold our result, same size as the original data. |
783 valSlice = reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len()) |
849 valSlice = reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len()) |
784 } |
850 } |
886 val.Set(dataVal) |
952 val.Set(dataVal) |
887 return nil |
953 return nil |
888 } |
954 } |
889 |
955 |
890 dataValKind := dataVal.Kind() |
956 dataValKind := dataVal.Kind() |
891 if dataValKind != reflect.Map { |
957 switch dataValKind { |
892 return fmt.Errorf("'%s' expected a map, got '%s'", name, dataValKind) |
958 case reflect.Map: |
893 } |
959 return d.decodeStructFromMap(name, dataVal, val) |
894 |
960 |
|
961 case reflect.Struct: |
|
962 // Not the most efficient way to do this but we can optimize later if |
|
963 // we want to. To convert from struct to struct we go to map first |
|
964 // as an intermediary. |
|
965 m := make(map[string]interface{}) |
|
966 mval := reflect.Indirect(reflect.ValueOf(&m)) |
|
967 if err := d.decodeMapFromStruct(name, dataVal, mval, mval); err != nil { |
|
968 return err |
|
969 } |
|
970 |
|
971 result := d.decodeStructFromMap(name, mval, val) |
|
972 return result |
|
973 |
|
974 default: |
|
975 return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind()) |
|
976 } |
|
977 } |
|
978 |
|
979 func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) error { |
895 dataValType := dataVal.Type() |
980 dataValType := dataVal.Type() |
896 if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface { |
981 if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface { |
897 return fmt.Errorf( |
982 return fmt.Errorf( |
898 "'%s' needs a map with string keys, has '%s' keys", |
983 "'%s' needs a map with string keys, has '%s' keys", |
899 name, dataValType.Key().Kind()) |
984 name, dataValType.Key().Kind()) |