schema-dump-reformat.escript 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. %% NOTE: Assuming schema would contain no types needing localized typedocs.
  18. emqx_conf:reformat_schema_dump(fix(Json), _Lang = "en").
  19. %% fix old type specs to make them compatible with new type specs
  20. fix(#{
  21. <<"kind">> := <<"union">>,
  22. <<"members">> := [#{<<"name">> := <<"string()">>}, #{<<"name">> := <<"function()">>}]
  23. }) ->
  24. %% s3_exporter.secret_access_key
  25. #{
  26. kind => primitive,
  27. name => <<"string()">>
  28. };
  29. fix(#{<<"kind">> := <<"primitive">>, <<"name">> := <<"emqx_conf_schema:log_level()">>}) ->
  30. #{
  31. kind => enum,
  32. symbols => [emergency, alert, critical, error, warning, notice, info, debug, none, all]
  33. };
  34. fix(#{<<"kind">> := <<"primitive">>, <<"name">> := <<"emqx_connector_http:pool_type()">>}) ->
  35. #{kind => enum, symbols => [random, hash]};
  36. fix(#{<<"kind">> := <<"primitive">>, <<"name">> := <<"emqx_bridge_http_connector:pool_type()">>}) ->
  37. #{kind => enum, symbols => [random, hash]};
  38. fix(Map) when is_map(Map) ->
  39. maps:from_list(fix(maps:to_list(Map)));
  40. fix(List) when is_list(List) ->
  41. lists:map(fun fix/1, List);
  42. fix({<<"kind">>, Kind}) ->
  43. {kind, binary_to_atom(Kind, utf8)};
  44. fix({<<"name">>, Type}) ->
  45. {name, fix_type(Type)};
  46. fix({K, V}) ->
  47. {binary_to_atom(K, utf8), fix(V)};
  48. fix(V) when is_number(V) ->
  49. V;
  50. fix(V) when is_atom(V) ->
  51. V;
  52. fix(V) when is_binary(V) ->
  53. V.
  54. %% ensure below ebin dirs are added to code path:
  55. %% _build/default/lib/*/ebin
  56. %% _build/emqx/lib/*/ebin
  57. %% _build/emqx-enterprise/lib/*/ebin
  58. add_libs() ->
  59. Profile = os:getenv("PROFILE"),
  60. case Profile of
  61. "emqx" ->
  62. ok;
  63. "emqx-enterprise" ->
  64. ok;
  65. _ ->
  66. io:format("PROFILE is not set~n"),
  67. halt(1)
  68. end,
  69. Dirs =
  70. filelib:wildcard("_build/default/lib/*/ebin") ++
  71. filelib:wildcard("_build/" ++ Profile ++ "/lib/*/ebin"),
  72. lists:foreach(fun add_lib/1, Dirs).
  73. add_lib(Dir) ->
  74. code:add_patha(Dir),
  75. Beams = filelib:wildcard(Dir ++ "/*.beam"),
  76. _ = spawn(fun() -> lists:foreach(fun load_beam/1, Beams) end),
  77. ok.
  78. load_beam(BeamFile) ->
  79. ModuleName = filename:basename(BeamFile, ".beam"),
  80. Module = list_to_atom(ModuleName),
  81. %% load the beams to make sure the atoms are existing
  82. code:ensure_loaded(Module),
  83. ok.
  84. fix_type(<<"[{string(), string()}]">>) ->
  85. <<"map()">>;
  86. fix_type(<<"[{binary(), binary()}]">>) ->
  87. <<"map()">>;
  88. fix_type(<<"emqx_limiter_schema:rate()">>) ->
  89. <<"string()">>;
  90. fix_type(<<"emqx_limiter_schema:burst_rate()">>) ->
  91. <<"string()">>;
  92. fix_type(<<"emqx_limiter_schema:capacity()">>) ->
  93. <<"string()">>;
  94. fix_type(<<"emqx_limiter_schema:initial()">>) ->
  95. <<"string()">>;
  96. fix_type(<<"emqx_limiter_schema:failure_strategy()">>) ->
  97. <<"string()">>;
  98. fix_type(<<"emqx_conf_schema:file()">>) ->
  99. <<"string()">>;
  100. fix_type(<<"#{term() => binary()}">>) ->
  101. <<"map()">>;
  102. fix_type(<<"[term()]">>) ->
  103. %% jwt claims
  104. <<"map()">>;
  105. fix_type(<<"emqx_ee_bridge_influxdb:write_syntax()">>) ->
  106. <<"string()">>;
  107. fix_type(<<"emqx_bridge_influxdb:write_syntax()">>) ->
  108. <<"string()">>;
  109. fix_type(<<"emqx_schema:mqtt_max_packet_size()">>) ->
  110. <<"non_neg_integer()">>;
  111. fix_type(<<"emqx_s3_schema:secret_access_key()">>) ->
  112. <<"string()">>;
  113. fix_type(Type) ->
  114. Type.
  115. %% ensure atoms are loaded
  116. %% these atoms are from older version of emqx
  117. atoms() ->
  118. [
  119. emqx_ee_connector_clickhouse,
  120. emqx_ee_bridge_gcp_pubsub,
  121. emqx_ee_bridge_influxdb,
  122. emqx_connector_http
  123. ].