Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/diffo/provider/assigner/assigner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,14 @@ defmodule Diffo.Provider.Assigner do
{_free, updated} =
Map.get_and_update(assignable_value, :free, fn free -> {free - 1, free - 1} end)

{:ok, new_struct} = Ash.Type.cast_input(AssignableValue, Map.from_struct(updated), AssignableValue.subtype_constraints())
new_value = Value.dynamic(AssignableValue, new_struct)
{:ok, new_struct} =
Ash.Type.cast_input(
AssignableValue,
Map.from_struct(updated),
AssignableValue.subtype_constraints()
)

new_value = Value.dynamic(new_struct)

case Diffo.Provider.update_characteristic(characteristic, %{value: new_value}) do
{:ok, _characteristic} ->
Expand All @@ -229,8 +235,14 @@ defmodule Diffo.Provider.Assigner do
{_free, updated} =
Map.get_and_update(assignable_value, :free, fn free -> {free + 1, free + 1} end)

{:ok, new_struct} = Ash.Type.cast_input(AssignableValue, Map.from_struct(updated), AssignableValue.subtype_constraints())
new_value = Value.dynamic(AssignableValue, new_struct)
{:ok, new_struct} =
Ash.Type.cast_input(
AssignableValue,
Map.from_struct(updated),
AssignableValue.subtype_constraints()
)

new_value = Value.dynamic(new_struct)

case Diffo.Provider.update_characteristic(characteristic, %{value: new_value}) do
{:ok, _characteristic} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Diffo.Provider.Instance.Characteristic do

Enum.reduce_while(characteristics, [], fn %{name: name, value_type: value_type}, acc ->
try do
value = Value.dynamic(value_type, struct(value_type))
value = Value.dynamic(struct(value_type))

case Provider.create_characteristic(%{name: name, type: type, value: value}) do
{:ok, result} ->
Expand Down Expand Up @@ -110,7 +110,7 @@ defmodule Diffo.Provider.Instance.Characteristic do
end)

new_value =
Value.dynamic(value_type, struct(value_type, Map.from_struct(updated)))
Value.dynamic(struct(value_type, Map.from_struct(updated)))

[{characteristic, new_value} | acc]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule Diffo.Provider.Instance.Feature do
characteristic_ids =
Enum.reduce_while(characteristics, [], fn %{name: name, value_type: value_type}, acc ->
try do
value = Value.dynamic(value_type, struct(value_type))
value = Value.dynamic(struct(value_type))

case Provider.create_characteristic(%{name: name, value: value, type: :feature}) do
{:ok, result} ->
Expand Down
4 changes: 3 additions & 1 deletion lib/diffo/type/value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ defmodule Diffo.Type.Value do

def primitive(type, value), do: Diffo.Type.Primitive.wrap(type, value)

def dynamic(type, value),
def dynamic(%type{} = dynamic), do: dynamic(type, dynamic)

defp dynamic(type, value),
do: %{type: "dynamic", value: %Diffo.Type.Dynamic{type: type, value: value}}

def wrap(type, value), do: %Ash.Union{type: type, value: value}
Expand Down
4 changes: 2 additions & 2 deletions test/provider/characteristic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ defmodule Diffo.Provider.CharacteristicTest do
updated_characteristic =
characteristic
|> Diffo.Provider.update_characteristic!(%{
value: Value.dynamic(Patch, %Patch{aEnd: 1, zEnd: 42})
value: Value.dynamic(%Patch{aEnd: 1, zEnd: 42})
})

assert Diffo.Unwrap.unwrap(updated_characteristic.value) == %Patch{aEnd: 1, zEnd: 42}
Expand All @@ -224,7 +224,7 @@ defmodule Diffo.Provider.CharacteristicTest do
use Outstand
@port1 %Diffo.Provider.Characteristic{name: "port", value: Value.primitive("integer", 1)}
@port3 %Diffo.Provider.Characteristic{name: "port", value: Value.primitive("integer", 3)}
#@port5 %Diffo.Provider.Characteristic{name: "port", value: Value.primitive("integer", 5)}
# @port5 %Diffo.Provider.Characteristic{name: "port", value: Value.primitive("integer", 5)}
@pair1 %Diffo.Provider.Characteristic{name: "pair", value: Value.primitive("integer", 1)}
@name_only %Diffo.Provider.Characteristic{name: "port"}
# map only
Expand Down
3 changes: 2 additions & 1 deletion test/support/characteristics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ defmodule Diffo.Test.Characteristics do
Enum.each(
expected,
fn {field, expected_value} ->
assert expected_value --- Map.get(Diffo.Unwrap.unwrap(characteristic.value), field) == nil
assert expected_value ---
Map.get(Diffo.Unwrap.unwrap(characteristic.value), field) == nil
end
)

Expand Down
6 changes: 3 additions & 3 deletions test/type/value_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Diffo.Type.ValueTest do

describe "value cast and dump" do
test "cast_input dynamic using Value.dynamic" do
value = Value.dynamic(Patch, %Patch{aEnd: 1, zEnd: 42})
value = Value.dynamic(%Patch{aEnd: 1, zEnd: 42})

assert {:ok, %Ash.Union{type: :dynamic, value: %Dynamic{type: Patch}}} =
Ash.Type.cast_input(Value, value, Value.subtype_constraints())
Expand All @@ -33,7 +33,7 @@ defmodule Diffo.Type.ValueTest do
Ash.Type.cast_input(Value, value, Value.subtype_constraints())
end

@tag bugged: "raw Dynamic struct cast_input requires Value.dynamic/2 wrapper"
@tag bugged: "raw Dynamic struct cast_input requires Value wrapper"
@tag :skip
test "cast_input dynamic" do
value = %Dynamic{type: Patch, value: %Patch{aEnd: 1, zEnd: 42}}
Expand Down Expand Up @@ -92,7 +92,7 @@ defmodule Diffo.Type.ValueTest do
end

test "roundtrip dynamic from Value.dynamic" do
value = Value.dynamic(Patch, %Patch{aEnd: 1, zEnd: 42})
value = Value.dynamic(%Patch{aEnd: 1, zEnd: 42})
{:ok, cast} = Ash.Type.cast_input(Value, value, Value.subtype_constraints())
{:ok, dumped} = Ash.Type.dump_to_native(Value, cast, Value.subtype_constraints())
{:ok, result} = Ash.Type.cast_stored(Value, dumped, Value.subtype_constraints())
Expand Down
Loading