Fix OTP 28 dialyzer warnings, rewrite gmser_dyn decoder
Gajumaru Serialization Tests / tests (push) Successful in -3m56s
Gajumaru Serialization Tests / tests (push) Successful in -3m56s
gmser_dyn no longer tries to compress output by omitting type tags. Decoding streams using custom template codes can either use 'strict' decoding, in which case matching templates must be registered on the decoding end; in `strict => false` mode, the stream can still be decoded without valudation if the custom template is missing.
This commit is contained in:
@@ -59,12 +59,12 @@ T = #{items => [{a,int},{opt,b,int},{c,int}]}
|
||||
E1 = gmser_dyn:encode_typed(T, #{a => 1, b => 2, c => 3}) ->
|
||||
[<<0>>,<<1>>,[<<252>>,
|
||||
[[[<<255>>,<<97>>],[<<248>>,<<1>>]],
|
||||
[[<<255>>,<<98>>],[<<248>>,<<2>>]],
|
||||
[[<<255>>,<<99>>],[<<248>>,<<3>>]]]]]
|
||||
[[<<255>>,<<98>>],[<<248>>,<<2>>]],
|
||||
[[<<255>>,<<99>>],[<<248>>,<<3>>]]]]]
|
||||
E2 = gmser_dyn:encode_typed(T, #{a => 1, c => 3}) ->
|
||||
[<<0>>,<<1>>,[<<252>>,
|
||||
[[[<<255>>,<<97>>],[<<248>>,<<1>>]],
|
||||
[[<<255>>,<<99>>],[<<248>>,<<3>>]]]]]
|
||||
[[<<255>>,<<99>>],[<<248>>,<<3>>]]]]]
|
||||
gmser_dyn:decode_typed(T,E2) ->
|
||||
#{c => 3,a => 1}
|
||||
```
|
||||
@@ -81,12 +81,12 @@ converted to binaries, and `create` means that the atom is created dynamically.
|
||||
|
||||
The option can be passed e.g.:
|
||||
```erlang
|
||||
gmser_dyn:deserialize(Binary, set_opts(#{missing_labels => convert}))
|
||||
gmser_dyn:deserialize(Binary, gmser_dyn:set_opts(#{missing_labels => convert}))
|
||||
```
|
||||
|
||||
or
|
||||
```erlang
|
||||
gmser_dyn:deserialize(Binary, set_opts(#{missing_labels => convert}, Types))
|
||||
gmser_dyn:deserialize(Binary, gmser_dyn:set_opts(#{missing_labels => convert}, Types))
|
||||
```
|
||||
|
||||
By calling `gmser_dyn:register_types/1`, after having added options to the type map,
|
||||
@@ -131,37 +131,68 @@ Templates can be provided to the encoder by either naming an already registered
|
||||
type, or by passing a template directly. In both cases, the encoder will enforce
|
||||
the type information in the template.
|
||||
|
||||
If the template has been registered, the encoder omits inner type tags (still
|
||||
inserting the top-level tag), leading to some compression of the output.
|
||||
This also means that the serialized term cannot be decoded without the same
|
||||
schema information on the decoder side.
|
||||
|
||||
In some cases, the type tags will still be emitted. These are when alternative types
|
||||
appear, and for enumerated map types (`#{items => ...}`). In the latter case, it is
|
||||
due to the support for optional items.
|
||||
|
||||
In the case of a directly provided template, all type information is inserted,
|
||||
such that the serialized term can be decoded without any added type information.
|
||||
The template types are still enforced during encoding.
|
||||
If the template has been registered, the encoder uses the registered type specification
|
||||
to drive the encoding. The code of the registered template is embedded in the encoded
|
||||
output:
|
||||
|
||||
```erlang
|
||||
ET = fun(Type,Term) -> io:fwrite("~w~n", [gmser_dyn:encode_typed(Type,Term)]) end.
|
||||
gmser_dyn:encode_typed({int,int,int}, {1,2,3}) ->
|
||||
[<<0>>,<<1>>,[<<253>>,
|
||||
[[<<248>>,<<1>>],[<<248>>,<<2>>],[<<248>>,<<3>>]]]]
|
||||
|
||||
ET([{int,int}], [{1,2}]) -> [<<0>>,<<1>>,[<<251>>,[[[<<248>>,<<1>>],[<<248>>,<<2>>]]]]]
|
||||
|
||||
gmser_dyn:register_type(1000,lt2i,[{int,int}]).
|
||||
ET(lt2i, [{1,2}]) -> [<<0>>,<<1>>,[<<3,232>>,[[<<1>>,<<2>>]]]]
|
||||
Types = gmser_dyn_types:add_type(t3,1013,{int,int,int}).
|
||||
gmser_dyn:encode_typed(t3, {1,2,3}, Types) ->
|
||||
[<<0>>,<<1>>,[[<<3,245>>,<<253>>],
|
||||
[[<<248>>,<<1>>],[<<248>>,<<2>>],[<<248>>,<<3>>]]]]
|
||||
```
|
||||
|
||||
Note that the original `<<253>>` type code is wrapped as `[<<3,245>>,<<253>>]`,
|
||||
where `<<3,245>>` corresponds to the custom code `1013`.
|
||||
|
||||
Using the default option `#{strict => true}`, the decoder will extract the custom
|
||||
type spec, and validate the encoded data against it. If the custom code is missing,
|
||||
the decoder aborts. Using `#{strict => false}`, the custom code is used if it exists,
|
||||
but otherwise, it's ignored, and the encoded data is decoded using the dynamic type
|
||||
info.
|
||||
|
||||
### Alternative types
|
||||
|
||||
The dynamic encoder supports two additions to the `gmserialization` template
|
||||
language: `any`, `#{alt => [AltTypes]}` and `#{switch => [AltTypes]}`.
|
||||
The dynamic encoder supports a few additions to the `gmserialization` template
|
||||
language: `any`, `#{list => Type}`, `#{alt => [AltTypes]}` and `#{switch => [AltTypes]}`.
|
||||
|
||||
#### `any`
|
||||
|
||||
The `any` type doesn't have an associated code, but enforces dynamic encoding.
|
||||
|
||||
#### `list`
|
||||
|
||||
The original list type notation expects a key-value list, e.g.
|
||||
|
||||
`[{name, binary}, {age, int}]`
|
||||
|
||||
```erlang
|
||||
EL = gmser_dyn:encode_typed([{name,binary},{age,int}], [{name,<<"Ulf">>},{age,29}]) ->
|
||||
[<<0>>,<<1>>,[<<251>>,
|
||||
[[<<253>>,[[<<255>>,<<110,97,109,101>>],[<<249>>,<<85,108,102>>]]],
|
||||
[<<253>>,[[<<255>>,<<97,103,101>>],[<<248>>,<<29>>]]]]]]
|
||||
```
|
||||
Note that the encoding explicitly lays out a `[{Key, Value}]` structure, all
|
||||
dynamically typed. This means it can be dynamically decoded without templates.
|
||||
|
||||
```erlang
|
||||
gmser_dyn:decode(EL).
|
||||
[{name,<<"Ulf">>},{age,29}]
|
||||
```
|
||||
|
||||
In order to specify something like Erlang's `[integer()]` type, we can use
|
||||
the following:
|
||||
|
||||
```erlang
|
||||
gmser_dyn:encode_typed(#{list => int}, [1,2,3,4]) ->
|
||||
[<<0>>,<<1>>,[<<251>>,
|
||||
[[<<248>>,<<1>>],[<<248>>,<<2>>],[<<248>>,<<3>>],[<<248>>,<<4>>]]]]
|
||||
```
|
||||
|
||||
#### `alt`
|
||||
|
||||
The `#{alt => [Type]}` construct also enforces dynamic encoding, and will try
|
||||
|
||||
Reference in New Issue
Block a user