-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbson.go
121 lines (103 loc) · 2.56 KB
/
bson.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
package bson
import (
"bytes"
"sort"
"strconv"
)
// Marshaler is the interface implemented by types that
// can marshal themselves into valid BSON.
type Marshaler interface {
MarshalBSON() ([]byte, error)
}
// Appender is the interface implemented by types that
// can append marshaled BSON representation of itself.
type Appender interface {
AppendBSON([]byte) ([]byte, error)
}
// Marshal returns BSON encoding of v.
func Marshal(v any) ([]byte, error) {
buf := &bytes.Buffer{}
if err := NewEncoder(buf).Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// MarshalTo returns BSON encoding of v written to dst.
func MarshalTo(dst []byte, v any) ([]byte, error) {
enc := &Encoder{buf: dst}
if err := enc.marshal(v); err != nil {
return nil, err
}
return enc.buf, nil
}
// Unmarshaler is the interface implemented by types that
// can unmarshal a BSON representation of themselves.
//
// The input can be assumed to be a valid encoding of a BSON.
// UnmarshalBSON must copy the BSON data if it wishes to retain the data after returning.
type Unmarshaler interface {
UnmarshalBSON([]byte) error
}
// Unmarshal parses the BSON data and stores the result
// in the value pointed to by v.
func Unmarshal(data []byte, v any) error {
d := NewDecodeBytes(data)
if err := d.Decode(v); err != nil {
return err
}
return nil
}
// A is a BSON array.
//
// Example:
//
// bson.A{"hello", "world", 3.14159, bson.D{{"foo", 12345}}}
type A []any
func (a A) AsD() D {
d := make(D, len(a))
for i, v := range a {
d[i] = e{K: strconv.Itoa(i), V: v}
}
return d
}
// D is an ordered representation of a BSON document.
//
// Example usage:
//
// bson.D{{"hello", "world"}, {"foo", "bar"}, {"pi", 3.14159}}
type D []e
// e represents a BSON element for a D. It is usually used inside a D.
type e struct {
K string
V any
}
func (d D) Len() int { return len(d) }
func (d D) Less(i, j int) bool { return d[i].K < d[j].K }
func (d D) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (d D) AsM() M {
m := make(M, len(d))
for _, pair := range d {
m[pair.K] = pair.V
}
return m
}
// M is an unordered representation of a BSON document.
//
// Example usage:
//
// bson.M{"hello": "world", "foo": "bar", "pi": 3.14159}
type M map[string]any
func (m M) AsD() D {
d := make(D, len(m))
i := 0
for k, v := range m {
d[i] = e{K: k, V: v}
i++
}
sort.Sort(d)
return d
}
// RawArray represents a raw array which will be encoded or decoded as is.
type RawArray []byte
// RawObject represents a raw object which will be encoded or decoded as is.
type RawObject []byte