-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathresponse.go
165 lines (146 loc) · 3.51 KB
/
response.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
package runtime
import (
"reflect"
"strings"
"github.com/iancoleman/strcase"
)
func derefValue(v reflect.Value) reflect.Value {
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
return v
}
func MarshalResponse(resp interface{}) interface{} {
// If response is nil, nothing to do.
if resp == nil {
return nil
}
v := derefValue(reflect.ValueOf(resp))
switch v.Kind() {
case reflect.Struct:
return marshalStruct(v)
case reflect.Map:
return marshalMap(v)
case reflect.Slice:
return marshalSlice(v)
default:
return primitive(v)
}
}
// Marshal reflect value to []interface{} with lower camel case field
func marshalSlice(v reflect.Value) []interface{} {
size := v.Len()
ret := make([]interface{}, size)
for i := 0; i < size; i++ {
vv := derefValue(v.Index(i))
switch vv.Kind() {
case reflect.Struct:
ret[i] = marshalStruct(vv)
case reflect.Map:
ret[i] = marshalMap(vv)
case reflect.Slice:
ret[i] = marshalSlice(vv)
default:
ret[i] = primitive(vv)
}
}
return ret
}
// Marshal reflect value to map[string]interface{} with lower camel case field
func marshalStruct(v reflect.Value) map[string]interface{} {
ret := make(map[string]interface{})
t := v.Type()
for i := 0; i < t.NumField(); i++ {
// If "json" tag is not set in struct field, it's not related to response field
// So we can skip marshaling
tag := t.Field(i).Tag.Get("json")
if tag == "" {
continue
}
name := strcase.ToLowerCamel(strings.TrimSuffix(tag, ",omitempty"))
vv := derefValue(v.Field(i))
switch vv.Kind() {
case reflect.Struct:
ret[name] = marshalStruct(vv)
case reflect.Map:
ret[name] = marshalMap(vv)
case reflect.Slice:
ret[name] = marshalSlice(vv)
default:
ret[name] = primitive(vv)
}
}
return ret
}
type mapValue struct {
Key interface{} `json:"key"`
Value interface{} `json:"value"`
}
// Marshal reflect value to []mapValue with lower camel case field
// Note that in GraphQL, Protocol Buffers map structure should be marshaled to an array of key-value object
func marshalMap(v reflect.Value) []mapValue {
var ret []mapValue
iter := v.MapRange()
for iter.Next() {
key := iter.Key()
value := iter.Value()
kk := derefValue(key)
vv := derefValue(value)
mapItem := mapValue{}
switch kk.Kind() {
case reflect.Struct:
mapItem.Key = marshalStruct(kk)
case reflect.Map:
mapItem.Key = marshalMap(kk)
case reflect.Slice:
mapItem.Key = marshalSlice(kk)
default:
mapItem.Key = primitive(kk)
}
switch vv.Kind() {
case reflect.Struct:
mapItem.Value = marshalStruct(vv)
case reflect.Map:
mapItem.Value = marshalMap(vv)
case reflect.Slice:
mapItem.Value = marshalSlice(vv)
default:
mapItem.Value = primitive(vv)
}
ret = append(ret, mapItem)
}
return ret
}
// Get primitive type value
// Protobuf in Go only use a few scalar types.
// See: https://developers.google.com/protocol-buffers/docs/proto3#scalar
func primitive(v reflect.Value) interface{} {
// Guard by cheking IsValid due to prevent panic in runtime
if !v.IsValid() {
return nil
}
switch v.Type().Kind() {
case reflect.String:
return v.String()
case reflect.Bool:
return v.Bool()
case reflect.Int:
return int(v.Int())
case reflect.Int32:
return int32(v.Int())
case reflect.Int64:
return int64(v.Int())
case reflect.Uint:
return uint(v.Uint())
case reflect.Uint32:
return uint32(v.Uint())
case reflect.Uint64:
return uint64(v.Uint())
case reflect.Float32:
return float32(v.Float())
case reflect.Float64:
return float64(v.Float())
default:
return nil
}
}