-
Notifications
You must be signed in to change notification settings - Fork 376
/
scalar.go
309 lines (279 loc) · 6.06 KB
/
scalar.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
package models
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"time"
"github.com/99designs/gqlgen/graphql"
)
// type ID struct {
// Table string
// ID string
// }
//
// func (id *ID) MarshalID(w io.Writer) {
// w.Write([]byte(base64.StdEncoding.EncodeToString([]byte(id.Table + ":" + id.ID))))
// }
//
// func (id *ID) UnmarshalID(v interface{}) error {
// switch v := v.(type) {
// case nil:
// return nil
// case string:
// decodedID, err := base64.StdEncoding.DecodeString(v)
// if err != nil {
// return err
// }
// splitedID := strings.SplitN(string(decodedID), ":", 2)
// id.Table = splitedID[0]
// id.ID = splitedID[1]
// return nil
// default:
// return nil
// }
// }
func MarshalID(v string) graphql.Marshaler {
return graphql.MarshalID(base64.StdEncoding.EncodeToString([]byte(v)))
}
func UnmarshalID(v interface{}) (string, error) {
switch v := v.(type) {
case nil:
return "", nil
case string:
id, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return "", err
}
return graphql.UnmarshalID(string(id))
default:
return graphql.UnmarshalID(v)
}
}
func MarshalString(v string) graphql.Marshaler {
return graphql.MarshalString(v)
}
func UnmarshalString(v interface{}) (string, error) {
switch v := v.(type) {
case nil:
return "", nil
default:
return graphql.UnmarshalString(v)
}
}
func MarshalTime(t time.Time) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.Quote(t.String()))
})
}
func UnmarshalTime(v interface{}) (time.Time, error) {
switch v := v.(type) {
case nil:
return time.Time{}, nil
case json.Number:
case string:
if len(v) == 0 {
return time.Time{}, nil
}
return time.Parse(time.RFC3339Nano, v)
default:
return time.Time{}, errors.New("time should be RFC3339Nano formatted string")
}
return graphql.UnmarshalTime(v)
}
func MarshalEnum(v int32) graphql.Marshaler {
return MarshalInt32(v)
}
func UnmarshalEnum(v interface{}) (int32, error) {
switch v := v.(type) {
case nil:
return 0, nil
case int:
return int32(v), nil
case int64:
return int32(v), nil
case int32:
return v, nil
case string:
res, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return 0, err
}
return int32(res), nil
case json.Number:
res, err := strconv.ParseInt(v.String(), 10, 32)
if err != nil {
return 0, err
}
return int32(res), nil
}
return 0, errors.New("not an enum")
}
func MarshalDouble(v float64) graphql.Marshaler {
return graphql.MarshalFloat(v)
}
func UnmarshalDouble(v interface{}) (float64, error) {
switch v := v.(type) {
case nil:
return 0.0, nil
default:
return graphql.UnmarshalFloat(v)
}
}
func MarshalInt64(v int64) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, err := io.WriteString(w, strconv.FormatInt(v, 10))
if err != nil {
logger().Error(err.Error())
return
}
})
}
func UnmarshalInt64(v interface{}) (int64, error) {
switch v := v.(type) {
case nil:
return 0, nil
case string:
case json.Number:
return strconv.ParseInt(string(v), 10, 64)
case int:
case int32:
return int64(v), nil
case int64:
return v, nil
default:
return 0, fmt.Errorf("%T is not an int64", v)
}
return 0, nil
}
func MarshalInt32(v int32) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, err := io.WriteString(w, strconv.Itoa(int(v)))
if err != nil {
logger().Error(err.Error())
return
}
})
}
func UnmarshalInt32(v interface{}) (int32, error) {
switch v := v.(type) {
case nil:
return 0, nil
case string:
case json.Number:
i, err := strconv.Atoi(string(v))
return int32(i), err
case int:
case int32:
return v, nil
case int64:
return int32(v), nil
default:
return 0, fmt.Errorf("%T is not an int32", v)
}
return 0, nil
}
func MarshalUint64(v uint64) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, err := io.WriteString(w, strconv.FormatUint(v, 10))
if err != nil {
logger().Error(err.Error())
return
}
})
}
func UnmarshalUint64(v interface{}) (uint64, error) {
switch v := v.(type) {
case nil:
return 0, nil
case string:
case json.Number:
return strconv.ParseUint(string(v), 10, 64)
case uint:
case uint32:
return uint64(v), nil
case uint64:
return v, nil
default:
return 0, fmt.Errorf("%T is not an uint64", v)
}
return 0, nil
}
func MarshalUint32(v uint32) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, err := io.WriteString(w, strconv.FormatUint(uint64(v), 10))
if err != nil {
logger().Error(err.Error())
return
}
})
}
func UnmarshalUint32(v interface{}) (uint32, error) {
switch v := v.(type) {
case nil:
return 0, nil
case string:
case json.Number:
i, err := strconv.ParseUint(string(v), 10, 32)
return uint32(i), err
case uint:
return uint32(v), nil
case uint32:
return v, nil
case uint64:
return uint32(v), nil
default:
return 0, fmt.Errorf("%T is not an uint64", v)
}
return 0, nil
}
func MarshalSint32(v int32) graphql.Marshaler {
return MarshalInt32(v)
}
func UnmarshalSint32(v interface{}) (int32, error) {
return UnmarshalInt32(v)
}
func MarshalFixed64(v uint64) graphql.Marshaler {
return MarshalUint64(v)
}
func UnmarshalFixed64(v interface{}) (uint64, error) {
return UnmarshalUint64(v)
}
func MarshalFixed32(v uint32) graphql.Marshaler {
return MarshalUint32(v)
}
func UnmarshalFixed32(v interface{}) (uint32, error) {
return UnmarshalUint32(v)
}
func MarshalBool(b bool) graphql.Marshaler {
return graphql.MarshalBoolean(b)
}
func UnmarshalBool(v interface{}) (bool, error) {
switch v := v.(type) {
case nil:
return false, nil
default:
return graphql.UnmarshalBoolean(v)
}
}
func MarshalByte(v byte) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, err := w.Write([]byte(strconv.FormatInt(int64(v), 10)))
if err != nil {
logger().Error(err.Error())
return
}
})
}
func UnmarshalByte(v interface{}) (byte, error) {
switch v := v.(type) {
default:
i, err := strconv.ParseInt(string(v.(json.Number)), 10, 8)
if err != nil {
return 0, err
}
return byte(i), nil
}
}