Просмотр исходного кода

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

Thales Macedo Garitezi 2 лет назад
Родитель
Сommit
659cf64ad7
1 измененных файлов с 31 добавлено и 10 удалено
  1. 31 10
      apps/emqx_bridge_pulsar/src/emqx_bridge_pulsar.erl

+ 31 - 10
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.