|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: 2025 diffo contributors <https://github.com/diffo-dev/diffo/graphs.contributors> |
| 3 | +
|
| 4 | +SPDX-License-Identifier: MIT |
| 5 | +--> |
| 6 | + |
| 7 | +# Using Diffo.Type |
| 8 | + |
| 9 | +```elixir |
| 10 | +Mix.install( |
| 11 | + [ |
| 12 | + #{:diffo, "~> 0.2.0"}, |
| 13 | + {:diffo, path: "/Users/beanlanda/git/diffo", override: true} |
| 14 | + ], |
| 15 | + consolidate_protocols: false |
| 16 | +) |
| 17 | +``` |
| 18 | + |
| 19 | +## Overview |
| 20 | + |
| 21 | +`Diffo.Type` provides three complementary types for carrying values on Diffo resources: |
| 22 | + |
| 23 | +* **`Diffo.Type.Primitive`** — a discriminated union of the standard TMF primitive types (string, integer, float, boolean, date, time, datetime, duration) |
| 24 | +* **`Diffo.Type.Dynamic`** — a runtime-typed wrapper for any `Ash.TypedStruct` or map-storage `Ash.Type.NewType` |
| 25 | +* **`Diffo.Type.Value`** — a union of Primitive or Dynamic; the attribute type used by `Diffo.Provider.Characteristic.value` |
| 26 | + |
| 27 | +All three types can also be used as array element types — `{:array, Diffo.Type.Value}`, `{:array, Diffo.Type.Primitive}`, or `{:array, Diffo.Type.Dynamic}` — when an attribute needs to hold multiple values. |
| 28 | + |
| 29 | +These types do not require a Neo4j connection. Everything in this livebook runs in pure Elixir. |
| 30 | + |
| 31 | +```elixir |
| 32 | +alias Diffo.Type.Value |
| 33 | +alias Diffo.Type.Primitive |
| 34 | +alias Diffo.Type.Dynamic |
| 35 | +``` |
| 36 | + |
| 37 | +## Diffo.Unwrap protocol |
| 38 | + |
| 39 | +`Diffo.Unwrap` is a protocol that extracts the underlying Elixir value from Diffo and Ash wrapper types. It is defined with `@fallback_to_any true`, so any value that does not have an explicit implementation is returned as-is. |
| 40 | + |
| 41 | +The protocol is recursive: each implementation calls `Diffo.Unwrap.unwrap/1` on its inner value, so nested wrappers are peeled all the way down to the plain Elixir value in one call. |
| 42 | + |
| 43 | +Built-in implementations: |
| 44 | + |
| 45 | +| Type | Behaviour | |
| 46 | +| ---------------------- | ------------------------------- | |
| 47 | +| `Ash.Union` | delegates to inner `:value` | |
| 48 | +| `Diffo.Type.Primitive` | returns the primitive value | |
| 49 | +| `Diffo.Type.Dynamic` | delegates to inner `:value` | |
| 50 | +| `Ash.CiString` | returns the comparable string | |
| 51 | +| `Ash.NotLoaded` | raises — field was not loaded | |
| 52 | +| `List` | unwraps each element | |
| 53 | +| `Any` | returns the value unchanged | |
| 54 | + |
| 55 | +### Implementing Diffo.Unwrap on your own types |
| 56 | + |
| 57 | +If your domain defines a struct that wraps a value, implement the protocol to make it transparent to Diffo: |
| 58 | + |
| 59 | +```elixir |
| 60 | +defmodule MyApp.Tagged do |
| 61 | + defstruct [:tag, :value] |
| 62 | +end |
| 63 | + |
| 64 | +defimpl Diffo.Unwrap, for: MyApp.Tagged do |
| 65 | + def unwrap(%{value: value}), do: Diffo.Unwrap.unwrap(value) |
| 66 | +end |
| 67 | +``` |
| 68 | + |
| 69 | +Because the implementation calls `Diffo.Unwrap.unwrap/1` on the inner value, nesting works automatically — a `MyApp.Tagged` wrapping a `Diffo.Type.Primitive` unwraps all the way to the raw Elixir value: |
| 70 | + |
| 71 | +```elixir |
| 72 | +tagged = %MyApp.Tagged{tag: "example", value: Primitive.wrap("integer", 7)} |
| 73 | +Diffo.Unwrap.unwrap(tagged) |
| 74 | +``` |
| 75 | + |
| 76 | +## Arrays |
| 77 | + |
| 78 | +All three types work as array element types. `Diffo.Unwrap` handles lists by unwrapping each element, so a stored list of wrapped values reduces to a plain Elixir list in one call. |
| 79 | + |
| 80 | +```elixir |
| 81 | +primitives = [ |
| 82 | + Primitive.wrap("integer", 1), |
| 83 | + Primitive.wrap("integer", 2), |
| 84 | + Primitive.wrap("integer", 3) |
| 85 | +] |
| 86 | + |
| 87 | +Diffo.Unwrap.unwrap(primitives) |
| 88 | +``` |
| 89 | + |
| 90 | +The same applies to `Value` — after a cast roundtrip, unwrapping the list gives back the raw values: |
| 91 | + |
| 92 | +```elixir |
| 93 | +values = [Value.primitive("string", "a"), Value.primitive("string", "b")] |
| 94 | + |
| 95 | +cast_values = |
| 96 | + Enum.map(values, fn v -> |
| 97 | + {:ok, cast} = Ash.Type.cast_input(Value, v, Value.subtype_constraints()) |
| 98 | + cast |
| 99 | + end) |
| 100 | + |
| 101 | +Diffo.Unwrap.unwrap(cast_values) |
| 102 | +``` |
| 103 | + |
| 104 | +## Primitive |
| 105 | + |
| 106 | +`Diffo.Type.Primitive` wraps a single primitive value. Use `wrap/2` to construct one from a type name and a value. Use `Diffo.Unwrap.unwrap/1` to extract the value. |
| 107 | + |
| 108 | +```elixir |
| 109 | +Primitive.wrap("string", "connectivity") |> Diffo.Unwrap.unwrap() |
| 110 | +``` |
| 111 | + |
| 112 | +```elixir |
| 113 | +Primitive.wrap("integer", 42) |> Diffo.Unwrap.unwrap() |
| 114 | +``` |
| 115 | + |
| 116 | +```elixir |
| 117 | +Primitive.wrap("float", 1.5) |> Diffo.Unwrap.unwrap() |
| 118 | +``` |
| 119 | + |
| 120 | +```elixir |
| 121 | +Primitive.wrap("boolean", false) |> Diffo.Unwrap.unwrap() |
| 122 | +``` |
| 123 | + |
| 124 | +### Temporal types |
| 125 | + |
| 126 | +Date, time, datetime, and duration values are converted to ISO 8601 strings internally. This avoids nested serialisation issues when storing through AshNeo4j. |
| 127 | + |
| 128 | +```elixir |
| 129 | +Primitive.wrap("date", ~D[2026-04-24]) |> Diffo.Unwrap.unwrap() |
| 130 | +``` |
| 131 | + |
| 132 | +```elixir |
| 133 | +Primitive.wrap("time", ~T[09:30:00]) |> Diffo.Unwrap.unwrap() |
| 134 | +``` |
| 135 | + |
| 136 | +```elixir |
| 137 | +Primitive.wrap("datetime", ~U[2026-04-24 09:30:00Z]) |> Diffo.Unwrap.unwrap() |
| 138 | +``` |
| 139 | + |
| 140 | +### Unknown types |
| 141 | + |
| 142 | +`wrap/2` returns `nil` for unrecognised type names. |
| 143 | + |
| 144 | +```elixir |
| 145 | +Primitive.wrap("unknown", "x") |
| 146 | +``` |
| 147 | + |
| 148 | +### Cast and dump roundtrip |
| 149 | + |
| 150 | +The Primitive type integrates with the Ash type system. |
| 151 | + |
| 152 | +```elixir |
| 153 | +value = Primitive.wrap("string", "connectivity") |
| 154 | +{:ok, cast} = Ash.Type.cast_input(Primitive, value, Primitive.subtype_constraints()) |
| 155 | +{:ok, dumped} = Ash.Type.dump_to_native(Primitive, cast, Primitive.subtype_constraints()) |
| 156 | +{:ok, result} = Ash.Type.cast_stored(Primitive, dumped, Primitive.subtype_constraints()) |
| 157 | +Diffo.Unwrap.unwrap(result) |
| 158 | +``` |
| 159 | + |
| 160 | +## Value |
| 161 | + |
| 162 | +`Diffo.Type.Value` is the union type used for `Diffo.Provider.Characteristic.value`. Use `Value.primitive/2` and `Value.dynamic/1` to construct values. Stored values are `%Ash.Union{}` structs — use `Diffo.Unwrap.unwrap/1` to extract the underlying Elixir value. |
| 163 | + |
| 164 | +```elixir |
| 165 | +Value.primitive("string", "connectivity") |> Diffo.Unwrap.unwrap() |
| 166 | +``` |
| 167 | + |
| 168 | +```elixir |
| 169 | +Value.primitive("integer", 42) |> Diffo.Unwrap.unwrap() |
| 170 | +``` |
| 171 | + |
| 172 | +### Nil values |
| 173 | + |
| 174 | +Setting a characteristic value to nil is fully supported. The `handle_change/3` override ensures Ash does not wrap nil in the previous member type. |
| 175 | + |
| 176 | +```elixir |
| 177 | +Ash.Type.handle_change(Value, nil, nil, Value.subtype_constraints()) |
| 178 | +``` |
| 179 | + |
| 180 | +```elixir |
| 181 | +old = %Ash.Union{type: :string, value: Primitive.wrap("string", "old")} |
| 182 | +Ash.Type.handle_change(Value, old, nil, Value.subtype_constraints()) |
| 183 | +``` |
| 184 | + |
| 185 | +### Full roundtrip for a primitive Value |
| 186 | + |
| 187 | +```elixir |
| 188 | +value = Value.primitive("float", 3.14) |
| 189 | +{:ok, cast} = Ash.Type.cast_input(Value, value, Value.subtype_constraints()) |
| 190 | +{:ok, dumped} = Ash.Type.dump_to_native(Value, cast, Value.subtype_constraints()) |
| 191 | +{:ok, result} = Ash.Type.cast_stored(Value, dumped, Value.subtype_constraints()) |
| 192 | +Diffo.Unwrap.unwrap(result) |
| 193 | +``` |
| 194 | + |
| 195 | +## Dynamic |
| 196 | + |
| 197 | +`Diffo.Type.Dynamic` carries a value whose type is known only at runtime. The `:type` field is the `Ash.Type.NewType` module; `:value` is the cast value. |
| 198 | + |
| 199 | +Dynamic is limited to types with `storage_type: :map` — `Ash.TypedStruct` and `Ash.Type.NewType` subtypes of `:struct`, `:map`, `:union`, `:keyword`, or `:tuple`. Scalar Ash types such as `Ash.Type.Date` are not supported. |
| 200 | + |
| 201 | +### Defining a typed struct |
| 202 | + |
| 203 | +First, define a struct that will be the dynamic value. In a real application this is defined in your own domain — it does not need to be in Diffo itself. |
| 204 | + |
| 205 | +```elixir |
| 206 | +defmodule MyApp.Patch do |
| 207 | + use Ash.TypedStruct |
| 208 | + |
| 209 | + typed_struct do |
| 210 | + field :a_end, :integer, constraints: [min: 0] |
| 211 | + field :z_end, :integer, constraints: [min: 0] |
| 212 | + end |
| 213 | +end |
| 214 | +``` |
| 215 | + |
| 216 | +### Creating a Dynamic value |
| 217 | + |
| 218 | +```elixir |
| 219 | +dynamic = %Dynamic{type: MyApp.Patch, value: %MyApp.Patch{a_end: 1, z_end: 42}} |
| 220 | +``` |
| 221 | + |
| 222 | +### Cast roundtrip |
| 223 | + |
| 224 | +```elixir |
| 225 | +{:ok, cast} = Ash.Type.cast_input(Dynamic, dynamic, []) |
| 226 | +{:ok, dumped} = Ash.Type.dump_to_native(Dynamic, cast, []) |
| 227 | +{:ok, result} = Ash.Type.cast_stored(Dynamic, dumped, []) |
| 228 | +result |
| 229 | +``` |
| 230 | + |
| 231 | +### Unwrapping |
| 232 | + |
| 233 | +```elixir |
| 234 | +Diffo.Unwrap.unwrap(result) |
| 235 | +``` |
| 236 | + |
| 237 | +### Using Dynamic inside Value |
| 238 | + |
| 239 | +Wrap the dynamic value using `Value.dynamic/1`, then round-trip through the Value union. |
| 240 | + |
| 241 | +```elixir |
| 242 | +value = Value.dynamic(%MyApp.Patch{a_end: 1, z_end: 42}) |
| 243 | +{:ok, cast} = Ash.Type.cast_input(Value, value, Value.subtype_constraints()) |
| 244 | +{:ok, dumped} = Ash.Type.dump_to_native(Value, cast, Value.subtype_constraints()) |
| 245 | +{:ok, result} = Ash.Type.cast_stored(Value, dumped, Value.subtype_constraints()) |
| 246 | +Diffo.Unwrap.unwrap(result) |
| 247 | +``` |
| 248 | + |
| 249 | +### Nil handling |
| 250 | + |
| 251 | +```elixir |
| 252 | +{:ok, nil} = Ash.Type.cast_input(Dynamic, nil, []) |
| 253 | +{:ok, nil} = Ash.Type.dump_to_native(Dynamic, nil, []) |
| 254 | +{:ok, nil} = Ash.Type.cast_stored(Dynamic, nil, []) |
| 255 | +:ok |
| 256 | +``` |
| 257 | + |
| 258 | +### Constraint validation |
| 259 | + |
| 260 | +Dynamic enforces the constraints defined on the inner type during casting. Here `MyApp.Patch` requires both fields to be `>= 0`, so passing a negative value returns an error: |
| 261 | + |
| 262 | +```elixir |
| 263 | +invalid = %Dynamic{type: MyApp.Patch, value: %MyApp.Patch{a_end: -1, z_end: 42}} |
| 264 | +Ash.Type.cast_input(Dynamic, invalid, []) |
| 265 | +``` |
| 266 | + |
| 267 | +A valid value casts successfully: |
| 268 | + |
| 269 | +```elixir |
| 270 | +valid = %Dynamic{type: MyApp.Patch, value: %MyApp.Patch{a_end: 0, z_end: 42}} |
| 271 | +Ash.Type.cast_input(Dynamic, valid, []) |
| 272 | +``` |
| 273 | + |
| 274 | +## Further reading |
| 275 | + |
| 276 | +* [Diffo Livebook](../../diffo.livemd) — full tutorial including Neo4j setup and Provider resources |
| 277 | +* [Using Diffo Provider Instance Extension](./use_diffo_provider_instance_extension.livemd) — defining custom resources with typed characteristics |
0 commit comments