From 1d973ac7500b45ceff40b63efd8497746125043c Mon Sep 17 00:00:00 2001 From: Hentioe Date: Tue, 27 Oct 2020 23:08:20 +0800 Subject: Fix module name --- lib/azure_ex/ast.ex | 68 ------------------------------------------------ lib/azure_ex/dsl.ex | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/azure_ex/endpoint.ex | 4 +-- 3 files changed, 70 insertions(+), 70 deletions(-) delete mode 100644 lib/azure_ex/ast.ex create mode 100644 lib/azure_ex/dsl.ex diff --git a/lib/azure_ex/ast.ex b/lib/azure_ex/ast.ex deleted file mode 100644 index 9ff7b0b..0000000 --- a/lib/azure_ex/ast.ex +++ /dev/null @@ -1,68 +0,0 @@ -defmodule AzureEx.AST do - @moduledoc false - - @path_var_re ~r/\{([^\}]+)\}/ - - defmacro defendpoint(name, raw_uri) do - # 扫描原始 URI 中的路径变量 - r = Regex.scan(@path_var_re, raw_uri) - # 处理并得到路径变量名称列表 - path_vars = - r - |> Enum.map(&List.last/1) - |> Enum.map(&Macro.underscore/1) - - # 路径变量列表对应的参数 AST - path_var_args_ast = - path_vars - |> Enum.map(fn var_name -> {String.to_atom(var_name), [], Elixir} end) - - # 函数签名 AST - fun_sign_ast = { - String.to_atom(Macro.underscore(name)), - [], - path_var_args_ast ++ [{:params, [], Elixir}] - } - - # 使用变量分割 URI - raw_segments = Regex.split(@path_var_re, raw_uri) - - unless length(raw_segments) == length(path_vars) + 1 do - raise """ - Error parsing raw URI, wrong number of path variables.\n - URI: #{raw_uri} - Vars: #{inspect(path_vars)} - """ - end - - # <<>> 宏的参数 AST - splicing_args_ast = - raw_segments - |> Enum.with_index() - |> Enum.reduce([], fn {seg, i}, acc -> - acc = acc ++ [seg] - - if var = Enum.at(path_vars, i) do - var_ast = - {:"::", [], - [ - {{:., [], [Kernel, :to_string]}, [], [{String.to_atom(var), [], Elixir}]}, - {:binary, [], Elixir} - ]} - - acc ++ [var_ast] - else - acc - end - end) - - # 函数体 AST(拼接字符串) - fun_body_ast = {:<<>>, [], splicing_args_ast} - - quote do - def unquote(fun_sign_ast) do - unquote(fun_body_ast) <> "?" <> URI.encode_query(unquote({:params, [], Elixir})) - end - end - end -end diff --git a/lib/azure_ex/dsl.ex b/lib/azure_ex/dsl.ex new file mode 100644 index 0000000..5463a9d --- /dev/null +++ b/lib/azure_ex/dsl.ex @@ -0,0 +1,68 @@ +defmodule AzureEx.DSL do + @moduledoc false + + @path_var_re ~r/\{([^\}]+)\}/ + + defmacro defendpoint(name, raw_uri) do + # 扫描原始 URI 中的路径变量 + r = Regex.scan(@path_var_re, raw_uri) + # 处理并得到路径变量名称列表 + path_vars = + r + |> Enum.map(&List.last/1) + |> Enum.map(&Macro.underscore/1) + + # 路径变量列表对应的参数 AST + path_var_args_ast = + path_vars + |> Enum.map(fn var_name -> {String.to_atom(var_name), [], Elixir} end) + + # 函数签名 AST + fun_sign_ast = { + String.to_atom(Macro.underscore(name)), + [], + path_var_args_ast ++ [{:params, [], Elixir}] + } + + # 使用变量分割 URI + raw_segments = Regex.split(@path_var_re, raw_uri) + + unless length(raw_segments) == length(path_vars) + 1 do + raise """ + Error parsing raw URI, wrong number of path variables.\n + URI: #{raw_uri} + Vars: #{inspect(path_vars)} + """ + end + + # <<>> 宏的参数 AST + splicing_args_ast = + raw_segments + |> Enum.with_index() + |> Enum.reduce([], fn {seg, i}, acc -> + acc = acc ++ [seg] + + if var = Enum.at(path_vars, i) do + var_ast = + {:"::", [], + [ + {{:., [], [Kernel, :to_string]}, [], [{String.to_atom(var), [], Elixir}]}, + {:binary, [], Elixir} + ]} + + acc ++ [var_ast] + else + acc + end + end) + + # 函数体 AST(拼接字符串) + fun_body_ast = {:<<>>, [], splicing_args_ast} + + quote do + def unquote(fun_sign_ast) do + unquote(fun_body_ast) <> "?" <> URI.encode_query(unquote({:params, [], Elixir})) + end + end + end +end diff --git a/lib/azure_ex/endpoint.ex b/lib/azure_ex/endpoint.ex index c880645..0d12cfd 100644 --- a/lib/azure_ex/endpoint.ex +++ b/lib/azure_ex/endpoint.ex @@ -1,8 +1,8 @@ defmodule AzureEx.Endpoint do @moduledoc false - require AzureEx.AST - import AzureEx.AST + require AzureEx.DSL + import AzureEx.DSL defendpoint( "ListAllVirtualMachines", -- cgit v1.2.3