-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_uuid.go
60 lines (48 loc) · 1.15 KB
/
json_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
48
49
50
51
52
53
54
55
56
57
58
59
60
package kittycad
import (
"bytes"
"github.com/google/uuid"
)
// UUID is a wrapper around uuid.UUID which marshals to and from empty strings.
type UUID struct {
*uuid.UUID
}
// MarshalJSON implements the json.Marshaler interface.
func (u UUID) MarshalJSON() ([]byte, error) {
if u.UUID == nil {
return []byte("null"), nil
}
return []byte(`"` + u.UUID.String() + `"`), nil
}
func (u UUID) String() string {
if u.UUID == nil {
return ""
}
return u.UUID.String()
}
// ParseUUID parses a UUID from a string.
func ParseUUID(s string) UUID {
u, _ := uuid.Parse(s)
return UUID{&u}
}
// UnmarshalJSON implements the json.Unmarshaler interface.
// The time is expected to be a quoted string in RFC 3339 format.
func (u *UUID) UnmarshalJSON(data []byte) (err error) {
// By convention, unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.
if bytes.Equal(data, []byte("null")) {
return nil
}
if bytes.Equal(data, []byte("")) {
return nil
}
if bytes.Equal(data, []byte(`""`)) {
return nil
}
// Fractional seconds are handled implicitly by Parse.
uu, err := uuid.Parse(string(data))
if err != nil {
return err
}
*u = UUID{&uu}
return
}