Weakly typed convert []uint8 to string
diff --git a/mapstructure.go b/mapstructure.go
index cdd87c0..854eddf 100644
--- a/mapstructure.go
+++ b/mapstructure.go
@@ -234,6 +234,7 @@
dataVal := reflect.ValueOf(data)
dataKind := d.getKind(dataVal)
+ converted := true
switch {
case dataKind == reflect.String:
val.SetString(dataVal.String())
@@ -249,7 +250,20 @@
val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
+ case dataKind == reflect.Slice && d.config.WeaklyTypedInput:
+ dataType := dataVal.Type()
+ elemKind := dataType.Elem().Kind()
+ switch {
+ case elemKind == reflect.Uint8:
+ val.SetString(string(dataVal.Interface().([]uint8)))
+ default:
+ converted = false
+ }
default:
+ converted = false
+ }
+
+ if !converted {
return fmt.Errorf(
"'%s' expected type '%s', got unconvertible type '%s'",
name, val.Type(), dataVal.Type())
diff --git a/mapstructure_test.go b/mapstructure_test.go
index 6b83c66..e82d508 100644
--- a/mapstructure_test.go
+++ b/mapstructure_test.go
@@ -66,28 +66,29 @@
}
type TypeConversionResult struct {
- IntToFloat float32
- IntToUint uint
- IntToBool bool
- IntToString string
- UintToInt int
- UintToFloat float32
- UintToBool bool
- UintToString string
- BoolToInt int
- BoolToUint uint
- BoolToFloat float32
- BoolToString string
- FloatToInt int
- FloatToUint uint
- FloatToBool bool
- FloatToString string
- StringToInt int
- StringToUint uint
- StringToBool bool
- StringToFloat float32
- SliceToMap map[string]interface{}
- MapToSlice []interface{}
+ IntToFloat float32
+ IntToUint uint
+ IntToBool bool
+ IntToString string
+ UintToInt int
+ UintToFloat float32
+ UintToBool bool
+ UintToString string
+ BoolToInt int
+ BoolToUint uint
+ BoolToFloat float32
+ BoolToString string
+ FloatToInt int
+ FloatToUint uint
+ FloatToBool bool
+ FloatToString string
+ SliceUint8ToString string
+ StringToInt int
+ StringToUint uint
+ StringToBool bool
+ StringToFloat float32
+ SliceToMap map[string]interface{}
+ MapToSlice []interface{}
}
func TestBasicTypes(t *testing.T) {
@@ -299,28 +300,29 @@
func TestDecode_TypeConversion(t *testing.T) {
input := map[string]interface{}{
- "IntToFloat": 42,
- "IntToUint": 42,
- "IntToBool": 1,
- "IntToString": 42,
- "UintToInt": 42,
- "UintToFloat": 42,
- "UintToBool": 42,
- "UintToString": 42,
- "BoolToInt": true,
- "BoolToUint": true,
- "BoolToFloat": true,
- "BoolToString": true,
- "FloatToInt": 42.42,
- "FloatToUint": 42.42,
- "FloatToBool": 42.42,
- "FloatToString": 42.42,
- "StringToInt": "42",
- "StringToUint": "42",
- "StringToBool": "1",
- "StringToFloat": "42.42",
- "SliceToMap": []interface{}{},
- "MapToSlice": map[string]interface{}{},
+ "IntToFloat": 42,
+ "IntToUint": 42,
+ "IntToBool": 1,
+ "IntToString": 42,
+ "UintToInt": 42,
+ "UintToFloat": 42,
+ "UintToBool": 42,
+ "UintToString": 42,
+ "BoolToInt": true,
+ "BoolToUint": true,
+ "BoolToFloat": true,
+ "BoolToString": true,
+ "FloatToInt": 42.42,
+ "FloatToUint": 42.42,
+ "FloatToBool": 42.42,
+ "FloatToString": 42.42,
+ "SliceUint8ToString": []uint8("foo"),
+ "StringToInt": "42",
+ "StringToUint": "42",
+ "StringToBool": "1",
+ "StringToFloat": "42.42",
+ "SliceToMap": []interface{}{},
+ "MapToSlice": map[string]interface{}{},
}
expectedResultStrict := TypeConversionResult{
@@ -336,28 +338,29 @@
}
expectedResultWeak := TypeConversionResult{
- IntToFloat: 42.0,
- IntToUint: 42,
- IntToBool: true,
- IntToString: "42",
- UintToInt: 42,
- UintToFloat: 42,
- UintToBool: true,
- UintToString: "42",
- BoolToInt: 1,
- BoolToUint: 1,
- BoolToFloat: 1,
- BoolToString: "1",
- FloatToInt: 42,
- FloatToUint: 42,
- FloatToBool: true,
- FloatToString: "42.42",
- StringToInt: 42,
- StringToUint: 42,
- StringToBool: true,
- StringToFloat: 42.42,
- SliceToMap: map[string]interface{}{},
- MapToSlice: []interface{}{},
+ IntToFloat: 42.0,
+ IntToUint: 42,
+ IntToBool: true,
+ IntToString: "42",
+ UintToInt: 42,
+ UintToFloat: 42,
+ UintToBool: true,
+ UintToString: "42",
+ BoolToInt: 1,
+ BoolToUint: 1,
+ BoolToFloat: 1,
+ BoolToString: "1",
+ FloatToInt: 42,
+ FloatToUint: 42,
+ FloatToBool: true,
+ FloatToString: "42.42",
+ SliceUint8ToString: "foo",
+ StringToInt: 42,
+ StringToUint: 42,
+ StringToBool: true,
+ StringToFloat: 42.42,
+ SliceToMap: map[string]interface{}{},
+ MapToSlice: []interface{}{},
}
// Test strict type conversion