Skip to content
This repository was archived by the owner on Dec 9, 2022. It is now read-only.

Commit d7daa17

Browse files
committedNov 8, 2020
support array & map args
1 parent 514c936 commit d7daa17

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed
 

‎convert.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func GetRealValue(val *reflect.Value) interface{} {
2525
return val.Uint()
2626
case reflect.Float32, reflect.Float64:
2727
return val.Float()
28+
case reflect.Slice, reflect.Map:
29+
return val.Interface()
2830
default:
2931
return nil
3032
}

‎gocelery_test.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,6 @@ func TestBoolNamedArguments(t *testing.T) {
946946
// TestArrayIntNamedArguments tests successful function execution
947947
// with array arguments and integer return value
948948
func TestArrayIntNamedArguments(t *testing.T) {
949-
t.Skip("Bug(sickyoon): array as an argument returns nil: https://github.com/gocelery/gocelery/issues/74")
950949
testCases := []struct {
951950
name string
952951
broker CeleryBroker
@@ -1020,7 +1019,6 @@ func TestArrayIntNamedArguments(t *testing.T) {
10201019
// TestArray tests successful function execution
10211020
// with array arguments and return value
10221021
func TestArray(t *testing.T) {
1023-
t.Skip("Bug(sickyoon): array as an argument returns nil: https://github.com/gocelery/gocelery/issues/74")
10241022
testCases := []struct {
10251023
name string
10261024
broker CeleryBroker
@@ -1078,7 +1076,7 @@ func TestArray(t *testing.T) {
10781076
cli.StopWorker()
10791077
continue
10801078
}
1081-
if !reflect.DeepEqual(tc.expected, res.([]string)) {
1079+
if !reflect.DeepEqual(tc.expected, convertInterfaceToStringSlice(res)) {
10821080
t.Errorf("test '%s': returned result %+v is different from expected result %+v", tc.name, res, tc.expected)
10831081
}
10841082
cli.StopWorker()
@@ -1088,7 +1086,6 @@ func TestArray(t *testing.T) {
10881086
// TestMap tests successful function execution
10891087
// with map arguments and return value
10901088
func TestMap(t *testing.T) {
1091-
t.Skip("Bug(sickyoon): map as an argument returns nil: https://github.com/gocelery/gocelery/issues/74")
10921089
testCases := []struct {
10931090
name string
10941091
broker CeleryBroker
@@ -1146,7 +1143,7 @@ func TestMap(t *testing.T) {
11461143
cli.StopWorker()
11471144
continue
11481145
}
1149-
if !reflect.DeepEqual(tc.expected, res.(map[string]string)) {
1146+
if !reflect.DeepEqual(tc.expected, convertInterfaceToStringMap(res)) {
11501147
t.Errorf("test '%s': returned result %+v is different from expected result %+v", tc.name, res, tc.expected)
11511148
}
11521149
cli.StopWorker()
@@ -1398,18 +1395,12 @@ func (m *maxArrLenTask) ParseKwargs(kwargs map[string]interface{}) error {
13981395
if !ok {
13991396
return fmt.Errorf("undefined kwarg a")
14001397
}
1401-
m.a, ok = kwargA.([]string)
1402-
if !ok {
1403-
return fmt.Errorf("malformed kwarg a")
1404-
}
1398+
m.a = convertInterfaceToStringSlice(kwargA)
14051399
kwargB, ok := kwargs["b"]
14061400
if !ok {
14071401
return fmt.Errorf("undefined kwarg b")
14081402
}
1409-
m.b, ok = kwargB.([]string)
1410-
if !ok {
1411-
return fmt.Errorf("malformed kwarg b")
1412-
}
1403+
m.b = convertInterfaceToStringSlice(kwargB)
14131404
return nil
14141405
}
14151406

@@ -1437,3 +1428,21 @@ func addMap(a, b map[string]interface{}) map[string]interface{} {
14371428
}
14381429
return c
14391430
}
1431+
1432+
func convertInterfaceToStringSlice(i interface{}) []string {
1433+
is := i.([]interface{})
1434+
stringSlice := make([]string, len(is))
1435+
for i, v := range is {
1436+
stringSlice[i] = fmt.Sprint(v)
1437+
}
1438+
return stringSlice
1439+
}
1440+
1441+
func convertInterfaceToStringMap(i interface{}) map[string]string {
1442+
im := i.(map[string]interface{})
1443+
stringMap := make(map[string]string, len(im))
1444+
for i, v := range im {
1445+
stringMap[i] = fmt.Sprint(v)
1446+
}
1447+
return stringMap
1448+
}

0 commit comments

Comments
 (0)
This repository has been archived.