diff options
| author | Simon MacMullen <simon@rabbitmq.com> | 2012-05-18 11:36:06 +0100 |
|---|---|---|
| committer | Simon MacMullen <simon@rabbitmq.com> | 2012-05-18 11:36:06 +0100 |
| commit | 3babc78551fe2d8126c56b26346f9d808d1730c2 (patch) | |
| tree | abd28ca8e24da0c1ce99cdbde6bf420806aedac8 /src | |
| parent | 7f1bda6108826ee32a4e1019b335734efcf8b478 (diff) | |
| download | rabbitmq-server-git-3babc78551fe2d8126c56b26346f9d808d1730c2.tar.gz | |
Move parameter validation into its own module.
Diffstat (limited to 'src')
| -rw-r--r-- | src/rabbit_parameter_validation.erl | 61 | ||||
| -rw-r--r-- | src/rabbit_policy.erl | 62 |
2 files changed, 67 insertions, 56 deletions
diff --git a/src/rabbit_parameter_validation.erl b/src/rabbit_parameter_validation.erl new file mode 100644 index 0000000000..af940dde97 --- /dev/null +++ b/src/rabbit_parameter_validation.erl @@ -0,0 +1,61 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License +%% at http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and +%% limitations under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developer of the Original Code is VMware, Inc. +%% Copyright (c) 2007-2012 VMware, Inc. All rights reserved. +%% + +-module(rabbit_parameter_validation). + +-export([number/2, binary/2, list/2, proplist/3]). + +number(_Name, Term) when is_number(Term) -> + ok; + +number(Name, Term) -> + {error, "~s should be number, actually was ~p", [Name, Term]}. + +binary(_Name, Term) when is_binary(Term) -> + ok; + +binary(Name, Term) -> + {error, "~s should be binary, actually was ~p", [Name, Term]}. + +list(_Name, Term) when is_list(Term) -> + ok; + +list(Name, Term) -> + {error, "~s should be list, actually was ~p", [Name, Term]}. + +proplist(Name, Constraints, Term) when is_list(Term) -> + {Results, Remainder} + = lists:foldl( + fun ({Key, Fun, Needed}, {Results0, Term0}) -> + case {lists:keytake(Key, 1, Term0), Needed} of + {{value, {Key, Value}, Term1}, _} -> + {[Fun(Key, Value) | Results0], + Term1}; + {false, mandatory} -> + {[{error, "Key \"~s\" not found in ~s", + [Key, Name]} | Results0], Term0}; + {false, optional} -> + {Results0, Term0} + end + end, {[], Term}, Constraints), + case Remainder of + [] -> Results; + _ -> [{error, "Unrecognised terms ~p in ~s", [Remainder, Name]} + | Results] + end; + +proplist(Name, _Constraints, Term) -> + {error, "~s not a list ~p", [Name, Term]}. diff --git a/src/rabbit_policy.erl b/src/rabbit_policy.erl index 2697b93ddd..d4c2dee046 100644 --- a/src/rabbit_policy.erl +++ b/src/rabbit_policy.erl @@ -60,8 +60,9 @@ get0(Name, List) -> case pget(<<"policy">>, List) of %%---------------------------------------------------------------------------- -validate(<<"policy">>, _Name, Term) -> - assert_contents(policy_validation(), Term). +validate(<<"policy">>, Name, Term) -> + rabbit_parameter_validation:proplist( + Name, policy_validation(), Term). validate_clear(<<"policy">>, _Name) -> ok. @@ -146,57 +147,6 @@ sort_pred(A, B) -> %%---------------------------------------------------------------------------- policy_validation() -> - [{<<"vhost">>, binary, optional}, - {<<"prefix">>, binary, mandatory}, - {<<"policy">>, list, mandatory}]. - -%% TODO this is mostly duplicated from -%% rabbit_federation_parameters. Sort that out in some way. - -assert_type(Name, {Type, Opts}, Term) -> - assert_type(Name, Type, Term), - case lists:member(Term, Opts) of - true -> ok; - false -> {error, "~s must be one of ~p", [Name, Opts]} - end; - -assert_type(_Name, number, Term) when is_number(Term) -> - ok; - -assert_type(Name, number, Term) -> - {error, "~s should be number, actually was ~p", [Name, Term]}; - -assert_type(_Name, binary, Term) when is_binary(Term) -> - ok; - -assert_type(Name, binary, Term) -> - {error, "~s should be binary, actually was ~p", [Name, Term]}; - -assert_type(_Name, list, Term) when is_list(Term) -> - ok; - -assert_type(Name, list, Term) -> - {error, "~s should be list, actually was ~p", [Name, Term]}. - -assert_contents(Constraints, Term) when is_list(Term) -> - {Results, Remainder} - = lists:foldl( - fun ({Name, Constraint, Needed}, {Results0, Term0}) -> - case {lists:keytake(Name, 1, Term0), Needed} of - {{value, {Name, Value}, Term1}, _} -> - {[assert_type(Name, Constraint, Value) | Results0], - Term1}; - {false, mandatory} -> - {[{error, "Key \"~s\" not found", [Name]} | - Results0], Term0}; - {false, optional} -> - {Results0, Term0} - end - end, {[], Term}, Constraints), - case Remainder of - [] -> Results; - _ -> [{error, "Unrecognised terms ~p", [Remainder]} | Results] - end; - -assert_contents(_Constraints, Term) -> - {error, "Not a list ~p", [Term]}. + [{<<"vhost">>, fun rabbit_parameter_validation:binary/2, optional}, + {<<"prefix">>, fun rabbit_parameter_validation:binary/2, mandatory}, + {<<"policy">>, fun rabbit_parameter_validation:list/2, mandatory}]. |
