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 typeobject/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'sflat_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, ageo_pointobject), 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, withunknown_fields: :raise, raises aDowser.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 keyload/3walks (default: identity).:types— theDowser.Typemodule leaves are cast through (defaultDowser.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::keepit as it is (default), or:raiseaDowser.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
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"}
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}}