-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Importers | ||
|
||
Defining importer functions is a way to import some fancy packed data into your datastructure. | ||
|
||
Techically speaking, it is called only within `new/1` function 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). | ||
|
||
You may refer to the [`priv_callbacks_on_import.erl`](/test/priv/priv_callbacks_on_import.erl) modified source code to understand what happens when you're declaring the importer: | ||
|
||
```erlang | ||
i_on_import(a, Var_value_0) -> | ||
on_import_a(Var_value_0); | ||
i_on_import(_, Var_value_0) -> | ||
Var_value_0. | ||
``` | ||
|
||
Please, refer to [Exporters](exporters.md) documentation to find a way to pack your data back into binary. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters