-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_test.go
158 lines (128 loc) · 3.07 KB
/
array_test.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
package bsony
import (
"fmt"
"testing"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func testArrayAdd(t *testing.T, addCases []AddTestCase) {
// Array.Add is a wrapper around Doc.Add, so we can use the same input data.
t.Run("Array.Add", func(t *testing.T) {
for _, c := range addCases {
// replace key in document with "0" (ASCII 0x30)
aHex := c.D[0:10] + "30" + c.D[12:]
a := fct.NewArray()
a.Add(c.V)
compareArrayHex(t, a, aHex, c.L)
a.Release()
}
})
// Test adding to array with type-specific functions. This is needed
// because Doc.Add delegates to the type-specific adders, but Array.Add
// delegates to Doc.Add. Therefore, we have to do our own type-specific
// test for arrays.
t.Run("Array.AddType", func(t *testing.T) {
for _, c := range addCases {
// replace key in document with "0" (ASCII 0x30)
aHex := c.D[0:10] + "30" + c.D[12:]
a := fct.NewArray()
if addByType(a, c.V) {
compareArrayHex(t, a, aHex, c.L)
}
a.Release()
}
})
}
// Switch adapted from Doc.Add and will need to be kept in sync if new types
// are added.
func addByType(a *Array, v interface{}) bool {
switch x := v.(type) {
// Type 01 - double
case float32:
a.AddDouble(float64(x))
case float64:
a.AddDouble(x)
// Type 02 - string
case string:
a.AddString(x)
// Type 03 - document
case Doc:
a.AddDoc(&x)
case *Doc:
a.AddDoc(x)
// Type 04 - array
case Array:
a.AddArray(&x)
case *Array:
a.AddArray(x)
// Type 05 - binary
case primitive.Binary:
a.AddBinary(&x)
case *primitive.Binary:
a.AddBinary(x)
// Type 06 - undefined (deprecated)
case primitive.Undefined:
a.AddUndefined()
// Type 07 - ObjectID
case primitive.ObjectID:
a.AddOID(x)
// Type 08 - boolean
case bool:
a.AddBool(x)
// Type 09 - UTC DateTime
case primitive.DateTime:
a.AddDateTime(x)
case time.Time:
a.AddDateTimeFromTime(x)
case *time.Time:
a.AddDateTimeFromTime(*x)
// Type 0A - null
case nil:
a.AddNull()
// Type 0B - regular expression
case primitive.Regex:
a.AddRegex(x)
case *primitive.Regex:
a.AddRegex(*x)
// Type 0C - DBPointer (deprecated)
case primitive.DBPointer:
a.AddDBPointer(x)
case *primitive.DBPointer:
a.AddDBPointer(*x)
// Type 0D - JavaScript code
case primitive.JavaScript:
a.AddJavaScript(x)
// Type 0E - Symbol (deprecated)
case primitive.Symbol:
a.AddSymbol(x)
// Type 0F - JavaScript code with scope
case CodeWithScope:
a.AddCodeScope(x)
case primitive.CodeWithScope:
// AddCodeScope doesn't convert this, only Add, so skip test
return false
// Type 10 - 32-bit integer
case int32:
a.AddInt32(x)
// Type 11 - timestamp
case primitive.Timestamp:
a.AddTimestamp(x)
// Type 12 - 64-bit integer
case int64:
a.AddInt64(x)
// Type 13 - 128-bit decimal floating point
case primitive.Decimal128:
a.AddDecimal128(x)
case *primitive.Decimal128:
a.AddDecimal128(*x)
// Type FF - Min key
case primitive.MinKey:
a.AddMinKey()
// Type 7F - Max key
case primitive.MaxKey:
a.AddMaxKey()
default:
panic(fmt.Sprintf("unsupported type: %T", v))
}
return true
}