feat(pulsar): use an union member selector for better error messages

This commit is contained in:
Thales Macedo Garitezi 2023-05-17 17:56:53 -03:00
parent dcccc0910a
commit 659cf64ad7
1 changed files with 31 additions and 10 deletions

View File

@ -45,7 +45,9 @@ fields(config) ->
}
)},
{authentication,
mk(hoconsc:union([none, ref(auth_basic), ref(auth_token)]), #{
mk(
hoconsc:union(fun auth_union_member_selector/1),
#{
default => none,
%% must mark this whole union as sensitive because
%% hocon ignores the `sensitive' metadata in struct
@ -54,7 +56,8 @@ fields(config) ->
%% sensitivity information from sibling types.
sensitive => true,
desc => ?DESC("authentication")
})}
}
)}
] ++ emqx_connector_schema_lib:ssl_fields();
fields(producer_opts) ->
[
@ -233,3 +236,21 @@ override_default(OriginalFn, NewDefault) ->
(default) -> NewDefault;
(Field) -> OriginalFn(Field)
end.
auth_union_member_selector(all_union_members) ->
[none, ref(auth_basic), ref(auth_token)];
auth_union_member_selector({value, V}) ->
case V of
#{<<"password">> := _} ->
[ref(auth_basic)];
#{<<"jwt">> := _} ->
[ref(auth_token)];
<<"none">> ->
[none];
_ ->
Expected = "none | basic | token",
throw(#{
field_name => authentication,
expected => Expected
})
end.