Boolean logic, and some other small enhancements: an {equals, A, B} query which just compares strings for equality, and a shortcut ability to use a string constant "foo" instead of {string, "foo"}.
This commit is contained in:
		
							parent
							
								
									5e6ee7b90f
								
							
						
					
					
						commit
						2cd07a3c07
					
				|  | @ -27,7 +27,15 @@ | |||
|                              {string, "can-declare-queues"}} | ||||
|                     }, | ||||
|                     {permission, write, {constant, true}}, | ||||
|                     {permission, read, {constant, true}} | ||||
|                     {permission, read, | ||||
|                      {'or', | ||||
|                       [{'and', | ||||
|                         {equals, "${name}", "test1"}, | ||||
|                         {equals, "${username}", "Simon MacMullen"}}, | ||||
|                        {'and', | ||||
|                         {equals, "${name}", "test2"}, | ||||
|                         {'not', {equals, "${username}", "Mike Bridgen"}}} | ||||
|                       ]}} | ||||
|                    ]}} | ||||
|             ]}} | ||||
|   ]} | ||||
|  |  | |||
|  | @ -132,6 +132,41 @@ evaluate0({in_group, DNPattern, Desc}, Args, | |||
|     ?L1("evaluated in_group for \"~s\": ~p", [DN, R], State), | ||||
|     R; | ||||
| 
 | ||||
| evaluate0({'not', SubQuery}, Args, User, LDAP, State) -> | ||||
|     R = evaluate(SubQuery, Args, User, LDAP, State), | ||||
|     ?L1("negated result to ~s", [R], State), | ||||
|     not R; | ||||
| 
 | ||||
| evaluate0({'and', SubQuery1, SubQuery2}, Args, User, LDAP, State) -> | ||||
|     evaluate0({'and', [SubQuery1, SubQuery2]}, Args, User, LDAP, State); | ||||
| 
 | ||||
| evaluate0({'and', Queries}, Args, User, LDAP, State) when is_list(Queries) -> | ||||
|     R = lists:foldl(fun (Q,  true)  -> evaluate(Q, Args, User, LDAP, State); | ||||
|                         (_Q, false) -> false | ||||
|                     end, true, Queries), | ||||
|     ?L1("'and' result: ~s", [R], State), | ||||
|     R; | ||||
| 
 | ||||
| evaluate0({'or', SubQuery1, SubQuery2}, Args, User, LDAP, State) -> | ||||
|     evaluate0({'or', [SubQuery1, SubQuery2]}, Args, User, LDAP, State); | ||||
| 
 | ||||
| evaluate0({'or', Queries}, Args, User, LDAP, State) when is_list(Queries) -> | ||||
|     R = lists:foldl(fun (_Q, true)  -> true; | ||||
|                         (Q,  false) -> evaluate(Q, Args, User, LDAP, State) | ||||
|                     end, false, Queries), | ||||
|     ?L1("'or' result: ~s", [R], State), | ||||
|     R; | ||||
| 
 | ||||
| evaluate0({equals, StringQuery1, StringQuery2}, Args, User, LDAP, State) -> | ||||
|     safe_eval(fun (String1, String2) -> | ||||
|                       R = String1 =:= String2, | ||||
|                       ?L1("evaluated equals \"~s\", \"~s\": ~s", | ||||
|                           [String1, String2, R], State), | ||||
|                       R | ||||
|               end, | ||||
|               evaluate(StringQuery1, Args, User, LDAP, State), | ||||
|               evaluate(StringQuery2, Args, User, LDAP, State)); | ||||
| 
 | ||||
| evaluate0({match, StringQuery, REQuery}, Args, User, LDAP, State) -> | ||||
|     safe_eval(fun (String, RE) -> | ||||
|                       R = case re:run(String, RE) of | ||||
|  | @ -145,6 +180,9 @@ evaluate0({match, StringQuery, REQuery}, Args, User, LDAP, State) -> | |||
|               evaluate(StringQuery, Args, User, LDAP, State), | ||||
|               evaluate(REQuery, Args, User, LDAP, State)); | ||||
| 
 | ||||
| evaluate0(StringPattern, Args, User, LDAP, State) when is_list(StringPattern) -> | ||||
|     evaluate0({string, StringPattern}, Args, User, LDAP, State); | ||||
| 
 | ||||
| evaluate0({string, StringPattern}, Args, _User, _LDAP, State) -> | ||||
|     R = fill(StringPattern, Args, State), | ||||
|     ?L1("evaluated string for \"~s\"", [R], State), | ||||
|  |  | |||
|  | @ -59,6 +59,16 @@ string_match_test_() -> | |||
|                 {?SIMON, B(<<"abc123">>),                     fail}, | ||||
|                 {?SIMON, B(<<"xch-Someone Else-abc123">>),    fail}]]. | ||||
| 
 | ||||
| boolean_logic_test_() -> | ||||
|     Q1 = [#'queue.declare'{queue = <<"test1">>}, | ||||
|           #'basic.consume'{queue = <<"test1">>}], | ||||
|     Q2 = [#'queue.declare'{queue = <<"test2">>}, | ||||
|           #'basic.consume'{queue = <<"test2">>}], | ||||
|     [test_resource_fun(PTR) || PTR <- [{?SIMON, Q1, ok}, | ||||
|                                        {?SIMON, Q2, ok}, | ||||
|                                        {?MIKEB, Q1, fail}, | ||||
|                                        {?MIKEB, Q2, fail}]]. | ||||
| 
 | ||||
| test_resource_fun({Person, Things, Result}) -> | ||||
|     fun() -> | ||||
|             {ok, Conn} = amqp_connection:start(Person), | ||||
|  | @ -66,6 +76,7 @@ test_resource_fun({Person, Things, Result}) -> | |||
|             ?assertEqual(Result, | ||||
|                          try | ||||
|                              [amqp_channel:call(Ch, T) || T <- Things], | ||||
|                              amqp_connection:close(Conn), | ||||
|                              ok | ||||
|                          catch exit:_ -> fail | ||||
|                          end) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue