Dowser.Source (Dowser.Client v0.5.0)

View Source

Walks one document source against its mapping: the structure half of casting, as Dowser.Type is the value half.

What a value is depends on its mapping entry, never on its shape:

  • an entry with "properties", or of type object/nested, is an object — a map is walked key by key against those properties, a list element by element, and on the way out a struct is turned into a map first;
  • an entry of an opaque type (opts[:opaque], the backend's flat_object/flattened), or one mapped "enabled": false, holds whatever the document put in it: it is returned exactly as it is;
  • any other entry with a "type" is a leaf — the whole value goes to the type module (opts[:types]), even when it is a map (a range, a geo_point object), and an array of them element by element;
  • no entry at all (a field the mapping doesn't know) is left as it is, only its keys going through opts[:key_fn] on the way in — or, with unknown_fields: :raise, raises a Dowser.UnknownFieldError.

A leaf whose type left a map uncast (no type for it, or a shape it didn't recognize) has its keys go through opts[:key_fn] like the rest of the document. An opaque subtree's keys never do: they are document content, not mapping names, and under keys: :atoms would turn any writer into a way to exhaust the atom table.

Options

  • :key_fn — applied to every key load/3 walks (default: identity).
  • :types — the Dowser.Type module leaves are cast through (default Dowser.Type).
  • :opaque — the mapping types whose subtree is left alone (default []).
  • :unknown_fields — what to do with a field an object's "properties" don't declare: :keep it as it is (default), or :raise a Dowser.UnknownFieldError. With no mapping at all, nothing is unknown.

Summary

Functions

Casts source back into its JSON representation against mapping. Keys are kept as given.

Casts source from its JSON representation against mapping.

Functions

dump(source, mapping, opts \\ [])

@spec dump(term(), map() | nil, keyword()) :: term()

Casts source back into its JSON representation against mapping. Keys are kept as given.

iex> mapping = %{"properties" => %{"ip" => %{"type" => "ip"}}}
iex> Dowser.Source.dump(%{ip: {127, 0, 0, 1}}, mapping)
%{ip: "127.0.0.1"}

load(source, mapping, opts \\ [])

@spec load(term(), map() | nil, keyword()) :: term()

Casts source from its JSON representation against mapping.

iex> mapping = %{"properties" => %{"ip" => %{"type" => "ip"}}}
iex> Dowser.Source.load(%{"ip" => "127.0.0.1"}, mapping, key_fn: &String.to_atom/1)
%{ip: {127, 0, 0, 1}}