Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func (ce *ConverterEngine) Convert(source interface{}, targetType reflect.Type)
if ok {
return kind2Exact(stringer.String(), targetType), nil
}
if sourceType == nil {
return kind2Exact("", targetType), nil
}
// Convert to string typical value types
switch sourceType.Kind() {
case reflect.Bool:
Expand All @@ -191,6 +194,20 @@ func (ce *ConverterEngine) Convert(source interface{}, targetType reflect.Type)

}

if sourceType == nil {
switch targetType.Kind() {
case reflect.String:
return kind2Exact("", targetType), nil
case reflect.Bool:
return kind2Exact(false, targetType), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return kind2Exact(0, targetType), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return kind2Exact(0, targetType), nil
case reflect.Float32, reflect.Float64:
return kind2Exact(0.0, targetType), nil
}
}
if sourceType.Kind() == reflect.String {
// Attempt to parse typical value types from the string
switch targetType.Kind() {
Expand Down
15 changes: 15 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,34 @@ var testData = []ConversionTest{
"uno": 1,
"dos": 2,
"tres": 3,
"nil": nil,
}, map[string]int{
"uno": 1,
"dos": 2,
"tres": 3,
"nil": 0,
}, nil},
{map[string]interface{}{
"1": "uno",
"2": "dos",
"3": "tres",
"4": nil,
}, map[int]string{
1: "uno",
2: "dos",
3: "tres",
4: "",
}, nil},
{map[string]interface{}{
"uno": 1,
"dos": 2,
"tres": 3,
"nil": nil,
}, map[string]int{
"uno": 1,
"dos": 2,
"tres": 3,
"nil": 0,
}, nil},
{[]byte{65, 66, 67, 0}, "ABC\x00", nil},
{"ABC\x00", []byte{65, 66, 67, 0}, nil},
Expand Down