-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.go
64 lines (55 loc) · 1.25 KB
/
request.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
package expensify
import (
"encoding/json"
"reflect"
)
const (
jobTypeCreate = "create"
)
type jobRequest struct {
Type string `json:"type"`
Credentials struct {
PartnerUserID string `json:"partnerUserID"`
PartnerUserSecret string `json:"partnerUserSecret"`
} `json:"credentials"`
InputSettings *inputSettings `json:"inputSettings"`
}
type inputSettings struct {
Type string `json:"type"`
data interface{}
}
// MarshalJSON implements json.Marshaler.
func (i inputSettings) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{})
m["type"] = i.Type
if i.data != nil {
for k, v := range structToMap(i.data) {
m[k] = v
}
}
return json.Marshal(m)
}
func structToMap(item interface{}) map[string]interface{} {
res := map[string]interface{}{}
if item == nil {
return res
}
v := reflect.TypeOf(item)
reflectValue := reflect.ValueOf(item)
reflectValue = reflect.Indirect(reflectValue)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
for i := 0; i < v.NumField(); i++ {
tag := v.Field(i).Tag.Get("json")
field := reflectValue.Field(i).Interface()
if tag != "" && tag != "-" {
if v.Field(i).Type.Kind() == reflect.Struct {
res[tag] = structToMap(field)
} else {
res[tag] = field
}
}
}
return res
}