Skip to content

Commit

Permalink
feat: build work sub modules
Browse files Browse the repository at this point in the history
  • Loading branch information
feng19 committed Mar 2, 2021
1 parent 2be40b7 commit b06daa8
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/wechat/builder/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ defmodule WeChat.Builder do
[base | get_funs]
end

defp gen_sub_modules(sub_modules, parent_module) do
def gen_sub_modules(sub_modules, parent_module, drop_amount \\ 1) do
client_module =
parent_module
|> Module.split()
Expand All @@ -150,15 +150,15 @@ defmodule WeChat.Builder do
sub_modules,
[],
fn module, acc ->
{file, ast} = gen_sub_module(module, parent_module, client_module)
{file, ast} = _gen_sub_module(module, parent_module, client_module, drop_amount)
{ast, [quote(do: @external_resource(unquote(file))) | acc]}
end
)

files ++ sub_module_ast_list
end

defp gen_sub_module(module, parent_module, client_module) do
defp _gen_sub_module(module, parent_module, client_module, drop_amount) do
file = module.__info__(:compile)[:source]

{:ok, ast} =
Expand All @@ -175,7 +175,7 @@ defmodule WeChat.Builder do
new_module_name_alias =
module
|> Module.split()
|> List.delete_at(0)
|> Enum.drop(drop_amount)
|> Enum.map(&String.to_atom/1)

ast =
Expand Down
31 changes: 30 additions & 1 deletion lib/wechat/builder/work_builder.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule WeChat.WorkBuilder do
@moduledoc false
alias WeChat.Work

@default_opts [
server_role: :client,
Expand All @@ -8,6 +9,14 @@ defmodule WeChat.WorkBuilder do
requester: WeChat.WorkRequester
]

@sub_modules [
Work.Message,
{
:contacts,
[Work.Contacts.Tag, Work.Contacts.User, Work.Contacts.Department]
}
]

defmacro __using__(options \\ []) do
opts = Macro.prewalk(options, &Macro.expand(&1, __CALLER__))
default_opts = Keyword.merge(@default_opts, opts)
Expand Down Expand Up @@ -104,6 +113,26 @@ defmodule WeChat.WorkBuilder do
end)
|> Enum.unzip()

[base | agent2cache_id_funs] ++ agent_secret_funs
sub_modules =
if Keyword.get(opts, :gen_sub_module?, true) do
client = __CALLER__.module

@sub_modules
|> Enum.reduce([], fn
{agent, modules}, acc ->
case Enum.find(agents, &match?(^agent, &1.id)) do
nil -> acc
_ -> modules ++ acc
end

module, acc ->
[module | acc]
end)
|> WeChat.Builder.gen_sub_modules(client, 2)
else
[]
end

[base | agent2cache_id_funs] ++ agent_secret_funs ++ sub_modules
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule WeChat.Work.Department do
defmodule WeChat.Work.Contacts.Department do
@moduledoc "通讯录管理-部门管理"

import WeChat.Utils, only: [work_doc_link_prefix: 0]
Expand Down
2 changes: 1 addition & 1 deletion lib/wechat/work/tag.ex → lib/wechat/work/contacts/tag.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule WeChat.Work.Tag do
defmodule WeChat.Work.Contacts.Tag do
@moduledoc "通讯录管理-标签管理"

import WeChat.Utils, only: [work_doc_link_prefix: 0]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule WeChat.Work.User do
defmodule WeChat.Work.Contacts.User do
@moduledoc "通讯录管理-成员管理"

import WeChat.Utils, only: [work_doc_link_prefix: 0]
Expand Down
2 changes: 1 addition & 1 deletion lib/wechat/work/work.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ defmodule WeChat.Work do
client,
quote do
@moduledoc false
use WeChat.WorkBuilder, unquote(options)
use WeChat.WorkBuilder, unquote(Macro.escape(options))
end,
Macro.Env.location(__ENV__)
) do
Expand Down
8 changes: 4 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule WeChat.MixProject do
extras: ["README.md", "LICENSE"],
groups_for_modules: groups_for_modules(),
groups_for_functions: [Action: &(&1[:doc_group] == :action)],
nest_modules_by_prefix: [ServerMessage, MiniProgram, Work]
nest_modules_by_prefix: [ServerMessage, MiniProgram, Work, Work.Contacts]
]
end

Expand Down Expand Up @@ -137,9 +137,9 @@ defmodule WeChat.MixProject do
Work,
Work.Agent,
Work.Message,
Work.Department,
Work.User,
Work.Tag
Work.Contacts.Department,
Work.Contacts.User,
Work.Contacts.Tag
]}
]
end
Expand Down
8 changes: 8 additions & 0 deletions test/support/wx_app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ defmodule WxWork do
%WeChat.Work.Agent{name: :agent_name, id: 10000, secret: "your_secret"}
]
end

defmodule WxWork2 do
use WeChat.Work,
corp_id: "corp_id",
agents: [
%WeChat.Work.Agent{name: :agent_name, id: 10000, secret: "your_secret"}
]
end
11 changes: 9 additions & 2 deletions test/wechat_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ defmodule WeChatTest do
assert WxWork.agent2cache_id(10000) == "corp_id_10000"
assert WxWork.agent2cache_id(:agent_name) == "corp_id_10000"

assert true = Enum.all?(1..3, &function_exported?(WxApp, :get, &1))
assert true = Enum.all?(2..4, &function_exported?(WxApp, :post, &1))
assert true = Enum.all?(1..3, &function_exported?(WxWork, :get, &1))
assert true = Enum.all?(2..4, &function_exported?(WxWork, :post, &1))
assert function_exported?(WxWork.Message, :send_message, 2)
assert function_exported?(WxWork.Contacts.Department, :list, 0)
end

test "Auto generate functions(Work) - exclude" do
assert function_exported?(WxWork2.Message, :send_message, 2)
assert false == function_exported?(WxWork2.Contacts.Department, :list, 0)
end

test "build official_account client" do
Expand Down

0 comments on commit b06daa8

Please sign in to comment.