-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid.go
47 lines (44 loc) · 1.01 KB
/
uuid.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
package scalars
import (
"fmt"
"github.com/99designs/gqlgen/graphql"
"github.com/google/uuid"
"github.com/weavingwebs/wwgo"
"io"
)
func MarshalUUIDScalar(id *uuid.UUID) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
if id == nil {
_, _ = w.Write([]byte("null"))
return
}
b, _ := id.MarshalText()
_, _ = w.Write([]byte(`"`))
_, _ = w.Write(b)
_, _ = w.Write([]byte(`"`))
})
}
func UnmarshalUUIDScalar(v interface{}) (*uuid.UUID, error) {
if v == nil {
return nil, nil
}
val, err := func() (uuid.UUID, error) {
switch v := v.(type) {
case string:
res, err := uuid.Parse(v)
if err != nil {
return uuid.Nil, wwgo.NewClientError("GQL_UUID_PARSE_EXCEPTION", err.Error(), err)
}
return res, nil
case []byte:
res, err := uuid.ParseBytes(v)
if err != nil {
return uuid.Nil, wwgo.NewClientError("GQL_UUID_PARSE_EXCEPTION", err.Error(), err)
}
return res, nil
default:
return uuid.Nil, fmt.Errorf("%T is not a string", v)
}
}()
return &val, err
}