Skip to content

Commit

Permalink
[#1] importers documented
Browse files Browse the repository at this point in the history
  • Loading branch information
shizzard committed Mar 5, 2019
1 parent 6234781 commit c598a82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 28 additions & 0 deletions docs/importers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Importers

Defining importer functions is a way to import some fancy packed data into your datastructure.

Techically speaking, it is called only within `i_new_required/3` and `i_new_optional/3` functions right before the [substructures](substructures.md) initialization if any.

You may declare a function called `on_import_FIELD_NAME` with the following spec:

```erlang
-spec on_import_FIELD_NAME(InValue :: term()) ->
OutValue :: term() | no_return().
```

For example, if the original map contains a structured binary value under the `binary` field, the following function will automatically transform it to map:

```erlang
on_import_binary(<<Length:16, Value:Length/binary>>) ->
#{
length => Length,
value => Value
};
on_import_binary(_) ->
error(badarg).
```

If you will then define a [substructure](substructures.md) for this field, a substructure constructor will be automatically called, and only then the value will be [validated](validators.md).

Please, refer to [Exporters](exporters.md) doumentation to find a way to pack your data back into binary.
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* [Compile-time options](compile-time-options.md): a set of options that may be used to change `cloak` behavior at compile-time
* [Field types](field-types.md): description of different field types `cloak` supports
* [Runtime errors](runtime-errors.md): runtime errors `cloak` can generate
* [Internal functions](internal-functions.md): description of auto-generated internal functions that you may use in your source code
* [Importers](importers.md): a way to import some fancy-packed data into your datastructure
* [Validators](validators.md): different ways to validate data with user-defined callbacks
* [Exporters](exporters.md): different ways to export data with user-defined callbacks
* [Substructures](substructures.md): explanation of nested data structures `cloak` supports
* [Internal functions](internal-functions.md): description of auto-generated internal functions that you may use in your source code
* [Tips and tricks](tips and tricks.md)
* [Known issues](known-issues.md)
* [Contributing](contributing.md)

0 comments on commit c598a82

Please sign in to comment.