@@ -28,21 +28,44 @@ 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+ { :ok , struct }
46+ | :error
47+ @ doc """
48+ Attempts to parse the potentially unsafe input into a valid LogData.
49+ """
50+ def parse ( type , unsafe_data_params ) ,
51+ do: dispatch ( type , :parse , unsafe_data_params )
52+
53+ @ spec dispatch ( type , atom , term ) ::
54+ term
55+ defp dispatch ( type , method , param ) when not is_list ( param ) ,
56+ do: dispatch ( type , method , [ param ] )
57+ defp dispatch ( type , method , params ) do
3858 type
3959 |> get_type_module ( )
40- |> apply ( :new , [ data_params ] )
60+ |> apply ( method , params )
4161 end
4262
4363 end
4464 end
4565
66+ @ doc """
67+ Top-level macro used to define a LogType and its underlying LogData.
68+ """
4669 defmacro log ( name , enum_id , do: block ) do
4770 module_name =
4871 __CALLER__ . module
@@ -67,6 +90,9 @@ defmodule Helix.Log.Model.LogType.Macros do
6790 end
6891 end
6992
93+ @ doc """
94+ Converts the module into a LogData struct.
95+ """
7096 defmacro data_struct ( keys ) do
7197 quote do
7298
@@ -76,17 +102,48 @@ defmodule Helix.Log.Model.LogType.Macros do
76102 end
77103 end
78104
79- defmacro new ( args , do: block ) do
105+ @ doc """
106+ Creates a new LogData from the given `data` map.
107+ """
108+ defmacro new ( data , do: block ) do
80109 quote do
81110
82111 @ doc false
83- def new ( unquote ( args ) ) do
112+ def new ( unquote ( data ) ) do
84113 unquote ( block )
85114 end
86115
87116 end
88117 end
89118
119+ @ doc """
120+ Attempts to parse the given unsafe input into a valid LogData.
121+ """
122+ defmacro parse ( data , do: block ) do
123+ quote do
124+
125+ @ spec parse ( term ) ::
126+ { :ok , data :: struct }
127+ | :error
128+ @ doc false
129+ def parse ( map = unquote ( data ) ) when is_map ( map ) do
130+ try do
131+ { :ok , unquote ( block ) }
132+ rescue
133+ RuntimeError ->
134+ :error
135+
136+ KeyError ->
137+ :error
138+ end
139+ end
140+
141+ def parse ( not_map ) when not is_map ( not_map ) ,
142+ do: :error
143+
144+ end
145+ end
146+
90147 @ doc """
91148 Generates the boilerplate for a n-field log type.
92149
@@ -116,22 +173,6 @@ defmodule Helix.Log.Model.LogType.Macros do
116173 defmacro gen3 ( p1 , p2 , p3 ) ,
117174 do: do_gen3 ( p1 , p2 , p3 )
118175
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-
135176 def validate ( field_type , field_value ) when is_atom ( field_type ) do
136177 fun = Utils . concat_atom ( :validate_ , field_type )
137178
@@ -222,9 +263,9 @@ defmodule Helix.Log.Model.LogType.Macros do
222263 parse ( unsafe ) do
223264 % __MODULE__ {
224265 unquote ( f1 ) =>
225- validate ( unquote ( v_f1 ) , Map . get ( unsafe , unquote ( str_f1 ) ) ) ,
266+ validate ( unquote ( v_f1 ) , Map . fetch! ( unsafe , unquote ( str_f1 ) ) ) ,
226267 unquote ( f2 ) =>
227- validate ( unquote ( v_f2 ) , Map . get ( unsafe , unquote ( str_f2 ) ) )
268+ validate ( unquote ( v_f2 ) , Map . fetch! ( unsafe , unquote ( str_f2 ) ) )
228269 }
229270 end
230271
@@ -249,11 +290,11 @@ defmodule Helix.Log.Model.LogType.Macros do
249290 parse ( unsafe ) do
250291 % __MODULE__ {
251292 unquote ( f1 ) =>
252- validate ( unquote ( v_f1 ) , Map . get ( unsafe , unquote ( str_f1 ) ) ) ,
293+ validate ( unquote ( v_f1 ) , Map . fetch! ( unsafe , unquote ( str_f1 ) ) ) ,
253294 unquote ( f2 ) =>
254- validate ( unquote ( v_f2 ) , Map . get ( unsafe , unquote ( str_f2 ) ) ) ,
295+ validate ( unquote ( v_f2 ) , Map . fetch! ( unsafe , unquote ( str_f2 ) ) ) ,
255296 unquote ( f3 ) =>
256- validate ( unquote ( v_f3 ) , Map . get ( unsafe , unquote ( str_f3 ) ) )
297+ validate ( unquote ( v_f3 ) , Map . fetch! ( unsafe , unquote ( str_f3 ) ) )
257298 }
258299 end
259300
0 commit comments