@@ -28,21 +28,43 @@ defmodule Helix.Log.Model.LogType.Macros do
2828
2929 defenum LogEnum , @ logs
3030
31- # @spec exists?(term) ::
32- # boolean
33- def exists? ( log ) do
34- Enum . any? ( @ logs , fn { valid_log , _ } -> valid_log == log end )
35- end
36-
37- def new ( type , data_params ) do
31+ @ spec exists? ( atom ) ::
32+ boolean
33+ def exists? ( log_type ) ,
34+ do: Enum . any? ( @ logs , fn { valid_type , _ } -> valid_type == log_type end )
35+
36+ @ spec new ( type , map ) ::
37+ struct
38+ @ doc """
39+ Creates a new struct for the given log `type`.
40+ """
41+ def new ( type , data_params ) ,
42+ do: dispatch ( type , :new , data_params )
43+
44+ @ spec parse ( type , map ) ::
45+ struct
46+ @ doc """
47+ Attempts to parse the potentially unsafe input into a valid LogData.
48+ """
49+ def parse ( type , unsafe_data_params ) ,
50+ do: dispatch ( type , :parse , unsafe_data_params )
51+
52+ @ spec dispatch ( type , atom , term ) ::
53+ term
54+ defp dispatch ( type , method , param ) when not is_list ( param ) ,
55+ do: dispatch ( type , method , [ param ] )
56+ defp dispatch ( type , method , params ) do
3857 type
3958 |> get_type_module ( )
40- |> apply ( :new , [ data_params ] )
59+ |> apply ( method , params )
4160 end
4261
4362 end
4463 end
4564
65+ @ doc """
66+ Top-level macro used to define a LogType and its underlying LogData.
67+ """
4668 defmacro log ( name , enum_id , do: block ) do
4769 module_name =
4870 __CALLER__ . module
@@ -67,6 +89,9 @@ defmodule Helix.Log.Model.LogType.Macros do
6789 end
6890 end
6991
92+ @ doc """
93+ Converts the module into a LogData struct.
94+ """
7095 defmacro data_struct ( keys ) do
7196 quote do
7297
@@ -76,17 +101,48 @@ defmodule Helix.Log.Model.LogType.Macros do
76101 end
77102 end
78103
79- defmacro new ( args , do: block ) do
104+ @ doc """
105+ Creates a new LogData from the given `data` map.
106+ """
107+ defmacro new ( data , do: block ) do
80108 quote do
81109
82110 @ doc false
83- def new ( unquote ( args ) ) do
111+ def new ( unquote ( data ) ) do
84112 unquote ( block )
85113 end
86114
87115 end
88116 end
89117
118+ @ doc """
119+ Attempts to parse the given unsafe input into a valid LogData.
120+ """
121+ defmacro parse ( data , do: block ) do
122+ quote do
123+
124+ @ spec parse ( term ) ::
125+ { :ok , data :: struct }
126+ | :error
127+ @ doc false
128+ def parse ( map = unquote ( data ) ) when is_map ( map ) do
129+ try do
130+ { :ok , unquote ( block ) }
131+ rescue
132+ RuntimeError ->
133+ :error
134+
135+ KeyError ->
136+ :error
137+ end
138+ end
139+
140+ def parse ( not_map ) when not is_map ( not_map ) ,
141+ do: :error
142+
143+ end
144+ end
145+
90146 @ doc """
91147 Generates the boilerplate for a n-field log type.
92148
@@ -116,22 +172,6 @@ defmodule Helix.Log.Model.LogType.Macros do
116172 defmacro gen3 ( p1 , p2 , p3 ) ,
117173 do: do_gen3 ( p1 , p2 , p3 )
118174
119- defmacro parse ( args , do: block ) do
120- quote do
121-
122- @ doc false
123- def parse ( unquote ( args ) ) do
124- try do
125- { :ok , unquote ( block ) }
126- rescue
127- RuntimeError ->
128- :error
129- end
130- end
131-
132- end
133- end
134-
135175 def validate ( field_type , field_value ) when is_atom ( field_type ) do
136176 fun = Utils . concat_atom ( :validate_ , field_type )
137177
@@ -222,9 +262,9 @@ defmodule Helix.Log.Model.LogType.Macros do
222262 parse ( unsafe ) do
223263 % __MODULE__ {
224264 unquote ( f1 ) =>
225- validate ( unquote ( v_f1 ) , Map . get ( unsafe , unquote ( str_f1 ) ) ) ,
265+ validate ( unquote ( v_f1 ) , Map . fetch! ( unsafe , unquote ( str_f1 ) ) ) ,
226266 unquote ( f2 ) =>
227- validate ( unquote ( v_f2 ) , Map . get ( unsafe , unquote ( str_f2 ) ) )
267+ validate ( unquote ( v_f2 ) , Map . fetch! ( unsafe , unquote ( str_f2 ) ) )
228268 }
229269 end
230270
@@ -249,11 +289,11 @@ defmodule Helix.Log.Model.LogType.Macros do
249289 parse ( unsafe ) do
250290 % __MODULE__ {
251291 unquote ( f1 ) =>
252- validate ( unquote ( v_f1 ) , Map . get ( unsafe , unquote ( str_f1 ) ) ) ,
292+ validate ( unquote ( v_f1 ) , Map . fetch! ( unsafe , unquote ( str_f1 ) ) ) ,
253293 unquote ( f2 ) =>
254- validate ( unquote ( v_f2 ) , Map . get ( unsafe , unquote ( str_f2 ) ) ) ,
294+ validate ( unquote ( v_f2 ) , Map . fetch! ( unsafe , unquote ( str_f2 ) ) ) ,
255295 unquote ( f3 ) =>
256- validate ( unquote ( v_f3 ) , Map . get ( unsafe , unquote ( str_f3 ) ) )
296+ validate ( unquote ( v_f3 ) , Map . fetch! ( unsafe , unquote ( str_f3 ) ) )
257297 }
258298 end
259299
0 commit comments