-
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
10 changed files
with
177 additions
and
61 deletions.
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 @@ | ||
# Exporters | ||
|
||
As opposed of [importers.md](importers.md), exporter functions may be used to pack your data according to your needs. | ||
|
||
Exporter function is called right before packing your datastructure back into the `map()`. | ||
|
||
You may declare a function `on_export_FIELD_NAME` with the following spec: | ||
|
||
```erlang | ||
-spec on_export_FIELD_NAME(InValue :: term()) -> | ||
OutValue :: term() | no_return(). | ||
``` | ||
|
||
You may find a simple example of such a function in [priv_callbacks_on_export.erl](/test/priv/priv_callbacks_on_export.erl): | ||
|
||
```erlang | ||
on_export_a(Value) -> | ||
Value * 2. | ||
``` | ||
|
||
As you may notice, this function doubles the `a` field value before packing it into the map. | ||
|
||
You may refer to the modified source code to understand what happens when you declare the exporter: | ||
|
||
```erlang | ||
export(#priv_callbacks_on_export{} = Var_record_0) -> | ||
#{a => i_on_export(a, Var_record_0#priv_callbacks_on_export.a)}; | ||
export(_) -> | ||
error(badarg). | ||
|
||
## ... | ||
|
||
i_on_export(a, Var_value_0) -> | ||
on_export_a(Var_value_0); | ||
i_on_export(_, Var_value_0) -> | ||
Var_value_0. | ||
``` |
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
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
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
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,71 @@ | ||
-module(cloak_generate_i_on_export). | ||
-behaviour(cloak_generate). | ||
-export([generate/1]). | ||
-include("cloak.hrl"). | ||
|
||
|
||
%% Generate callback | ||
|
||
|
||
generate(_Forms) -> | ||
[ | ||
i_on_export__( | ||
?get_state()#state.required_record_fields | ||
++ ?get_state()#state.optional_record_fields | ||
) | ||
]. | ||
|
||
|
||
%% Validators (?MODULE:validate/2) | ||
|
||
|
||
i_on_export__(RecordFields) -> | ||
?es:function(?es:atom(?cloak_generated_function_i_on_export), i_on_export_clauses__(RecordFields)). | ||
|
||
|
||
i_on_export_clauses__(RecordsFields) -> | ||
i_on_export_clauses__(RecordsFields, []). | ||
|
||
|
||
i_on_export_clauses__([], Acc) -> | ||
lists:reverse([?es:clause( | ||
i_on_export_clause_patterns_mismatch__(), | ||
_Guards = none, | ||
i_on_export_clause_body_mismatch__() | ||
) | Acc]); | ||
|
||
i_on_export_clauses__([#record_field{name = Name} | RecordFields], Acc) -> | ||
MaybeUserDefinedImportCallback = ?user_definable_export_callback_name(Name), | ||
case lists:member( | ||
MaybeUserDefinedImportCallback, | ||
?get_state()#state.user_defined_export_callbacks | ||
) of | ||
true -> | ||
i_on_export_clauses__(RecordFields, [?es:clause( | ||
i_on_export_clause_patterns_match__(Name), | ||
_Guards = none, | ||
i_on_export_clause_body_match__(Name) | ||
) | Acc]); | ||
false -> | ||
i_on_export_clauses__(RecordFields, Acc) | ||
end. | ||
|
||
|
||
i_on_export_clause_patterns_match__(Name) -> | ||
[ | ||
?es:atom(Name), | ||
cloak_generate:var__(value, 0) | ||
]. | ||
|
||
|
||
i_on_export_clause_body_match__(Name) -> | ||
MaybeUserDefinedImportCallback = ?user_definable_export_callback_name(Name), | ||
[?es:application(?es:atom(MaybeUserDefinedImportCallback), [cloak_generate:var__(value, 0)])]. | ||
|
||
|
||
i_on_export_clause_patterns_mismatch__() -> | ||
[?es:underscore(), cloak_generate:var__(value, 0)]. | ||
|
||
|
||
i_on_export_clause_body_mismatch__() -> | ||
[cloak_generate:var__(value, 0)]. |
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
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
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
Oops, something went wrong.