-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
166 lines (141 loc) · 4.2 KB
/
helpers.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package wampprotocli
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"github.com/xconnio/wampproto-go/serializers"
wampprotobuf "github.com/xconnio/wampproto-protobuf/go"
)
func HexToBase64(hexStr string) (string, error) {
bytes, err := hex.DecodeString(hexStr)
if err != nil {
return "", err
}
base64Str := base64.StdEncoding.EncodeToString(bytes)
return base64Str, nil
}
func EnsureBase64(str string) string {
base64Str, err := HexToBase64(str)
if err == nil {
return base64Str
}
_, err = base64.StdEncoding.DecodeString(str)
if err == nil {
return str
}
return base64.StdEncoding.EncodeToString([]byte(str))
}
func DecodeHexOrBase64(str string) ([]byte, error) {
bytes, err := hex.DecodeString(str)
if err == nil {
return bytes, nil
}
bytes, err = base64.StdEncoding.DecodeString(str)
if err == nil {
return bytes, nil
}
return nil, fmt.Errorf("must be in either hexadecimal or base64 format")
}
func FormatOutput(outputFormat, outputString string) (string, error) {
switch outputFormat {
case HexFormat, RawFormat:
return outputString, nil
case Base64Format:
base64Str, err := HexToBase64(outputString)
if err != nil {
return "", err
}
return base64Str, err
default:
return "", fmt.Errorf("invalid output format")
}
}
func FormatOutputBytes(outputFormat string, outputBytes []byte) (string, error) {
switch outputFormat {
case RawFormat:
return string(outputBytes), nil
case HexFormat:
return hex.EncodeToString(outputBytes), nil
case Base64Format:
return base64.StdEncoding.EncodeToString(outputBytes), nil
default:
return "", fmt.Errorf("invalid output format")
}
}
func SerializerByName(name string) serializers.Serializer {
switch name {
case JsonSerializer:
return &serializers.JSONSerializer{}
case CborSerializer:
return &serializers.CBORSerializer{}
case MsgpackSerializer:
return &serializers.MsgPackSerializer{}
case ProtobufSerializer:
return &wampprotobuf.ProtobufSerializer{}
}
return nil
}
func StringsToTypedList(strings []string) (typedList []any) {
typedList = make([]any, 0)
for _, value := range strings {
if number, errNumber := strconv.Atoi(value); errNumber == nil {
typedList = append(typedList, number)
} else if float, errFloat := strconv.ParseFloat(value, 64); errFloat == nil {
typedList = append(typedList, float)
} else if boolean, errBoolean := strconv.ParseBool(value); errBoolean == nil {
typedList = append(typedList, boolean)
} else {
var jsonMap map[string]any
var jsonMapList []map[string]any
var list []any
if errJson := json.Unmarshal([]byte(value), &jsonMap); errJson == nil {
typedList = append(typedList, jsonMap)
} else if errMapList := json.Unmarshal([]byte(value), &jsonMapList); errMapList == nil {
typedList = append(typedList, jsonMapList)
} else if errList := json.Unmarshal([]byte(value), &list); errList == nil {
typedList = append(typedList, list)
} else {
typedList = append(typedList, value)
}
}
}
return typedList
}
func StringMapToTypedMap(stringMap map[string]string) (typesMap map[string]any) {
typesMap = make(map[string]any)
for key, value := range stringMap {
if number, errNumber := strconv.Atoi(value); errNumber == nil {
typesMap[key] = number
} else if float, errFloat := strconv.ParseFloat(value, 64); errFloat == nil {
typesMap[key] = float
} else if boolean, errBoolean := strconv.ParseBool(value); errBoolean == nil {
typesMap[key] = boolean
} else {
var jsonMap map[string]any
var jsonMapList []map[string]any
var list []any
if errJson := json.Unmarshal([]byte(value), &jsonMap); errJson == nil {
typesMap[key] = jsonMap
} else if errMapList := json.Unmarshal([]byte(value), &jsonMapList); errMapList == nil {
typesMap[key] = jsonMapList
} else if errList := json.Unmarshal([]byte(value), &list); errList == nil {
typesMap[key] = list
} else {
typesMap[key] = value
}
}
}
return typesMap
}
func UpdateArgsKwArgsIfEmpty(arguments []any, kwargs map[string]any) ([]any, map[string]any) {
if len(kwargs) == 0 {
kwargs = nil
// should only be set to nil only if kwargs are also nil
if len(arguments) == 0 {
arguments = nil
}
}
return arguments, kwargs
}