Skip to content

Commit 231f31b

Browse files
committed
feat: add decodeComplex
1 parent bec4b05 commit 231f31b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

mapstructure.go

+20
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) e
478478
err = d.decodeUint(name, input, outVal)
479479
case reflect.Float32:
480480
err = d.decodeFloat(name, input, outVal)
481+
case reflect.Complex64:
482+
err = d.decodeComplex(name, input, outVal)
481483
case reflect.Struct:
482484
err = d.decodeStruct(name, input, outVal)
483485
case reflect.Map:
@@ -796,6 +798,22 @@ func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value)
796798
return nil
797799
}
798800

801+
func (d *Decoder) decodeComplex(name string, data interface{}, val reflect.Value) error {
802+
dataVal := reflect.Indirect(reflect.ValueOf(data))
803+
dataKind := getKind(dataVal)
804+
805+
switch {
806+
case dataKind == reflect.Complex64:
807+
val.SetComplex(dataVal.Complex())
808+
default:
809+
return fmt.Errorf(
810+
"'%s' expected type '%s', got unconvertible type '%s', value: '%v'",
811+
name, val.Type(), dataVal.Type(), data)
812+
}
813+
814+
return nil
815+
}
816+
799817
func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) error {
800818
valType := val.Type()
801819
valKeyType := valType.Key()
@@ -1531,6 +1549,8 @@ func getKind(val reflect.Value) reflect.Kind {
15311549
return reflect.Uint
15321550
case kind >= reflect.Float32 && kind <= reflect.Float64:
15331551
return reflect.Float32
1552+
case kind >= reflect.Complex64 && kind <= reflect.Complex128:
1553+
return reflect.Complex64
15341554
default:
15351555
return kind
15361556
}

mapstructure_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type Basic struct {
2828
VjsonUint64 uint64
2929
VjsonFloat float64
3030
VjsonNumber json.Number
31+
Vcomplex64 complex64
32+
Vcomplex128 complex128
3133
}
3234

3335
type BasicPointer struct {
@@ -248,6 +250,8 @@ func TestBasicTypes(t *testing.T) {
248250
"vjsonUint64": json.Number("9223372036854775809"), // 2^63 + 1
249251
"vjsonFloat": json.Number("1234.5"),
250252
"vjsonNumber": json.Number("1234.5"),
253+
"vcomplex64": complex(float32(42), float32(42)),
254+
"vcomplex128": complex(42, 42),
251255
}
252256

253257
var result Basic
@@ -320,6 +324,14 @@ func TestBasicTypes(t *testing.T) {
320324
if !reflect.DeepEqual(result.VjsonNumber, json.Number("1234.5")) {
321325
t.Errorf("vjsonnumber value should be '1234.5': %T, %#v", result.VjsonNumber, result.VjsonNumber)
322326
}
327+
328+
if real(result.Vcomplex64) != 42 || imag(result.Vcomplex64) != 42 {
329+
t.Errorf("vcomplex64 value shou be 42+42i: %#v", result.Vcomplex64)
330+
}
331+
332+
if real(result.Vcomplex128) != 42 || imag(result.Vcomplex128) != 42 {
333+
t.Errorf("vcomplex64 value shou be 42+42i: %#v", result.Vcomplex128)
334+
}
323335
}
324336

325337
func TestBasic_IntWithFloat(t *testing.T) {
@@ -1933,6 +1945,8 @@ func TestDecodeTable(t *testing.T) {
19331945
"VjsonUint64": uint64(0),
19341946
"VjsonFloat": 0.0,
19351947
"VjsonNumber": json.Number(""),
1948+
"Vcomplex64": complex64(0),
1949+
"Vcomplex128": complex128(0),
19361950
},
19371951
false,
19381952
},
@@ -1975,6 +1989,8 @@ func TestDecodeTable(t *testing.T) {
19751989
"VjsonUint64": uint64(0),
19761990
"VjsonFloat": 0.0,
19771991
"VjsonNumber": json.Number(""),
1992+
"Vcomplex64": complex64(0),
1993+
"Vcomplex128": complex128(0),
19781994
},
19791995
},
19801996
false,

0 commit comments

Comments
 (0)