uw-switch-semantics #53

Merged
uwiger merged 4 commits from uw-switch-semantics into master 2025-04-29 03:57:17 +09:00
Owner

Test case illustrates:

t_switch() ->
    T = #{switch => #{a => int, b => binary}},
    t_round_trip_typed(T, #{a => 17}),
    t_round_trip_typed(T, #{b => <<"foo">>}),
    ?assertError({illegal,int,<<"foo">>}, encode_typed(T, #{a => <<"foo">>})),
    MMap = #{a => 17, b => <<"foo">>},
    ?assertError({illegal, singleton_map, MMap}, encode_typed(T, MMap)).

That is, the construct asserts a 'singleton map' (only one map key), essentially working like a tagged tuple. This can be used to provide a dictionary of valid message types, each 'tagged' with a 'singleton map'.

(One could imagine letting the pattern work on records as well, but nyi).

Test case illustrates: ```erlang t_switch() -> T = #{switch => #{a => int, b => binary}}, t_round_trip_typed(T, #{a => 17}), t_round_trip_typed(T, #{b => <<"foo">>}), ?assertError({illegal,int,<<"foo">>}, encode_typed(T, #{a => <<"foo">>})), MMap = #{a => 17, b => <<"foo">>}, ?assertError({illegal, singleton_map, MMap}, encode_typed(T, MMap)). ``` That is, the construct asserts a 'singleton map' (only one map key), essentially working like a tagged tuple. This can be used to provide a dictionary of valid message types, each 'tagged' with a 'singleton map'. (One could imagine letting the pattern work on records as well, but nyi).
uwiger added 2 commits 2025-04-28 18:58:26 +09:00
Add switch semantics
Some checks failed
Gajumaru Serialization Tests / tests (push) Failing after 49m54s
b358dfe914
test case for 'switch'
Some checks failed
Gajumaru Serialization Tests / tests (push) Failing after 49m55s
5df23c05c1
uwiger added 1 commit 2025-04-28 19:02:25 +09:00
Add gmser_dyn_types.erl
All checks were successful
Gajumaru Serialization Tests / tests (push) Successful in 49m54s
b9a51acf55
Author
Owner

The PR also adds version-controlled type management, and the gmser_dyn_types module provides some slightly improved ways to define a type map.

The PR also adds version-controlled type management, and the `gmser_dyn_types` module provides some slightly improved ways to define a type map.
uwiger added 1 commit 2025-04-28 19:15:44 +09:00
Add forgotten exports, expand(Types) function
All checks were successful
Gajumaru Serialization Tests / tests (push) Successful in 49m55s
f996253e6b
Author
Owner

Example of Types expansion (e.g. for sending to a client):

Eshell V15.2.1 (press Ctrl+G to abort, type help(). for help)
1> Ts = gmser_dyn_types:from_list([{vsn,1},{foo,1000,fun() -> int end},{bar,1001,fun(1) -> binary end}],gmser_dyn:registered_types()).
#{rev =>
      #{binary => 249,id => 254,label => 255,list => 251,map => 252,
        bool => 250,int => 248,tuple => 253,anyint => 246,
        negint => 247,foo => 1000,bar => 1001},
  options => #{},vsn => 1,labels => #{},
  codes =>
      #{246 => anyint,247 => negint,248 => int,249 => binary,
        250 => bool,251 => list,252 => map,253 => tuple,254 => id,
        255 => label,1000 => foo,1001 => bar},
  templates =>
      #{binary => binary,id => id,label => label,list => list,
        map => map,bool => bool,int => int,tuple => tuple,
        anyint => #{alt => [negint,int]},
        negint => negint,foo => #Fun<erl_eval.43.18682967>,
        bar => #Fun<erl_eval.42.18682967>},
  rev_labels => #{}}
2> gmser_dyn_types:expand(Ts).
#{rev =>
      #{binary => 249,id => 254,label => 255,list => 251,map => 252,
        bool => 250,int => 248,tuple => 253,anyint => 246,
        negint => 247,foo => 1000,bar => 1001},
  options => #{},vsn => 1,labels => #{},
  codes =>
      #{246 => anyint,247 => negint,248 => int,249 => binary,
        250 => bool,251 => list,252 => map,253 => tuple,254 => id,
        255 => label,1000 => foo,1001 => bar},
  templates =>
      #{binary => binary,id => id,label => label,list => list,
        map => map,bool => bool,int => int,tuple => tuple,
        anyint => #{alt => [negint,int]},
        negint => negint,foo => int,bar => binary},
  rev_labels => #{}}

Note how the template funs for types foo and bar have been expanded, using the embedded vsn attribute.

Example of Types expansion (e.g. for sending to a client): ``` Eshell V15.2.1 (press Ctrl+G to abort, type help(). for help) 1> Ts = gmser_dyn_types:from_list([{vsn,1},{foo,1000,fun() -> int end},{bar,1001,fun(1) -> binary end}],gmser_dyn:registered_types()). #{rev => #{binary => 249,id => 254,label => 255,list => 251,map => 252, bool => 250,int => 248,tuple => 253,anyint => 246, negint => 247,foo => 1000,bar => 1001}, options => #{},vsn => 1,labels => #{}, codes => #{246 => anyint,247 => negint,248 => int,249 => binary, 250 => bool,251 => list,252 => map,253 => tuple,254 => id, 255 => label,1000 => foo,1001 => bar}, templates => #{binary => binary,id => id,label => label,list => list, map => map,bool => bool,int => int,tuple => tuple, anyint => #{alt => [negint,int]}, negint => negint,foo => #Fun<erl_eval.43.18682967>, bar => #Fun<erl_eval.42.18682967>}, rev_labels => #{}} 2> gmser_dyn_types:expand(Ts). #{rev => #{binary => 249,id => 254,label => 255,list => 251,map => 252, bool => 250,int => 248,tuple => 253,anyint => 246, negint => 247,foo => 1000,bar => 1001}, options => #{},vsn => 1,labels => #{}, codes => #{246 => anyint,247 => negint,248 => int,249 => binary, 250 => bool,251 => list,252 => map,253 => tuple,254 => id, 255 => label,1000 => foo,1001 => bar}, templates => #{binary => binary,id => id,label => label,list => list, map => map,bool => bool,int => int,tuple => tuple, anyint => #{alt => [negint,int]}, negint => negint,foo => int,bar => binary}, rev_labels => #{}} ``` Note how the template funs for types `foo` and `bar` have been expanded, using the embedded `vsn` attribute.
uwiger merged commit 4ac7531351 into master 2025-04-29 03:57:17 +09:00
uwiger deleted branch uw-switch-semantics 2025-04-29 03:57:30 +09:00
Sign in to join this conversation.
No description provided.