Skip to content

Commit

Permalink
selector: add Edgehog.Selector.Filter protocol
Browse files Browse the repository at this point in the history
Allow converting a group filter into an Ash expression

Signed-off-by: Riccardo Binetti <[email protected]>
  • Loading branch information
rbino committed Apr 3, 2024
1 parent 9469c15 commit 1b4a1d0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions backend/lib/edgehog/selector/ast/attribute_filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,11 @@ defmodule Edgehog.Selector.AST.AttributeFilter do
where: compare(cast_json_variant(a.typed_value, unquote(type)), unquote(operator), ^value)
end
end

defimpl Edgehog.Selector.Filter do
# TODO: see https://github.com/edgehog-device-manager/edgehog/issues/486
def to_ash_expr(_attribute_filter) do
raise "TODO"
end
end
end
14 changes: 14 additions & 0 deletions backend/lib/edgehog/selector/ast/binary_op.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@

defmodule Edgehog.Selector.AST.BinaryOp do
defstruct [:operator, :lhs, :rhs]

require Ash.Query

defimpl Edgehog.Selector.Filter do
def to_ash_expr(binary_op) do
lhs = Edgehog.Selector.Filter.to_ash_expr(binary_op.lhs)
rhs = Edgehog.Selector.Filter.to_ash_expr(binary_op.rhs)

case binary_op.operator do
:and -> Ash.Query.expr(^lhs and ^rhs)
:or -> Ash.Query.expr(^lhs or ^rhs)
end
end
end
end
12 changes: 12 additions & 0 deletions backend/lib/edgehog/selector/ast/tag_filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule Edgehog.Selector.AST.TagFilter do
import Ecto.Query
alias Edgehog.Labeling
alias Edgehog.Selector.AST.TagFilter
require Ash.Query

@doc """
Converts a `%TagFilter{}` to a dynamic where clause filtering `Astarte.Device`s that match the
Expand All @@ -51,4 +52,15 @@ defmodule Edgehog.Selector.AST.TagFilter do

{:ok, dynamic}
end

defimpl Edgehog.Selector.Filter do
def to_ash_expr(tag_filter) do
tag_name = tag_filter.tag

case tag_filter.operator do
:in -> Ash.Query.expr(exists(tags, name == ^tag_name))
:not_in -> Ash.Query.expr(not exists(tags, name == ^tag_name))
end
end
end
end
24 changes: 24 additions & 0 deletions backend/lib/edgehog/selector/filter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# This file is part of Edgehog.
#
# Copyright 2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defprotocol Edgehog.Selector.Filter do
@doc "Converts the filter into an `%Ash.Expr{}` matching it"
def to_ash_expr(filter)
end
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
defmodule Edgehog.Selector do
alias Edgehog.Devices
alias Edgehog.Selector.AST.{AttributeFilter, BinaryOp, TagFilter}
alias Edgehog.Selector.Filter
alias Edgehog.Selector.Parser
alias Edgehog.Selector.Parser.Error

Expand Down Expand Up @@ -50,6 +51,18 @@ defmodule Edgehog.Selector do
end
end

@doc """
Translates a selector to an `%Ash.Expr{}` matching all devices matched by the selector.
It accepts the AST root (the Selector must be parsed separately).
Returns `%Ash.Expr{}`.
"""
def to_ash_expr(%node{} = ast_root)
when node in [AttributeFilter, BinaryOp, TagFilter] do
Filter.to_ash_expr(ast_root)
end

@doc """
Parses a selector, returning an AST (or an error). The root node can be one of the structs contained in
the `Edgehog.Selector.AST` namespace.
Expand Down

0 comments on commit 1b4a1d0

Please sign in to comment.