-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathcuttlefish_nested_schema_test.erl
More file actions
60 lines (56 loc) · 1.97 KB
/
Copy pathcuttlefish_nested_schema_test.erl
File metadata and controls
60 lines (56 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-module(cuttlefish_nested_schema_test).
-include_lib("eunit/include/eunit.hrl").
nested_schema_test() ->
Conf = [
{["thing", "a"], foo},
{["nested", "thing1", "type"], "thing"},
{["nested", "thing1", "thing", "a"], 0},
{["nested", "thing2", "type"], "thing"}
],
Config = cuttlefish_generator:map(schema(), Conf),
?assertEqual(
[{thing, [{a, foo}]},
{nested_things, [{thing, [{a, 0}]}, {thing, [{a, 5}]}]}],
Config
),
ok.
schema() ->
Mappings = [
{mapping, "thing.a", "thing.a", [
{datatype, [{atom, foo}, integer]},
{default, 5}
]},
{mapping, "nested.$name.type", "nested_things", [
{datatype, {enum, [thing]}}
]},
{mapping, "nested.$name.thing.a", "nested_things", [
{datatype, [{atom, foo}, integer]},
{default, 5}
]}
],
Translations = [
{translation, "nested_things",
fun(Conf, Schema) ->
NestedNames = cuttlefish_variable:fuzzy_matches(["nested","$name","type"], Conf),
Things = [ begin
ConfigName = ["nested", Name],
Prefix = ConfigName ++ ["thing"],
SubConf = [ begin
{Key -- ConfigName, Value}
end || {Key, Value} <- cuttlefish_variable:filter_by_prefix(Prefix, Conf)],
Proplist = cuttlefish_generator:map(Schema, SubConf),
case cuttlefish_error:is_error(Proplist) of
true -> cuttlefish:invalid("gtfo");
_ -> {thing, proplists:get_value(thing, Proplist)}
end
end|| {"$name", Name} <- NestedNames],
case Things of
[] -> cuttlefish:unset();
_ -> Things
end
end}
],
{
[ cuttlefish_translation:parse(T) || T <- Translations],
[ cuttlefish_mapping:parse(M) || M <- Mappings],
[]}.