Преглед изворни кода

修改代码

增加测试 和 number integer类型
genProto_V1
AICells пре 5 година
родитељ
комит
d62dbcc40f
8 измењених фајлова са 1901 додато и 650 уклоњено
  1. +207
    -0
      proto/0_test.msg
  2. +313
    -189
      src/protoCode.erl
  3. +76
    -72
      src/protoField.erl
  4. +316
    -193
      src/protoGen.erl
  5. +573
    -186
      test/protoMsg.erl
  6. +184
    -10
      test/protoMsg.hrl
  7. +1
    -0
      test/start.bat
  8. +231
    -0
      test/test.erl

+ 207
- 0
proto/0_test.msg Прегледај датотеку

@ -22,3 +22,210 @@ addressBook {
list[person] person;
list[person] other;
}
union{
string test;
int32 type;
}
tbool{
bool bool;
}
tint8{
int8 int1;
int8 int2;
}
tuint8{
uint8 int1;
uint8 int2;
}
tint16{
int16 int1;
int16 int2;
}
tuint16{
uint16 int1;
uint16 int2;
}
tint32{
int32 int1;
int32 int2;
}
tuint32{
uint32 int1;
uint32 int2;
}
tint64{
int64 int1;
int64 int2;
}
tuint64{
uint64 int1;
uint64 int2;
}
tinteger{
integer int1;
integer int2;
integer int3;
integer int4;
integer int5;
integer int6;
integer int7;
integer int8;
}
tnumber{
number int1;
number int2;
number int3;
number int4;
number int5;
number int6;
number int7;
number int8;
number float1;
number float2;
}
tfloat{
float int1;
float int2;
}
tdouble{
double int1;
double int2;
}
tstring{
string int1;
string int2;
}
tlistbool{
list[bool] int1;
}
tlistint8{
list[int8] int1;
}
tlistuint8{
list[uint8] int1;
}
tlistint16{
list[int16] int1;
}
tlistuint16{
list[uint16] int1;
}
tlistint32{
list[int32] int1;
}
tlistuint32{
list[uint32] int1;
}
tlistint64{
list[int64] int1;
}
tlistuint64{
list[uint64] int1;
}
tlistinteger{
list[integer] int1;
}
tlistnumber{
list[number] int1;
}
tlistfloat{
list[float] int1;
}
tlistdouble{
list[double] int1;
}
tliststring{
list[string] int1;
}
tlistunion{
list[union] int1;
}
%% 测试所有类型数据
allType{
bool bool;
int8 int8;
uint8 uint8;
int16 int16;
uint16 uint16;
int32 int32;
uint32 uint32;
int64 int64;
uint64 uint64;
integer inte8;
integer uinte8;
integer inte16;
integer uinte16;
integer inte32;
integer uinte32;
integer inte64;
integer uinte64;
number num8;
number unum8;
number num16;
number unum16;
number num32;
number unum32;
number num64;
number unum64;
number numfloat;
number numdouble;
float float;
double double;
string string1; //英文
string string2; //汉字
union union;
list[bool] lbool;
list[int8] lint8;
list[uint8] luint8;
list[int16] lint16;
list[uint16] luint16;
list[int32] lint32;
list[uint32] luint32;
list[int64] lint64;
list[uint64] luint64;
list[integer] linte8;
list[integer] linte16;
list[integer] linte32;
list[integer] linte64;
list[number] lnum8;
list[number] lnum16;
list[number] lnum32;
list[number] lnum64;
list[number] lnfloat32;
list[number] lnfloat64;
list[float] lfloat;
list[double] ldouble;
list[string] lstring;
list[union] lunion;
}

+ 313
- 189
src/protoCode.erl Прегледај датотеку

@ -4,244 +4,368 @@
-export([encode/1, decode/1, encodeRec/1, decodeRec/2]).
-define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)).
-define(min8, -128).
-define(max8, 127).
-define(min16, -32768).
-define(max16, 32767).
-define(min32, -2147483648).
-define(max32, 2147483647).
-define(min64, -9223372036854775808).
-define(max64, 9223372036854775807).
-define(minF32, -3.4E+38).
-define(maxF32, 3.4E+38).
-define(minF64, -1.797E-308).
-define(maxF64, 1.797E+308).
-define(int8(V), <<V:8>>).
-define(uint8(V), <<V:8>>).
-define(int16(V), <<V:16/big>>).
-define(uint16(V), <<V:16/big>>).
-define(int32(V), <<V:32/big>>).
-define(uint32(V), <<V:32/big>>).
-define(int64(V), <<V:64/big>>).
-define(uint64(V), <<V:64/big>>).
-define(float(V), <<V:32/big-float>>).
-define(double(V), <<V:64/big-float>>).
-define(int16(V), <<V:16/little>>).
-define(uint16(V), <<V:16/little>>).
-define(int32(V), <<V:32/little>>).
-define(uint32(V), <<V:32/little>>).
-define(int64(V), <<V:64/little>>).
-define(uint64(V), <<V:64/little>>).
-define(integer(V), (integer(V))).
-define(number(V), (number(V))).
-define(string(V), (string(V))).
-define(float(V), <<V:32/little-float>>).
-define(double(V), <<V:64/little-float>>).
-define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)).
-define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)).
-define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]).
-define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]).
-define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]).
-define(list_int16(List), [<<(length(List)):16/big>>, [?int16(V) || V <- List]]).
-define(list_uint16(List), [<<(length(List)):16/big>>, [?uint16(V) || V <- List]]).
-define(list_int32(List), [<<(length(List)):16/big>>, [?int32(V) || V <- List]]).
-define(list_uint32(List), [<<(length(List)):16/big>>, [?uint32(V) || V <- List]]).
-define(list_int64(List), [<<(length(List)):16/big>>, [?int64(V) || V <- List]]).
-define(list_uint64(List), [<<(length(List)):16/big>>, [?uint64(V) || V <- List]]).
-define(list_float(List), [<<(length(List)):16/big>>, [?float(V) || V <- List]]).
-define(list_double(List), [<<(length(List)):16/big>>, [?double(V) || V <- List]]).
-define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]).
-define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]).
-define(list_bool(List), [<<(length(List)):16/little>>, [?bool(V) || V <- List]]).
-define(list_int8(List), [<<(length(List)):16/little>>, [?int8(V) || V <- List]]).
-define(list_uint8(List), [<<(length(List)):16/little>>, [?uint8(V) || V <- List]]).
-define(list_int16(List), [<<(length(List)):16/little>>, [?int16(V) || V <- List]]).
-define(list_uint16(List), [<<(length(List)):16/little>>, [?uint16(V) || V <- List]]).
-define(list_int32(List), [<<(length(List)):16/little>>, [?int32(V) || V <- List]]).
-define(list_uint32(List), [<<(length(List)):16/little>>, [?uint32(V) || V <- List]]).
-define(list_int64(List), [<<(length(List)):16/little>>, [?int64(V) || V <- List]]).
-define(list_uint64(List), [<<(length(List)):16/little>>, [?uint64(V) || V <- List]]).
-define(list_float(List), [<<(length(List)):16/little>>, [?float(V) || V <- List]]).
-define(list_double(List), [<<(length(List)):16/little>>, [?double(V) || V <- List]]).
-define(list_integer(List), [<<(length(List)):16/little>>, [integer(V) || V <- List]]).
-define(list_number(List), [<<(length(List)):16/little>>, [number(V) || V <- List]]).
-define(list_string(List), [<<(length(List)):16/little>>, [string(V) || V <- List]]).
-define(list_record(List), [<<(length(List)):16/little>>, [encodeRec(V) || V <- List]]).
integer(V) ->
if
V >= ?min8 andalso V =< ?max8 ->
<<8:8, <<V:8>>/binary>>;
V >= ?min16 andalso V =< ?max16 ->
<<16:8, <<V:16/little>>/binary>>;
V >= ?min32 andalso V =< ?max32 ->
<<32:8, <<V:32/little>>/binary>>;
V >= ?min64 andalso V =< ?max64 ->
<<64:8, <<V:64/little>>/binary>>;
true ->
throw(exceeded_the_integer)
end.
numInteger(V) ->
if
V >= ?min8 andalso V =< ?max8 ->
<<8:8, <<V:8>>/binary>>;
V >= ?min16 andalso V =< ?max16 ->
<<16:8, <<V:16/little>>/binary>>;
V >= ?min32 andalso V =< ?max32 ->
<<32:8, <<V:32/little>>/binary>>;
V >= ?min64 andalso V =< ?max64 ->
<<64:8, <<V:64/little>>/binary>>;
true ->
throw(exceeded_the_integer)
end.
numFloat(V) ->
if
V >= ?minF32 andalso V =< ?maxF32 ->
<<33:8, <<V:32/little-float>>/binary>>;
V >= ?minF64 andalso V =< ?maxF64 ->
<<65:8, <<V:64/little-float>>/binary>>;
true ->
throw(exceeded_the_float)
end.
number(V) ->
if
erlang:is_integer(V) == true ->
numInteger(V);
erlang:is_float(V) == true ->
numFloat(V);
true ->
throw(is_not_number)
end.
string(Str) when is_binary(Str) ->
[<<(byte_size(Str)):16/big>>, Str];
[<<(byte_size(Str)):16/little>>, Str];
string(Str) ->
Str2 = unicode:characters_to_binary(Str, utf8),
[<<(byte_size(Str2)):16/big>>, Str2].
Str2 = unicode:characters_to_binary(Str, utf8),
[<<(byte_size(Str2)):16/little>>, Str2].
encode(Record) ->
MsgBin = encodeRec(Record),
MsgId = getMsgId(element(1, Record)),
[<<MsgId:16/big>>, MsgBin].
MsgBin = encodeRec(Record),
MsgId = getMsgId(element(1, Record)),
[<<MsgId:16/little>>, MsgBin].
decode(Bin) ->
<<MsgId:16/big, MsgBin/binary>> = Bin,
SchList = getMsgSchema(MsgId),
{<<>>, ResultList} = decodeField(SchList, MsgBin, [getMsgType(MsgId)]),
list_to_tuple(ResultList).
<<MsgId:16/little, MsgBin/binary>> = Bin,
SchList = getMsgSchema(MsgId),
{<<>>, ResultList} = decodeField(SchList, MsgBin, [getMsgType(MsgId)]),
list_to_tuple(ResultList).
decodeRec(RecordName, Bin) ->
SchList = getMsgSchema(RecordName),
{LeftBin, Result} = decodeField(SchList, Bin, [RecordName]),
{LeftBin, list_to_tuple(Result)}.
SchList = getMsgSchema(RecordName),
{LeftBin, Result} = decodeField(SchList, Bin, [RecordName]),
{LeftBin, list_to_tuple(Result)}.
decodeField([], LeftBin, Result) ->
{LeftBin, lists:reverse(Result)};
{LeftBin, lists:reverse(Result)};
decodeField([Type | SchList], MsgBin, Result) ->
case Type of
int32 ->
<<Int:32/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint32 ->
<<Int:32/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
string ->
<<Len:16/big, StrBin:Len/binary, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [StrBin | Result]);
int16 ->
<<Int:16/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint16 ->
<<Int:16/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int8 ->
<<Int:8/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint8 ->
<<Int:8/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int64 ->
<<Int:64/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint64 ->
<<Int:64/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
bool ->
<<Bool:8/big-unsigned, LeftBin/binary>> = MsgBin,
case Bool =:= 1 of
true ->
decodeField(SchList, LeftBin, [true | Result]);
_ ->
decodeField(SchList, LeftBin, [false | Result])
end;
falat ->
<<Float:32/big-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
double ->
<<Float:64/big-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
{list, int32} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint32} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int16} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint16} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int8} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint8} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, string} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deStringList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int64} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint64} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, bool} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deBoolList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, flat} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deFloatList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, double} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deDoubleList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, RecordName} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deRecordList(Len, RecordName, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
RecordName ->
<<IsUndef:8, LeftBin/binary>> = MsgBin,
case IsUndef of
0 ->
decodeField(SchList, LeftBin, [undefined | Result]);
_ ->
SubSchList = getMsgSchema(RecordName),
{SubLeftBin, SubResultList} = decodeField(SubSchList, LeftBin, [RecordName]),
decodeField(SchList, SubLeftBin, [list_to_tuple(SubResultList) | Result])
end
end.
case Type of
int32 ->
<<Int:32/little-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint32 ->
<<Int:32/little-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
string ->
<<Len:16/little, StrBin:Len/binary, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [StrBin | Result]);
int16 ->
<<Int:16/little-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint16 ->
<<Int:16/little-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int8 ->
<<Int:8/little-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint8 ->
<<Int:8/little-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int64 ->
<<Int:64/little-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint64 ->
<<Int:64/little-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
integer ->
<<IntBits:8, Int:IntBits/little-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
number ->
<<NumBits:8, NumBin/binary>> = MsgBin,
case NumBits of
33 ->
<<Float:32/little-float, LeftBin/binary>> = NumBin,
decodeField(SchList, LeftBin, [Float | Result]);
65 ->
<<Float:64/little-float, LeftBin/binary>> = NumBin,
decodeField(SchList, LeftBin, [Float | Result]);
_ ->
<<Int:NumBits/little-signed, LeftBin/binary>> = NumBin,
decodeField(SchList, LeftBin, [Int | Result])
end;
bool ->
<<Bool:8/little-unsigned, LeftBin/binary>> = MsgBin,
case Bool =:= 1 of
true ->
decodeField(SchList, LeftBin, [true | Result]);
_ ->
decodeField(SchList, LeftBin, [false | Result])
end;
float ->
<<Float:32/little-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
double ->
<<Float:64/little-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
{list, int32} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint32} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int16} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint16} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int8} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint8} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, string} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deStringList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int64} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint64} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, integer} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deIntegerList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, number} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deNumberList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, bool} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deBoolList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, float} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deFloatList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, double} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deDoubleList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, RecordName} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deRecordList(Len, RecordName, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
RecordName ->
<<IsUndef:8, LeftBin/binary>> = MsgBin,
case IsUndef of
0 ->
decodeField(SchList, LeftBin, [undefined | Result]);
_ ->
SubSchList = getMsgSchema(RecordName),
{SubLeftBin, SubResultList} = decodeField(SubSchList, LeftBin, [RecordName]),
decodeField(SchList, SubLeftBin, [list_to_tuple(SubResultList) | Result])
end
end.
deBoolList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deBoolList(N, <<Bool:8, LeftBin/binary>>, RetList) ->
case Bool =:= 1 of
true ->
deBoolList(N - 1, LeftBin, [true | RetList]);
_ ->
deBoolList(N - 1, LeftBin, [false | RetList])
end.
{MsgBin, lists:reverse(RetList)};
deBoolList(N, MsgBin, RetList) ->
<<Bool:8, LeftBin/binary>> = MsgBin,
case Bool =:= 1 of
true ->
deBoolList(N - 1, LeftBin, [true | RetList]);
_ ->
deBoolList(N - 1, LeftBin, [false | RetList])
end.
deInt8List(0, MsgBin, RetList) ->
{MsgBin, RetList};
deInt8List(N, <<Int:8/big-signed, LeftBin/binary>>, RetList) ->
deInt8List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, RetList};
deInt8List(N, MsgBin, RetList) ->
<<Int:8/little-signed, LeftBin/binary>> = MsgBin,
deInt8List(N - 1, LeftBin, [Int | RetList]).
deUint8List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint8List(N, <<Int:8/big-unsigned, LeftBin/binary>>, RetList) ->
deUint8List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint8List(N, MsgBin, RetList) ->
<<Int:8/little-unsigned, LeftBin/binary>> = MsgBin,
deUint8List(N - 1, LeftBin, [Int | RetList]).
deInt16List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deInt16List(N, <<Int:16/big-signed, LeftBin/binary>>, RetList) ->
deInt16List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deInt16List(N, MsgBin, RetList) ->
<<Int:16/little-signed, LeftBin/binary>> = MsgBin,
deInt16List(N - 1, LeftBin, [Int | RetList]).
deUint16List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint16List(N, <<Int:16/big-unsigned, LeftBin/binary>>, RetList) ->
deUint16List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint16List(N, MsgBin, RetList) ->
<<Int:16/little-unsigned, LeftBin/binary>> = MsgBin,
deUint16List(N - 1, LeftBin, [Int | RetList]).
deInt32List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deInt32List(N, <<Int:32/big-signed, LeftBin/binary>>, RetList) ->
deInt32List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deInt32List(N, MsgBin, RetList) ->
<<Int:32/little-signed, LeftBin/binary>> = MsgBin,
deInt32List(N - 1, LeftBin, [Int | RetList]).
deUint32List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint32List(N, <<Int:32/big-unsigned, LeftBin/binary>>, RetList) ->
deUint32List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint32List(N, MsgBin, RetList) ->
<<Int:32/little-unsigned, LeftBin/binary>> = MsgBin,
deUint32List(N - 1, LeftBin, [Int | RetList]).
deInt64List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deInt64List(N, <<Int:64/big-signed, LeftBin/binary>>, RetList) ->
deInt64List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deInt64List(N, MsgBin, RetList) ->
<<Int:64/little-signed, LeftBin/binary>> = MsgBin,
deInt64List(N - 1, LeftBin, [Int | RetList]).
deUint64List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint64List(N, <<Int:64/big-unsigned, LeftBin/binary>>, RetList) ->
deUint64List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint64List(N, MsgBin, RetList) ->
<<Int:64/little-unsigned, LeftBin/binary>> = MsgBin,
deUint64List(N - 1, LeftBin, [Int | RetList]).
deIntegerList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deIntegerList(N, MsgBin, RetList) ->
<<IntBits:8, Int:IntBits/little-signed, LeftBin/binary>> = MsgBin,
deIntegerList(N - 1, LeftBin, [Int | RetList]).
deNumberList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deNumberList(N, MsgBin, RetList) ->
<<NumBits:8, NumBin/binary>> = MsgBin,
case NumBits of
33 ->
<<Float:32/little-float, LeftBin/binary>> = NumBin,
deNumberList(N - 1, LeftBin, [Float | RetList]);
65 ->
<<Float:64/little-float, LeftBin/binary>> = NumBin,
deNumberList(N - 1, LeftBin, [Float | RetList]);
_ ->
<<Int:NumBits/little-signed, LeftBin/binary>> = NumBin,
deNumberList(N - 1, LeftBin, [Int | RetList])
end.
deFloatList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deFloatList(N, <<Float:32/big-float, LeftBin/binary>>, RetList) ->
deFloatList(N - 1, LeftBin, [Float | RetList]).
{MsgBin, lists:reverse(RetList)};
deFloatList(N, MsgBin, RetList) ->
<<Float:32/little-float, LeftBin/binary>> = MsgBin,
deFloatList(N - 1, LeftBin, [Float | RetList]).
deDoubleList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deDoubleList(N, <<Float:64/big-float, LeftBin/binary>>, RetList) ->
deDoubleList(N - 1, LeftBin, [Float | RetList]).
{MsgBin, lists:reverse(RetList)};
deDoubleList(N, MsgBin, RetList) ->
<<Float:64/little-float, LeftBin/binary>> = MsgBin,
deDoubleList(N - 1, LeftBin, [Float | RetList]).
deStringList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deStringList(N, <<Len:16/big, StrBin:Len/binary-unit:8, LeftBin/binary>>, RetList) ->
deStringList(N - 1, LeftBin, [StrBin | RetList]).
{MsgBin, lists:reverse(RetList)};
deStringList(N, MsgBin, RetList) ->
<<Len:16/little, StrBin:Len/binary-unit:8, LeftBin/binary>> = MsgBin,
deStringList(N - 1, LeftBin, [StrBin | RetList]).
deRecordList(0, _RecordName, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
{MsgBin, lists:reverse(RetList)};
deRecordList(N, RecordName, MsgBin, RetList) ->
{LeftBin, Tuple} = decodeRec(RecordName, MsgBin),
deRecordList(N - 1, RecordName, LeftBin, [Tuple | RetList]).
{LeftBin, Tuple} = decodeRec(RecordName, MsgBin),
deRecordList(N - 1, RecordName, LeftBin, [Tuple | RetList]).
%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
encodeRec(_) ->
ok.
ok.
getMsgId(_) ->
ok.
ok.
getMsgType(_) ->
ok.
ok.
getMsgSchema(_) ->
ok.
ok.

+ 76
- 72
src/protoField.erl Прегледај датотеку

@ -3,87 +3,91 @@
-compile([export_all, nowarn_export_all]).
-define(TypeList, [
"bool",
"int8",
"uint8",
"int16",
"uint16",
"int32",
"uint32",
"int64",
"uint64",
"float",
"double",
"string"
"bool"
, "int8"
, "uint8"
, "int16"
, "uint16"
, "int32"
, "uint32"
, "int64"
, "uint64"
, "integer"
, "number"
, "float"
, "double"
, "string"
]).
-define(TypeValue, [
{"bool", "false", "boolean"},
{"int8", "0", "int8()"},
{"uint8", "0", "uint8()"},
{"int16", "0", "int16()"},
{"uint16", "0", "uint16()"},
{"int32", "0", "int32()"},
{"uint32", "0", "uint32()"},
{"int64", "0", "int64()"},
{"uint64", "0", "uint64()"},
{"float", "0.0", "float()"},
{"double", "0.0", "flaot"},
{"string", "\"\"", "string()"}
{"bool", "false", "boolean()"}
, {"int8", "0", "int8()"}
, {"uint8", "0", "uint8()"}
, {"int16", "0", "int16()"}
, {"uint16", "0", "uint16()"}
, {"int32", "0", "int32()"}
, {"uint32", "0", "uint32()"}
, {"int64", "0", "int64()"}
, {"uint64", "0", "uint64()"}
, {"integer", "0", "integer()"}
, {"number", "0", "number()"}
, {"float", "0.0", "float()"}
, {"double", "0.0", "double()"}
, {"string", "\"\"", "string()"}
]).
getSchemaInfo(TypeStr) ->
case lists:member(TypeStr, ?TypeList) of
true ->
erlang:list_to_atom(TypeStr);
_ ->
case TypeStr of
"list[" ++ LeftStr ->
[SubTypeStr | _] = re:split(LeftStr, "\\]", [{return, list}]),
{list, erlang:list_to_atom(SubTypeStr)};
_ ->
erlang:list_to_atom(TypeStr)
end
end.
case lists:member(TypeStr, ?TypeList) of
true ->
erlang:list_to_atom(TypeStr);
_ ->
case TypeStr of
"list[" ++ LeftStr ->
[SubTypeStr | _] = re:split(LeftStr, "\\]", [{return, list}]),
{list, erlang:list_to_atom(SubTypeStr)};
_ ->
erlang:list_to_atom(TypeStr)
end
end.
builtRecStr({TypeStr, NameStr}) ->
case lists:keyfind(TypeStr, 1, ?TypeValue) of
{TypeStr, DefValueStr, DefTypeStr} ->
NameStr ++ " = " ++ DefValueStr ++ " :: " ++ DefTypeStr ++ "\n\t";
_ ->
case TypeStr of
"list[" ++ LeftStr ->
[SubTypeStr | _] = re:split(LeftStr, "\\]", [{return, list}]),
case lists:keyfind(SubTypeStr, 1, ?TypeValue) of
{SubTypeStr, _DefSubValueStr, DefSubTypeStr} ->
NameStr ++ " = [] " ++ " :: [" ++ DefSubTypeStr ++ "]\n\t";
_ ->
NameStr ++ " = [] " ++ " :: [#" ++ SubTypeStr ++ "{}]\n\t"
end;
_ ->
NameStr ++ " = undefined " ++ " :: #" ++ TypeStr ++ "{}\n\t"
end
end.
case lists:keyfind(TypeStr, 1, ?TypeValue) of
{TypeStr, DefValueStr, DefTypeStr} ->
NameStr ++ " = " ++ DefValueStr ++ " :: " ++ DefTypeStr ++ "\n";
_ ->
case TypeStr of
"list[" ++ LeftStr ->
[SubTypeStr | _] = re:split(LeftStr, "\\]", [{return, list}]),
case lists:keyfind(SubTypeStr, 1, ?TypeValue) of
{SubTypeStr, _DefSubValueStr, DefSubTypeStr} ->
NameStr ++ " = [] " ++ " :: [" ++ DefSubTypeStr ++ "]\n";
_ ->
NameStr ++ " = [] " ++ " :: [#" ++ SubTypeStr ++ "{}]\n"
end;
_ ->
NameStr ++ " = undefined " ++ " :: #" ++ TypeStr ++ "{}\n"
end
end.
builtPackStr(TypeStr) ->
case lists:member(TypeStr, ?TypeList) of
true ->
"?" ++ TypeStr ++ "(";
_ ->
case TypeStr of
"list[" ++ LeftStr ->
[SubTypeStr | _] = re:split(LeftStr, "\\]", [{return, list}]),
SubStr =
case lists:member(SubTypeStr, ?TypeList) of
true ->
SubTypeStr;
_ ->
"record"
end,
"?list_" ++ SubStr ++ "(";
_ ->
"?record("
end
end.
case lists:member(TypeStr, ?TypeList) of
true ->
"?" ++ TypeStr ++ "(";
_ ->
case TypeStr of
"list[" ++ LeftStr ->
[SubTypeStr | _] = re:split(LeftStr, "\\]", [{return, list}]),
SubStr =
case lists:member(SubTypeStr, ?TypeList) of
true ->
SubTypeStr;
_ ->
"record"
end,
"?list_" ++ SubStr ++ "(";
_ ->
"?record("
end
end.

+ 316
- 193
src/protoGen.erl Прегледај датотеку

@ -17,255 +17,378 @@ protoHrlHeader() ->
-opaque uint16() :: 0..65536.
-opaque uint32() :: 0..4294967295.
-opaque uint64() :: 0..18446744073709551615.
-opaque single() :: float().
-opaque double() :: float().\n\n".
protoErlHeader() ->
"-module(protoMsg).\n\n
-export([encode/1, decode/1, encodeRec/1, decodeRec/2]).
-define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)).
-define(min8, -128).
-define(max8, 127).
-define(min16, -32768).
-define(max16, 32767).
-define(min32, -2147483648).
-define(max32, 2147483647).
-define(min64, -9223372036854775808).
-define(max64, 9223372036854775807).
-define(minF32, -3.4E+38).
-define(maxF32, 3.4E+38).
-define(minF64, -1.797E-308).
-define(maxF64, 1.797E+308).
-define(int8(V), <<V:8>>).
-define(uint8(V), <<V:8>>).
-define(int16(V), <<V:16/big>>).
-define(uint16(V), <<V:16/big>>).
-define(int32(V), <<V:32/big>>).
-define(uint32(V), <<V:32/big>>).
-define(int64(V), <<V:64/big>>).
-define(uint64(V), <<V:64/big>>).
-define(float(V), <<V:32/big-float>>).
-define(double(V), <<V:64/big-float>>).
-define(int16(V), <<V:16/little>>).
-define(uint16(V), <<V:16/little>>).
-define(int32(V), <<V:32/little>>).
-define(uint32(V), <<V:32/little>>).
-define(int64(V), <<V:64/little>>).
-define(uint64(V), <<V:64/little>>).
-define(integer(V), (integer(V))).
-define(number(V), (number(V))).
-define(string(V), (string(V))).
-define(float(V), <<V:32/little-float>>).
-define(double(V), <<V:64/little-float>>).
-define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)).
-define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)).
-define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]).
-define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]).
-define(list_uint8(List), [<<(length(List)):16/big>>, [?uint8(V) || V <- List]]).
-define(list_int16(List), [<<(length(List)):16/big>>, [?int16(V) || V <- List]]).
-define(list_uint16(List), [<<(length(List)):16/big>>, [?uint16(V) || V <- List]]).
-define(list_int32(List), [<<(length(List)):16/big>>, [?int32(V) || V <- List]]).
-define(list_uint32(List), [<<(length(List)):16/big>>, [?uint32(V) || V <- List]]).
-define(list_int64(List), [<<(length(List)):16/big>>, [?int64(V) || V <- List]]).
-define(list_uint64(List), [<<(length(List)):16/big>>, [?uint64(V) || V <- List]]).
-define(list_float(List), [<<(length(List)):16/big>>, [?float(V) || V <- List]]).
-define(list_double(List), [<<(length(List)):16/big>>, [?double(V) || V <- List]]).
-define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]).
-define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]).
-define(list_bool(List), [<<(length(List)):16/little>>, [?bool(V) || V <- List]]).
-define(list_int8(List), [<<(length(List)):16/little>>, [?int8(V) || V <- List]]).
-define(list_uint8(List), [<<(length(List)):16/little>>, [?uint8(V) || V <- List]]).
-define(list_int16(List), [<<(length(List)):16/little>>, [?int16(V) || V <- List]]).
-define(list_uint16(List), [<<(length(List)):16/little>>, [?uint16(V) || V <- List]]).
-define(list_int32(List), [<<(length(List)):16/little>>, [?int32(V) || V <- List]]).
-define(list_uint32(List), [<<(length(List)):16/little>>, [?uint32(V) || V <- List]]).
-define(list_int64(List), [<<(length(List)):16/little>>, [?int64(V) || V <- List]]).
-define(list_uint64(List), [<<(length(List)):16/little>>, [?uint64(V) || V <- List]]).
-define(list_float(List), [<<(length(List)):16/little>>, [?float(V) || V <- List]]).
-define(list_double(List), [<<(length(List)):16/little>>, [?double(V) || V <- List]]).
-define(list_integer(List), [<<(length(List)):16/little>>, [integer(V) || V <- List]]).
-define(list_number(List), [<<(length(List)):16/little>>, [number(V) || V <- List]]).
-define(list_string(List), [<<(length(List)):16/little>>, [string(V) || V <- List]]).
-define(list_record(List), [<<(length(List)):16/little>>, [encodeRec(V) || V <- List]]).
integer(V) ->
if
V >= ?min8 andalso V =< ?max8 ->
<<8:8, <<V:8>>/binary>>;
V >= ?min16 andalso V =< ?max16 ->
<<16:8, <<V:16/little>>/binary>>;
V >= ?min32 andalso V =< ?max32 ->
<<32:8, <<V:32/little>>/binary>>;
V >= ?min64 andalso V =< ?max64 ->
<<64:8, <<V:64/little>>/binary>>;
true ->
throw(exceeded_the_integer)
end.
numInteger(V) ->
if
V >= ?min8 andalso V =< ?max8 ->
<<8:8, <<V:8>>/binary>>;
V >= ?min16 andalso V =< ?max16 ->
<<16:8, <<V:16/little>>/binary>>;
V >= ?min32 andalso V =< ?max32 ->
<<32:8, <<V:32/little>>/binary>>;
V >= ?min64 andalso V =< ?max64 ->
<<64:8, <<V:64/little>>/binary>>;
true ->
throw(exceeded_the_integer)
end.
numFloat(V) ->
if
V >= ?minF32 andalso V =< ?maxF32 ->
<<33:8, <<V:32/little-float>>/binary>>;
V >= ?minF64 andalso V =< ?maxF64 ->
<<65:8, <<V:64/little-float>>/binary>>;
true ->
throw(exceeded_the_float)
end.
number(V) ->
if
erlang:is_integer(V) == true ->
numInteger(V);
erlang:is_float(V) == true ->
numFloat(V);
true ->
throw(is_not_number)
end.
string(Str) when is_binary(Str) ->
[<<(byte_size(Str)):16/big>>, Str];
[<<(byte_size(Str)):16/little>>, Str];
string(Str) ->
Str2 = unicode:characters_to_binary(Str, utf8),
[<<(byte_size(Str2)):16/big>>, Str2].
Str2 = unicode:characters_to_binary(Str, utf8),
[<<(byte_size(Str2)):16/little>>, Str2].
encode(Record) ->
MsgBin = encodeRec(Record),
MsgId = getMsgId(element(1, Record)),
[<<MsgId:16/big>>, MsgBin].
MsgBin = encodeRec(Record),
MsgId = getMsgId(element(1, Record)),
[<<MsgId:16/little>>, MsgBin].
decode(Bin) ->
<<MsgId:16/big, MsgBin/binary>> = Bin,
SchList = getMsgSchema(MsgId),
{<<>>, ResultList} = decodeField(SchList, MsgBin, [getMsgType(MsgId)]),
list_to_tuple(ResultList).
<<MsgId:16/little, MsgBin/binary>> = Bin,
SchList = getMsgSchema(MsgId),
{<<>>, ResultList} = decodeField(SchList, MsgBin, [getMsgType(MsgId)]),
list_to_tuple(ResultList).
decodeRec(RecordName, Bin) ->
SchList = getMsgSchema(RecordName),
{LeftBin, Result} = decodeField(SchList, Bin, [RecordName]),
{LeftBin, list_to_tuple(Result)}.
SchList = getMsgSchema(RecordName),
{LeftBin, Result} = decodeField(SchList, Bin, [RecordName]),
{LeftBin, list_to_tuple(Result)}.
decodeField([], LeftBin, Result) ->
{LeftBin, lists:reverse(Result)};
{LeftBin, lists:reverse(Result)};
decodeField([Type | SchList], MsgBin, Result) ->
case Type of
int32 ->
<<Int:32/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint32 ->
<<Int:32/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
string ->
<<Len:16/big, StrBin:Len/binary, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [StrBin | Result]);
int16 ->
<<Int:16/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint16 ->
<<Int:16/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int8 ->
<<Int:8/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint8 ->
<<Int:8/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int64 ->
<<Int:64/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint64 ->
<<Int:64/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
bool ->
<<Bool:8/big-unsigned, LeftBin/binary>> = MsgBin,
case Bool =:= 1 of
true ->
decodeField(SchList, LeftBin, [true | Result]);
_ ->
decodeField(SchList, LeftBin, [false | Result])
end;
falat ->
<<Float:32/big-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
double ->
<<Float:64/big-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
{list, int32} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint32} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int16} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint16} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int8} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint8} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, string} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deStringList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int64} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint64} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, bool} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deBoolList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, flat} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deFloatList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, double} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deDoubleList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, RecordName} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deRecordList(Len, RecordName, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
case Type of
int32 ->
<<Int:32/little-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint32 ->
<<Int:32/little-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
string ->
<<Len:16/little, StrBin:Len/binary, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [StrBin | Result]);
int16 ->
<<Int:16/little-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint16 ->
<<Int:16/little-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int8 ->
<<Int:8/little-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint8 ->
<<Int:8/little-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int64 ->
<<Int:64/little-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint64 ->
<<Int:64/little-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
integer ->
<<IntBits:8, Int:IntBits/little-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
number ->
<<NumBits:8, NumBin/binary>> = MsgBin,
case NumBits of
33 ->
<<Float:32/little-float, LeftBin/binary>> = NumBin,
decodeField(SchList, LeftBin, [Float | Result]);
65 ->
<<Float:64/little-float, LeftBin/binary>> = NumBin,
decodeField(SchList, LeftBin, [Float | Result]);
_ ->
<<Int:NumBits/little-signed, LeftBin/binary>> = NumBin,
decodeField(SchList, LeftBin, [Int | Result])
end;
bool ->
<<Bool:8/little-unsigned, LeftBin/binary>> = MsgBin,
case Bool =:= 1 of
true ->
decodeField(SchList, LeftBin, [true | Result]);
_ ->
decodeField(SchList, LeftBin, [false | Result])
end;
float ->
<<Float:32/little-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
double ->
<<Float:64/little-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
{list, int32} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint32} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int16} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint16} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int8} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint8} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, string} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deStringList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int64} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint64} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, integer} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deIntegerList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, number} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deNumberList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, bool} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deBoolList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, float} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deFloatList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, double} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deDoubleList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, RecordName} ->
<<Len:16/little, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deRecordList(Len, RecordName, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
RecordName ->
<<IsUndef:8, LeftBin/binary>> = MsgBin,
case IsUndef of
0 ->
decodeField(SchList, LeftBin, [undefined | Result]);
_ ->
SubSchList = getMsgSchema(RecordName),
{SubLeftBin, SubResultList} = decodeField(SubSchList, LeftBin, [RecordName]),
decodeField(SchList, SubLeftBin, [list_to_tuple(SubResultList) | Result])
end
end.
<<IsUndef:8, LeftBin/binary>> = MsgBin,
case IsUndef of
0 ->
decodeField(SchList, LeftBin, [undefined | Result]);
_ ->
SubSchList = getMsgSchema(RecordName),
{SubLeftBin, SubResultList} = decodeField(SubSchList, LeftBin, [RecordName]),
decodeField(SchList, SubLeftBin, [list_to_tuple(SubResultList) | Result])
end
end.
deBoolList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deBoolList(N, <<Bool:8, LeftBin/binary>>, RetList) ->
case Bool =:= 1 of
true ->
deBoolList(N - 1, LeftBin, [true | RetList]);
_ ->
deBoolList(N - 1, LeftBin, [false | RetList])
end.
{MsgBin, lists:reverse(RetList)};
deBoolList(N, MsgBin, RetList) ->
<<Bool:8, LeftBin/binary>> = MsgBin,
case Bool =:= 1 of
true ->
deBoolList(N - 1, LeftBin, [true | RetList]);
_ ->
deBoolList(N - 1, LeftBin, [false | RetList])
end.
deInt8List(0, MsgBin, RetList) ->
{MsgBin, RetList};
deInt8List(N, <<Int:8/big-signed, LeftBin/binary>>, RetList) ->
deInt8List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, RetList};
deInt8List(N, MsgBin, RetList) ->
<<Int:8/little-signed, LeftBin/binary>> = MsgBin,
deInt8List(N - 1, LeftBin, [Int | RetList]).
deUint8List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint8List(N, <<Int:8/big-unsigned, LeftBin/binary>>, RetList) ->
deUint8List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint8List(N, MsgBin, RetList) ->
<<Int:8/little-unsigned, LeftBin/binary>> = MsgBin,
deUint8List(N - 1, LeftBin, [Int | RetList]).
deInt16List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deInt16List(N, <<Int:16/big-signed, LeftBin/binary>>, RetList) ->
deInt16List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deInt16List(N, MsgBin, RetList) ->
<<Int:16/little-signed, LeftBin/binary>> = MsgBin,
deInt16List(N - 1, LeftBin, [Int | RetList]).
deUint16List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint16List(N, <<Int:16/big-unsigned, LeftBin/binary>>, RetList) ->
deUint16List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint16List(N, MsgBin, RetList) ->
<<Int:16/little-unsigned, LeftBin/binary>> = MsgBin,
deUint16List(N - 1, LeftBin, [Int | RetList]).
deInt32List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deInt32List(N, <<Int:32/big-signed, LeftBin/binary>>, RetList) ->
deInt32List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deInt32List(N, MsgBin, RetList) ->
<<Int:32/little-signed, LeftBin/binary>> = MsgBin,
deInt32List(N - 1, LeftBin, [Int | RetList]).
deUint32List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint32List(N, <<Int:32/big-unsigned, LeftBin/binary>>, RetList) ->
deUint32List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint32List(N, MsgBin, RetList) ->
<<Int:32/little-unsigned, LeftBin/binary>> = MsgBin,
deUint32List(N - 1, LeftBin, [Int | RetList]).
deInt64List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deInt64List(N, <<Int:64/big-signed, LeftBin/binary>>, RetList) ->
deInt64List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deInt64List(N, MsgBin, RetList) ->
<<Int:64/little-signed, LeftBin/binary>> = MsgBin,
deInt64List(N - 1, LeftBin, [Int | RetList]).
deUint64List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint64List(N, <<Int:64/big-unsigned, LeftBin/binary>>, RetList) ->
deUint64List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint64List(N, MsgBin, RetList) ->
<<Int:64/little-unsigned, LeftBin/binary>> = MsgBin,
deUint64List(N - 1, LeftBin, [Int | RetList]).
deIntegerList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deIntegerList(N, MsgBin, RetList) ->
<<IntBits:8, Int:IntBits/little-signed, LeftBin/binary>> = MsgBin,
deIntegerList(N - 1, LeftBin, [Int | RetList]).
deNumberList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deNumberList(N, MsgBin, RetList) ->
<<NumBits:8, NumBin/binary>> = MsgBin,
case NumBits of
33 ->
<<Float:32/little-float, LeftBin/binary>> = NumBin,
deNumberList(N - 1, LeftBin, [Float | RetList]);
65 ->
<<Float:64/little-float, LeftBin/binary>> = NumBin,
deNumberList(N - 1, LeftBin, [Float | RetList]);
_ ->
<<Int:NumBits/little-signed, LeftBin/binary>> = NumBin,
deNumberList(N - 1, LeftBin, [Int | RetList])
end.
deFloatList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deFloatList(N, <<Float:32/big-float, LeftBin/binary>>, RetList) ->
deFloatList(N - 1, LeftBin, [Float | RetList]).
{MsgBin, lists:reverse(RetList)};
deFloatList(N, MsgBin, RetList) ->
<<Float:32/little-float, LeftBin/binary>> = MsgBin,
deFloatList(N - 1, LeftBin, [Float | RetList]).
deDoubleList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deDoubleList(N, <<Float:64/big-float, LeftBin/binary>>, RetList) ->
deDoubleList(N - 1, LeftBin, [Float | RetList]).
{MsgBin, lists:reverse(RetList)};
deDoubleList(N, MsgBin, RetList) ->
<<Float:64/little-float, LeftBin/binary>> = MsgBin,
deDoubleList(N - 1, LeftBin, [Float | RetList]).
deStringList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deStringList(N, <<Len:16/big, StrBin:Len/binary-unit:8, LeftBin/binary>>, RetList) ->
deStringList(N - 1, LeftBin, [StrBin | RetList]).
{MsgBin, lists:reverse(RetList)};
deStringList(N, MsgBin, RetList) ->
<<Len:16/little, StrBin:Len/binary-unit:8, LeftBin/binary>> = MsgBin,
deStringList(N - 1, LeftBin, [StrBin | RetList]).
deRecordList(0, _RecordName, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
{MsgBin, lists:reverse(RetList)};
deRecordList(N, RecordName, MsgBin, RetList) ->
{LeftBin, Tuple} = decodeRec(RecordName, MsgBin),
deRecordList(N - 1, RecordName, LeftBin, [Tuple | RetList]).\n\n".
{LeftBin, Tuple} = decodeRec(RecordName, MsgBin),
deRecordList(N - 1, RecordName, LeftBin, [Tuple | RetList]).\n\n".
genSchema({FieldType, _FieldName}, AccList) ->
SchemaStr = protoField:getSchemaInfo(FieldType),
[SchemaStr | AccList].
genMsgHrl(FieldInfo, {Index, AccList}) ->
genMsgHrl(FieldInfo, {Index, Len, AccList}) ->
TemStr =
case Index of
1 ->
"";
_ ->
","
", "
end,
RecStr = TemStr ++ protoField:builtRecStr(FieldInfo),
{Index - 1, [RecStr | AccList]}.
RecStr = TemStr ++ protoField:builtRecStr(FieldInfo) ++ (case Index == Len of true -> ""; _ -> "\t" end),
{Index - 1, Len, [RecStr | AccList]}.
genEncodeRec({MsgName, _MsgId, FieldList}) ->
FieldLen = length(FieldList),
@ -330,18 +453,18 @@ convertDir(ProtoDir, HrlDir, ErlDir) ->
MessageId1 > MessageId2 end, SProtoList),
FunSpell =
fun({MsgName, MsgId, FieldList} = MsgInfo, {MsgHrlAcc, MsgTypeAcc, MsgIdAcc, MsgEndStr, MsgSchemaAcc}) ->
TypeStr = "getMsgType(" ++ integer_to_list(MsgId) ++ ")-> class="se">\n\t" ++ MsgName ++ ";\n",
IdStr = "getMsgId(" ++ MsgName ++ ")-> class="se">\n\t" ++ integer_to_list(MsgId) ++ ";\n",
TypeStr = "getMsgType(" ++ integer_to_list(MsgId) ++ ")-> " ++ MsgName ++ ";\n",
IdStr = "getMsgId(" ++ MsgName ++ ")-> " ++ integer_to_list(MsgId) ++ ";\n",
EncodeStr = genEncodeRec(MsgInfo),
Len = erlang:length(FieldList),
SchemaList = lists:foldr(fun genSchema/2, [], FieldList),
SchemaStr = "getMsgSchema(" ++ MsgName ++ ")->\n\t" ++ "getMsgSchema(" ++ integer_to_list(MsgId) ++ ");\n" ++
"getMsgSchema(" ++ integer_to_list(MsgId) ++ ")->\n\t" ++ io_lib:format("~p", [SchemaList]) ++ ";\n",
{_, LastFieldStr} = lists:foldr(fun genMsgHrl/2, {Len, ""}, FieldList),
{_, Len, LastFieldStr} = lists:foldr(fun genMsgHrl/2, {Len, Len, ""}, FieldList),
HrlStr = "-record(" ++ MsgName ++ " ,{\n\t" ++ LastFieldStr ++ "}).\n",
{[HrlStr | MsgHrlAcc], [TypeStr | MsgTypeAcc], [IdStr | MsgIdAcc], [EncodeStr | MsgEndStr], [SchemaStr | MsgSchemaAcc]}
end,
{MsgHrlStr, MsgTypeStr, MsgIdStr, MsgEndStr, MsgSchemaStr} = lists:foldl(FunSpell, {[], ["getMsgType(_) -> class="se">\n\tundefined.\n\n"], ["getMsgId(_) -> class="se">\n\t0.\n\n"], ["encodeRec(_) ->\n\t[].\n\n"], ["getMsgSchema(_) ->\n\t[].\n\n"]}, SortedSProtoList),
{MsgHrlStr, MsgTypeStr, MsgIdStr, MsgEndStr, MsgSchemaStr} = lists:foldl(FunSpell, {[], ["getMsgType(_) -> undefined.\n\n"], ["getMsgId(_) -> 0.\n\n"], ["encodeRec(_) ->\n\t[].\n\n"], ["getMsgSchema(_) ->\n\t[].\n\n"]}, SortedSProtoList),
ModStr = protoErlHeader(),
OutputStr = ModStr ++ MsgTypeStr ++ MsgIdStr ++ MsgEndStr ++ MsgSchemaStr,

+ 573
- 186
test/protoMsg.erl Прегледај датотеку

@ -3,7 +3,20 @@
-export([encode/1, decode/1, encodeRec/1, decodeRec/2]).
-define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)).
-define(min8, -128).
-define(max8, 127).
-define(min16, -32768).
-define(max16, 32767).
-define(min32, -2147483648).
-define(max32, 2147483647).
-define(min64, -9223372036854775808).
-define(max64, 9223372036854775807).
-define(minF32, -3.4E+38).
-define(maxF32, 3.4E+38).
-define(minF64, -1.797E-308).
-define(maxF64, 1.797E+308).
-define(int8(V), <<V:8>>).
-define(uint8(V), <<V:8>>).
-define(int16(V), <<V:16/big>>).
@ -12,9 +25,12 @@
-define(uint32(V), <<V:32/big>>).
-define(int64(V), <<V:64/big>>).
-define(uint64(V), <<V:64/big>>).
-define(integer(V), (integer(V))).
-define(number(V), (number(V))).
-define(string(V), (string(V))).
-define(float(V), <<V:32/big-float>>).
-define(double(V), <<V:64/big-float>>).
-define(string(V), (string(V))).
-define(bool(V), (case V of true -> <<1:8>>; _ -> <<0:8>> end)).
-define(record(V), (case V of undefined -> [<<0:8>>]; V -> [<<1:8>>, encodeRec(V)] end)).
-define(list_bool(List), [<<(length(List)):16/big>>, [?bool(V) || V <- List]]).
-define(list_int8(List), [<<(length(List)):16/big>>, [?int8(V) || V <- List]]).
@ -27,231 +43,391 @@
-define(list_uint64(List), [<<(length(List)):16/big>>, [?uint64(V) || V <- List]]).
-define(list_float(List), [<<(length(List)):16/big>>, [?float(V) || V <- List]]).
-define(list_double(List), [<<(length(List)):16/big>>, [?double(V) || V <- List]]).
-define(list_integer(List), [<<(length(List)):16/big>>, [integer(V) || V <- List]]).
-define(list_number(List), [<<(length(List)):16/big>>, [number(V) || V <- List]]).
-define(list_string(List), [<<(length(List)):16/big>>, [string(V) || V <- List]]).
-define(list_record(List), [<<(length(List)):16/big>>, [encodeRec(V) || V <- List]]).
integer(V) ->
if
V >= ?min8 andalso V =< ?max8 ->
<<8:8, <<V:8>>/binary>>;
V >= ?min16 andalso V =< ?max16 ->
<<16:8, <<V:16/big>>/binary>>;
V >= ?min32 andalso V =< ?max32 ->
<<32:8, <<V:32/big>>/binary>>;
V >= ?min64 andalso V =< ?max64 ->
<<64:8, <<V:64/big>>/binary>>;
true ->
throw(exceeded_the_integer)
end.
numInteger(V) ->
if
V >= ?min8 andalso V =< ?max8 ->
<<8:8, <<V:8>>/binary>>;
V >= ?min16 andalso V =< ?max16 ->
<<16:8, <<V:16/big>>/binary>>;
V >= ?min32 andalso V =< ?max32 ->
<<32:8, <<V:32/big>>/binary>>;
V >= ?min64 andalso V =< ?max64 ->
<<64:8, <<V:64/big>>/binary>>;
true ->
throw(exceeded_the_integer)
end.
numFloat(V) ->
if
V >= ?minF32 andalso V =< ?maxF32 ->
<<33:8, <<V:32/big-float>>/binary>>;
V >= ?minF64 andalso V =< ?maxF64 ->
<<65:8, <<V:64/big-float>>/binary>>;
true ->
throw(exceeded_the_float)
end.
number(V) ->
if
erlang:is_integer(V) == true ->
numInteger(V);
erlang:is_float(V) == true ->
numFloat(V);
true ->
throw(is_not_number)
end.
string(Str) when is_binary(Str) ->
[<<(byte_size(Str)):16/big>>, Str];
[<<(byte_size(Str)):16/big>>, Str];
string(Str) ->
Str2 = unicode:characters_to_binary(Str, utf8),
[<<(byte_size(Str2)):16/big>>, Str2].
Str2 = unicode:characters_to_binary(Str, utf8),
[<<(byte_size(Str2)):16/big>>, Str2].
encode(Record) ->
MsgBin = encodeRec(Record),
MsgId = getMsgId(element(1, Record)),
[<<MsgId:16/big>>, MsgBin].
MsgBin = encodeRec(Record),
MsgId = getMsgId(element(1, Record)),
[<<MsgId:16/big>>, MsgBin].
decode(Bin) ->
<<MsgId:16/big, MsgBin/binary>> = Bin,
SchList = getMsgSchema(MsgId),
{<<>>, ResultList} = decodeField(SchList, MsgBin, [getMsgType(MsgId)]),
list_to_tuple(ResultList).
<<MsgId:16/big, MsgBin/binary>> = Bin,
SchList = getMsgSchema(MsgId),
{<<>>, ResultList} = decodeField(SchList, MsgBin, [getMsgType(MsgId)]),
list_to_tuple(ResultList).
decodeRec(RecordName, Bin) ->
SchList = getMsgSchema(RecordName),
{LeftBin, Result} = decodeField(SchList, Bin, [RecordName]),
{LeftBin, list_to_tuple(Result)}.
SchList = getMsgSchema(RecordName),
{LeftBin, Result} = decodeField(SchList, Bin, [RecordName]),
{LeftBin, list_to_tuple(Result)}.
decodeField([], LeftBin, Result) ->
{LeftBin, lists:reverse(Result)};
{LeftBin, lists:reverse(Result)};
decodeField([Type | SchList], MsgBin, Result) ->
case Type of
int32 ->
<<Int:32/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint32 ->
<<Int:32/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
string ->
<<Len:16/big, StrBin:Len/binary, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [StrBin | Result]);
int16 ->
<<Int:16/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint16 ->
<<Int:16/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int8 ->
<<Int:8/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint8 ->
<<Int:8/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int64 ->
<<Int:64/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint64 ->
<<Int:64/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
bool ->
<<Bool:8/big-unsigned, LeftBin/binary>> = MsgBin,
case Bool =:= 1 of
true ->
decodeField(SchList, LeftBin, [true | Result]);
_ ->
decodeField(SchList, LeftBin, [false | Result])
end;
falat ->
<<Float:32/big-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
double ->
<<Float:64/big-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
{list, int32} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint32} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int16} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint16} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int8} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint8} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, string} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deStringList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int64} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint64} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, bool} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deBoolList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, flat} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deFloatList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, double} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deDoubleList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, RecordName} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deRecordList(Len, RecordName, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
case Type of
int32 ->
<<Int:32/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint32 ->
<<Int:32/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
string ->
<<Len:16/big, StrBin:Len/binary, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [StrBin | Result]);
int16 ->
<<Int:16/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint16 ->
<<Int:16/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int8 ->
<<Int:8/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint8 ->
<<Int:8/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
int64 ->
<<Int:64/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
uint64 ->
<<Int:64/big-unsigned, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
integer ->
<<IntBits:8, Int:IntBits/big-signed, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Int | Result]);
number ->
<<NumBits:8, NumBin/binary>> = MsgBin,
case NumBits of
33 ->
<<Float:32/big-float, LeftBin/binary>> = NumBin,
decodeField(SchList, LeftBin, [Float | Result]);
65 ->
<<Float:64/big-float, LeftBin/binary>> = NumBin,
decodeField(SchList, LeftBin, [Float | Result]);
_ ->
<<Int:NumBits/big-signed, LeftBin/binary>> = NumBin,
decodeField(SchList, LeftBin, [Int | Result])
end;
bool ->
<<Bool:8/big-unsigned, LeftBin/binary>> = MsgBin,
case Bool =:= 1 of
true ->
decodeField(SchList, LeftBin, [true | Result]);
_ ->
decodeField(SchList, LeftBin, [false | Result])
end;
float ->
<<Float:32/big-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
double ->
<<Float:64/big-float, LeftBin/binary>> = MsgBin,
decodeField(SchList, LeftBin, [Float | Result]);
{list, int32} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint32} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint32List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int16} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint16} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint16List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int8} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint8} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint8List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, string} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deStringList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, int64} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deInt64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, uint64} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deUint64List(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, integer} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deIntegerList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, number} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deNumberList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, bool} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deBoolList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, float} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deFloatList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, double} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deDoubleList(Len, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
{list, RecordName} ->
<<Len:16/big, LeftListBin/binary>> = MsgBin,
{LeftBin, RetList} = deRecordList(Len, RecordName, LeftListBin, []),
decodeField(SchList, LeftBin, [RetList | Result]);
RecordName ->
<<IsUndef:8, LeftBin/binary>> = MsgBin,
case IsUndef of
0 ->
decodeField(SchList, LeftBin, [undefined | Result]);
_ ->
SubSchList = getMsgSchema(RecordName),
{SubLeftBin, SubResultList} = decodeField(SubSchList, LeftBin, [RecordName]),
decodeField(SchList, SubLeftBin, [list_to_tuple(SubResultList) | Result])
end
end.
<<IsUndef:8, LeftBin/binary>> = MsgBin,
case IsUndef of
0 ->
decodeField(SchList, LeftBin, [undefined | Result]);
_ ->
SubSchList = getMsgSchema(RecordName),
{SubLeftBin, SubResultList} = decodeField(SubSchList, LeftBin, [RecordName]),
decodeField(SchList, SubLeftBin, [list_to_tuple(SubResultList) | Result])
end
end.
deBoolList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deBoolList(N, <<Bool:8, LeftBin/binary>>, RetList) ->
case Bool =:= 1 of
true ->
deBoolList(N - 1, LeftBin, [true | RetList]);
_ ->
deBoolList(N - 1, LeftBin, [false | RetList])
end.
{MsgBin, lists:reverse(RetList)};
deBoolList(N, MsgBin, RetList) ->
<<Bool:8, LeftBin/binary>> = MsgBin,
case Bool =:= 1 of
true ->
deBoolList(N - 1, LeftBin, [true | RetList]);
_ ->
deBoolList(N - 1, LeftBin, [false | RetList])
end.
deInt8List(0, MsgBin, RetList) ->
{MsgBin, RetList};
deInt8List(N, <<Int:8/big-signed, LeftBin/binary>>, RetList) ->
deInt8List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, RetList};
deInt8List(N, MsgBin, RetList) ->
<<Int:8/big-signed, LeftBin/binary>> = MsgBin,
deInt8List(N - 1, LeftBin, [Int | RetList]).
deUint8List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint8List(N, <<Int:8/big-unsigned, LeftBin/binary>>, RetList) ->
deUint8List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint8List(N, MsgBin, RetList) ->
<<Int:8/big-unsigned, LeftBin/binary>> = MsgBin,
deUint8List(N - 1, LeftBin, [Int | RetList]).
deInt16List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deInt16List(N, <<Int:16/big-signed, LeftBin/binary>>, RetList) ->
deInt16List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deInt16List(N, MsgBin, RetList) ->
<<Int:16/big-signed, LeftBin/binary>> = MsgBin,
deInt16List(N - 1, LeftBin, [Int | RetList]).
deUint16List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint16List(N, <<Int:16/big-unsigned, LeftBin/binary>>, RetList) ->
deUint16List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint16List(N, MsgBin, RetList) ->
<<Int:16/big-unsigned, LeftBin/binary>> = MsgBin,
deUint16List(N - 1, LeftBin, [Int | RetList]).
deInt32List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deInt32List(N, <<Int:32/big-signed, LeftBin/binary>>, RetList) ->
deInt32List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deInt32List(N, MsgBin, RetList) ->
<<Int:32/big-signed, LeftBin/binary>> = MsgBin,
deInt32List(N - 1, LeftBin, [Int | RetList]).
deUint32List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint32List(N, <<Int:32/big-unsigned, LeftBin/binary>>, RetList) ->
deUint32List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint32List(N, MsgBin, RetList) ->
<<Int:32/big-unsigned, LeftBin/binary>> = MsgBin,
deUint32List(N - 1, LeftBin, [Int | RetList]).
deInt64List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deInt64List(N, <<Int:64/big-signed, LeftBin/binary>>, RetList) ->
deInt64List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deInt64List(N, MsgBin, RetList) ->
<<Int:64/big-signed, LeftBin/binary>> = MsgBin,
deInt64List(N - 1, LeftBin, [Int | RetList]).
deUint64List(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deUint64List(N, <<Int:64/big-unsigned, LeftBin/binary>>, RetList) ->
deUint64List(N - 1, LeftBin, [Int | RetList]).
{MsgBin, lists:reverse(RetList)};
deUint64List(N, MsgBin, RetList) ->
<<Int:64/big-unsigned, LeftBin/binary>> = MsgBin,
deUint64List(N - 1, LeftBin, [Int | RetList]).
deIntegerList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deIntegerList(N, MsgBin, RetList) ->
<<IntBits:8, Int:IntBits/big-signed, LeftBin/binary>> = MsgBin,
deIntegerList(N - 1, LeftBin, [Int | RetList]).
deNumberList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deNumberList(N, MsgBin, RetList) ->
<<NumBits:8, NumBin/binary>> = MsgBin,
case NumBits of
33 ->
<<Float:32/big-float, LeftBin/binary>> = NumBin,
deNumberList(N - 1, LeftBin, [Float | RetList]);
65 ->
<<Float:64/big-float, LeftBin/binary>> = NumBin,
deNumberList(N - 1, LeftBin, [Float | RetList]);
_ ->
<<Int:NumBits/big-signed, LeftBin/binary>> = NumBin,
deNumberList(N - 1, LeftBin, [Int | RetList])
end.
deFloatList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deFloatList(N, <<Float:32/big-float, LeftBin/binary>>, RetList) ->
deFloatList(N - 1, LeftBin, [Float | RetList]).
{MsgBin, lists:reverse(RetList)};
deFloatList(N, MsgBin, RetList) ->
<<Float:32/big-float, LeftBin/binary>> = MsgBin,
deFloatList(N - 1, LeftBin, [Float | RetList]).
deDoubleList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deDoubleList(N, <<Float:64/big-float, LeftBin/binary>>, RetList) ->
deDoubleList(N - 1, LeftBin, [Float | RetList]).
{MsgBin, lists:reverse(RetList)};
deDoubleList(N, MsgBin, RetList) ->
<<Float:64/big-float, LeftBin/binary>> = MsgBin,
deDoubleList(N - 1, LeftBin, [Float | RetList]).
deStringList(0, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
deStringList(N, <<Len:16/big, StrBin:Len/binary-unit:8, LeftBin/binary>>, RetList) ->
deStringList(N - 1, LeftBin, [StrBin | RetList]).
{MsgBin, lists:reverse(RetList)};
deStringList(N, MsgBin, RetList) ->
<<Len:16/big, StrBin:Len/binary-unit:8, LeftBin/binary>> = MsgBin,
deStringList(N - 1, LeftBin, [StrBin | RetList]).
deRecordList(0, _RecordName, MsgBin, RetList) ->
{MsgBin, lists:reverse(RetList)};
{MsgBin, lists:reverse(RetList)};
deRecordList(N, RecordName, MsgBin, RetList) ->
{LeftBin, Tuple} = decodeRec(RecordName, MsgBin),
deRecordList(N - 1, RecordName, LeftBin, [Tuple | RetList]).
getMsgType(1)->
test;
getMsgType(2)->
phoneNumber;
getMsgType(3)->
person;
getMsgType(4)->
addressBook;
getMsgType(_) ->
undefined.
getMsgId(test)->
1;
getMsgId(phoneNumber)->
2;
getMsgId(person)->
3;
getMsgId(addressBook)->
4;
getMsgId(_) ->
0.
{LeftBin, Tuple} = decodeRec(RecordName, MsgBin),
deRecordList(N - 1, RecordName, LeftBin, [Tuple | RetList]).
getMsgType(1)-> test;
getMsgType(2)-> phoneNumber;
getMsgType(3)-> person;
getMsgType(4)-> addressBook;
getMsgType(5)-> union;
getMsgType(6)-> tbool;
getMsgType(7)-> tint8;
getMsgType(8)-> tuint8;
getMsgType(9)-> tint16;
getMsgType(10)-> tuint16;
getMsgType(11)-> tint32;
getMsgType(12)-> tuint32;
getMsgType(13)-> tint64;
getMsgType(14)-> tuint64;
getMsgType(15)-> tinteger;
getMsgType(16)-> tnumber;
getMsgType(17)-> tfloat;
getMsgType(18)-> tdouble;
getMsgType(19)-> tstring;
getMsgType(20)-> tlistbool;
getMsgType(21)-> tlistint8;
getMsgType(22)-> tlistuint8;
getMsgType(23)-> tlistint16;
getMsgType(24)-> tlistuint16;
getMsgType(25)-> tlistint32;
getMsgType(26)-> tlistuint32;
getMsgType(27)-> tlistint64;
getMsgType(28)-> tlistuint64;
getMsgType(29)-> tlistinteger;
getMsgType(30)-> tlistnumber;
getMsgType(31)-> tlistfloat;
getMsgType(32)-> tlistdouble;
getMsgType(33)-> tliststring;
getMsgType(34)-> tlistunion;
getMsgType(35)-> allType;
getMsgType(_) -> undefined.
getMsgId(test)-> 1;
getMsgId(phoneNumber)-> 2;
getMsgId(person)-> 3;
getMsgId(addressBook)-> 4;
getMsgId(union)-> 5;
getMsgId(tbool)-> 6;
getMsgId(tint8)-> 7;
getMsgId(tuint8)-> 8;
getMsgId(tint16)-> 9;
getMsgId(tuint16)-> 10;
getMsgId(tint32)-> 11;
getMsgId(tuint32)-> 12;
getMsgId(tint64)-> 13;
getMsgId(tuint64)-> 14;
getMsgId(tinteger)-> 15;
getMsgId(tnumber)-> 16;
getMsgId(tfloat)-> 17;
getMsgId(tdouble)-> 18;
getMsgId(tstring)-> 19;
getMsgId(tlistbool)-> 20;
getMsgId(tlistint8)-> 21;
getMsgId(tlistuint8)-> 22;
getMsgId(tlistint16)-> 23;
getMsgId(tlistuint16)-> 24;
getMsgId(tlistint32)-> 25;
getMsgId(tlistuint32)-> 26;
getMsgId(tlistint64)-> 27;
getMsgId(tlistuint64)-> 28;
getMsgId(tlistinteger)-> 29;
getMsgId(tlistnumber)-> 30;
getMsgId(tlistfloat)-> 31;
getMsgId(tlistdouble)-> 32;
getMsgId(tliststring)-> 33;
getMsgId(tlistunion)-> 34;
getMsgId(allType)-> 35;
getMsgId(_) -> 0.
encodeRec({test, V1}) ->
[?string(V1)];
@ -261,6 +437,68 @@ encodeRec({person, V1, V2, V3, V4}) ->
[?string(V1), ?int32(V2), ?string(V3), ?list_record(V4)];
encodeRec({addressBook, V1, V2}) ->
[?list_record(V1), ?list_record(V2)];
encodeRec({union, V1, V2}) ->
[?string(V1), ?int32(V2)];
encodeRec({tbool, V1}) ->
[?bool(V1)];
encodeRec({tint8, V1, V2}) ->
[?int8(V1), ?int8(V2)];
encodeRec({tuint8, V1, V2}) ->
[?uint8(V1), ?uint8(V2)];
encodeRec({tint16, V1, V2}) ->
[?int16(V1), ?int16(V2)];
encodeRec({tuint16, V1, V2}) ->
[?uint16(V1), ?uint16(V2)];
encodeRec({tint32, V1, V2}) ->
[?int32(V1), ?int32(V2)];
encodeRec({tuint32, V1, V2}) ->
[?uint32(V1), ?uint32(V2)];
encodeRec({tint64, V1, V2}) ->
[?int64(V1), ?int64(V2)];
encodeRec({tuint64, V1, V2}) ->
[?uint64(V1), ?uint64(V2)];
encodeRec({tinteger, V1, V2, V3, V4, V5, V6, V7, V8}) ->
[?integer(V1), ?integer(V2), ?integer(V3), ?integer(V4), ?integer(V5), ?integer(V6), ?integer(V7), ?integer(V8)];
encodeRec({tnumber, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10}) ->
[?number(V1), ?number(V2), ?number(V3), ?number(V4), ?number(V5), ?number(V6), ?number(V7), ?number(V8), ?number(V9), ?number(V10)];
encodeRec({tfloat, V1, V2}) ->
[?float(V1), ?float(V2)];
encodeRec({tdouble, V1, V2}) ->
[?double(V1), ?double(V2)];
encodeRec({tstring, V1, V2}) ->
[?string(V1), ?string(V2)];
encodeRec({tlistbool, V1}) ->
[?list_bool(V1)];
encodeRec({tlistint8, V1}) ->
[?list_int8(V1)];
encodeRec({tlistuint8, V1}) ->
[?list_uint8(V1)];
encodeRec({tlistint16, V1}) ->
[?list_int16(V1)];
encodeRec({tlistuint16, V1}) ->
[?list_uint16(V1)];
encodeRec({tlistint32, V1}) ->
[?list_int32(V1)];
encodeRec({tlistuint32, V1}) ->
[?list_uint32(V1)];
encodeRec({tlistint64, V1}) ->
[?list_int64(V1)];
encodeRec({tlistuint64, V1}) ->
[?list_uint64(V1)];
encodeRec({tlistinteger, V1}) ->
[?list_integer(V1)];
encodeRec({tlistnumber, V1}) ->
[?list_number(V1)];
encodeRec({tlistfloat, V1}) ->
[?list_float(V1)];
encodeRec({tlistdouble, V1}) ->
[?list_double(V1)];
encodeRec({tliststring, V1}) ->
[?list_string(V1)];
encodeRec({tlistunion, V1}) ->
[?list_record(V1)];
encodeRec({allType, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55}) ->
[?bool(V1), ?int8(V2), ?uint8(V3), ?int16(V4), ?uint16(V5), ?int32(V6), ?uint32(V7), ?int64(V8), ?uint64(V9), ?integer(V10), ?integer(V11), ?integer(V12), ?integer(V13), ?integer(V14), ?integer(V15), ?integer(V16), ?integer(V17), ?number(V18), ?number(V19), ?number(V20), ?number(V21), ?number(V22), ?number(V23), ?number(V24), ?number(V25), ?number(V26), ?number(V27), ?float(V28), ?double(V29), ?string(V30), ?string(V31), ?record(V32), ?list_bool(V33), ?list_int8(V34), ?list_uint8(V35), ?list_int16(V36), ?list_uint16(V37), ?list_int32(V38), ?list_uint32(V39), ?list_int64(V40), ?list_uint64(V41), ?list_integer(V42), ?list_integer(V43), ?list_integer(V44), ?list_integer(V45), ?list_number(V46), ?list_number(V47), ?list_number(V48), ?list_number(V49), ?list_number(V50), ?list_number(V51), ?list_float(V52), ?list_double(V53), ?list_string(V54), ?list_record(V55)];
encodeRec(_) ->
[].
@ -280,6 +518,155 @@ getMsgSchema(addressBook)->
getMsgSchema(4);
getMsgSchema(4)->
[{list,person},{list,person}];
getMsgSchema(union)->
getMsgSchema(5);
getMsgSchema(5)->
[string,int32];
getMsgSchema(tbool)->
getMsgSchema(6);
getMsgSchema(6)->
[bool];
getMsgSchema(tint8)->
getMsgSchema(7);
getMsgSchema(7)->
[int8,int8];
getMsgSchema(tuint8)->
getMsgSchema(8);
getMsgSchema(8)->
[uint8,uint8];
getMsgSchema(tint16)->
getMsgSchema(9);
getMsgSchema(9)->
[int16,int16];
getMsgSchema(tuint16)->
getMsgSchema(10);
getMsgSchema(10)->
[uint16,uint16];
getMsgSchema(tint32)->
getMsgSchema(11);
getMsgSchema(11)->
[int32,int32];
getMsgSchema(tuint32)->
getMsgSchema(12);
getMsgSchema(12)->
[uint32,uint32];
getMsgSchema(tint64)->
getMsgSchema(13);
getMsgSchema(13)->
[int64,int64];
getMsgSchema(tuint64)->
getMsgSchema(14);
getMsgSchema(14)->
[uint64,uint64];
getMsgSchema(tinteger)->
getMsgSchema(15);
getMsgSchema(15)->
[integer,integer,integer,integer,integer,integer,integer,integer];
getMsgSchema(tnumber)->
getMsgSchema(16);
getMsgSchema(16)->
[number,number,number,number,number,number,number,number,number,number];
getMsgSchema(tfloat)->
getMsgSchema(17);
getMsgSchema(17)->
[float,float];
getMsgSchema(tdouble)->
getMsgSchema(18);
getMsgSchema(18)->
[double,double];
getMsgSchema(tstring)->
getMsgSchema(19);
getMsgSchema(19)->
[string,string];
getMsgSchema(tlistbool)->
getMsgSchema(20);
getMsgSchema(20)->
[{list,bool}];
getMsgSchema(tlistint8)->
getMsgSchema(21);
getMsgSchema(21)->
[{list,int8}];
getMsgSchema(tlistuint8)->
getMsgSchema(22);
getMsgSchema(22)->
[{list,uint8}];
getMsgSchema(tlistint16)->
getMsgSchema(23);
getMsgSchema(23)->
[{list,int16}];
getMsgSchema(tlistuint16)->
getMsgSchema(24);
getMsgSchema(24)->
[{list,uint16}];
getMsgSchema(tlistint32)->
getMsgSchema(25);
getMsgSchema(25)->
[{list,int32}];
getMsgSchema(tlistuint32)->
getMsgSchema(26);
getMsgSchema(26)->
[{list,uint32}];
getMsgSchema(tlistint64)->
getMsgSchema(27);
getMsgSchema(27)->
[{list,int64}];
getMsgSchema(tlistuint64)->
getMsgSchema(28);
getMsgSchema(28)->
[{list,uint64}];
getMsgSchema(tlistinteger)->
getMsgSchema(29);
getMsgSchema(29)->
[{list,integer}];
getMsgSchema(tlistnumber)->
getMsgSchema(30);
getMsgSchema(30)->
[{list,number}];
getMsgSchema(tlistfloat)->
getMsgSchema(31);
getMsgSchema(31)->
[{list,float}];
getMsgSchema(tlistdouble)->
getMsgSchema(32);
getMsgSchema(32)->
[{list,double}];
getMsgSchema(tliststring)->
getMsgSchema(33);
getMsgSchema(33)->
[{list,string}];
getMsgSchema(tlistunion)->
getMsgSchema(34);
getMsgSchema(34)->
[{list,union}];
getMsgSchema(allType)->
getMsgSchema(35);
getMsgSchema(35)->
[bool,int8,uint8,int16,uint16,int32,uint32,int64,uint64,integer,integer,
integer,integer,integer,integer,integer,integer,number,number,number,number,
number,number,number,number,number,number,float,double,string,string,union,
{list,bool},
{list,int8},
{list,uint8},
{list,int16},
{list,uint16},
{list,int32},
{list,uint32},
{list,int64},
{list,uint64},
{list,integer},
{list,integer},
{list,integer},
{list,integer},
{list,number},
{list,number},
{list,number},
{list,number},
{list,number},
{list,number},
{list,float},
{list,double},
{list,string},
{list,union}];
getMsgSchema(_) ->
[].

+ 184
- 10
test/protoMsg.hrl Прегледај датотеку

@ -6,23 +6,197 @@
-opaque uint16() :: 0..65536.
-opaque uint32() :: 0..4294967295.
-opaque uint64() :: 0..18446744073709551615.
-opaque single() :: float().
-opaque double() :: float().
-record(test ,{
aa = "" :: string()
}).
}).
-record(phoneNumber ,{
number = undefined :: #test{}
,type = 0 :: int32()
}).
, type = 0 :: int32()
}).
-record(person ,{
name = "" :: string()
,id = 0 :: int32()
,email = "" :: string()
,phone = [] :: [#phoneNumber{}]
}).
, id = 0 :: int32()
, email = "" :: string()
, phone = [] :: [#phoneNumber{}]
}).
-record(addressBook ,{
person = [] :: [#person{}]
,other = [] :: [#person{}]
}).
, other = [] :: [#person{}]
}).
-record(union ,{
test = "" :: string()
, type = 0 :: int32()
}).
-record(tbool ,{
bool = false :: boolean()
}).
-record(tint8 ,{
int1 = 0 :: int8()
, int2 = 0 :: int8()
}).
-record(tuint8 ,{
int1 = 0 :: uint8()
, int2 = 0 :: uint8()
}).
-record(tint16 ,{
int1 = 0 :: int16()
, int2 = 0 :: int16()
}).
-record(tuint16 ,{
int1 = 0 :: uint16()
, int2 = 0 :: uint16()
}).
-record(tint32 ,{
int1 = 0 :: int32()
, int2 = 0 :: int32()
}).
-record(tuint32 ,{
int1 = 0 :: uint32()
, int2 = 0 :: uint32()
}).
-record(tint64 ,{
int1 = 0 :: int64()
, int2 = 0 :: int64()
}).
-record(tuint64 ,{
int1 = 0 :: uint64()
, int2 = 0 :: uint64()
}).
-record(tinteger ,{
int1 = 0 :: integer()
, int2 = 0 :: integer()
, int3 = 0 :: integer()
, int4 = 0 :: integer()
, int5 = 0 :: integer()
, int6 = 0 :: integer()
, int7 = 0 :: integer()
, int8 = 0 :: integer()
}).
-record(tnumber ,{
int1 = 0 :: number()
, int2 = 0 :: number()
, int3 = 0 :: number()
, int4 = 0 :: number()
, int5 = 0 :: number()
, int6 = 0 :: number()
, int7 = 0 :: number()
, int8 = 0 :: number()
, float1 = 0 :: number()
, float2 = 0 :: number()
}).
-record(tfloat ,{
int1 = 0.0 :: float()
, int2 = 0.0 :: float()
}).
-record(tdouble ,{
int1 = 0.0 :: double()
, int2 = 0.0 :: double()
}).
-record(tstring ,{
int1 = "" :: string()
, int2 = "" :: string()
}).
-record(tlistbool ,{
int1 = [] :: [boolean()]
}).
-record(tlistint8 ,{
int1 = [] :: [int8()]
}).
-record(tlistuint8 ,{
int1 = [] :: [uint8()]
}).
-record(tlistint16 ,{
int1 = [] :: [int16()]
}).
-record(tlistuint16 ,{
int1 = [] :: [uint16()]
}).
-record(tlistint32 ,{
int1 = [] :: [int32()]
}).
-record(tlistuint32 ,{
int1 = [] :: [uint32()]
}).
-record(tlistint64 ,{
int1 = [] :: [int64()]
}).
-record(tlistuint64 ,{
int1 = [] :: [uint64()]
}).
-record(tlistinteger ,{
int1 = [] :: [integer()]
}).
-record(tlistnumber ,{
int1 = [] :: [number()]
}).
-record(tlistfloat ,{
int1 = [] :: [float()]
}).
-record(tlistdouble ,{
int1 = [] :: [double()]
}).
-record(tliststring ,{
int1 = [] :: [string()]
}).
-record(tlistunion ,{
int1 = [] :: [#union{}]
}).
-record(allType ,{
bool = false :: boolean()
, int8 = 0 :: int8()
, uint8 = 0 :: uint8()
, int16 = 0 :: int16()
, uint16 = 0 :: uint16()
, int32 = 0 :: int32()
, uint32 = 0 :: uint32()
, int64 = 0 :: int64()
, uint64 = 0 :: uint64()
, inte8 = 0 :: integer()
, uinte8 = 0 :: integer()
, inte16 = 0 :: integer()
, uinte16 = 0 :: integer()
, inte32 = 0 :: integer()
, uinte32 = 0 :: integer()
, inte64 = 0 :: integer()
, uinte64 = 0 :: integer()
, num8 = 0 :: number()
, unum8 = 0 :: number()
, num16 = 0 :: number()
, unum16 = 0 :: number()
, num32 = 0 :: number()
, unum32 = 0 :: number()
, num64 = 0 :: number()
, unum64 = 0 :: number()
, numfloat = 0 :: number()
, numdouble = 0 :: number()
, float = 0.0 :: float()
, double = 0.0 :: double()
, string1 = "" :: string()
, string2 = "" :: string()
, union = undefined :: #union{}
, lbool = [] :: [boolean()]
, lint8 = [] :: [int8()]
, luint8 = [] :: [uint8()]
, lint16 = [] :: [int16()]
, luint16 = [] :: [uint16()]
, lint32 = [] :: [int32()]
, luint32 = [] :: [uint32()]
, lint64 = [] :: [int64()]
, luint64 = [] :: [uint64()]
, linte8 = [] :: [integer()]
, linte16 = [] :: [integer()]
, linte32 = [] :: [integer()]
, linte64 = [] :: [integer()]
, lnum8 = [] :: [number()]
, lnum16 = [] :: [number()]
, lnum32 = [] :: [number()]
, lnum64 = [] :: [number()]
, lnfloat32 = [] :: [number()]
, lnfloat64 = [] :: [number()]
, lfloat = [] :: [float()]
, ldouble = [] :: [double()]
, lstring = [] :: [string()]
, lunion = [] :: [#union{}]
}).

+ 1
- 0
test/start.bat Прегледај датотеку

@ -0,0 +1 @@
start werl.exe

+ 231
- 0
test/test.erl Прегледај датотеку

@ -3,6 +3,237 @@
-include("protoMsg.hrl").
-compile(export_all).
test1() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tbool{bool = true}))).
test21() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint8{int1 = 123, int2 = -22}))).
test22() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint8{int1 = 123, int2 = 182}))).
test31() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint16{int1 = 12343, int2 = -3422}))).
test32() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint16{int1 = 43244, int2 = 43243}))).
test41() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint32{int1 = 12343434, int2 = -34434322}))).
test42() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint32{int1 = 432444343, int2 = 432443433}))).
test51() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tint64{int1 = 12344343434, int2 = -344343434322}))).
test52() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tuint64{int1 = 4343432444343, int2 = 4324434343433}))).
test6() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tinteger{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434}))).
test7() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tnumber{int1 = -1, int2 = 1, int3 = 128, int4 = -128, int5 = -3244232, int6 = 432423432, int7 = -43434343434434, int8 = 432424242434, float1 = -34234343.343, float2 = 43242342342342.434}))).
test81() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tfloat{int1 = -34234343.343, int2 = 42342342.434}))).
test82() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tdouble{int1 = -342343433333.343, int2 = 423423423333.434}))).
test9() ->
protoMsg:decode(iolist_to_binary(protoMsg:encode(#tstring{int1 = "dfdf143242", int2 = "发地方撒发送"}))).
allType() ->
AllType = #allType{
bool = false
,int8 = -66
,uint8 = 66
,int16 = -666
,uint16 = 666
,int32 = -66666
,uint32 = 66666
,int64 = -5294967296
,uint64 = 5294967296
,inte8 = -88
,uinte8 = 88
,inte16 = -8888
,uinte16 = 8888
,inte32 = -888888
,uinte32 = 888888
,inte64 = -8888888888
,uinte64 = 8888888888
,num8 = -99
,unum8 = 99
,num16 = -9999
,unum16 = 9999
,num32 = -999999
,unum32 = 999999
,num64 = -9999999999
,unum64 = 9999999999
,numfloat = 999999.99999
,numdouble = 9999999999.99999
,float = 123456.789321
,double = 4300000000.789321
,string1 = "this is a test!!!!"
,string2 = "这是一个测试, 等会看结果~!!!"
,lbool = [true, false, true, false]
,lint8 = [123, -123, 66, -66, 88, -88]
,luint8 = [1,2,3,123,67,88]
,lint16 = [-12345, 12345, 6666, -6666]
,luint16 = [12345, 12345, 6666, 6666]
,lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666]
,luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666]
,lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666]
,luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666]
,linte8 = [123, 12, -66, 66, 34]
,linte16 = [12334, 12, -6666, 6666, 3412]
,linte32 = [12334, 12, -666666666, 6666, 3412]
,linte64 = [12334, 12, -666666666666, 6666, 3412]
,lnum8 = [123, 12.123, -66.456789, 66, 34]
,lnum16 = [12334, -12, -6666.6666, 6666, 3412]
,lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412]
,lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999]
,lnfloat32 = [-666666.88888,4434.434, 434.43, 3434]
,lnfloat64 = [-666666666666.88888,4434.434, 434.43, 11111111111.34343,5566]
,lfloat = [1.1, 2.2, 3.3, 666666.666]
,ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666]
,lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式">>]
,lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}]
},
List = protoMsg:encode(AllType),
io:format("~p~n",[List]),
protoMsg:decode(iolist_to_binary(List)).
tall1(0) ->
ok;
tall1(N) ->
AllType = #allType{
bool = false
,int8 = -66
,uint8 = 66
,int16 = -666
,uint16 = 666
,int32 = -66666
,uint32 = 66666
,int64 = -5294967296
,uint64 = 5294967296
,inte8 = -88
,uinte8 = 88
,inte16 = -8888
,uinte16 = 8888
,inte32 = -888888
,uinte32 = 888888
,inte64 = -8888888888
,uinte64 = 8888888888
,num8 = -99
,unum8 = 99
,num16 = -9999
,unum16 = 9999
,num32 = -999999
,unum32 = 999999
,num64 = -9999999999
,unum64 = 9999999999
,numfloat = 999999.99999
,numdouble = 9999999999.99999
,float = 123456.789321
,double = 4300000000.789321
,string1 = "this is a test!!!!"
,string2 = "这是一个测试, 等会看结果~!!!"
,lbool = [true, false, true, false]
,lint8 = [123, -123, 66, -66, 88, -88]
,luint8 = [1,2,3,123,67,88]
,lint16 = [-12345, 12345, 6666, -6666]
,luint16 = [12345, 12345, 6666, 6666]
,lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666]
,luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666]
,lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666]
,luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666]
,linte8 = [123, 12, -66, 66, 34]
,linte16 = [12334, 12, -6666, 6666, 3412]
,linte32 = [12334, 12, -666666666, 6666, 3412]
,linte64 = [12334, 12, -666666666666, 6666, 3412]
,lnum8 = [123, 12.123, -66.456789, 66, 34]
,lnum16 = [12334, -12, -6666.6666, 6666, 3412]
,lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412]
,lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999]
,lnfloat32 = [-666666.88888,4434.434, 434.43, 3434]
,lnfloat64 = [-666666666666.88888,4434.434, 434.43, 11111111111.34343,5566]
,lfloat = [1.1, 2.2, 3.3, 666666.666]
,ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666]
,lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式">>]
,lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}]
},
%protoMsg:encode(AllType),
term_to_binary(AllType),
tall1(N - 1).
tall(N) ->
AllType = #allType{
bool = false
,int8 = -66
,uint8 = 66
,int16 = -666
,uint16 = 666
,int32 = -66666
,uint32 = 66666
,int64 = -5294967296
,uint64 = 5294967296
,inte8 = -88
,uinte8 = 88
,inte16 = -8888
,uinte16 = 8888
,inte32 = -888888
,uinte32 = 888888
,inte64 = -8888888888
,uinte64 = 8888888888
,num8 = -99
,unum8 = 99
,num16 = -9999
,unum16 = 9999
,num32 = -999999
,unum32 = 999999
,num64 = -9999999999
,unum64 = 9999999999
,numfloat = 999999.99999
,numdouble = 9999999999.99999
,float = 123456.789321
,double = 4300000000.789321
,string1 = "this is a test!!!!"
,string2 = "这是一个测试, 等会看结果~!!!"
,lbool = [true, false, true, false]
,lint8 = [123, -123, 66, -66, 88, -88]
,luint8 = [1,2,3,123,67,88]
,lint16 = [-12345, 12345, 6666, -6666]
,luint16 = [12345, 12345, 6666, 6666]
,lint32 = [-12345, 12345, 6666, -6666, -4000000000, 66666666]
,luint32 = [12345, 12345, 6666, 6666, 4000000000, 66666666]
,lint64 = [-12345, 12345, -6666, 6666, 400000000000, -66666666666666666]
,luint64 = [12345, 12345, 6666, 6666, 400000000000, 66666666666666666]
,linte8 = [123, 12, -66, 66, 34]
,linte16 = [12334, 12, -6666, 6666, 3412]
,linte32 = [12334, 12, -666666666, 6666, 3412]
,linte64 = [12334, 12, -666666666666, 6666, 3412]
,lnum8 = [123, 12.123, -66.456789, 66, 34]
,lnum16 = [12334, -12, -6666.6666, 6666, 3412]
,lnum32 = [12334, 12.7777, -666666666.666777, 6666, 3412]
,lnum64 = [12334, 12, -666666666666.88888, 6666, 3412.9999]
,lnfloat32 = [-666666.88888,4434.434, 434.43, 3434]
,lnfloat64 = [-666666666666.88888,4434.434, 434.43, 11111111111.34343,5566]
,lfloat = [1.1, 2.2, 3.3, 666666.666]
,ldouble = [111111111.1, 22222222.2, 3.3, 66666622333333.666]
,lstring = ["fdsafsdfsfs", "电风扇打法胜多负少的", <<"fdsfasdfsfs">>, <<"大丰收大丰收的方式">>]
,lunion = [#union{}, #union{type = 1, test = "aaaaa"}, #union{type = 2, test = "嘿嘿嘿嘿"}]
},
%List = protoMsg:encode(AllType),
tall(N, term_to_binary(AllType)).
tall(0, Bin) ->
Bin;
tall(N, Bin) ->
%protoMsg:decode(Bin),
binary_to_term(Bin),
tall(N - 1, Bin).
hh1(0) ->
ok;
hh1(N) ->

Loading…
Откажи
Сачувај