Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit e244d89

Browse files
committed
refactor(editor): use macros to reduce similar code
1 parent 2ece8ea commit e244d89

File tree

2 files changed

+33
-51
lines changed

2 files changed

+33
-51
lines changed

lib/helper/validate_by_schema.ex

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
defmodule Helper.ValidateBySchema.Matchers do
2+
@moduledoc """
3+
matchers for basic type, support required option
4+
"""
5+
6+
defmacro __using__(types) do
7+
# can not use Enum.each here, see https://elixirforum.com/t/define-multiple-modules-in-macro-only-last-one-gets-created/1654/4
8+
for type <- types do
9+
guard_name = if type == :string, do: "binary", else: type |> to_string
10+
11+
quote do
12+
defp match(field, nil, [unquote(type), required: false]), do: done(field, nil)
13+
14+
defp match(field, value, [unquote(type), required: false])
15+
when unquote(:"is_#{guard_name}")(value) do
16+
done(field, value)
17+
end
18+
19+
defp match(field, value, [unquote(type)]) when unquote(:"is_#{guard_name}")(value),
20+
do: done(field, value)
21+
22+
defp match(field, value, [unquote(type), required: false]),
23+
do: error(field, value, unquote(type))
24+
25+
defp match(field, value, [unquote(type)]), do: error(field, value, unquote(type))
26+
end
27+
end
28+
end
29+
end
30+
131
defmodule Helper.ValidateBySchema do
232
@moduledoc """
333
validate json data by given schema, mostly used in editorjs validator
@@ -21,6 +51,8 @@ defmodule Helper.ValidateBySchema do
2151
data = %{checked: true, label: "done"}
2252
ValidateBySchema.cast(schema, data)
2353
"""
54+
use Helper.ValidateBySchema.Matchers, [:string, :number, :list, :boolean]
55+
2456
def cast(schema, data) do
2557
errors_info = cast_errors(schema, data)
2658

@@ -47,56 +79,6 @@ defmodule Helper.ValidateBySchema do
4779
end)
4880
end
4981

50-
# 这里试过用 macro 消除重复代码,但是不可行。
51-
# macro 对于重复定义的 quote 只会覆盖掉,除非每个 quote 中定义的内容不一样
52-
# 参考: https://elixirforum.com/t/define-multiple-modules-in-macro-only-last-one-gets-created/1654
53-
54-
# boolean field
55-
defp match(field, nil, [:boolean, required: false]), do: done(field, nil)
56-
57-
defp match(field, value, [:boolean, required: false]) when is_boolean(value) do
58-
done(field, value)
59-
end
60-
61-
defp match(field, value, [:boolean]) when is_boolean(value), do: done(field, value)
62-
defp match(field, value, [:boolean, required: false]), do: error(field, value, :boolean)
63-
defp match(field, value, [:boolean]), do: error(field, value, :boolean)
64-
65-
# string field
66-
defp match(field, nil, [:string, required: false]), do: done(field, nil)
67-
68-
defp match(field, value, [:string, required: false]) when is_binary(value) do
69-
done(field, value)
70-
end
71-
72-
defp match(field, value, [:string]) when is_binary(value), do: done(field, value)
73-
defp match(field, value, [:string, required: false]), do: error(field, value, :string)
74-
defp match(field, value, [:string]), do: error(field, value, :string)
75-
76-
defp match(field, nil, [:string, required: false]), do: done(field, nil)
77-
78-
# number
79-
defp match(field, nil, [:number, required: false]), do: done(field, nil)
80-
81-
defp match(field, value, [:number, required: false]) when is_number(value) do
82-
done(field, value)
83-
end
84-
85-
defp match(field, value, [:number]) when is_number(value), do: done(field, value)
86-
defp match(field, value, [:number, required: false]), do: error(field, value, :number)
87-
defp match(field, value, [:number]), do: error(field, value, :number)
88-
89-
# list
90-
defp match(field, nil, [:list, required: false]), do: done(field, nil)
91-
92-
defp match(field, value, [:list, required: false]) when is_list(value) do
93-
done(field, value)
94-
end
95-
96-
defp match(field, value, [:list]) when is_list(value), do: done(field, value)
97-
defp match(field, value, [:list, required: false]), do: error(field, value, :list)
98-
defp match(field, value, [:list]), do: error(field, value, :list)
99-
10082
# enum
10183
defp match(field, nil, enum: _, required: false), do: done(field, nil)
10284

test/helper/converter/editor_to_html_test/list_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do
6161
],
6262
"version" => "2.15.0"
6363
}
64-
@tag :wip
64+
@tag :wip2
6565
test "invalid list data parse should raise error message" do
6666
{:ok, editor_string} = Jason.encode(@editor_json)
6767
{:error, err_msg} = Parser.to_html(editor_string)

0 commit comments

Comments
 (0)