-
Notifications
You must be signed in to change notification settings - Fork 6
/
marshal.go
165 lines (137 loc) · 4.44 KB
/
marshal.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 uuid
import (
"bytes"
)
// ErrNotJSONString occurs when attempting to parse an UUID from a JSON string
// which is too short or is missing quotes.
type ErrNotJSONString struct{}
func (e ErrNotJSONString) Error() string {
return "invalid UUID: invalid JSON string"
}
var nullByteString = []byte("null")
// MarshalText returns the string-representation of the UUID as a byte-array.
func (u UUID) MarshalText() ([]byte, error) {
/* Inlined UUID.String() implementation, cannot reuse the one from
UUID.String() as that cast will force an additional memory
allocation because current version the compiler (go 1.3.1) cannot
realize that the data pointer of the result of the UUID.String() call
can be moved to the byte-slice return. Speed difference from inlining
is about +20% */
/* Equivalent code:
return []byte(u.String()), nil */
/* NOTE: If this is changed, also look at the following methods:
UUID.MarshalJSON() and UUID.String() */
b := [36]byte{}
for i, n := range []int{
0, 2, 4, 6,
9, 11,
14, 16,
19, 21,
24, 26, 28, 30, 32, 34,
} {
b[n] = halfbyte2hexchar[(u[i]>>4)&0x0f]
b[n+1] = halfbyte2hexchar[u[i]&0x0f]
}
b[8] = '-'
b[13] = '-'
b[18] = '-'
b[23] = '-'
return b[:], nil
}
// MarshalJSON returns the string-representation of the UUID as a JSON-string.
func (u UUID) MarshalJSON() ([]byte, error) {
/* Needs a slightly different code yet inlined to prevent extra memory
allocations, it needs to be 38 bytes and the indices are shifted once
to the right to make room for quotation marks */
/* Equivalent code:
return []byte("\"" + u.String() + "\""), nil */
/* NOTE: If this is changed, also look at the following methods:
UUID.MarshalText() and UUID.String() */
b := [38]byte{}
for i, n := range []int{
1, 3, 5, 7,
10, 12,
15, 17,
20, 22,
25, 27, 29, 31, 33, 35,
} {
b[n] = halfbyte2hexchar[(u[i]>>4)&0x0f]
b[n+1] = halfbyte2hexchar[u[i]&0x0f]
}
b[0] = '"'
b[9] = '-'
b[14] = '-'
b[19] = '-'
b[24] = '-'
b[37] = '"'
return b[:], nil
}
// UnmarshalText reads an UUID from a string into the UUID instance.
// If this fails the state of the UUID is undetermined.
func (u *UUID) UnmarshalText(data []byte) error {
return u.ReadBytes(data)
}
// UnmarshalJSON reads an UUID from a JSON-string into the UUID instance.
// If this fails the state of the UUID is undetermined.
func (u *UUID) UnmarshalJSON(data []byte) error {
l := len(data)
if l < 2 || data[0] != '"' || data[l-1] != '"' {
return &ErrNotJSONString{}
}
return u.ReadBytes(data[1 : l-1])
}
// MarshalJSON marshals a potentially null UUID into either a string-
// representation of the UUID or the null-constant depending on the
// Valid property.
func (n NullUUID) MarshalJSON() ([]byte, error) {
if n.Valid {
return n.UUID.MarshalJSON()
} else {
return nullByteString, nil
}
}
// MarshalText marshals a potentially null UUID into either a string-
// representation of the UUID or the null-constant depending on the
// Valid property.
func (n NullUUID) MarshalText() ([]byte, error) {
if n.Valid {
return n.UUID.MarshalText()
} else {
return nullByteString, nil
}
}
// UnmarshalJSON parses a potentially null UUID into a NullUUI instance.
// If the source is the null-constant ("null"), Valid is set to false,
// otherwise it will attempt to parse the given string into the UUID property
// of the NullUUID instance, setting Valid to true if no error is encountered.
// If an error is encountered, Valid is set to false.
func (n *NullUUID) UnmarshalJSON(data []byte) error {
if bytes.Compare(data, nullByteString) == 0 {
n.Valid = false
return nil
}
dataLen := len(data)
if dataLen < 2 {
n.Valid = false
return nil
}
err := n.UUID.ReadBytes(data[1 : len(data)-1])
/* Because we modify in place we set Valid to false in case of an error */
n.Valid = err == nil
return err
}
// UnmarshalText parses a potentially null UUID into a NullUUI instance.
// If the source is the null-constant ("null"), Valid is set to false,
// otherwise it will attempt to parse the given string into the UUID property
// of the NullUUID instance, setting Valid to true if no error is encountered.
// If an error is encountered, Valid is set to false.
func (n *NullUUID) UnmarshalText(data []byte) error {
if bytes.Compare(data, nullByteString) == 0 {
n.Valid = false
return nil
}
err := n.UUID.ReadBytes(data)
/* Because we modify in place we set Valid to false in case of an error */
n.Valid = err == nil
return err
}