-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
378 lines (336 loc) · 7.9 KB
/
types.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package graphql
import (
"context"
"encoding/json"
"fmt"
stringsi "github.com/hopeio/cherry/utils/strings"
"google.golang.org/protobuf/types/known/anypb"
"log"
"io"
"strconv"
)
type DummyResolver struct{}
func (r *DummyResolver) Dummy(ctx context.Context) (*bool, error) { return nil, nil }
func MarshalBytes(b []byte) Marshaler {
return WriterFunc(func(w io.Writer) {
_, _ = fmt.Fprintf(w, "%q", string(b))
})
}
func UnmarshalBytes(v interface{}) ([]byte, error) {
switch v := v.(type) {
case string:
return []byte(v), nil
case *string:
return []byte(*v), nil
case []byte:
return v, nil
case json.RawMessage:
return v, nil
default:
return nil, fmt.Errorf("%T is not []byte", v)
}
}
func MarshalAny(any anypb.Any) Marshaler {
return WriterFunc(func(w io.Writer) {
d, err := any.UnmarshalNew()
if err != nil {
log.Println("unable to unmarshal any: ", err)
return
}
if err := json.NewEncoder(w).Encode(d); err != nil {
log.Println("unable to encode json: ", err)
}
})
}
func UnmarshalAny(v interface{}) (anypb.Any, error) {
switch v := v.(type) {
case []byte:
return anypb.Any{}, nil //TODO add an unmarshal mechanism
case json.RawMessage:
return anypb.Any{}, nil
default:
return anypb.Any{}, fmt.Errorf("%T is not json.RawMessage", v)
}
}
func MarshalInt(i int) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.Itoa(i))
})
}
func UnmarshalInt(v interface{}) (int, error) {
switch v := v.(type) {
case string:
return strconv.Atoi(v)
case int:
return v, nil
case int64:
return int(v), nil
case json.Number:
return strconv.Atoi(string(v))
default:
return 0, fmt.Errorf("%T is not an int", v)
}
}
func MarshalInt8(any int8) Marshaler {
return WriterFunc(func(w io.Writer) {
_, _ = w.Write(stringsi.ToBytes(strconv.Itoa(int(any))))
})
}
func UnmarshalInt8(v interface{}) (int8, error) {
switch v := v.(type) {
case string:
iv, err := strconv.ParseInt(v, 10, 8)
return int8(iv), err
case int:
return int8(v), nil
case int64:
return int8(v), nil
case json.Number:
i, err := v.Int64()
return int8(i), err
default:
return 0, fmt.Errorf("%T is not int8", v)
}
}
func MarshalInt16(any int16) Marshaler {
return WriterFunc(func(w io.Writer) {
_, _ = w.Write(stringsi.ToBytes(strconv.Itoa(int(any))))
})
}
func UnmarshalInt16(v interface{}) (int16, error) {
switch v := v.(type) {
case string:
iv, err := strconv.ParseInt(v, 10, 16)
return int16(iv), err
case int:
return int16(v), nil
case int64:
return int16(v), nil
case json.Number:
i, err := v.Int64()
return int16(i), err
default:
return 0, fmt.Errorf("%T is not int16", v)
}
}
func MarshalInt32(i int32) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.FormatInt(int64(i), 10))
})
}
func UnmarshalInt32(v interface{}) (int32, error) {
switch v := v.(type) {
case string:
iv, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return 0, err
}
return int32(iv), nil
case int:
return int32(v), nil
case int64:
return int32(v), nil
case json.Number:
iv, err := strconv.ParseInt(string(v), 10, 32)
if err != nil {
return 0, err
}
return int32(iv), nil
default:
return 0, fmt.Errorf("%T is not an int", v)
}
}
func MarshalInt64(i int64) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.FormatInt(i, 10))
})
}
func UnmarshalInt64(v interface{}) (int64, error) {
switch v := v.(type) {
case string:
return strconv.ParseInt(v, 10, 64)
case int:
return int64(v), nil
case int64:
return v, nil
case json.Number:
return strconv.ParseInt(string(v), 10, 64)
default:
return 0, fmt.Errorf("%T is not an int", v)
}
}
func MarshalUint(any uint) Marshaler {
return WriterFunc(func(w io.Writer) {
_, _ = w.Write(stringsi.ToBytes(strconv.Itoa(int(any))))
})
}
func UnmarshalUint(v interface{}) (uint, error) {
switch v := v.(type) {
case string:
iv, err := strconv.ParseUint(v, 10, 64)
return uint(iv), err
case int:
return uint(v), nil
case int64:
return uint(v), nil //TODO add an unmarshal mechanism
case json.Number:
i, err := v.Int64()
return uint(i), err
default:
return 0, fmt.Errorf("%T is not uint", v)
}
}
func MarshalUint8(any uint8) Marshaler {
return WriterFunc(func(w io.Writer) {
_, _ = w.Write(stringsi.ToBytes(strconv.Itoa(int(any))))
})
}
func UnmarshalUint8(v interface{}) (uint8, error) {
switch v := v.(type) {
case string:
iv, err := strconv.ParseUint(v, 10, 8)
return uint8(iv), err
case int:
return uint8(v), nil
case int64:
return uint8(v), nil //TODO add an unmarshal mechanism
case json.Number:
i, err := v.Int64()
return uint8(i), err
default:
return 0, fmt.Errorf("%T is not uint64", v)
}
}
func MarshalUint16(any uint16) Marshaler {
return WriterFunc(func(w io.Writer) {
_, _ = w.Write(stringsi.ToBytes(strconv.Itoa(int(any))))
})
}
func UnmarshalUint16(v interface{}) (uint16, error) {
switch v := v.(type) {
case string:
iv, err := strconv.ParseUint(v, 10, 16)
return uint16(iv), err
case int:
return uint16(v), nil
case int64:
return uint16(v), nil //TODO add an unmarshal mechanism
case json.Number:
i, err := v.Int64()
return uint16(i), err
default:
return 0, fmt.Errorf("%T is not uint64", v)
}
}
func MarshalUint32(any uint32) Marshaler {
return WriterFunc(func(w io.Writer) {
_, _ = w.Write([]byte(strconv.Itoa(int(any))))
})
}
func UnmarshalUint32(v interface{}) (uint32, error) {
switch v := v.(type) {
case string:
iv, err := strconv.ParseUint(v, 10, 32)
return uint32(iv), err
case int:
return uint32(v), nil
case int64:
return uint32(v), nil
case json.Number:
i, err := v.Int64()
return uint32(i), err
default:
return 0, fmt.Errorf("%T is not int32", v)
}
}
func MarshalUint64(i uint64) Marshaler {
return WriterFunc(func(w io.Writer) {
w.Write(stringsi.ToBytes(strconv.FormatUint(i, 10)))
})
}
func UnmarshalUint64(v interface{}) (uint64, error) {
switch v := v.(type) {
case string:
return strconv.ParseUint(v, 10, 64)
case int:
return uint64(v), nil
case int64:
return uint64(v), nil
case json.Number:
return strconv.ParseUint(string(v), 10, 64)
default:
return 0, fmt.Errorf("%T is not an int", v)
}
}
func MarshalFloat32(any float32) Marshaler {
return WriterFunc(func(w io.Writer) {
_, _ = w.Write(stringsi.ToBytes(strconv.Itoa(int(any))))
})
}
func UnmarshalFloat32(v interface{}) (float32, error) {
switch v := v.(type) {
case string:
iv, err := strconv.ParseFloat(v, 32)
return float32(iv), err
case float64:
return float32(v), nil
case json.Number:
f, err := v.Float64()
return float32(f), err
default:
return 0, fmt.Errorf("%T is not float32", v)
}
}
func MarshalFloat64(any float32) Marshaler {
return WriterFunc(func(w io.Writer) {
_, _ = w.Write(stringsi.ToBytes(strconv.FormatFloat(float64(any), 'g', -1, 64)))
})
}
func UnmarshalFloat64(v interface{}) (float64, error) {
switch v := v.(type) {
case string:
return strconv.ParseFloat(v, 64)
case float64:
return v, nil
case json.Number:
f, err := v.Float64()
return f, err
default:
return 0, fmt.Errorf("%T is not float32", v)
}
}
type Marshaler interface {
MarshalGQL(w io.Writer)
}
type WriterFunc func(writer io.Writer)
func (f WriterFunc) MarshalGQL(w io.Writer) {
f(w)
}
func MarshalHeader(val map[string]string) Marshaler {
return WriterFunc(func(w io.Writer) {
err := json.NewEncoder(w).Encode(val)
if err != nil {
panic(err)
}
})
}
func UnmarshalHeader(v interface{}) (map[string]string, error) {
if m, ok := v.(map[string]string); ok {
return m, nil
}
return nil, fmt.Errorf("%T is not a map", v)
}
func MarshalMap(val map[string]interface{}) Marshaler {
return WriterFunc(func(w io.Writer) {
err := json.NewEncoder(w).Encode(val)
if err != nil {
panic(err)
}
})
}
func UnmarshalMap(v interface{}) (map[string]interface{}, error) {
if m, ok := v.(map[string]interface{}); ok {
return m, nil
}
return nil, fmt.Errorf("%T is not a map", v)
}