diff --git a/lib/helper/derive/parser.ex b/lib/helper/derive/parser.ex index 533fc1e..d62a007 100644 --- a/lib/helper/derive/parser.ex +++ b/lib/helper/derive/parser.ex @@ -36,6 +36,15 @@ defmodule MishkaDeveloperTools.Helper.Derive.Parser do {:sub_field, line, add_parent_tags(items, parent)} {:conditional_field, line, items} -> + raise(""" + \n ----------------------------------------------------------\n + | Unfortunately, this macro does not support the nested mode in the conditional_field macro. + | If you can add this feature I would be very happy to send a PR. + | More information: https://github.com/mishka-group/mishka_developer_tools/issues/25 + | Parent Issue: https://github.com/mishka-group/mishka_developer_tools/issues/23 + \n ----------------------------------------------------------\n + """) + {:conditional_field, line, elements_unification(add_parent_tags(items, parent, "conds"), parent)} end @@ -50,6 +59,15 @@ defmodule MishkaDeveloperTools.Helper.Derive.Parser do {:sub_field, line, add_parent_tags(items, parent)} {:conditional_field, line, items} -> + raise(""" + \n ----------------------------------------------------------\n + | Unfortunately, this macro does not support the nested mode in the conditional_field macro. + | If you can add this feature I would be very happy to send a PR. + | More information: https://github.com/mishka-group/mishka_developer_tools/issues/25 + | Parent Issue: https://github.com/mishka-group/mishka_developer_tools/issues/23 + \n ----------------------------------------------------------\n + """) + comverted_items = add_parent_tags(items, parent, "conds") recursive_children = @@ -66,7 +84,7 @@ defmodule MishkaDeveloperTools.Helper.Derive.Parser do end) end - defp find_node_tags([_name, _type, opts | _reset] = _items) do + def find_node_tags([_name, _type, opts | _reset] = _items) do %{parent: opts[:__node_parent_tree__], type: opts[:__node_type__], id: opts[:__node_id__]} end diff --git a/lib/macros/guarded_struct.ex b/lib/macros/guarded_struct.ex index 27eb243..a49dec0 100644 --- a/lib/macros/guarded_struct.ex +++ b/lib/macros/guarded_struct.ex @@ -1400,6 +1400,7 @@ defmodule GuardedStruct do defmacro conditional_field(name, type, opts \\ [], do: block) do # type = Macro.escape(quote do: struct()) type = Macro.escape(type) + Parser.parser(block, :conditional) quote do GuardedStruct.__field__(unquote(name), unquote(type), unquote(opts), __ENV__, true, true) diff --git a/test/guarded_struct_test/nested_conditional_field.exs b/test/guarded_struct_test/nested_conditional_field.exs deleted file mode 100644 index 7f5dde5..0000000 --- a/test/guarded_struct_test/nested_conditional_field.exs +++ /dev/null @@ -1,50 +0,0 @@ -defmodule MishkaDeveloperToolsTest.GuardedStruct.NestedConditionalFieldTest do - use ExUnit.Case, async: true - - ######### (▰˘◡˘▰) NestedConditionalFieldTest GuardedStructTest Data (▰˘◡˘▰) ########## - defmodule Actor do - use GuardedStruct - @types ["Application", "Group", "Organization", "Person", "Service"] - - guardedstruct do - field(:id, String.t(), derive: "sanitize(tag=strip_tags) validate(url)") - - field(:type, String.t(), - derive: "sanitize(tag=strip_tags) validate(enum=String[#{Enum.join(@types, "::")}])", - default: "Person" - ) - - field(:summary, String.t(), - enforce: true, - derive: "sanitize(tag=strip_tags) validate(not_empty_string, max_len=364, min_len=3)" - ) - end - end - - defmodule Conditional do - use GuardedStruct - - guardedstruct do - conditional_field(:actor, any()) do - field(:actor, struct(), struct: Actor, derive: "validate(map, not_empty)") - - conditional_field(:actor, any(), - structs: true, - derive: "validate(list, not_empty, not_flatten_empty_item)" - ) do - field(:actor, struct(), struct: Actor, derive: "validate(map, not_empty)") - - field(:actor, String.t(), derive: "sanitize(tag=strip_tags) validate(url, max_len=160)") - end - - field(:actor, String.t(), derive: "sanitize(tag=strip_tags) validate(url, max_len=160)") - end - end - end - - test "nested conditional field with same name" do - end - - test "call derive on main conditional field to check whole entries" do - end -end diff --git a/test/guarded_struct_test/nested_conditional_field_test.exs b/test/guarded_struct_test/nested_conditional_field_test.exs new file mode 100644 index 0000000..6b57fd4 --- /dev/null +++ b/test/guarded_struct_test/nested_conditional_field_test.exs @@ -0,0 +1,57 @@ +defmodule MishkaDeveloperToolsTest.GuardedStruct.NestedConditionalFieldTest do + use ExUnit.Case, async: true + + # ---------------------------------------------------------- + # | Unfortunately, this macro does not support the nested mode in the conditional_field macro. + # | If you can add this feature I would be very happy to send a PR. + # | More information: https://github.com/mishka-group/mishka_developer_tools/issues/25 + # | Parent Issue: https://github.com/mishka-group/mishka_developer_tools/issues/23 + # ---------------------------------------------------------- + + ######### (▰˘◡˘▰) NestedConditionalFieldTest GuardedStructTest Data (▰˘◡˘▰) ########## + # defmodule Actor do + # use GuardedStruct + # @types ["Application", "Group", "Organization", "Person", "Service"] + + # guardedstruct do + # field(:id, String.t(), derive: "sanitize(tag=strip_tags) validate(url)") + + # field(:type, String.t(), + # derive: "sanitize(tag=strip_tags) validate(enum=String[#{Enum.join(@types, "::")}])", + # default: "Person" + # ) + + # field(:summary, String.t(), + # enforce: true, + # derive: "sanitize(tag=strip_tags) validate(not_empty_string, max_len=364, min_len=3)" + # ) + # end + # end + + # defmodule Conditional do + # use GuardedStruct + + # guardedstruct do + # conditional_field(:actor, any()) do + # field(:actor, struct(), struct: Actor, derive: "validate(map, not_empty)") + + # conditional_field(:actor, any(), + # structs: true, + # derive: "validate(list, not_empty, not_flatten_empty_item)" + # ) do + # field(:actor, struct(), struct: Actor, derive: "validate(map, not_empty)") + + # field(:actor, String.t(), derive: "sanitize(tag=strip_tags) validate(url, max_len=160)") + # end + + # field(:actor, String.t(), derive: "sanitize(tag=strip_tags) validate(url, max_len=160)") + # end + # end + # end + + # test "nested conditional field with same name" do + # end + + # test "call derive on main conditional field to check whole entries" do + # end +end