-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathnumberstostring.go
88 lines (79 loc) · 1.92 KB
/
numberstostring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package json
import (
"bytes"
"encoding/json"
"fmt"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/expression/function"
)
func init() {
_ = function.Register(&fnNumbersToString{})
}
type fnNumbersToString struct {
}
// Name returns the name of the function
func (fnNumbersToString) Name() string {
return "numbersToString"
}
// Sig returns the function signature
func (fnNumbersToString) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeAny}, false
}
// Eval executes the function
func (fnNumbersToString) Eval(params ...interface{}) (interface{}, error) {
inputBytes, err := json.Marshal(params[0])
if err != nil {
return nil, err
}
reader := bytes.NewReader(inputBytes)
decoder := json.NewDecoder(reader)
decoder.UseNumber()
switch t := params[0].(type) {
case []interface{}:
outputArr := make([]interface{}, len(t))
err = decoder.Decode(&outputArr)
if err != nil {
return nil, err
}
return handleArray(outputArr), nil
case map[string]interface{}:
outputMap := make(map[string]interface{})
err = decoder.Decode(&outputMap)
if err != nil {
return nil, err
}
encodeNumbersToString(outputMap)
return outputMap, nil
default:
return nil, fmt.Errorf("Unsupported json object type [%T]", params[0])
}
}
func encodeNumbersToString(m map[string]interface{}) {
for k, v := range m {
switch t := v.(type) {
case json.Number:
m[k] = t.String()
case map[string]interface{}:
encodeNumbersToString(t)
case []interface{}:
m[k] = handleArray(t)
default:
fmt.Printf("Unsupported type: %T\n", v)
}
}
}
func handleArray(arr []interface{}) []interface{} {
for i, v := range arr {
switch t := v.(type) {
case json.Number:
arr[i] = t.String()
case map[string]interface{}:
encodeNumbersToString(t)
case []interface{}:
arr[i] = handleArray(t)
default:
fmt.Printf("Unsupported type inside array: %T\n", v)
}
}
return arr
}