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).
27 , find_unique_presence_violations/2 % No optional keys
28 , find_unique_presence_violations/3 % Specify optional keys
29 , validate_unique_presence/2 % No optional keys
30 , validate_unique_presence/3 % Specify optional keys
31 , presence_violations_to_list/1
38 -type presence_violations(A) ::
39 % This is a hack to effectively parametarize the types of record fields.
40 % IMPORTANT: Make sure that the order of fields matches the definition of
41 % #hope_kv_list_presence_violations
42 { hope_kv_list_presence_violations
44 , [A] % keys_duplicated
45 , [A] % keys_unsupported
48 -type presence_error(A) ::
50 | {keys_duplicated , [A]}
51 | {keys_unsupported , [A]}
55 %% ============================================================================
57 %% ============================================================================
64 -spec get(t(K, V), K) ->
67 case lists:keyfind(K, 1, T)
72 -spec get(t(K, V), K, V) ->
76 hope_option:get(Vopt, Default).
78 -spec set(t(K, V), K, V) ->
81 lists:keystore(K, 1, T, {K, V}).
83 -spec update(t(K, V), K, fun((hope_option:t(V)) -> V)) ->
88 % TODO: Eliminate the 2nd lookup.
91 -spec pop(t(K, V), K) ->
92 {hope_option:t(V), t(K, V)}.
94 case lists:keytake(K, 1, T1)
95 of {value, {K, V}, T2} -> {{some, V}, T2}
96 ; false -> {none , T1}
99 -spec iter(t(K, V), fun((K, V) -> ok)) ->
103 lists:foreach(F2, T).
105 -spec map(t(K, V), fun((K, V) -> V)) ->
108 F2 = fun ({K, _}=X) -> {K, apply_map(F1, X)} end,
111 -spec filter(t(K, V), fun((K, V) -> boolean())) ->
117 -spec fold(t(K, V), fun((K, V, Acc) -> Acc), Acc) ->
119 fold(T, F1, Accumulator) ->
120 F2 = fun ({K, V}, Acc) -> F1(K, V, Acc) end,
121 lists:foldl(F2, Accumulator, T).
123 -spec to_kv_list(t(K, V)) ->
128 -spec of_kv_list([{K, V}]) ->
131 % TODO: Decide if validation is to be done here. Do so if yes.
134 -spec validate_unique_presence(t(K, _V), [K]) ->
135 hope_result:t(ok, [presence_error(K)]).
136 validate_unique_presence(T, KeysRequired) ->
138 validate_unique_presence(T, KeysRequired, KeysOptional).
140 -spec validate_unique_presence(t(K, _V), [K], [K]) ->
141 hope_result:t(ok, [presence_error(K)]).
142 validate_unique_presence(T, KeysRequired, KeysOptional) ->
143 case find_unique_presence_violations(T, KeysRequired, KeysOptional)
144 of #hope_kv_list_presence_violations
146 , keys_duplicated = []
147 , keys_unsupported = []
150 ; #hope_kv_list_presence_violations{}=Violations ->
151 {error, presence_violations_to_list(Violations)}
154 -spec find_unique_presence_violations(t(K, _V), [K]) ->
155 presence_violations(K).
156 find_unique_presence_violations(T, KeysRequired) ->
158 find_unique_presence_violations(T, KeysRequired, KeysOptional).
160 -spec find_unique_presence_violations(t(K, _V), [K], [K]) ->
161 presence_violations(K).
162 find_unique_presence_violations(T, KeysRequired, KeysOptional) ->
163 KeysSupported = KeysRequired ++ KeysOptional,
164 KeysGiven = [K || {K, _V} <- T],
165 KeysGivenUnique = lists:usort(KeysGiven),
166 KeysDuplicated = lists:usort(KeysGiven -- KeysGivenUnique),
167 KeysMissing = KeysRequired -- KeysGivenUnique,
168 KeysUnsupported = KeysGivenUnique -- KeysSupported,
169 #hope_kv_list_presence_violations
170 { keys_missing = KeysMissing
171 , keys_duplicated = KeysDuplicated
172 , keys_unsupported = KeysUnsupported
175 -spec presence_violations_to_list(presence_violations(K)) ->
177 presence_violations_to_list(#hope_kv_list_presence_violations
178 { keys_missing = KeysMissing
179 , keys_duplicated = KeysDuplicated
180 , keys_unsupported = KeysUnsupported
185 ; [_|_] -> [{keys_missing, KeysMissing}]
190 ; [_|_] -> [{keys_duplicated, KeysDuplicated}]
195 ; [_|_] -> [{keys_unsupported, KeysUnsupported}]
197 ErrorDups ++ ErrorMissing ++ ErrorUnsupported.
200 %% ============================================================================
202 %% ============================================================================
206 when F :: fun(( K, V1 ) -> V2)
207 , G :: fun(({K, V1}) -> V2)
210 fun (X) -> apply_map(F, X) end.
212 -spec apply_map(fun((K, V1) -> V2), {K, V1}) ->
214 apply_map(F, {K, V}) ->