Skip to content

Commit 0564bfd

Browse files
committed
add addressbook demo
1 parent ad584df commit 0564bfd

File tree

4 files changed

+112
-8
lines changed

4 files changed

+112
-8
lines changed

addressbook.pb

412 Bytes
Binary file not shown.

addressbook.proto

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// See README.txt for information and build instructions.
2+
3+
package tutorial;
4+
5+
option java_package = "com.example.tutorial";
6+
option java_outer_classname = "AddressBookProtos";
7+
8+
message Person {
9+
required string name = 1;
10+
required int32 id = 2; // Unique ID number for this person.
11+
optional string email = 3;
12+
13+
enum PhoneType {
14+
MOBILE = 0;
15+
HOME = 1;
16+
WORK = 2;
17+
}
18+
19+
message PhoneNumber {
20+
required string number = 1;
21+
optional PhoneType type = 2 [default = HOME];
22+
}
23+
24+
repeated PhoneNumber phone = 4;
25+
repeated int32 test = 5 [packed=true];
26+
27+
extensions 10 to max;
28+
}
29+
30+
message Ext {
31+
extend Person {
32+
optional int32 test = 10;
33+
}
34+
}
35+
36+
// Our address book file is just one of these.
37+
message AddressBook {
38+
repeated Person person = 1;
39+
}

pb.lua

+31-8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ local function make_package(package)
5151
return cur
5252
end
5353

54+
function pb.cleartypes()
55+
package.loaded.pb_typeinfo = nil
56+
typeinfo = require "pb_typeinfo"
57+
end
58+
59+
function pb.type(qname)
60+
if qname then
61+
return qualitied_type(qname)
62+
else
63+
return typeinfo
64+
end
65+
end
66+
5467
local buffer_pool = {}
5568
local buffer_used = setmetatable({}, { __mode="k" })
5669

@@ -242,18 +255,22 @@ function encode(buff, t, ptype)
242255
end
243256

244257
function pb.decode(s, ptype)
245-
local realtype = qualitied_type(ptype)
258+
if type(ptype) ~= "table" then
259+
ptype = qualitied_type(ptype)
260+
end
246261
dec:source(s)
247-
local res = decode(dec, realtype)
262+
local res = decode(dec, ptype)
248263
dec:reset()
249264
return res
250265
end
251266

252267
function pb.encode(t, ptype)
253-
local realtype = qualitied_type(ptype)
268+
if type(ptype) ~= "table" then
269+
ptype = qualitied_type(ptype)
270+
end
254271
local buff = get_buffer()
255272
buff:clear()
256-
encode(buff, t, realtype)
273+
encode(buff, t, ptype)
257274
local res = buff:clear(nil, true)
258275
put_buffer(buff)
259276
return res
@@ -406,11 +423,14 @@ local function load_fileset(pb)
406423
end
407424

408425
function pb.load(data)
409-
load_fileset(pb.decode(data, "google.protobuf.FileDescriptorSet"))
426+
local proto = pb.decode(data,
427+
"google.protobuf.FileDescriptorSet")
428+
load_fileset(proto)
429+
return proto
410430
end
411431

412432
function pb.loadfile(filename)
413-
return pb.load(pbio.read(filename))
433+
return pb.load(assert(pbio.read(filename)))
414434
end
415435

416436
------------------------------------------------------------
@@ -606,9 +626,12 @@ local function dump_info(info)
606626
G "}\n\n"
607627
end
608628

609-
function pb.dump(filename, qtype)
629+
function pb.dump(filename, ptype)
610630
local realtype = typeinfo
611-
if qtype then realtype = qualitied_type(type) end
631+
if ptype then
632+
realtype = type(ptype) == "table" and ptype
633+
or qualitied_type(ptype)
634+
end
612635
io.output(filename)
613636
dump_info(realtype)
614637
io.output():close()

test_protobuf.lua

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local protobuf = require "pb"
2+
t = protobuf.loadfile "addressbook.pb"
3+
4+
proto = t.file[1]
5+
6+
print(proto.name)
7+
print(proto.package)
8+
9+
message = proto.message_type
10+
11+
for _,v in ipairs(message) do
12+
print(v.name)
13+
for _,v in ipairs(v.field or {}) do
14+
print("\t".. v.name .. " ["..v.number.."] " .. v.label)
15+
end
16+
end
17+
18+
addressbook = {
19+
name = "Alice",
20+
id = 12345,
21+
phone = {
22+
{ number = "1301234567" },
23+
{ number = "87654321", type = "WORK" },
24+
}
25+
}
26+
27+
local Person = protobuf.type "tutorial.Person"
28+
code = protobuf.encode(addressbook, Person)
29+
30+
decode = protobuf.decode(code, Person)
31+
32+
print(decode.name)
33+
print(decode.id)
34+
for _,v in ipairs(decode.phone) do
35+
print("\t"..v.number, v.type)
36+
end
37+
38+
--[[
39+
phonebuf = protobuf.pack("tutorial.Person.PhoneNumber number","87654321")
40+
buffer = protobuf.pack("tutorial.Person name id phone", "Alice", 123, { phonebuf })
41+
print(protobuf.unpack("tutorial.Person name id phone", buffer))
42+
--]]

0 commit comments

Comments
 (0)