Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jinjiazhang committed Nov 17, 2017
1 parent 3503676 commit f5aece2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ ProtoLua is a google protocol buffers C library for Lua which implement less tha
## Quick Example
person.proto
```C
syntax = "proto2";
syntax = "proto3";

message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
string name = 1;
int32 id = 2;
string email = 3;

enum PhoneType {
MOBILE = 0;
Expand All @@ -22,11 +22,12 @@ message Person {
}

message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = MOBILE];
string number = 1;
PhoneType type = 2;
}

repeated PhoneNumber phones = 4;
map<int32, string> subjects = 5;
}
```
test.lua
Expand All @@ -41,28 +42,33 @@ local player = {
phones = {
{number = "1818864xxxx", type = 1},
{number = "1868200xxxx", type = 2},
},
subjects = {
[101] = "Chinese",
[102] = "English",
[103] = "Maths",
}
}

local data = proto.encode("Person", player)
local clone = proto.decode("Person", data)

local data = proto.pack("Person", player.name, player.id, player.email, player.phones)
local name, id, email, phones = proto.unpack("Person", data)
local data = proto.pack("Person", player.name, player.id, player.email, player.phones, player.subjects)
local name, id, email, phones, subjects = proto.unpack("Person", data)
```
## Advance Example
```C
syntax = "proto2";
syntax = "proto3";

message OnBuyItemReq {
required int32 goodsId = 1;
required int32 goodsNum = 2;
int32 goodsId = 1;
int32 goodsNum = 2;
}

message OnBuyItemRsp {
required int32 retCode = 1;
required int32 goodsId = 2;
required int32 goodsNum = 3;
int32 retCode = 1;
int32 goodsId = 2;
int32 goodsNum = 3;
}
```
```Lua
Expand Down
Binary file modified libs/protolua.dll
Binary file not shown.
8 changes: 6 additions & 2 deletions libs/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ local data = proto.pack("Person", player.name, player.id, player.email, player.p
local name, id, email, phones, subjects = proto.unpack("Person", data)

serpent = require("serpent")
print(serpent.block(clone))
print(name, id, email, serpent.block(phones), serpent.block(subjects))
print("clone:", serpent.block(clone))
print("name:", name)
print("id:", id)
print("email:", email)
print("phones:", serpent.block(phones))
print("subjects:", serpent.block(subjects))
5 changes: 3 additions & 2 deletions protolua/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ bool EncodeMessage(Message* message, const Descriptor* descriptor, lua_State* L,

bool ProtoEncode(const char* proto, lua_State* L, int index, char* output, size_t* size)
{
PROTO_ASSERT(index > 0);
const Descriptor* descriptor = g_descriptor_pool->FindMessageTypeByName(proto);
PROTO_ASSERT(descriptor);

Expand All @@ -233,6 +232,7 @@ bool ProtoEncode(const char* proto, lua_State* L, int index, char* output, size_
Message* message = prototype->New();
PROTO_ASSERT(message);

index = lua_absindex(L, index);
PROTO_DO(EncodeMessage(message, descriptor, L, index));
message->SerializeToArray(output, *size);
*size = message->ByteSizeLong();
Expand All @@ -241,7 +241,6 @@ bool ProtoEncode(const char* proto, lua_State* L, int index, char* output, size_

bool ProtoPack(const char* proto, lua_State* L, int start, int end, char* output, size_t* size)
{
PROTO_ASSERT(start > 0 && end >= start);
const Descriptor* descriptor = g_descriptor_pool->FindMessageTypeByName(proto);
PROTO_ASSERT(descriptor);

Expand All @@ -251,6 +250,8 @@ bool ProtoPack(const char* proto, lua_State* L, int start, int end, char* output
Message* message = prototype->New();
PROTO_ASSERT(message);

start = lua_absindex(L, start);
end = lua_absindex(L, end);
std::vector<const FieldDescriptor*> fields = SortFieldsByNumber(descriptor);
for (int i = 0; i < (int)fields.size() && start + i <= end; i++)
{
Expand Down

0 comments on commit f5aece2

Please sign in to comment.