From 659cf64ad7f40e9f436b7dd234a5c55b124ea098 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Wed, 17 May 2023 17:56:53 -0300 Subject: [PATCH] feat(pulsar): use an union member selector for better error messages --- .../src/emqx_bridge_pulsar.erl | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/apps/emqx_bridge_pulsar/src/emqx_bridge_pulsar.erl b/apps/emqx_bridge_pulsar/src/emqx_bridge_pulsar.erl index 56c20130c..7d1b20d24 100644 --- a/apps/emqx_bridge_pulsar/src/emqx_bridge_pulsar.erl +++ b/apps/emqx_bridge_pulsar/src/emqx_bridge_pulsar.erl @@ -45,16 +45,19 @@ fields(config) -> } )}, {authentication, - mk(hoconsc:union([none, ref(auth_basic), ref(auth_token)]), #{ - default => none, - %% must mark this whole union as sensitive because - %% hocon ignores the `sensitive' metadata in struct - %% fields... Also, when trying to type check a struct - %% that doesn't match the intended type, it won't have - %% sensitivity information from sibling types. - sensitive => true, - desc => ?DESC("authentication") - })} + 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 + %% fields... Also, when trying to type check a struct + %% that doesn't match the intended type, it won't have + %% 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.