Skip to content

Commit

Permalink
Add raise message for nested condition
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed Jan 15, 2024
1 parent ce752cb commit 23b01d9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 51 deletions.
20 changes: 19 additions & 1 deletion lib/helper/derive/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/macros/guarded_struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 0 additions & 50 deletions test/guarded_struct_test/nested_conditional_field.exs

This file was deleted.

57 changes: 57 additions & 0 deletions test/guarded_struct_test/nested_conditional_field_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 23b01d9

Please sign in to comment.