Browse Source

ft: 目录结构调整 模块重命名 为后续其他语言做准备

master
SisMaker 4 years ago
parent
commit
421c0000c8
8 changed files with 79 additions and 77 deletions
  1. +0
    -9
      ebin/genProto.app
  2. +1
    -0
      include/genDef.hrl
  3. +59
    -0
      src/genProto.erl
  4. +1
    -1
      src/writeErl/gErlCode.erl
  5. +1
    -1
      src/writeErl/gErlField.erl
  6. +15
    -64
      src/writeErl/gGenErl.erl
  7. +1
    -1
      test/genProto.bat
  8. +1
    -1
      test/protoMsg.erl

+ 0
- 9
ebin/genProto.app View File

@ -1,9 +0,0 @@
{application,genProto,
[{description,"An OTP library"},
{vsn,"0.1.0"},
{registered,[]},
{applications,[kernel,stdlib]},
{env,[]},
{modules,[protoCode,protoField,protoGen,protoParse]},
{licenses,["Apache 2.0"]},
{links,[]}]}.

+ 1
- 0
include/genDef.hrl View File

@ -0,0 +1 @@
-define(MsgIdSegSize, 1000).

+ 59
- 0
src/genProto.erl View File

@ -0,0 +1,59 @@
-module(genProto).
-include("genDef.hrl").
-export([
convertFile/1
, convert/1
, convertDir/0
, convertDir/1
, convertDir/3
]).
convertFile(File) ->
protoParse:parseFile(File).
convert([ProtoDir, HrlDir, ErlDir]) ->
convertDir(atom_to_list(ProtoDir), atom_to_list(HrlDir), atom_to_list(ErlDir)).
convertDir() ->
convertDir("./", "./", "./").
convertDir(ProtoDir) ->
convertDir(ProtoDir, "./", "./").
convertDir(ProtoDir, HrlDir, ErlDir) ->
erlang:put(pd_errlist, []),
erlang:put(pd_handler, []),
FunRead =
fun(File, ProAcc) ->
case filename:extension(File) == ".mpdf" of
true ->
io:format("Convert proto msg file: ~s ~n", [File]),
BaseName = filename:basename(File, ".mpdf"),
[ModIndex, ModName] = re:split(BaseName, "_"),
Index = binary_to_integer(ModIndex),
erlang:put(pd_messageid, Index * ?MsgIdSegSize + 1),
erlang:put(pd_handler, [{Index, ModName} | erlang:get(pd_handler)]),
erlang:put(pd_errcodeid, Index * ?MsgIdSegSize + 1),
SProto = protoParse:parseFile(File),
ErrCode = erlang:get(pd_errlist),
Handler = erlang:get(pd_handler),
erlang:erase(),
erlang:put(pd_errlist, ErrCode),
erlang:put(pd_handler, Handler),
[SProto | ProAcc];
_ ->
ProAcc
end
end,
SProtoListOfList = filelib:fold_files(ProtoDir, "\\.mpdf$", true, FunRead, []),
SProtoList = lists:append(SProtoListOfList),
ErrCodeList = erlang:get(pd_errlist),
SortedSProtoList = lists:sort(fun({_Name1, MessageId1, _FieldList1}, {_Name2, MessageId2, _FieldList2}) ->
MessageId1 > MessageId2 end, SProtoList),
SortedErrList = lists:sort(fun({_ErrName1, ErrCodeId1, _Desc1}, {_ErrName2, ErrCodeId2, _Desc2}) ->
ErrCodeId1 > ErrCodeId2 end, ErrCodeList),
gGenErl:genErl(SortedSProtoList, SortedErrList, HrlDir, ErlDir).
%%

src/protoCode.erl → src/writeErl/gErlCode.erl View File

@ -1,4 +1,4 @@
-module(protoCode).
-module(gErlCode).
-compile([export_all, nowarn_unused_function, nowarn_export_all]).
-export([encodeIol/1, encodeBin/1, encodeIol/2, subEncode/1, subEncode/2, decode/1, decodeBin/2]).

src/protoField.erl → src/writeErl/gErlField.erl View File

@ -1,4 +1,4 @@
-module(protoField).
-module(gErlField).
-compile([export_all, nowarn_export_all]).

src/protoGen.erl → src/writeErl/gGenErl.erl View File

@ -1,25 +1,20 @@
-module(protoGen).
-module(gGenErl).
-include("genDef.hrl").
-export([
convertFile/1
, convert/1
, convertDir/0
, convertDir/1
, convertDir/3
genErl/4
]).
-define(MsgIdSegSize, 1000).
protoHrlHeader() ->
"-opaque int8() :: -128..127.
-opaque int16() :: -32768..32767.
-opaque int32() :: -2147483648..2147483647.
-opaque int64() :: -9223372036854775808..9223372036854775807.
-opaque uint8() :: 0..255.
-opaque uint16() :: 0..65536.
-opaque uint32() :: 0..4294967295.
-opaque uint64() :: 0..18446744073709551615.
-opaque double() :: float().\n\n".
"-opaque int8() :: -128..127.
-opaque int16() :: -32768..32767.
-opaque int32() :: -2147483648..2147483647.
-opaque int64() :: -9223372036854775808..9223372036854775807.
-opaque uint8() :: 0..255.
-opaque uint16() :: 0..65536.
-opaque uint32() :: 0..4294967295.
-opaque uint64() :: 0..18446744073709551615.
-opaque double() :: float().\n\n".
protoErlHeader() ->
"-module(protoMsg).\n\n
@ -189,7 +184,7 @@ genMsgHrl(FieldInfo, {Index, Len, AccList}) ->
_ ->
", "
end,
RecStr = TemStr ++ protoField:builtRecStr(FieldInfo) ++ (case Index == Len of true -> ""; _ -> "\t" end),
RecStr = TemStr ++ gErlField:builtRecStr(FieldInfo) ++ (case Index == Len of true -> ""; _ -> "\t" end),
{Index - 1, Len, [RecStr | AccList]}.
genErrCodeHrl({ErrName, ErrCodeId, ComDesc}, AccList) ->
@ -214,7 +209,7 @@ genEncodeRec({MsgName, MsgId, FieldList}, IsForBin) ->
FunBody =
fun({FieldType, _FieldName}, {Index, PStrAcc}) ->
TemV = "V" ++ integer_to_list(Index),
PackStr = protoField:builtPackStr(FieldType) ++ TemV ++ ")",
PackStr = gErlField:builtPackStr(FieldType) ++ TemV ++ ")",
case Index == 1 of
true ->
{Index - 1, PackStr ++ PStrAcc};
@ -650,50 +645,8 @@ genDecodeBin({MsgName, MsgId, FieldList}, SortedSProtoList, IsForBin) ->
end,
HeadStr ++ LBodyStr ++ RetStr.
convertFile(File) ->
protoParse:parseFile(File).
convert([ProtoDir, HrlDir, ErlDir]) ->
convertDir(atom_to_list(ProtoDir), atom_to_list(HrlDir), atom_to_list(ErlDir)).
convertDir() ->
convertDir("./", "./", "./").
convertDir(ProtoDir) ->
convertDir(ProtoDir, "./", "./").
convertDir(ProtoDir, HrlDir, ErlDir) ->
erlang:put(pd_errlist, []),
erlang:put(pd_handler, []),
FunRead =
fun(File, ProAcc) ->
case filename:extension(File) == ".mpdf" of
true ->
io:format("Convert proto msg file: ~s ~n", [File]),
BaseName = filename:basename(File, ".mpdf"),
[ModIndex , ModName] = re:split(BaseName, "_"),
Index = binary_to_integer(ModIndex),
erlang:put(pd_messageid, Index * ?MsgIdSegSize + 1),
erlang:put(pd_handler, [{Index, ModName} | erlang:get(pd_handler)]),
erlang:put(pd_errcodeid, Index * ?MsgIdSegSize + 1),
SProto = protoParse:parseFile(File),
ErrCode = erlang:get(pd_errlist),
Handler = erlang:get(pd_handler),
erlang:erase(),
erlang:put(pd_errlist, ErrCode),
erlang:put(pd_handler, Handler),
[SProto | ProAcc];
_ ->
ProAcc
end
end,
%% .mpdf结尾的文件 FunRead函数中纠正处理一下
SProtoListOfList = filelib:fold_files(ProtoDir, "\\.mpdf$", true, FunRead, []),
SProtoList = lists:append(SProtoListOfList),
ErrCodeList = erlang:get(pd_errlist),
genErl(SortedSProtoList, SortedErrList, HrlDir, ErlDir) ->
initSubRec(),
SortedSProtoList = lists:sort(fun({_Name1, MessageId1, _FieldList1}, {_Name2, MessageId2, _FieldList2}) ->
MessageId1 > MessageId2 end, SProtoList),
FunSpell =
fun({MsgName, _MsgId, FieldList} = MsgInfo, {MsgHrlAcc, _MsgIdAcc, MsgEncodeAcc, MsgDecodeAcc}) ->
%% gen hrl str
@ -715,8 +668,6 @@ convertDir(ProtoDir, HrlDir, ErlDir) ->
end,
{MsgHrlStr, _MsgIdStr, MsgEncodeStr, MsgDecodeStr} = lists:foldl(FunSpell, {[], ["getMsgId(_) -> 0.\n\n"], ["encodeIol(_, _) ->\n\t[].\n\n"], ["decodeBin(_, _) ->\n\t{undefinedHer, undefined, {}}.\n\n"]}, SortedSProtoList),
SortedErrList = lists:sort(fun({_ErrName1, ErrCodeId1, _Desc1}, {_ErrName2, ErrCodeId2, _Desc2}) ->
ErrCodeId1 > ErrCodeId2 end, ErrCodeList),
ErrCodeStr = lists:foldl(fun genErrCodeHrl/2, [], SortedErrList) ++ "\n\n",
%% gen decodeRec str

+ 1
- 1
test/genProto.bat View File

@ -1 +1 @@
erl -pa ../ebin -s protoGen convert ../proto ./ ./
erl -pa ../ebin -s genProto convert ../proto ./ ./

+ 1
- 1
test/protoMsg.erl View File

@ -847,5 +847,5 @@ decodeBin(1001, LeftBin0) ->
{V4, LeftBin5} = deRecordList(Len3, 2, LeftBin4, []),
{errorHer, person1, {person1, V1, V2, V3, V4}};
decodeBin(_, _) ->
{undefined, {}}.
{undefinedHer, undefined, {}}.

Loading…
Cancel
Save