defmodule MyApi.PackageOption do
use Ash.Type.NewType,
subtype_of: :union,
constraints: [
types: [
boolean: [
type: MyApi.PackageOption.Boolean
tag: :type,
tag_value: :boolean
],
string: [
type: MyApi.PackageOption.String
tag: :type,
tag_value: :string
]
]
]
end
defmodule MyApi.PackageOption.Boolean do
use Ash.Resource, data_layer: :embedded
attributes do
uuid_primary_key :id
attribute :key, :ci_string, allow_nil?: false, constraints: [match: ~r/^[a-z0-9-]+$/]
attribute :value, :boolean
attribute :type, :atom, constraints: [one_of: [:boolean]]
attribute :enabled, :boolean, allow_nil?: false
end
identities do
identity :key, [:key]
end
end
defmodule MyApi.PackageOption.String do
use Ash.Resource, data_layer: :embedded
attributes do
uuid_primary_key :id
attribute :key, :ci_string, allow_nil?: false, constraints: [match: ~r/^[a-z0-9-]+$/]
attribute :value, :text
attribute :type, :atom, constraints: [one_of: [:string]]
attribute :enabled, :boolean, allow_nil?: false
end
identities do
identity :key, [:key]
end
end
defmodule MyApi.PackageOption do
use Ash.Type.NewType,
subtype_of: :union,
constraints: [
types: [
boolean: [
type: MyApi.PackageOption.Boolean
tag: :type,
tag_value: :boolean
],
string: [
type: MyApi.PackageOption.String
tag: :type,
tag_value: :string
]
]
]
end
defmodule MyApi.PackageOption.Boolean do
use Ash.Resource, data_layer: :embedded
attributes do
uuid_primary_key :id
attribute :key, :ci_string, allow_nil?: false, constraints: [match: ~r/^[a-z0-9-]+$/]
attribute :value, :boolean
attribute :type, :atom, constraints: [one_of: [:boolean]]
attribute :enabled, :boolean, allow_nil?: false
end
identities do
identity :key, [:key]
end
end
defmodule MyApi.PackageOption.String do
use Ash.Resource, data_layer: :embedded
attributes do
uuid_primary_key :id
attribute :key, :ci_string, allow_nil?: false, constraints: [match: ~r/^[a-z0-9-]+$/]
attribute :value, :text
attribute :type, :atom, constraints: [one_of: [:string]]
attribute :enabled, :boolean, allow_nil?: false
end
identities do
identity :key, [:key]
end
end