A set of utilities for working with the AT Protocol in Elixir.

fix(lexicon): correct a bunch of mistakes in `deflexicon`

ovyerus.com 92be9319 eca88e6e

verified
Changed files
+55 -50
lib
+1 -1
lib/atex/handle.ex
··· 1 1 defmodule Atex.Handle do 2 - @re ~r/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/ 2 + @re ~r/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/ 3 3 4 4 @spec re() :: Regex.t() 5 5 def re, do: @re
+45 -40
lib/atex/lexicon.ex
··· 64 64 def_name, 65 65 %{ 66 66 type: "object", 67 - properties: properties, 68 - required: required 67 + properties: properties 69 68 } = def 70 69 ) do 70 + required = Map.get(def, :required, []) 71 71 nullable = Map.get(def, :nullable, []) 72 72 73 73 properties ··· 114 114 [schema] = 115 115 def_to_schema(nsid, "params", %{ 116 116 type: "object", 117 - required: def.parameters.required, 118 - nullable: [], 117 + required: Map.get(def.parameters, :required, []), 119 118 properties: def.parameters.properties 120 119 }) 121 120 ··· 123 122 end 124 123 125 124 output = 126 - if def.output && def.output.schema do 125 + if def[:output] && def.output[:schema] do 127 126 [schema] = def_to_schema(nsid, "output", def.output.schema) 128 127 schema 129 128 end ··· 139 138 [schema] = 140 139 def_to_schema(nsid, "params", %{ 141 140 type: "object", 142 - required: def.parameters.required, 141 + required: Map.get(def.parameters, :required, []), 143 142 properties: def.parameters.properties 144 143 }) 145 144 ··· 147 146 end 148 147 149 148 output = 150 - if def[:output] && def.output.schema do 149 + if def[:output] && def.output[:schema] do 151 150 [schema] = def_to_schema(nsid, "output", def.output.schema) 152 151 schema 153 152 end 154 153 155 154 input = 156 - if def[:input] && def.input.schema do 157 - [schema] = def_to_schema(nsid, "output", def.input.schema) 155 + if def[:input] && def.input[:schema] do 156 + [schema] = def_to_schema(nsid, "input", def.input.schema) 158 157 schema 159 158 end 160 159 ··· 168 167 [schema] = 169 168 def_to_schema(nsid, "params", %{ 170 169 type: "object", 171 - required: def.parameters.required, 170 + required: Map.get(def.parameters, :required, []), 172 171 properties: def.parameters.properties 173 172 }) 174 173 ··· 234 233 |> Enum.map(fn {k, v} -> {Recase.to_snake(k), v} end) 235 234 |> then(&{:custom, {Validators.String, :validate, [&1]}}) 236 235 |> maybe_default(field) 237 - |> then( 238 - &{Macro.escape(&1), 239 - quote do 240 - String.t() 241 - end} 242 - ) 243 236 end 237 + |> then( 238 + &{Macro.escape(&1), 239 + quote do 240 + String.t() 241 + end} 242 + ) 244 243 end 245 244 246 245 defp field_to_schema(%{type: "boolean"} = field, _nsid) do ··· 351 350 end 352 351 353 352 defp field_to_schema(%{type: "union", refs: refs}, nsid) do 354 - refs 355 - |> Enum.map(fn ref -> 356 - {nsid, fragment} = 357 - nsid 358 - |> Atex.NSID.expand_possible_fragment_shorthand(ref) 359 - |> Atex.NSID.to_atom_with_fragment() 353 + if refs == [] do 354 + {quote do 355 + {:oneof, []} 356 + end, nil} 357 + else 358 + refs 359 + |> Enum.map(fn ref -> 360 + {nsid, fragment} = 361 + nsid 362 + |> Atex.NSID.expand_possible_fragment_shorthand(ref) 363 + |> Atex.NSID.to_atom_with_fragment() 360 364 361 - {quote do 362 - unquote(nsid).get_schema(unquote(fragment)) 363 - end, 364 - quote do 365 - unquote(nsid).unquote(fragment)() 366 - end} 367 - end) 368 - |> Enum.reduce({[], []}, fn {quoted_schema, quoted_type}, {schemas, types} -> 369 - {[quoted_schema | schemas], [quoted_type | types]} 370 - end) 371 - |> then(fn {schemaa, types} -> 372 - {quote do 373 - {:oneof, unquote(schemaa)} 374 - end, 375 - quote do 376 - unquote(join_with_pipe(types)) 377 - end} 378 - end) 365 + {quote do 366 + unquote(nsid).get_schema(unquote(fragment)) 367 + end, 368 + quote do 369 + unquote(nsid).unquote(fragment)() 370 + end} 371 + end) 372 + |> Enum.reduce({[], []}, fn {quoted_schema, quoted_type}, {schemas, types} -> 373 + {[quoted_schema | schemas], [quoted_type | types]} 374 + end) 375 + |> then(fn {schemas, types} -> 376 + {quote do 377 + {:oneof, unquote(schemas)} 378 + end, 379 + quote do 380 + unquote(join_with_pipe(types)) 381 + end} 382 + end) 383 + end 379 384 end 380 385 381 386 # TODO: apparently should be a data object, not a primitive?
+7 -8
lib/atex/lexicon/schema.ex
··· 3 3 4 4 defschema :lexicon, %{ 5 5 lexicon: {:required, {:literal, 1}}, 6 - id: 7 - {:required, 8 - {:string, 9 - {:regex, 10 - ~r/^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z]([a-zA-Z]{0,61}[a-zA-Z])?)$/}}}, 6 + id: {:required, {:string, {:regex, Atex.NSID.re()}}}, 11 7 revision: {:integer, {:gte, 0}}, 12 8 description: :string, 13 9 defs: { ··· 66 62 defschema :parameters, %{ 67 63 type: {:required, {:literal, "params"}}, 68 64 description: :string, 69 - required: {{:list, :string}, {:default, []}}, 65 + # required: {{:list, :string}, {:default, []}}, 66 + required: {:list, :string}, 70 67 properties: 71 68 {:required, {:map, {:either, {get_schema(:primitive), get_schema(:primitive_array)}}}} 72 69 } ··· 145 142 defschema :object, %{ 146 143 type: {:required, {:literal, "object"}}, 147 144 description: :string, 148 - required: {{:list, :string}, {:default, []}}, 149 - nullable: {{:list, :string}, {:default, []}}, 145 + # required: {{:list, :string}, {:default, []}}, 146 + # nullable: {{:list, :string}, {:default, []}}, 147 + required: {:list, :string}, 148 + nullable: {:list, :string}, 150 149 properties: 151 150 {:required, 152 151 {:map,
+2 -1
lib/atex/nsid.ex
··· 1 1 defmodule Atex.NSID do 2 - @re ~r/^[a-zA-Z](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?:\\.[a-zA-Z](?:[a-zA-Z0-9]{0,62})?)$/ 2 + @re ~r/^[a-zA-Z](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?:\.[a-zA-Z](?:[a-zA-Z0-9]{0,62})?)$/ 3 + # TODO: regex with support for fragment 3 4 4 5 @spec re() :: Regex.t() 5 6 def re, do: @re