Break out state and event from typedefs and update docs

This commit is contained in:
Robert Virding 2019-05-08 16:06:58 +02:00
parent d2cd97def7
commit d16fb82e25
2 changed files with 66 additions and 31 deletions

View File

@ -31,11 +31,7 @@ generates the following JSON structure representing the contract interface:
{ {
"contract": { "contract": {
"name": "Answers", "name": "Answers",
"type_defs": [ "state": {
{
"name": "state",
"vars": [],
"typedef": {
"record": [ "record": [
{ {
"name": "a", "name": "a",
@ -47,8 +43,8 @@ generates the following JSON structure representing the contract interface:
} }
} }
] ]
}
}, },
"type_defs": [
{ {
"name": "answers", "name": "answers",
"vars": [], "vars": [],
@ -84,15 +80,11 @@ generates the following JSON structure representing the contract interface:
"arguments": [ "arguments": [
{ {
"name": "q", "name": "q",
"type": [ "type": "string"
"string"
]
}, },
{ {
"name": "a", "name": "a",
"type": [ "type": "int"
"int"
]
} }
], ],
"returns": { "returns": {
@ -132,6 +124,18 @@ ConstractString = contract_string()
JSONstring = json_string() JSONstring = json_string()
``` ```
This is equivalent to `aeso_aci:encode_contract(ConstractString, [])`.
#### encode_contract(ContractString, Options) -> {ok,JSONstring} | {error,ErrorString}
Types
``` erlang
ConstractString = contract_string()
Options = [option()]
JSONstring = json_string()
```
Generate the JSON encoding of the interface to a contract. The type definitions and non-private functions are included in the JSON string. Generate the JSON encoding of the interface to a contract. The type definitions and non-private functions are included in the JSON string.
#### decode_contract(JSONstring) -> ConstractString. #### decode_contract(JSONstring) -> ConstractString.
@ -185,15 +189,19 @@ JSONstring = json_string()
Generate the JSON encoding of an expression from the AST of the expression. Generate the JSON encoding of an expression from the AST of the expression.
### Notes
The deprecated functions `aseo_aci:encode/2` and `aeso_aci:decode/1` are still available but should not be used.
### Example run ### Example run
This is an example of using the ACI generator from an Erlang shell. The file called `aci_test.aes` contains the contract in the description from which we want to generate files `aci_test.json` which is the JSON encoding of the contract interface and `aci_test.include` which is the contract definition to be included inside another contract. This is an example of using the ACI generator from an Erlang shell. The file called `aci_test.aes` contains the contract in the description from which we want to generate files `aci_test.json` which is the JSON encoding of the contract interface and `aci_test.include` which is the contract definition to be included inside another contract.
``` erlang ``` erlang
1> {ok,Contract} = file:read_file("aci_test.aes"). 1> {ok,Contract} = file:read_file("aci_test.aes").
{ok,<<"contract Answers =\n record state = { a : answers }\n type answers() = map(string, int)\n\n stateful function"...>>} {ok,<<"contract Answers =\n\n record state = { a : answers }\n type answers() = map(string, int)\n\n stateful functio"...>>}
2> {ok,Encoding} = aeso_aci:encode_contract(Contract). 2> {ok,Encoding} = aeso_aci:encode_contract(Contract).
<<"{\"contract\":{\"name\":\"Answers\",\"type_defs\":[{\"name\":\"state\",\"vars\":[],\"typedef\":\"{a : map(string,int)}\"},{\"name\":\"ans"...>> {ok,<<"{\"contract\":{\"name\":\"Answers\",\"state\":{\"record\":[{\"name\":\"a\",\"type\":{\"map\":{\"key\":\"string\",\"value\":\"int\"}}}]"...>>}
3> file:write_file("aci_test.aci", Encoding). 3> file:write_file("aci_test.aci", Encoding).
ok ok
4> Decoded = aeso_aci:decode_contract(Encoding). 4> Decoded = aeso_aci:decode_contract(Encoding).
@ -201,7 +209,7 @@ ok
5> file:write_file("aci_test.include", Decoded). 5> file:write_file("aci_test.include", Decoded).
ok ok
6> jsx:prettify(Encoding). 6> jsx:prettify(Encoding).
<<"{\n \"contract\": {\n \"name\": \"Answers\",\n \"type_defs\": [\n {\n \"name\": \"state\",\n \"vars\": [],\n "...>> <<"{\n \"contract\": {\n \"name\": \"Answers\",\n \"state\": {\n \"record\": [\n {\n \"name\": \"a\",\n "...>>
``` ```
The final call to `jsx:prettify(Encoding)` returns the encoding in a The final call to `jsx:prettify(Encoding)` returns the encoding in a

View File

@ -9,6 +9,7 @@
-module(aeso_aci). -module(aeso_aci).
%% Old deprecated interface.
-export([encode/1,encode/2,decode/1]). -export([encode/1,encode/2,decode/1]).
-export([encode_contract/1,encode_contract/2,decode_contract/1]). -export([encode_contract/1,encode_contract/2,decode_contract/1]).
@ -93,13 +94,13 @@ encode_contract(ContractString, Options) ->
%% We find and look at the last contract. %% We find and look at the last contract.
Contract = lists:last(TypedAst), Contract = lists:last(TypedAst),
Cname = contract_name(Contract), Cname = contract_name(Contract),
Tdefs = [ do_encode_typedef(T) || Tdefs = do_encode_contract_typedefs(sort_decls(contract_types(Contract))),
T <- sort_decls(contract_types(Contract)) ],
Fdefs = [ do_encode_func(F) || F <- sort_decls(contract_funcs(Contract)), Fdefs = [ do_encode_func(F) || F <- sort_decls(contract_funcs(Contract)),
not is_private_func(F) ], not is_private_func(F) ],
Jmap = [{<<"contract">>, [{<<"name">>, do_encode_name(Cname)}, Jmap = [{<<"contract">>,
{<<"type_defs">>, Tdefs}, [{<<"name">>, do_encode_name(Cname)}] ++
{<<"functions">>, Fdefs}]}], Tdefs ++
[{<<"functions">>, Fdefs}]}],
%% io:format("~p\n", [Jmap]), %% io:format("~p\n", [Jmap]),
{ok,jsx:encode(Jmap)} {ok,jsx:encode(Jmap)}
catch catch
@ -118,6 +119,32 @@ join_errors(Prefix, Errors, Pfun) ->
Ess = [ Pfun(E) || E <- Errors ], Ess = [ Pfun(E) || E <- Errors ],
list_to_binary(string:join([Prefix|Ess], "\n")). list_to_binary(string:join([Prefix|Ess], "\n")).
%% do_encode_contract_typedefs(TypeDefs) -> [JSON].
%% Return a list of typedefs and state and event if they occur.
do_encode_contract_typedefs(Tdefs) ->
Fun = fun(T, {Ts,Ss,Es}) ->
%% Only one state and event.
case typedef_name(T) of
"state" -> {Ts,[do_encode_state_typedef(T)],Es};
"event" -> {Ts,Ss,[do_encode_event_typedef(T)]};
_Name -> {Ts ++ [do_encode_typedef(T)],Ss,Es}
end
end,
{Ts,Ss,Es} = lists:foldl(Fun, {[],[],[]}, Tdefs),
Ss ++ [{<<"type_defs">>, Ts}] ++ Es.
%% do_encode_state_typedef(StateTdef) -> JSON.
%% do_encode_event_typedef(EventTdef) -> JSON.
do_encode_state_typedef(State) ->
Def = typedef_def(State),
{<<"state">>,do_encode_alias(Def)}.
do_encode_event_typedef(State) ->
Def = typedef_def(State),
{<<"event">>,do_encode_alias(Def)}.
%% encode_func(TypedAST) -> JSON. %% encode_func(TypedAST) -> JSON.
%% Encode a function AST into a JSON structure. %% Encode a function AST into a JSON structure.