Skip to content

Commit 7343f1c

Browse files
committedDec 12, 2023
feat: percent support 12.345%
1 parent ebc78bb commit 7343f1c

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.eunit
2+
.tool-versions
23
*.o
34
*.beam
45
*.plt

‎sample-schemas/demo_schema.erl

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
-behaviour(hocon_schema).
88

99
-type duration() :: integer().
10+
-type percent() :: float().
1011

1112
-typerefl_from_string({duration/0, emqx_schema, to_duration}).
13+
-typerefl_from_string({percent/0, emqx_schema, to_percent}).
1214

13-
-reflect_type([duration/0]).
15+
-reflect_type([duration/0, percent/0]).
1416

1517
-export([roots/0, fields/1, translations/0, translation/1]).
1618

@@ -35,6 +37,7 @@ fields("a_b") ->
3537
[ {"some_int", hoconsc:mk(integer(), #{mapping => "a_b.some_int",
3638
importance => hidden
3739
})}
40+
, {"some_percent", fun some_percent/1}
3841
];
3942

4043
fields("b") ->
@@ -112,6 +115,10 @@ numbers(_) -> undefined.
112115
priv_duration(type) -> duration();
113116
priv_duration(_) -> undefined.
114117

118+
some_percent(type) -> percent();
119+
some_percent(mapping) -> "a_b.some_percent";
120+
some_percent(_) -> undefined.
121+
115122
priv_bool(type) -> boolean();
116123
priv_bool(_) -> undefined.
117124

‎src/hocon_postprocess.erl

+5-2
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ percent(Other) ->
131131
Other.
132132

133133
do_percent(Str) ->
134-
{ok, MP} = re:compile("([0-9]+)(%)$"),
134+
{ok, MP} = re:compile("([0-9.]+)(%)$"),
135135
case re_run_first(Str, MP) of
136136
{match, [Val, _Unit]} ->
137-
list_to_integer(Val) / 100;
137+
case string:to_integer(Val) of
138+
{Int, []} -> Int / 100;
139+
_ -> round(list_to_float(Val) * 100) / 10000
140+
end;
138141
_ ->
139142
Str
140143
end.

‎test/hocon_tconf_tests.erl

+12
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,18 @@ mapping_test_() ->
332332
F("foo.numbers=[1,2,3]")
333333
),
334334
?_assertEqual([{["a_b", "some_int"], 1}, Setting], F("a_b.some_int=1")),
335+
?_assertEqual([{["a_b", "some_percent"], 0.12}, Setting], F("a_b.some_percent=12%")),
336+
?_assertEqual([{["a_b", "some_percent"], 0.123}, Setting], F("a_b.some_percent=\"12.3%\"")),
337+
?_assertEqual(
338+
[{["a_b", "some_percent"], 0.1234}, Setting], F("a_b.some_percent=\"12.34%\"")
339+
),
340+
%% we only support 2 decimal places
341+
?_assertEqual(
342+
[{["a_b", "some_percent"], 0.4321}, Setting], F("a_b.some_percent=\"43.214%\"")
343+
),
344+
?_assertEqual(
345+
[{["a_b", "some_percent"], 0.1235}, Setting], F("a_b.some_percent=\"12.345%\"")
346+
),
335347
?_assertEqual([Setting], F("foo.ref_x_y={some_int = 1}")),
336348
?GEN_VALIDATION_ERR(_, F("foo.ref_x_y={some_int = aaa}")),
337349
?_assertEqual(

0 commit comments

Comments
 (0)
Please sign in to comment.