View Source parthenon (parthenon v0.7.1)

Parse Athena structures and schemas.

Link to this section Summary

Functions

Parse the provided schema and register it in the list of available schemas as the specified name.
Parse the raw Athena structure with the specified schema.

Parse the raw Athena structure with the specified schema and apply the specified options.

Link to this section Functions

Link to this function

add_schema(SchemaName, RawSchema)

View Source
-spec add_schema(SchemaName :: atom(), RawSchema :: binary() | string()) -> ok | {error, term()}.
Parse the provided schema and register it in the list of available schemas as the specified name.
Link to this function

decode(SchemaName, Binary)

View Source
-spec decode(SchemaName :: atom(), Binary :: binary()) ->
          {ok, parthenon_decode:value()} | {error, term()}.
Parse the raw Athena structure with the specified schema.
Link to this function

decode(SchemaName, Binary, Options)

View Source
-spec decode(SchemaName :: atom(), Binary :: binary(), Options :: [parthenon_decode:option()]) ->
          {ok, parthenon_decode:value()} | {error, term()}.

Parse the raw Athena structure with the specified schema and apply the specified options.

The supported options are:

  • object_format: Specify the format of the objects.
    • maps (default): return objects as maps
    • proplists: return objects as proplists
    • tuple will return objects in the {[{<key>, <value>}]} format
  • key_format: Specify the format of the keys.
    • atom: return the keys as atoms
    • binary will return the keys as binary.
    • existing_atom (default): return the keys as atoms if they already exists or return them into binary if they do not

Example usage:

  %% Register the `point' schema into the registry
  ok = parthenon:add_schema(point, <<"struct<x: int, y: int, z: int>">>).
 
  %% Register the `coordinates' schema into the registry
  ok = parthenon:add_schema(coordinates, <<"array<struct<x: int, y: int, z: int>>">>).
 
  %% Decode the `point' structure into a map with binary keys
  {ok, #{<<"x">> := 3, <<"y">> := 2, <<"z">> := 4}} = parthenon:decode(
      point, <<"{x=3, y=2, z=4}">>, [{key_format, binary}, {object_format, maps}]
  ).
 
  %% Decode the `coordinates' array into a list of maps with binary keys
  {ok, [#{<<"x">> := 3, <<"y">> := 2, <<"z">> := 4}, #{<<"x">> := 5, <<"y">> := 6, <<"z">> := 7}]} = parthenon:decode(
       coordinates, <<"[{x=3, y=2, z=4}, {x=5, y=6, z=7}]">>, [
           {key_format, binary}, {object_format, maps}
       ]
   ).