Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 43 additions & 38 deletions src/hocon_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,43 +115,48 @@
| field_schema_fun().

-type field_schema_fun() :: fun((_) -> _).
-type field_schema_map() ::
#{
type := type(),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given how loosely defined other types are, I think just changing to type => type() here could be a good practical solution.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For defining a schema, the type is really required, so I think it's good to make it explicit in the type.

Though, if the macro hack had not worked, I probably would have gone that way too. 😅

default => term(),
examples => term(),
mapping => undefined | string(),
converter => undefined | translationfunc(),
validator => undefined | validationfun(),
%% set false if a field is allowed to be `undefined`
%% NOTE: has no point setting it to `true` if field has a default value

% default = false
required => boolean() | {false, recursively},
%% for sensitive data obfuscation (password, token)
sensitive => boolean(),
desc => desc(),
%% hide it from doc generation
importance => importance(),
%% Set to {since, Version} to mark field as deprecated.
%% deprecated field can not be removed due to compatibility reasons.
%% The value will be dropped,
%% Deprecated fields are treated as required => {false, recursively}
deprecated => {since, binary() | string()} | false,
%% Other names to reference this field.
%% this can be useful when we need to rename some filed names
%% while keeping backward compatibility.
%% For one struct, no duplication is allowed in the collection of
%% all field names and aliases.
%% The no-duplication assertion is made when dumping the schema to JSON.
%% see `hocon_schema_json'.
%% When checking values against the schema, the look up is first
%% done with the current field name, if not found, try the aliases
%% in the defined order until one is found (i.e. first match wins).
aliases => [name()],
%% transparent metadata
extra => map()
}.

-define(FIELD_SCHEMA_MAP(TYPE_KV), #{
TYPE_KV,
default => term(),
examples => term(),
mapping => undefined | string(),
converter => undefined | translationfunc(),
validator => undefined | validationfun(),
%% set false if a field is allowed to be `undefined`
%% NOTE: has no point setting it to `true` if field has a default value

% default = false
required => boolean() | {false, recursively},
%% for sensitive data obfuscation (password, token)
sensitive => boolean(),
desc => desc(),
%% hide it from doc generation
importance => importance(),
%% Set to {since, Version} to mark field as deprecated.
%% deprecated field can not be removed due to compatibility reasons.
%% The value will be dropped,
%% Deprecated fields are treated as required => {false, recursively}
deprecated => {since, binary() | string()} | false,
%% Other names to reference this field.
%% this can be useful when we need to rename some filed names
%% while keeping backward compatibility.
%% For one struct, no duplication is allowed in the collection of
%% all field names and aliases.
%% The no-duplication assertion is made when dumping the schema to JSON.
%% see `hocon_schema_json'.
%% When checking values against the schema, the look up is first
%% done with the current field name, if not found, try the aliases
%% in the defined order until one is found (i.e. first match wins).
aliases => [name()],
%% transparent metadata
extra => map()
}).

-type field_schema_map() :: ?FIELD_SCHEMA_MAP(type := type()).

%% Same as `field_schema_map/0`, but `type` is optional when overriding...
-type field_schema_map_override() :: ?FIELD_SCHEMA_MAP(type => type()).

-type field() :: {name(), typefunc() | field_schema()}.
-type fields() ::
Expand Down Expand Up @@ -216,7 +221,7 @@ validations(Sc) ->
maps:get(validations, Sc, []).

%% @doc Make a higher order schema by overriding `Base' with `OnTop'
-spec override(field_schema(), field_schema_map()) -> field_schema_fun().
-spec override(field_schema(), field_schema_map_override()) -> field_schema_fun().
override(Base, OnTop) ->
fun(SchemaKey) ->
case maps:is_key(SchemaKey, OnTop) of
Expand Down