/
serialize.go
272 lines (235 loc) · 6.96 KB
/
serialize.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
package mongo
import (
"github.com/imulab/go-scim/core/prop"
"github.com/imulab/go-scim/core/spec"
"go.mongodb.org/mongo-driver/bson"
"math"
"strconv"
"time"
)
// Create an adapter to BSON that implements the bson.Marshaler interface so it can be directly
// feed to MongoDB driver methods.
func newBsonAdapter(resource *prop.Resource) bson.Marshaler {
return &bsonAdapter{resource: resource}
}
// Adapter of resource to bson.Marshaler
type bsonAdapter struct {
resource *prop.Resource
}
func (d *bsonAdapter) MarshalBSON() ([]byte, error) {
visitor := &serializer{
buf: make([]byte, 0),
stack: make([]*frame, 0),
}
err := d.resource.Visit(visitor)
if err != nil {
return nil, err
}
return visitor.buf, nil
}
// BSON serializer that implements prop.Visitor interface.
type serializer struct {
buf []byte
// Stack to keep track of traversal context.
// Context is pushed and popped only for container properties.
stack []*frame
}
func (s *serializer) ShouldVisit(property prop.Property) bool {
// In case of a top level unassigned complex property, which pushes the state mObject onto the frame,
// we cannot prevent the complex property itself from calling Visit/BeginComplex/EndComplex,
// because we still want it to be serialized as NULL. However, we wouldn't want to visit its sub properties,
// because we already know that are all unassigned.
if s.current().mode == mObject {
return !property.IsUnassigned()
}
return true
}
func (s *serializer) Visit(property prop.Property) error {
if property.Attribute().MultiValued() {
s.serializeMultiProperty(property)
return nil
}
switch property.Attribute().Type() {
case spec.TypeString, spec.TypeReference, spec.TypeBinary:
s.serializeStringProperty(property)
case spec.TypeDateTime:
s.serializeDateTimeProperty(property)
case spec.TypeInteger:
s.serializeIntegerProperty(property)
case spec.TypeDecimal:
s.serializeDecimalProperty(property)
case spec.TypeBoolean:
s.serializeBooleanProperty(property)
case spec.TypeComplex:
s.serializeComplexProperty(property)
default:
panic("invalid property type")
}
if property.Attribute().Type() != spec.TypeComplex {
s.current().index++
}
return nil
}
func (s *serializer) serializeComplexProperty(property prop.Property) {
// We only serialize the name and the kind here, because sub properties, if any, will be visited later.
if property.IsUnassigned() {
s.addName(0x0A, property.Attribute())
return
}
s.addName(0x03, property.Attribute())
}
func (s *serializer) serializeMultiProperty(property prop.Property) {
// First off, we only serialize the name and the kind here, because elements, if any, will
// be visited later.
//
// Secondly, we always serialize the multiValued property as an BSON array even if it's empty.
// This is consistent with the concept of "unassigned" in the specification: an empty
// multiValued property is considered to be unassigned while all other nulls are considered
// unassigned.
//
// By doing this, we also avoid one other problem: if we serialize an unassigned multiValued
// property as NULL, we will create a problem where the visitor calls BeginMulti and EndMulti
// while attempting to traverse an empty array. BeginMulti would have to reserve an int32
// element length marker, which is not consistent to the NULL type.
s.addName(0x04, property.Attribute())
}
func (s *serializer) serializeStringProperty(property prop.Property) {
if property.IsUnassigned() {
s.addName(0x0A, property.Attribute())
return
}
s.addName(0x02, property.Attribute())
value := property.Raw().(string)
s.addInt32(int32(len(value) + 1))
s.addBytes([]byte(value)...)
s.addBytes(0)
}
func (s *serializer) serializeDateTimeProperty(property prop.Property) {
if property.IsUnassigned() {
s.addName(0x0A, property.Attribute())
return
}
s.addName(0x09, property.Attribute())
// mongodb stores milliseconds
t, _ := time.Parse(prop.ISO8601, property.Raw().(string))
s.addInt64(t.Unix()*1000 + int64(t.Nanosecond()/1e6))
}
func (s *serializer) serializeIntegerProperty(property prop.Property) {
if property.IsUnassigned() {
s.addName(0x0A, property.Attribute())
return
}
s.addName(0x12, property.Attribute())
s.addInt64(property.Raw().(int64))
}
func (s *serializer) serializeDecimalProperty(property prop.Property) {
if property.IsUnassigned() {
s.addName(0x0A, property.Attribute())
return
}
s.addName(0x01, property.Attribute())
s.addInt64(int64(math.Float64bits(property.Raw().(float64))))
}
func (s *serializer) serializeBooleanProperty(property prop.Property) {
if property.IsUnassigned() {
s.addName(0x0A, property.Attribute())
return
}
s.addName(0x08, property.Attribute())
if property.Raw().(bool) {
s.addBytes(1)
} else {
s.addBytes(0)
}
}
func (s *serializer) BeginChildren(container prop.Container) {
if container.Attribute().MultiValued() {
s.push(mArray)
s.current().start = s.reserveInt32()
} else {
if len(s.stack) == 0 {
s.push(mTop)
} else {
s.push(mObject)
}
if !container.IsUnassigned() {
s.current().start = s.reserveInt32()
}
}
}
func (s *serializer) EndChildren(container prop.Container) {
if container.Attribute().MultiValued() {
s.addBytes(0)
start := s.current().start
s.setInt32(start, int32(len(s.buf)-start))
s.pop()
s.current().index++
} else {
if !container.IsUnassigned() {
s.addBytes(0)
start := s.current().start
s.setInt32(start, int32(len(s.buf)-start))
}
s.pop()
if len(s.stack) > 0 {
s.current().index++
}
}
}
func (s *serializer) addName(kind byte, attr *spec.Attribute) {
var name string
{
switch s.current().mode {
case mArray:
name = strconv.Itoa(s.current().index)
case mObject, mTop:
name = attr.Name()
if md, ok := metadataHub[attr.ID()]; ok {
name = md.MongoName
}
}
}
s.addBytes(kind)
s.addBytes([]byte(name)...)
s.addBytes(0)
}
func (s *serializer) addInt64(v int64) {
u := uint64(v)
s.addBytes(byte(u), byte(u>>8), byte(u>>16), byte(u>>24),
byte(u>>32), byte(u>>40), byte(u>>48), byte(u>>56))
}
// Reserve 4 bytes at current position to write the length of the data in the current frame later.
func (s *serializer) reserveInt32() (pos int) {
pos = len(s.buf)
s.addBytes(0, 0, 0, 0)
return
}
func (s *serializer) addInt32(v int32) {
u := uint32(v)
s.addBytes(byte(u), byte(u>>8), byte(u>>16), byte(u>>24))
}
// Add bytes to buffer
func (s *serializer) addBytes(v ...byte) {
s.buf = append(s.buf, v...)
}
// Set the int32 value at the previously reserved position from reserveInt32. This shall be called after
// container elements have exhausted and we now know the length of them.
func (s *serializer) setInt32(pos int, v int32) {
s.buf[pos+0] = byte(v)
s.buf[pos+1] = byte(v >> 8)
s.buf[pos+2] = byte(v >> 16)
s.buf[pos+3] = byte(v >> 24)
}
func (s *serializer) push(m mode) {
s.stack = append(s.stack, &frame{
mode: m,
index: 0,
start: 0,
})
}
func (s *serializer) pop() {
s.stack = s.stack[:len(s.stack)-1]
}
func (s *serializer) current() *frame {
return s.stack[len(s.stack)-1]
}