1 %%%----------------------------------------------------------------------------
2 %%% Equivalent to stdlib's orddict, but with a pretty (IMO), uniform interface.
3 %%%----------------------------------------------------------------------------
6 -include_lib("hope_kv_list.hrl").
8 -behavior(hope_gen_dictionary).
17 , get/3 % get existing or default
18 , get/4 % get existing if valid, or default
28 , find_unique_presence_violations/2 % No optional keys
29 , find_unique_presence_violations/3 % Specify optional keys
30 , validate_unique_presence/2 % No optional keys
31 , validate_unique_presence/3 % Specify optional keys
32 , presence_violations_to_list/1
39 -type presence_violations(A) ::
40 % This is a hack to effectively parametarize the types of record fields.
41 % IMPORTANT: Make sure that the order of fields matches the definition of
42 % #hope_kv_list_presence_violations
43 { hope_kv_list_presence_violations
45 , [A] % keys_duplicated
46 , [A] % keys_unsupported
49 -type presence_error(A) ::
51 | {keys_duplicated , [A]}
52 | {keys_unsupported , [A]}
56 %% ============================================================================
58 %% ============================================================================
65 -spec get(t(K, V), K) ->
68 case lists:keyfind(K, 1, T)
73 -spec get(t(K, V), K, V) ->
77 hope_option:get(Vopt, Default).
79 -spec get(t(K, V), K, V, fun((V) -> boolean())) ->
81 get(T, K, Default, IsValid) ->
83 VOpt2 = hope_option:validate(VOpt1, IsValid),
84 hope_option:get(VOpt2, Default).
86 -spec set(t(K, V), K, V) ->
89 lists:keystore(K, 1, T, {K, V}).
91 -spec update(t(K, V), K, fun((hope_option:t(V)) -> V)) ->
96 % TODO: Eliminate the 2nd lookup.
99 -spec pop(t(K, V), K) ->
100 {hope_option:t(V), t(K, V)}.
102 case lists:keytake(K, 1, T1)
103 of {value, {K, V}, T2} -> {{some, V}, T2}
104 ; false -> {none , T1}
107 -spec iter(t(K, V), fun((K, V) -> ok)) ->
111 lists:foreach(F2, T).
113 -spec map(t(K, V), fun((K, V) -> V)) ->
116 F2 = fun ({K, _}=X) -> {K, apply_map(F1, X)} end,
119 -spec filter(t(K, V), fun((K, V) -> boolean())) ->
125 -spec fold(t(K, V), fun((K, V, Acc) -> Acc), Acc) ->
127 fold(T, F1, Accumulator) ->
128 F2 = fun ({K, V}, Acc) -> F1(K, V, Acc) end,
129 lists:foldl(F2, Accumulator, T).
131 -spec to_kv_list(t(K, V)) ->
136 -spec of_kv_list([{K, V}]) ->
139 % TODO: Decide if validation is to be done here. Do so if yes.
142 -spec validate_unique_presence(T, [K]) ->
143 hope_result:t(T, [presence_error(K)])
145 validate_unique_presence(T, KeysRequired) ->
147 validate_unique_presence(T, KeysRequired, KeysOptional).
149 -spec validate_unique_presence(t(K, _V), [K], [K]) ->
150 hope_result:t(T, [presence_error(K)])
152 validate_unique_presence(T, KeysRequired, KeysOptional) ->
153 case find_unique_presence_violations(T, KeysRequired, KeysOptional)
154 of #hope_kv_list_presence_violations
156 , keys_duplicated = []
157 , keys_unsupported = []
160 ; #hope_kv_list_presence_violations{}=Violations ->
161 {error, presence_violations_to_list(Violations)}
164 -spec find_unique_presence_violations(t(K, _V), [K]) ->
165 presence_violations(K).
166 find_unique_presence_violations(T, KeysRequired) ->
168 find_unique_presence_violations(T, KeysRequired, KeysOptional).
170 -spec find_unique_presence_violations(t(K, _V), [K], [K]) ->
171 presence_violations(K).
172 find_unique_presence_violations(T, KeysRequired, KeysOptional) ->
173 KeysSupported = KeysRequired ++ KeysOptional,
174 KeysGiven = [K || {K, _V} <- T],
175 KeysGivenUnique = lists:usort(KeysGiven),
176 KeysDuplicated = lists:usort(KeysGiven -- KeysGivenUnique),
177 KeysMissing = KeysRequired -- KeysGivenUnique,
178 KeysUnsupported = KeysGivenUnique -- KeysSupported,
179 #hope_kv_list_presence_violations
180 { keys_missing = KeysMissing
181 , keys_duplicated = KeysDuplicated
182 , keys_unsupported = KeysUnsupported
185 -spec presence_violations_to_list(presence_violations(K)) ->
187 presence_violations_to_list(#hope_kv_list_presence_violations
188 { keys_missing = KeysMissing
189 , keys_duplicated = KeysDuplicated
190 , keys_unsupported = KeysUnsupported
195 ; [_|_] -> [{keys_missing, KeysMissing}]
200 ; [_|_] -> [{keys_duplicated, KeysDuplicated}]
205 ; [_|_] -> [{keys_unsupported, KeysUnsupported}]
207 ErrorDups ++ ErrorMissing ++ ErrorUnsupported.
210 %% ============================================================================
212 %% ============================================================================
216 when F :: fun(( K, V1 ) -> V2)
217 , G :: fun(({K, V1}) -> V2)
220 fun (X) -> apply_map(F, X) end.
222 -spec apply_map(fun((K, V1) -> V2), {K, V1}) ->
224 apply_map(F, {K, V}) ->