···
|> then(&Recase.Enumerable.atomize_keys/1)
|> then(&Atex.Lexicon.Schema.lexicon!/1)
27
+
lexicon_id = Atex.NSID.to_atom(lexicon.id)
|> Enum.flat_map(fn {def_name, def} -> def_to_schema(lexicon.id, def_name, def) end)
30
-
|> Enum.map(fn {schema_key, quoted_schema, quoted_type} ->
33
+
{schema_key, quoted_schema, quoted_type} -> {schema_key, quoted_schema, quoted_type, nil}
36
+
|> Enum.map(fn {schema_key, quoted_schema, quoted_type, quoted_struct} ->
32
-
if schema_key === :main do
38
+
if schema_key == :main do
@type t() :: unquote(quoted_type)
45
+
if schema_key == :main do
48
+
nested_module_name =
50
+
|> Recase.to_pascal()
54
+
defmodule unquote({:__aliases__, [alias: false], [nested_module_name]}) do
55
+
unquote(quoted_struct)
···
defschema unquote(schema_key), unquote(quoted_schema)
47
-
def id, do: unquote(Atex.NSID.to_atom(lexicon.id))
72
+
def id, do: unquote(lexicon_id)
74
+
unquote_splicing(defs)
49
-
unquote_splicing(defs)
77
+
if lexicon.id == "app.bsky.feed.post" do
79
+
foo |> Macro.expand(__ENV__) |> Macro.to_string() |> IO.puts()
85
+
# For records and objects:
86
+
# - [x] `main` is in core module, otherwise nested with its name (should probably be handled above instead of in `def_to_schema`, like expanding typespecs)
87
+
# - [x] Define all keys in the schema, `@enforce`ing non-nullable/required fields
88
+
# - [x] `$type` field with the full NSID
89
+
# - [x] Custom JSON encoder function that omits optional fields that are `nil`, due to different semantics
90
+
# - [ ] Add `$type` to schema but make it optional - allowing unbranded types through, but mismatching brand will fail.
91
+
# - [ ] `t()` type should be the struct in it. (add to non-main structs too?)
@spec def_to_schema(nsid :: String.t(), def_name :: String.t(), lexicon_def :: map()) ::
54
-
list({key :: atom(), quoted_schema :: term(), quoted_type :: term()})
97
+
quoted_schema :: term(),
98
+
quoted_type :: term()
102
+
quoted_schema :: term(),
103
+
quoted_type :: term(),
104
+
quoted_struct :: term()
defp def_to_schema(nsid, def_name, %{type: "record", record: record}) do
# TODO: record rkey format validator
···
required = Map.get(def, :required, [])
nullable = Map.get(def, :nullable, [])
74
-
|> Enum.map(fn {key, field} ->
75
-
{quoted_schema, quoted_type} = field_to_schema(field, nsid)
76
-
is_nullable = key in nullable
77
-
is_required = key in required
125
+
{quoted_schemas, quoted_types} =
127
+
|> Enum.map(fn {key, field} ->
128
+
{quoted_schema, quoted_type} = field_to_schema(field, nsid)
129
+
string_key = to_string(key)
130
+
is_nullable = string_key in nullable
131
+
is_required = string_key in required
136
+
&if is_nullable, do: quote(do: {:either, {{:literal, nil}, unquote(&1)}}), else: &1
138
+
|> then(&if is_required, do: quote(do: {:required, unquote(&1)}), else: &1)
139
+
|> then(&{key, &1})
141
+
key_type = if is_required, do: :required, else: :optional
147
+
{:|, [], [&1, nil]}
152
+
|> then(&{{key_type, [], [key]}, &1})
154
+
{quoted_schema, quoted_type}
156
+
|> Enum.reduce({[], []}, fn {quoted_schema, quoted_type}, {schemas, types} ->
157
+
{[quoted_schema | schemas], [quoted_type | types]}
161
+
Enum.map(properties, fn
162
+
{key, %{default: default}} -> {key, default}
163
+
{key, _field} -> {key, nil}
164
+
end) ++ [{:"$type", if(def_name == :main, do: nsid, else: "#{nsid}##{def_name}")}]
82
-
&if is_nullable, do: quote(do: {:either, {{:literal, nil}, unquote(&1)}}), else: &1
84
-
|> then(&if is_required, do: quote(do: {:required, unquote(&1)}), else: &1)
166
+
enforced_keys = properties |> Map.keys() |> Enum.filter(&(to_string(&1) in required))
87
-
key_type = if is_required, do: :required, else: :optional
168
+
optional_if_nil_keys =
171
+
|> Enum.filter(fn key ->
172
+
key = to_string(key)
173
+
# TODO: what if it is nullable but not required?
174
+
key not in required && key not in nullable
179
+
@enforce_keys unquote(enforced_keys)
180
+
defstruct unquote(struct_keys)
182
+
defimpl JSON.Encoder do
183
+
@optional_if_nil_keys unquote(optional_if_nil_keys)
185
+
def encode(value, encoder) do
187
+
|> Map.from_struct()
188
+
|> Enum.reject(fn {k, v} -> k in @optional_if_nil_keys && v == nil end)
190
+
|> Jason.Encoder.encode(encoder)
98
-
|> then(&{{key_type, [], [key]}, &1})
100
-
{quoted_schema, quoted_type}
102
-
|> Enum.reduce({[], []}, fn {quoted_schema, quoted_type}, {schemas, types} ->
103
-
{[quoted_schema | schemas], [quoted_type | types]}
105
-
|> then(fn {quoted_schemas, quoted_types} ->
106
-
[{atomise(def_name), {:%{}, [], quoted_schemas}, {:%{}, [], quoted_types}}]
194
+
defimpl Jason.Encoder do
195
+
@optional_if_nil_keys unquote(optional_if_nil_keys)
197
+
def encode(value, options) do
199
+
|> Map.from_struct()
200
+
|> Enum.reject(fn {k, v} -> k in @optional_if_nil_keys && v == nil end)
202
+
|> Jason.Encode.map(options)
207
+
[{atomise(def_name), {:%{}, [], quoted_schemas}, {:%{}, [], quoted_types}, quoted_struct}]
# TODO: validating errors?
···
|> Enum.map(fn {k, v} -> {Recase.to_snake(k), v} end)
234
-
|> then(&{:custom, {Validators.String, :validate, [&1]}})
334
+
|> Validators.string()
···
|> Map.take([:maximum, :minimum])
265
-
|> then(&{:custom, {Validators.Integer, [&1]}})
365
+
|> Validators.integer()
···
|> Enum.map(fn {k, v} -> {Recase.to_snake(k), v} end)
|> then(&Validators.array(inner_schema, &1))
387
+
# TODO: we should be able to unquote this now...
# Can't unquote the inner_schema beforehand as that would risk evaluating `get_schema`s which don't exist yet.
# There's probably a better way to do this lol.
|> then(fn {:custom, {:{}, c, [Validators.Array, :validate, [quoted_inner_schema | args]]}} ->
···
|> Atex.NSID.expand_possible_fragment_shorthand(ref)
|> Atex.NSID.to_atom_with_fragment()
345
-
unquote(nsid).get_schema(unquote(fragment))
348
-
unquote(nsid).unquote(fragment)()
446
+
Macro.escape(Validators.lazy_ref(nsid, fragment)),
448
+
unquote(nsid).unquote(fragment)()
defp field_to_schema(%{type: "union", refs: refs}, nsid) do
···
|> Atex.NSID.expand_possible_fragment_shorthand(ref)
|> Atex.NSID.to_atom_with_fragment()
366
-
unquote(nsid).get_schema(unquote(fragment))
369
-
unquote(nsid).unquote(fragment)()
467
+
Macro.escape(Validators.lazy_ref(nsid, fragment)),
469
+
unquote(nsid).unquote(fragment)()
|> Enum.reduce({[], []}, fn {quoted_schema, quoted_type}, {schemas, types} ->
{[quoted_schema | schemas], [quoted_type | types]}