-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdemo_schema.erl
140 lines (109 loc) · 3.58 KB
/
demo_schema.erl
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
-module(demo_schema).
-include_lib("typerefl/include/types.hrl").
-define(DESC(Module, Id), {desc, Module, Id}).
-define(DESC(Id), ?DESC(?MODULE, Id)).
-behaviour(hocon_schema).
-type duration() :: integer().
-type percent() :: float().
-typerefl_from_string({duration/0, emqx_schema, to_duration}).
-typerefl_from_string({percent/0, emqx_schema, to_percent}).
-reflect_type([duration/0, percent/0]).
-export([roots/0, fields/1, translations/0, translation/1, namespace/0]).
-define(FIELD(NAME, TYPE), hoconsc:mk(TYPE, #{mapping => NAME})).
namespace() -> "demo".
roots() -> [foo, {"a_b", hoconsc:mk(hoconsc:ref("a_b"), #{importance => hidden})}, "b", person, "vm"].
translations() -> ["app_foo"].
fields(foo) ->
#{fields => [ {setting, fun setting/1}
, {endpoint, fun endpoint/1}
, {greet, fun greet/1}
, {numbers, fun numbers/1}
, {ref_x_y, fun ref_x_y/1}
, {ref_j_k, fun ref_j_k/1}
, {min, integer()}
, {max, integer()}
],
desc => "foo doc"};
fields("a_b") ->
[ {"some_int", hoconsc:mk(integer(), #{mapping => "a_b.some_int",
importance => hidden
})}
, {"some_percent", fun some_percent/1}
];
fields("b") ->
[ {"u", fun (type) -> hoconsc:union(["priv.bool", "priv.int"]);
(mapping) -> "app_foo.u";
(_) -> undefined end}
, {"arr", fun (type) -> hoconsc:array("priv.int");
(mapping) -> "app_foo.arr";
(_) -> undefined end}
, {"ua", hoconsc:mk(hoconsc:array(hoconsc:union(["priv.int", "priv.bool"])),
#{mapping => "app_foo.ua"})}
];
fields("priv.bool") ->
[ {"val", fun priv_bool/1}
];
fields("priv.int") ->
[ {"val", integer()}
];
fields("x.y") ->
[ {"some_int", integer()}
, {"some_dur", fun priv_duration/1}
];
fields("j.k") ->
[ {"some_int", integer()}
];
fields(person) ->
[{id, ?FIELD("person.id", "id")}];
fields("id") ->
[{id, ?FIELD("num", integer())}];
fields("vm") ->
[ {"name", fun nodename/1}
, {"max_ports", fun max_ports/1}
].
translation("app_foo") ->
[ {"range", fun range/1} ].
setting(mapping) -> "app_foo.setting";
setting(sensitive) -> true;
setting(type) -> string();
setting(default) -> "default";
setting(_) -> undefined.
range(Conf) ->
Min = hocon_maps:get("foo.min", Conf),
Max = hocon_maps:get("foo.max", Conf),
case Min < Max of
true ->
{Min, Max};
_ ->
undefined
end.
endpoint(mapping) -> "app_foo.endpoint";
endpoint(type) -> typerefl:ip4_address();
endpoint(_) -> undefined.
greet(mapping) -> "app_foo.greet";
greet(type) -> typerefl:regexp_string("^hello$");
greet(desc) -> ?DESC("greetings");
greet(_) -> undefined.
numbers(mapping) -> "app_foo.numbers";
numbers(type) -> list(integer());
numbers(desc) -> "list of numbers";
numbers(_) -> undefined.
priv_duration(type) -> duration();
priv_duration(_) -> undefined.
some_percent(type) -> percent();
some_percent(mapping) -> "a_b.some_percent";
some_percent(_) -> undefined.
priv_bool(type) -> boolean();
priv_bool(_) -> undefined.
ref_x_y(mapping) -> undefined;
ref_x_y(type) -> "x.y";
ref_x_y(_) -> undefined.
ref_j_k(mapping) -> "app_foo.refjk";
ref_j_k(type) -> "j.k";
ref_j_k(_) -> undefined.
nodename(mapping) -> "vm_args.-name";
nodename(type) -> string();
nodename(_) -> undefined.
max_ports(mapping) -> "vm_args.-env ERL_MAX_PORTS";
max_ports(type) -> integer();
max_ports(_) -> undefined.