Skip to content

Commit

Permalink
Support overridable messages for the GuardedStruct module
Browse files Browse the repository at this point in the history
Support overridable messages for the `GuardedStruct` module with support for multiple languages

Close #10
  • Loading branch information
shahryarjb committed Dec 22, 2024
1 parent 8e93175 commit fbd3108
Show file tree
Hide file tree
Showing 7 changed files with 532 additions and 163 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Changelog for GuardedStruct 0.0.4

- Fix deprecated code from Elixir 1.18
- Support overridable messages for the `GuardedStruct` module with support for multiple languages

# Changelog for GuardedStruct 0.0.3

- Fix deprecated code from Elixir 1.18.0-rc.0
Expand Down
26 changes: 5 additions & 21 deletions lib/derive/parser.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule GuardedStruct.Derive.Parser do
import GuardedStruct.Messages, only: [translated_message: 1]

@spec parser(list(String.t()) | String.t()) :: any()
def parser(inputs) when is_list(inputs) do
Enum.map(inputs, &parser(&1))
Expand Down Expand Up @@ -35,14 +37,7 @@ defmodule GuardedStruct.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/guarded_struct/issues/7
Parent Issue: https://github.com/mishka-group/guarded_struct/issues/8
\n ----------------------------------------------------------\n
""")
raise(translated_message(:unsupported_conditional_field))

{:conditional_field, line,
elements_unification(add_parent_tags(items, parent, "conds"), parent)}
Expand All @@ -58,14 +53,7 @@ defmodule GuardedStruct.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/guarded_struct/issues/7
Parent Issue: https://github.com/mishka-group/guarded_struct/issues/8
\n ----------------------------------------------------------\n
""")
raise(translated_message(:unsupported_conditional_field))

comverted_items = add_parent_tags(items, parent, "conds")

Expand Down Expand Up @@ -251,11 +239,7 @@ defmodule GuardedStruct.Derive.Parser do

def field_value(output) when is_list(output), do: output

def field_value(nil),
do:
raise(
"Oh no!, I think you have not made all the subfields of a conditional field to the same name"
)
def field_value(nil), do: raise(translated_message(:parser_field_value))

@spec conds_list(list(map()) | map(), String.t()) :: any()
def conds_list(data, parent_key) do
Expand Down
Loading

0 comments on commit fbd3108

Please sign in to comment.