schema-dump-reformat.escript 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env escript
  2. %% This script translates the hocon_schema_json's schema dump to a new format.
  3. %% It is used to convert older version EMQX's schema dumps to the new format
  4. %% after all files are upgraded to the new format, this script can be removed.
  5. -mode(compile).
  6. main([Input]) ->
  7. ok = add_libs(),
  8. _ = atoms(),
  9. {ok, Data} = file:read_file(Input),
  10. Json = jsx:decode(Data),
  11. NewJson = reformat(Json),
  12. io:format("~s~n", [jsx:encode(NewJson)]);
  13. main(_) ->
  14. io:format("Usage: schema-dump-reformat.escript <input.json>~n"),
  15. halt(1).
  16. reformat(Json) ->
  17. emqx_conf:reformat_schema_dump(fix(Json)).
  18. %% fix old type specs to make them compatible with new type specs
  19. fix(#{
  20. <<"kind">> := <<"union">>,
  21. <<"members">> := [#{<<"name">> := <<"string()">>}, #{<<"name">> := <<"function()">>}]
  22. }) ->
  23. %% s3_exporter.secret_access_key
  24. #{
  25. kind => primitive,
  26. name => <<"string()">>
  27. };
  28. fix(#{<<"kind">> := <<"primitive">>, <<"name">> := <<"emqx_conf_schema:log_level()">>}) ->
  29. #{
  30. kind => enum,
  31. symbols => [emergency, alert, critical, error, warning, notice, info, debug, none, all]
  32. };
  33. fix(#{<<"kind">> := <<"primitive">>, <<"name">> := <<"emqx_connector_http:pool_type()">>}) ->
  34. #{kind => enum, symbols => [random, hash]};
  35. fix(#{<<"kind">> := <<"primitive">>, <<"name">> := <<"emqx_bridge_http_connector:pool_type()">>}) ->
  36. #{kind => enum, symbols => [random, hash]};
  37. fix(Map) when is_map(Map) ->
  38. maps:from_list(fix(maps:to_list(Map)));
  39. fix(List) when is_list(List) ->
  40. lists:map(fun fix/1, List);
  41. fix({<<"kind">>, Kind}) ->
  42. {kind, binary_to_atom(Kind, utf8)};
  43. fix({<<"name">>, Type}) ->
  44. {name, fix_type(Type)};
  45. fix({K, V}) ->
  46. {binary_to_atom(K, utf8), fix(V)};
  47. fix(V) when is_number(V) ->
  48. V;
  49. fix(V) when is_atom(V) ->
  50. V;
  51. fix(V) when is_binary(V) ->
  52. V.
  53. %% ensure below ebin dirs are added to code path:
  54. %% _build/default/lib/*/ebin
  55. %% _build/emqx/lib/*/ebin
  56. %% _build/emqx-enterprise/lib/*/ebin
  57. add_libs() ->
  58. Profile = os:getenv("PROFILE"),
  59. case Profile of
  60. "emqx" ->
  61. ok;
  62. "emqx-enterprise" ->
  63. ok;
  64. _ ->
  65. io:format("PROFILE is not set~n"),
  66. halt(1)
  67. end,
  68. Dirs =
  69. filelib:wildcard("_build/default/lib/*/ebin") ++
  70. filelib:wildcard("_build/" ++ Profile ++ "/lib/*/ebin"),
  71. lists:foreach(fun add_lib/1, Dirs).
  72. add_lib(Dir) ->
  73. code:add_patha(Dir),
  74. Beams = filelib:wildcard(Dir ++ "/*.beam"),
  75. _ = spawn(fun() -> lists:foreach(fun load_beam/1, Beams) end),
  76. ok.
  77. load_beam(BeamFile) ->
  78. ModuleName = filename:basename(BeamFile, ".beam"),
  79. Module = list_to_atom(ModuleName),
  80. %% load the beams to make sure the atoms are existing
  81. code:ensure_loaded(Module),
  82. ok.
  83. fix_type(<<"[{string(), string()}]">>) ->
  84. <<"map()">>;
  85. fix_type(<<"[{binary(), binary()}]">>) ->
  86. <<"map()">>;
  87. fix_type(<<"emqx_limiter_schema:rate()">>) ->
  88. <<"string()">>;
  89. fix_type(<<"emqx_limiter_schema:burst_rate()">>) ->
  90. <<"string()">>;
  91. fix_type(<<"emqx_limiter_schema:capacity()">>) ->
  92. <<"string()">>;
  93. fix_type(<<"emqx_limiter_schema:initial()">>) ->
  94. <<"string()">>;
  95. fix_type(<<"emqx_limiter_schema:failure_strategy()">>) ->
  96. <<"string()">>;
  97. fix_type(<<"emqx_conf_schema:file()">>) ->
  98. <<"string()">>;
  99. fix_type(<<"#{term() => binary()}">>) ->
  100. <<"map()">>;
  101. fix_type(<<"[term()]">>) ->
  102. %% jwt claims
  103. <<"map()">>;
  104. fix_type(<<"emqx_ee_bridge_influxdb:write_syntax()">>) ->
  105. <<"string()">>;
  106. fix_type(<<"emqx_bridge_influxdb:write_syntax()">>) ->
  107. <<"string()">>;
  108. fix_type(<<"emqx_schema:mqtt_max_packet_size()">>) ->
  109. <<"non_neg_integer()">>;
  110. fix_type(<<"emqx_s3_schema:secret_access_key()">>) ->
  111. <<"string()">>;
  112. fix_type(Type) ->
  113. Type.
  114. %% ensure atoms are loaded
  115. %% these atoms are from older version of emqx
  116. atoms() ->
  117. [
  118. emqx_ee_connector_clickhouse,
  119. emqx_ee_bridge_gcp_pubsub,
  120. emqx_ee_bridge_influxdb,
  121. emqx_connector_http
  122. ].