vendor/github.com/mitchellh/mapstructure/mapstructure.go
changeset 251 1c52a0eeb952
parent 242 2a9ec03fe5a1
child 256 6d9efbef00a9
equal deleted inserted replaced
250:c040f992052f 251:1c52a0eeb952
   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()))
   235 			}
   246 			}
   236 		}
   247 		}
   237 		return nil
   248 		return nil
   238 	}
   249 	}
   239 
   250 
   240 	inputVal := reflect.ValueOf(input)
       
   241 	if !inputVal.IsValid() {
   251 	if !inputVal.IsValid() {
   242 		// If the input value is invalid, then we just set the value
   252 		// If the input value is invalid, then we just set the value
   243 		// to be the zero value.
   253 		// to be the zero value.
   244 		outVal.Set(reflect.Zero(outVal.Type()))
   254 		outVal.Set(reflect.Zero(outVal.Type()))
   245 		if d.config.Metadata != nil && name != "" {
   255 		if d.config.Metadata != nil && name != "" {
   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()
   321 	val.Set(dataVal)
   340 	val.Set(dataVal)
   322 	return nil
   341 	return nil
   323 }
   342 }
   324 
   343 
   325 func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error {
   344 func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error {
   326 	dataVal := reflect.ValueOf(data)
   345 	dataVal := reflect.Indirect(reflect.ValueOf(data))
   327 	dataKind := getKind(dataVal)
   346 	dataKind := getKind(dataVal)
   328 
   347 
   329 	converted := true
   348 	converted := true
   330 	switch {
   349 	switch {
   331 	case dataKind == reflect.String:
   350 	case dataKind == reflect.String:
   373 
   392 
   374 	return nil
   393 	return nil
   375 }
   394 }
   376 
   395 
   377 func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error {
   396 func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error {
   378 	dataVal := reflect.ValueOf(data)
   397 	dataVal := reflect.Indirect(reflect.ValueOf(data))
   379 	dataKind := getKind(dataVal)
   398 	dataKind := getKind(dataVal)
   380 	dataType := dataVal.Type()
   399 	dataType := dataVal.Type()
   381 
   400 
   382 	switch {
   401 	switch {
   383 	case dataKind == reflect.Int:
   402 	case dataKind == reflect.Int:
   415 
   434 
   416 	return nil
   435 	return nil
   417 }
   436 }
   418 
   437 
   419 func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error {
   438 func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error {
   420 	dataVal := reflect.ValueOf(data)
   439 	dataVal := reflect.Indirect(reflect.ValueOf(data))
   421 	dataKind := getKind(dataVal)
   440 	dataKind := getKind(dataVal)
   422 
   441 
   423 	switch {
   442 	switch {
   424 	case dataKind == reflect.Int:
   443 	case dataKind == reflect.Int:
   425 		i := dataVal.Int()
   444 		i := dataVal.Int()
   458 
   477 
   459 	return nil
   478 	return nil
   460 }
   479 }
   461 
   480 
   462 func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error {
   481 func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error {
   463 	dataVal := reflect.ValueOf(data)
   482 	dataVal := reflect.Indirect(reflect.ValueOf(data))
   464 	dataKind := getKind(dataVal)
   483 	dataKind := getKind(dataVal)
   465 
   484 
   466 	switch {
   485 	switch {
   467 	case dataKind == reflect.Bool:
   486 	case dataKind == reflect.Bool:
   468 		val.SetBool(dataVal.Bool())
   487 		val.SetBool(dataVal.Bool())
   489 
   508 
   490 	return nil
   509 	return nil
   491 }
   510 }
   492 
   511 
   493 func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
   512 func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
   494 	dataVal := reflect.ValueOf(data)
   513 	dataVal := reflect.Indirect(reflect.ValueOf(data))
   495 	dataKind := getKind(dataVal)
   514 	dataKind := getKind(dataVal)
   496 	dataType := dataVal.Type()
   515 	dataType := dataVal.Type()
   497 
   516 
   498 	switch {
   517 	switch {
   499 	case dataKind == reflect.Int:
   518 	case dataKind == reflect.Int:
   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())