Leave state/event blank if not present

This commit is contained in:
Hans Svensson 2019-05-28 13:07:24 +02:00
parent 0ded431df8
commit 4b0837dc59
2 changed files with 23 additions and 31 deletions

View File

@ -76,29 +76,30 @@ join_errors(Prefix, Errors, Pfun) ->
list_to_binary(string:join([Prefix|Ess], "\n")). list_to_binary(string:join([Prefix|Ess], "\n")).
encode_contract(Contract) -> encode_contract(Contract) ->
Cname = contract_name(Contract), C0 = #{name => encode_name(contract_name(Contract))},
Tdefs0 = [ encode_typedef(T) Tdefs0 = [ encode_typedef(T) || T <- sort_decls(contract_types(Contract)) ],
|| T <- sort_decls(contract_types(Contract)) ], FilterT = fun(N) -> fun(#{name := N1}) -> N == N1 end end,
Tdefs = [ T || T = #{name := N} <- Tdefs0, N /= <<"state">>, N /= <<"event">> ], {Es, Tdefs1} = lists:partition(FilterT(<<"event">>), Tdefs0),
StateT = case [ T || T = #{name := N} <- Tdefs0, N == <<"state">> ] of {Ss, Tdefs} = lists:partition(FilterT(<<"state">>), Tdefs1),
[#{typedef := ST}] -> ST;
[] -> #{tuple => []} C1 = C0#{type_defs => Tdefs},
end,
EventT = case [ T || T = #{name := N} <- Tdefs0, N == <<"event">> ] of C2 = case Es of
[#{typedef := ET}] -> ET; [] -> C1;
[] -> #{variant => [#{<<"NoEventsDefined">> => []}]} [#{typedef := ET}] -> C1#{event => ET}
end, end,
C3 = case Ss of
[] -> C2;
[#{typedef := ST}] -> C2#{state => ST}
end,
Fdefs = [ encode_function(F) Fdefs = [ encode_function(F)
|| F <- sort_decls(contract_funcs(Contract)), || F <- sort_decls(contract_funcs(Contract)),
not is_private(F) ], not is_private(F) ],
#{contract => #{name => encode_name(Cname), #{contract => C3#{functions => Fdefs}}.
type_defs => Tdefs,
state => StateT,
event => EventT,
functions => Fdefs}}.
%% Encode a function definition. Currently we are only interested in %% Encode a function definition. Currently we are only interested in
%% the interface and type. %% the interface and type.
@ -215,14 +216,13 @@ do_render_aci_json(Json) ->
{ok, list_to_binary(string:join(DecodedContracts, "\n"))}. {ok, list_to_binary(string:join(DecodedContracts, "\n"))}.
decode_contract(#{name := Name, decode_contract(#{name := Name,
type_defs := Ts, type_defs := Ts0,
event := E, functions := Fs} = C) ->
state := S,
functions := Fs}) ->
MkTDef = fun(N, T) -> #{name => N, vars => [], typedef => T} end, MkTDef = fun(N, T) -> #{name => N, vars => [], typedef => T} end,
Ts = [ MkTDef(<<"state">>, maps:get(state, C)) || maps:is_key(state, C) ] ++
[ MkTDef(<<"event">>, maps:get(event, C)) || maps:is_key(event, C) ] ++ Ts0,
["contract"," ",io_lib:format("~s", [Name])," =\n", ["contract"," ",io_lib:format("~s", [Name])," =\n",
decode_tdefs([MkTDef(state, S), MkTDef(event, E) | Ts]), decode_tdefs(Ts), decode_funcs(Fs)].
decode_funcs(Fs)].
decode_funcs(Fs) -> [ decode_func(F) || F <- Fs ]. decode_funcs(Fs) -> [ decode_func(F) || F <- Fs ].

View File

@ -19,8 +19,6 @@ test_cases(1) ->
MapACI = #{contract => MapACI = #{contract =>
#{name => <<"C">>, #{name => <<"C">>,
type_defs => [], type_defs => [],
event => #{variant => [#{<<"NoEventsDefined">> => []}]},
state => #{tuple => []},
functions => functions =>
[#{name => <<"a">>, [#{name => <<"a">>,
arguments => arguments =>
@ -29,8 +27,6 @@ test_cases(1) ->
returns => <<"int">>, returns => <<"int">>,
stateful => false}]}}, stateful => false}]}},
DecACI = <<"contract C =\n" DecACI = <<"contract C =\n"
" type state = ()\n"
" datatype event = NoEventsDefined\n"
" function a : (int) => int\n">>, " function a : (int) => int\n">>,
{Contract,MapACI,DecACI}; {Contract,MapACI,DecACI};
@ -44,8 +40,6 @@ test_cases(2) ->
[#{name => <<"allan">>, [#{name => <<"allan">>,
typedef => <<"int">>, typedef => <<"int">>,
vars => []}], vars => []}],
event => #{variant => [#{<<"NoEventsDefined">> => []}]},
state => #{tuple => []},
functions => functions =>
[#{arguments => [#{arguments =>
[#{name => <<"i">>, [#{name => <<"i">>,
@ -54,8 +48,6 @@ test_cases(2) ->
returns => <<"int">>, returns => <<"int">>,
stateful => false}]}}, stateful => false}]}},
DecACI = <<"contract C =\n" DecACI = <<"contract C =\n"
" type state = ()\n"
" datatype event = NoEventsDefined\n"
" type allan = int\n" " type allan = int\n"
" function a : (C.allan) => int\n">>, " function a : (C.allan) => int\n">>,
{Contract,MapACI,DecACI}; {Contract,MapACI,DecACI};