Skip to content

Commit 880ea36

Browse files
committed
value dynamic 1 added, 2 hidden
1 parent 9994aab commit 880ea36

7 files changed

Lines changed: 29 additions & 14 deletions

File tree

lib/diffo/provider/assigner/assigner.ex

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,14 @@ defmodule Diffo.Provider.Assigner do
208208
{_free, updated} =
209209
Map.get_and_update(assignable_value, :free, fn free -> {free - 1, free - 1} end)
210210

211-
{:ok, new_struct} = Ash.Type.cast_input(AssignableValue, Map.from_struct(updated), AssignableValue.subtype_constraints())
212-
new_value = Value.dynamic(AssignableValue, new_struct)
211+
{:ok, new_struct} =
212+
Ash.Type.cast_input(
213+
AssignableValue,
214+
Map.from_struct(updated),
215+
AssignableValue.subtype_constraints()
216+
)
217+
218+
new_value = Value.dynamic(new_struct)
213219

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

232-
{:ok, new_struct} = Ash.Type.cast_input(AssignableValue, Map.from_struct(updated), AssignableValue.subtype_constraints())
233-
new_value = Value.dynamic(AssignableValue, new_struct)
238+
{:ok, new_struct} =
239+
Ash.Type.cast_input(
240+
AssignableValue,
241+
Map.from_struct(updated),
242+
AssignableValue.subtype_constraints()
243+
)
244+
245+
new_value = Value.dynamic(new_struct)
234246

235247
case Diffo.Provider.update_characteristic(characteristic, %{value: new_value}) do
236248
{:ok, _characteristic} ->

lib/diffo/provider/components/instance/extension/characteristic.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ defmodule Diffo.Provider.Instance.Characteristic do
4848

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

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

112112
new_value =
113-
Value.dynamic(value_type, struct(value_type, Map.from_struct(updated)))
113+
Value.dynamic(struct(value_type, Map.from_struct(updated)))
114114

115115
[{characteristic, new_value} | acc]
116116

lib/diffo/provider/components/instance/extension/feature.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ defmodule Diffo.Provider.Instance.Feature do
5454
characteristic_ids =
5555
Enum.reduce_while(characteristics, [], fn %{name: name, value_type: value_type}, acc ->
5656
try do
57-
value = Value.dynamic(value_type, struct(value_type))
57+
value = Value.dynamic(struct(value_type))
5858

5959
case Provider.create_characteristic(%{name: name, value: value, type: :feature}) do
6060
{:ok, result} ->

lib/diffo/type/value.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ defmodule Diffo.Type.Value do
6767

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

70-
def dynamic(type, value),
70+
def dynamic(%type{} = dynamic), do: dynamic(type, dynamic)
71+
72+
defp dynamic(type, value),
7173
do: %{type: "dynamic", value: %Diffo.Type.Dynamic{type: type, value: value}}
7274

7375
def wrap(type, value), do: %Ash.Union{type: type, value: value}

test/provider/characteristic_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ defmodule Diffo.Provider.CharacteristicTest do
199199
updated_characteristic =
200200
characteristic
201201
|> Diffo.Provider.update_characteristic!(%{
202-
value: Value.dynamic(Patch, %Patch{aEnd: 1, zEnd: 42})
202+
value: Value.dynamic(%Patch{aEnd: 1, zEnd: 42})
203203
})
204204

205205
assert Diffo.Unwrap.unwrap(updated_characteristic.value) == %Patch{aEnd: 1, zEnd: 42}
@@ -224,7 +224,7 @@ defmodule Diffo.Provider.CharacteristicTest do
224224
use Outstand
225225
@port1 %Diffo.Provider.Characteristic{name: "port", value: Value.primitive("integer", 1)}
226226
@port3 %Diffo.Provider.Characteristic{name: "port", value: Value.primitive("integer", 3)}
227-
#@port5 %Diffo.Provider.Characteristic{name: "port", value: Value.primitive("integer", 5)}
227+
# @port5 %Diffo.Provider.Characteristic{name: "port", value: Value.primitive("integer", 5)}
228228
@pair1 %Diffo.Provider.Characteristic{name: "pair", value: Value.primitive("integer", 1)}
229229
@name_only %Diffo.Provider.Characteristic{name: "port"}
230230
# map only

test/support/characteristics.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ defmodule Diffo.Test.Characteristics do
2525
Enum.each(
2626
expected,
2727
fn {field, expected_value} ->
28-
assert expected_value --- Map.get(Diffo.Unwrap.unwrap(characteristic.value), field) == nil
28+
assert expected_value ---
29+
Map.get(Diffo.Unwrap.unwrap(characteristic.value), field) == nil
2930
end
3031
)
3132

test/type/value_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule Diffo.Type.ValueTest do
1313

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

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

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

9494
test "roundtrip dynamic from Value.dynamic" do
95-
value = Value.dynamic(Patch, %Patch{aEnd: 1, zEnd: 42})
95+
value = Value.dynamic(%Patch{aEnd: 1, zEnd: 42})
9696
{:ok, cast} = Ash.Type.cast_input(Value, value, Value.subtype_constraints())
9797
{:ok, dumped} = Ash.Type.dump_to_native(Value, cast, Value.subtype_constraints())
9898
{:ok, result} = Ash.Type.cast_stored(Value, dumped, Value.subtype_constraints())

0 commit comments

Comments
 (0)