Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

128 Zeilen
3.4 KiB

  1. %% @doc Built in operators.
  2. -module(glc_ops).
  3. -export([
  4. lt/2,
  5. eq/2,
  6. gt/2,
  7. wc/1,
  8. nf/1
  9. ]).
  10. -export([
  11. all/1,
  12. any/1,
  13. null/1,
  14. with/2
  15. ]).
  16. -export([
  17. union/1
  18. ]).
  19. -type op() ::
  20. {atom(), '<', term()} |
  21. {atom(), '=', term()} |
  22. {atom(), '>', term()} |
  23. {atom(), '*'} |
  24. {atom(), '!'} |
  25. {any, [op(), ...]} |
  26. {all, [op(), ...]} |
  27. {null, true|false}.
  28. -export_type([op/0]).
  29. %% @doc Test that a field value is less than a term.
  30. -spec lt(atom(), term()) -> op().
  31. lt(Key, Term) when is_atom(Key) ->
  32. {Key, '<', Term};
  33. lt(Key, Term) ->
  34. erlang:error(badarg, [Key, Term]).
  35. %% @doc Test that a field value is equal to a term.
  36. -spec eq(atom(), term()) -> op().
  37. eq(Key, Term) when is_atom(Key) ->
  38. {Key, '=', Term};
  39. eq(Key, Term) ->
  40. erlang:error(badarg, [Key, Term]).
  41. %% @doc Test that a field value is greater than a term.
  42. -spec gt(atom(), term()) -> op().
  43. gt(Key, Term) when is_atom(Key) ->
  44. {Key, '>', Term};
  45. gt(Key, Term) ->
  46. erlang:error(badarg, [Key, Term]).
  47. %% @doc Test that a field exists.
  48. -spec wc(atom()) -> op().
  49. wc(Key) when is_atom(Key) ->
  50. {Key, '*'};
  51. wc(Key) ->
  52. erlang:error(badarg, [Key]).
  53. %% @doc Test that a field is not found.
  54. -spec nf(atom()) -> op().
  55. nf(Key) when is_atom(Key) ->
  56. {Key, '!'};
  57. nf(Key) ->
  58. erlang:error(badarg, [Key]).
  59. %% @doc Filter the input using multiple filters.
  60. %%
  61. %% For an input to be considered valid output the all filters specified
  62. %% in the list must hold for the input event. The list is expected to
  63. %% be a non-empty list. If the list of filters is an empty list a `badarg'
  64. %% error will be thrown.
  65. -spec all([op()]) -> op().
  66. all([_|_]=Conds) ->
  67. {all, Conds};
  68. all(Other) ->
  69. erlang:error(badarg, [Other]).
  70. %% @doc Filter the input using one of multiple filters.
  71. %%
  72. %% For an input to be considered valid output on of the filters specified
  73. %% in the list must hold for the input event. The list is expected to be
  74. %% a non-empty list. If the list of filters is an empty list a `badarg'
  75. %% error will be thrown.
  76. -spec any([op()]) -> op().
  77. any([_|_]=Conds) ->
  78. {any, Conds};
  79. any(Other) ->
  80. erlang:error(badarg, [Other]).
  81. %% @doc Always return `true' or `false'.
  82. -spec null(boolean()) -> op().
  83. null(Result) when is_boolean(Result) ->
  84. {null, Result};
  85. null(Result) ->
  86. erlang:error(badarg, [Result]).
  87. %% @doc Apply a function to each output of a query.
  88. %%
  89. %% Updating the output action of a query finalizes it. Attempting
  90. %% to use a finalized query to construct a new query will result
  91. %% in a `badarg' error.
  92. -spec with(op(), fun((gre:event()) -> term())) -> op().
  93. with(Query, Fun) when is_function(Fun, 1) ->
  94. {with, Query, Fun};
  95. with(Query, Fun) ->
  96. erlang:error(badarg, [Query, Fun]).
  97. %% @doc Return a union of multiple queries.
  98. %%
  99. %% The union of multiple queries is the equivalent of executing multiple
  100. %% queries separately on the same input event. The advantage is that filter
  101. %% conditions that are common to all or some of the queries only need to
  102. %% be tested once.
  103. %%
  104. %% All queries are expected to be valid and have an output action other
  105. %% than the default which is `output'. If these expectations don't hold
  106. %% a `badarg' error will be thrown.
  107. -spec union([op()]) -> op().
  108. union(Queries) ->
  109. case [Query || Query <- Queries, glc_lib:onoutput(Query) =:= output] of
  110. [] -> {union, Queries};
  111. [_|_] -> erlang:error(badarg, [Queries])
  112. end.