11defmodule Radius.Dict do
2- alias Radius.Dict.Parser
2+ @ moduledoc """
3+ Parses dictionaries and generates lookup functions and helper macro's.
4+
5+ The helper macro's give the benefit of compile time checks and faster encoding without losing
6+ readability.
7+ """
8+ require Radius.Dict.Helpers
39 alias Radius.Dict.EntryNotFoundError
10+ alias Radius.Dict.Helpers
11+ alias Radius.Dict.Parser
412
513 defmacro __using__ ( _ ) do
614 quote do
@@ -59,39 +67,41 @@ defmodule Radius.Dict do
5967 end
6068 )
6169
62- for attribute <- attributes do
63- attr_fun_name = "attr_#{ attribute . name } " |> String . replace ( "-" , "_" ) |> String . to_atom ( )
70+ Helpers . define_attribute_doc_helpers ( )
6471
65- defmacro unquote ( attr_fun_name ) ( ) , do: unquote ( attribute . id )
66- defmacro unquote ( attr_fun_name ) ( val ) , do: { unquote ( attribute . id ) , val }
67- def attribute_by_id ( unquote ( attribute . id ) ) , do: unquote ( Macro . escape ( attribute ) )
68- def attribute_by_name ( unquote ( attribute . name ) ) , do: unquote ( Macro . escape ( attribute ) )
72+ for attribute <- attributes do
73+ Helpers . define_attribute_functions ( attribute )
6974 end
7075
71- for val <- values do
72- val_fun_name = "val_#{ val . attr } _#{ val . name } " |> String . replace ( "-" , "_" ) |> String . to_atom ( )
76+ Helpers . define_value_doc_helpers ( )
7377
74- defmacro unquote ( val_fun_name ) ( ) , do: unquote ( val . value )
75- def value_by_value ( unquote ( val . attr ) , unquote ( val . value ) ) , do: unquote ( Macro . escape ( val ) )
76- def value_by_name ( unquote ( val . attr ) , unquote ( val . name ) ) , do: unquote ( Macro . escape ( val ) )
78+ for val <- values do
79+ Helpers . define_value_functions ( val )
7780 end
7881
82+ @ doc """
83+ Get vendor struct based on vendor id
84+ """
85+ @ doc group: :lookup
86+ def vendor_by_id ( attr_id )
87+
88+ @ doc """
89+ Get vendor struct based on vendor name
90+ """
91+ @ doc group: :lookup
92+ def vendor_by_name ( attr_name )
93+
7994 for vendor <- vendors do
80- mod = Module . concat ( __MODULE__ , : "Vendor#{ String . replace ( vendor . name , "-" , "_" ) } " )
95+ mod = Module . concat ( __MODULE__ , Helpers . safe_name ( "Vendor#{ vendor . name } " ) )
8196 [ tl , ll ] = Keyword . get ( vendor . opts , :format ) || [ 1 , 1 ]
8297 vendor_data = % { id: vendor . id , name: vendor . name , format: { tl , ll } , module: mod }
8398 def vendor_by_id ( unquote ( vendor . id ) ) , do: unquote ( Macro . escape ( vendor_data ) )
8499 def vendor_by_name ( unquote ( vendor . name ) ) , do: unquote ( Macro . escape ( vendor_data ) )
85100
86101 attribute_funs =
87102 for attribute <- vendor . attributes do
88- attr_fun_name = "attr_#{ attribute . name } " |> String . replace ( "-" , "_" ) |> String . to_atom ( )
89-
90- quote location: :keep do
91- defmacro unquote ( attr_fun_name ) ( ) , do: unquote ( attribute . id )
92- defmacro unquote ( attr_fun_name ) ( val ) , do: { unquote ( attribute . id ) , val }
93- def attribute_by_id ( unquote ( attribute . id ) ) , do: unquote ( Macro . escape ( attribute ) )
94- def attribute_by_name ( unquote ( attribute . name ) ) , do: unquote ( Macro . escape ( attribute ) )
103+ quote do
104+ unquote ( Helpers . generate_attribute_functions ( Macro . escape ( attribute ) ) )
95105 end
96106 end
97107
@@ -100,22 +110,13 @@ defmodule Radius.Dict do
100110 value_funs =
101111 if Enum . count ( vendor . attributes ++ vendor . values ) < elixir_compiler_limit do
102112 for val <- vendor . values do
103- val_fun_name =
104- "val_#{ val . attr } _#{ val . name } " |> String . replace ( "-" , "_" ) |> String . to_atom ( )
105-
106- quote location: :keep do
107- defmacro unquote ( val_fun_name ) ( ) , do: unquote ( val . value )
108-
109- def value_by_value ( unquote ( val . attr ) , unquote ( val . value ) ) ,
110- do: unquote ( Macro . escape ( val ) )
111-
112- def value_by_name ( unquote ( val . attr ) , unquote ( val . name ) ) ,
113- do: unquote ( Macro . escape ( val ) )
113+ quote do
114+ unquote ( Helpers . generate_value_functions ( Macro . escape ( val ) ) )
114115 end
115116 end
116117 else
117118 for { attr , vals } <- Enum . group_by ( vendor . values , & & 1 . attr ) do
118- val_fun_name = "val_#{ attr } " |> String . replace ( "-" , "_" ) |> String . to_atom ( )
119+ val_fun_name = Helpers . safe_name ( "val_#{ attr } " )
119120
120121 quote location: :keep do
121122 defmacro unquote ( val_fun_name ) ( val_name ) ,
@@ -143,7 +144,14 @@ defmodule Radius.Dict do
143144 end
144145
145146 IO . puts ( "Compiling #{ mod } " )
146- Module . create ( mod , attribute_funs ++ value_funs , Macro.Env . location ( __ENV__ ) )
147+
148+ value_doc_funs = if value_funs == [ ] , do: [ ] , else: [ Helpers . generate_value_doc_helpers ( ) ]
149+
150+ module_body =
151+ [ Helpers . generate_attribute_doc_helpers ( ) ] ++
152+ attribute_funs ++ value_funs ++ value_doc_funs
153+
154+ Module . create ( mod , module_body , Macro.Env . location ( __ENV__ ) )
147155 end
148156
149157 def attribute_by_id ( id ) , do: raise ( EntryNotFoundError , type: :attribute , key: id )
0 commit comments