forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestbson.lua
95 lines (80 loc) · 1.38 KB
/
testbson.lua
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
local bson = require "bson"
sub = bson.encode_order( "hello", 1, "world", 2 )
local function tbl_next(...)
print("--- next.a", ...)
local k, v = next(...)
print("--- next.b", k, v)
return k, v
end
local function tbl_pairs(obj)
return tbl_next, obj.__data, nil
end
local obj_a = {
__data = {
[1] = 2,
[3] = 4,
[5] = 6,
}
}
setmetatable(
obj_a,
{
__index = obj_a.__data,
__pairs = tbl_pairs,
}
)
local obj_b = {
__data = {
[7] = 8,
[9] = 10,
[11] = obj_a,
}
}
setmetatable(
obj_b,
{
__index = obj_b.__data,
__pairs = tbl_pairs,
}
)
local metaarray = setmetatable({ n = 5 }, {
__len = function(self) return self.n end,
__index = function(self, idx) return tostring(idx) end,
})
b = bson.encode {
a = 1,
b = true,
c = bson.null,
d = { 1,2,3,4 },
e = bson.binary "hello",
f = bson.regex ("*","i"),
g = bson.regex "hello",
h = bson.date (os.time()),
i = bson.timestamp(os.time()),
j = bson.objectid(),
k = { a = false, b = true },
l = {},
m = bson.minkey,
n = bson.maxkey,
o = sub,
p = 2^32-1,
q = obj_b,
r = metaarray,
}
print "\n[before replace]"
t = b:decode()
for k, v in pairs(t) do
print(k,type(v))
end
for k,v in ipairs(t.r) do
print(k,v)
end
b:makeindex()
b.a = 2
b.b = false
b.h = bson.date(os.time())
b.i = bson.timestamp(os.time())
b.j = bson.objectid()
print "\n[after replace]"
t = b:decode()
print("o.hello", bson.type(t.o.hello))