Current section

Files

Jump to
diffo lib diffo provider components feature.ex
Raw

lib/diffo/provider/components/feature.ex

# SPDX-FileCopyrightText: 2025 diffo contributors <https://github.com/diffo-dev/diffo/graphs.contributors>
#
# SPDX-License-Identifier: MIT
defmodule Diffo.Provider.Feature do
@moduledoc """
Diffo - TMF Service and Resource Management with a difference
Feature - Ash Resource for a TMF Feature
"""
use Ash.Resource,
otp_app: :diffo,
domain: Diffo.Provider,
data_layer: AshNeo4j.DataLayer,
extensions: [AshOutstanding.Resource, AshJason.Resource]
resource do
description "An Ash Resource for a TMF Feature"
plural_name :features
end
neo4j do
relate [
{:instance, :HAS, :incoming, :Instance},
{:characteristics, :HAS, :outgoing, :Characteristic}
]
guard [
{:HAS, :incoming, :Instance}
]
end
jason do
pick [:name, :isEnabled, :characteristics]
customize fn result, _record ->
result
|> Diffo.Util.suppress_rename(:characteristics, :featureCharacteristic)
end
end
outstanding do
expect [:name, :isEnabled, :characteristics]
end
actions do
defaults [:read, :destroy]
create :create do
description "creates a feature, optionally with related characteristic"
accept [:name, :isEnabled]
argument :characteristics, {:array, :uuid}
change manage_relationship(:characteristics, type: :append)
end
read :find_by_name do
description "finds features by name"
get? false
argument :query, :ci_string do
description "Return only features with names including the given value."
end
filter expr(contains(name, ^arg(:query)))
end
read :list do
description "lists all features"
end
update :update do
primary? true
description "updates the feature isEnabled or instance relationship"
accept [:isEnabled]
argument :instance_id, :uuid
change manage_relationship(:instance_id, :instance, type: :append_and_remove)
end
update :relate_characteristics do
description "relates characteristics to the feature"
argument :characteristics, {:array, :uuid}
change manage_relationship(:characteristics, type: :append)
end
update :unrelate_characteristics do
description "unrelates characteristics from the feature"
argument :characteristics, {:array, :uuid}
change manage_relationship(:characteristics, type: :remove)
end
end
attributes do
uuid_primary_key :id do
description "a uuid4, unique to this feature, generated by default"
public? false
end
attribute :name, :atom do
description "the name of this feature"
allow_nil? false
public? true
end
attribute :isEnabled, :boolean do
description "indicates whether the feature is enabled"
public? true
default true
end
create_timestamp :created_at
update_timestamp :updated_at
end
relationships do
belongs_to :instance, Diffo.Provider.Instance do
description "the instance the feature defines"
allow_nil? true
public? true
end
has_many :characteristics, Diffo.Provider.Characteristic do
description "the feature's collection of defining characteristics"
public? true
end
end
identities do
identity :instance_feature_uniqueness, [:instance_id, :name] do
message "instance has duplicate feature"
pre_check? true
end
end
preparations do
prepare build(load: [:characteristics], sort: [name: :asc])
end
@doc """
Compares two feature, by ascending name
## Examples
iex> Diffo.Provider.Feature.compare(%{name: "a"}, %{name: "a"})
:eq
iex> Diffo.Provider.Feature.compare(%{name: "b"}, %{name: "a"})
:gt
iex> Diffo.Provider.Feature.compare(%{name: "a"}, %{name: "b"})
:lt
"""
def compare(%{name: name0}, %{name: name1}), do: Diffo.Util.compare(name0, name1)
end