当前位置: 代码迷 >> 综合 >> erlang protobuffer
  详细解决方案

erlang protobuffer

热度:92   发布时间:2024-01-14 11:00:29.0

% how to use
% 方法1
% 生成相应的erl和hrl文件
%protobuffs_compile:scan_file_src("X.proto").


%方法2
% 生成相应的beam和hrl文件
%protobuffs_compile:scan_file("X.proto").


%方法2即可满足需求;
%调用X_pb:encode_tagName 来生成相应tagName的protobuff数据;decode_tagName来从protobuff数据解码出tagName的record;


1、待测试协议 ;test.proto

message Person {required int32 age = 1;required string name = 2;
}message Family {repeated Person person = 1;
}

2、调用protobuffs_compile:scan_file("test.proto"). ,生成.beam和hrl


3、测试代码(调用刚生成的test_pb模块来生成proto或解为record):

-module(t).-compile([export_all]).-include("test_pb.hrl").encode() ->Person = #person{age=25, name="John"},test_pb:encode_person(Person).decode() ->Data = encode(),test_pb:decode_person(Data).encode_repeat() ->RepeatData =[#person{age=25, name="John"},#person{age=23, name="Lucy"},#person{age=2, name="Tony"}],Family = #family{person=RepeatData},test_pb:encode_family(Family).decode_repeat() ->Data = encode_repeat(),test_pb:decode_family(Data).