Skip to content

lua内置模块实现 通过直接在模块内调用protobuf反射功能实现在lua层自动反射 支持pb2, pb3.

License

Notifications You must be signed in to change notification settings

brinkqiang2cpp/luapb

Repository files navigation

luapb

Copyright (c) 2013-2018 brinkqiang ([email protected])

luapb License blog Open Source Love GitHub stars GitHub forks

Build status

Linux MacOSX Windows
lin-badge osx-badge win-badge

Intro

luapb

win

cd bin\debug
lua ..\script\msg.lua  

linux

cd bin
lua script/msg.lua
pb_import enter
pb_import leave
pbtest ��13615632545��[email protected] �(�2�first2�second2�three
-- lua script

local pb = require("luapb")
pb.import("net.proto")

function tbcreate()
    local msg = pb.new("net.tb_Person")
    msg.number = "13615632545"
    msg.email = "[email protected]"
    msg.age = 28
    msg.ptype = 2
    msg.desc:add("first")
    msg.desc:add("second")
    msg.desc:add("three")
end

function pbtest()
    local msg = pb.new("net.tb_Person")
    msg.number = "13615632545"
    msg.email = "[email protected]"
    msg.age = 28
    msg.ptype = 2
    msg.desc:add("first")
    msg.desc:add("second")
    msg.desc:add("three")

    local binstr = pb.serializeToString(msg)
    local msg2 = pb.new("net.tb_Person")
    pb.parseFromString(msg2, binstr)

    print("pbtest " .. binstr)
end

function pbtest2()
    local msg = pb.new("net.tb_Person")
    msg.number = "13615632545"
    msg.email = "[email protected]"
    msg.age = 28
    msg.ptype = 2
    msg.desc:add("first")
    msg.desc:add("second")
    msg.desc:add("three")

    for i = 1,1000000 do
        local binstr = pb.serializeToString(msg)
        local msg2 = pb.new("net.tb_Person")
        pb.parseFromString(msg2, binstr)
    end
end

function pbtest3() 
    local message = {
        number = "13615632545",
        email = "[email protected]",
        age = 28,
        ptype = "WORK",
        desc = {"first", "second", "three"}
    }
    for k, v in pairs(message) do
        print(type(v))
        print(k)
    end

    local buffer = pb.encode("net.tb_Person", message)
    local msg = pb.decode("net.tb_Person", buffer)

    assert(msg.number == "13615632545")
    assert(msg.email == "[email protected]")
    assert(msg.age == 28)
    assert(msg.ptype == "WORK")
    assert(msg.desc[1] == "first")
    assert(msg.desc[2] == "second")
    assert(msg.desc[3] == "three")

    print("pbtest3 " .. buffer)
end

pbtest()

Contacts

Join the chat

Thanks

About

lua内置模块实现 通过直接在模块内调用protobuf反射功能实现在lua层自动反射 支持pb2, pb3.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages