fix: Properly case enum values on cast
This commit is contained in:
parent
dee77ae424
commit
a620037f18
6 changed files with 34 additions and 13 deletions
|
@ -9,7 +9,7 @@ Kindling can be installed by adding `kindling` to your list of dependencies in `
|
||||||
```elixir
|
```elixir
|
||||||
def deps do
|
def deps do
|
||||||
[
|
[
|
||||||
{:kindling, "~> 0.1.0"}
|
{:kindling, "~> 1.0.1"}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
|
@ -10,7 +10,8 @@ defmodule Kindling.Converter do
|
||||||
the module namespace where your resource schema module have been generated (e.g. `FHIR.R4`)
|
the module namespace where your resource schema module have been generated (e.g. `FHIR.R4`)
|
||||||
and `resource_json` is the map of data.
|
and `resource_json` is the map of data.
|
||||||
"""
|
"""
|
||||||
@spec convert(atom() | binary(), map()) :: Kindling.Schema.Resource.schema()
|
@spec convert(atom() | binary(), map() | binary()) ::
|
||||||
|
Kindling.Schema.Resource.schema() | binary()
|
||||||
def convert(version_namespace, %{"resourceType" => resource_type} = resource_json) do
|
def convert(version_namespace, %{"resourceType" => resource_type} = resource_json) do
|
||||||
resource_module = Module.concat(version_namespace, Resource.class_name(resource_type))
|
resource_module = Module.concat(version_namespace, Resource.class_name(resource_type))
|
||||||
resource_list_module = Module.concat(version_namespace, "ResourceList")
|
resource_list_module = Module.concat(version_namespace, "ResourceList")
|
||||||
|
@ -18,6 +19,10 @@ defmodule Kindling.Converter do
|
||||||
structify(resource_module, resource_list_module, resource_json)
|
structify(resource_module, resource_list_module, resource_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def convert(_version_namespace, string) when is_binary(string) do
|
||||||
|
string
|
||||||
|
end
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
def structify(
|
def structify(
|
||||||
resource_module,
|
resource_module,
|
||||||
|
@ -108,6 +113,18 @@ defmodule Kindling.Converter do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp do_cast_field({:parameterized, Ecto.Enum, _} = type, value) do
|
||||||
|
value = Recase.to_snake(value)
|
||||||
|
|
||||||
|
case Ecto.Type.cast(type, value) do
|
||||||
|
{:ok, v} ->
|
||||||
|
v
|
||||||
|
|
||||||
|
other ->
|
||||||
|
other
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp do_cast_field(type, value) do
|
defp do_cast_field(type, value) do
|
||||||
case Ecto.Type.cast(type, value) do
|
case Ecto.Type.cast(type, value) do
|
||||||
{:ok, v} ->
|
{:ok, v} ->
|
||||||
|
|
|
@ -37,8 +37,11 @@ defmodule <%= @namespace %>.<%= @version %>.<%= class_name(@resource_name) %> do
|
||||||
|
|
||||||
<%= for {name, choices} <- @choices do %>
|
<%= for {name, choices} <- @choices do %>
|
||||||
def choices(<%= inspect(name) %>) do
|
def choices(<%= inspect(name) %>) do
|
||||||
[<%= Enum.map_join(choices, ", ", &":#{&1}") %>]
|
[<%= Enum.map_join(choices, ", ", &":#{name}_#{Recase.to_snake(&1)}") %>]
|
||||||
end
|
end
|
||||||
|
<%= for choice <- choices do %>
|
||||||
|
def choices(<%= inspect("#{name}#{choice}") %>), do: :error
|
||||||
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
def choices(_), do: nil
|
def choices(_), do: nil
|
||||||
|
|
|
@ -90,13 +90,8 @@ defmodule Kindling.Schema do
|
||||||
other
|
other
|
||||||
end
|
end
|
||||||
|
|
||||||
field_names =
|
|
||||||
Enum.map(types_list, fn type ->
|
|
||||||
"#{field_name}_#{Recase.to_snake(type)}"
|
|
||||||
end)
|
|
||||||
|
|
||||||
existing_choices = Map.get(type, "__choices", %{})
|
existing_choices = Map.get(type, "__choices", %{})
|
||||||
new_choices = Map.put(existing_choices, field_name, field_names)
|
new_choices = Map.put(existing_choices, field_name, types_list)
|
||||||
|
|
||||||
put_in(schema, ["definitions", type_name, "__choices"], new_choices)
|
put_in(schema, ["definitions", type_name, "__choices"], new_choices)
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,8 +41,11 @@ defmodule <%= @namespace %>.<%= @version %>.<%= class_name(@resource_name) %> do
|
||||||
|
|
||||||
<%= for {name, choices} <- @choices do %>
|
<%= for {name, choices} <- @choices do %>
|
||||||
def choices(<%= inspect(name) %>) do
|
def choices(<%= inspect(name) %>) do
|
||||||
[<%= Enum.map_join(choices, ", ", &":#{&1}") %>]
|
[<%= Enum.map_join(choices, ", ", &":#{name}_#{Recase.to_snake(&1)}") %>]
|
||||||
end
|
end
|
||||||
|
<%= for choice <- choices do %>
|
||||||
|
def choices(<%= inspect("#{name}#{choice}") %>), do: :error
|
||||||
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
def choices(_), do: nil
|
def choices(_), do: nil
|
||||||
|
|
9
mix.exs
9
mix.exs
|
@ -8,6 +8,7 @@ defmodule Kindling.MixProject do
|
||||||
elixir: "~> 1.15",
|
elixir: "~> 1.15",
|
||||||
start_permanent: Mix.env() == :prod,
|
start_permanent: Mix.env() == :prod,
|
||||||
deps: deps(),
|
deps: deps(),
|
||||||
|
description: "Resource generators and API clients for HL7 FHIR.",
|
||||||
|
|
||||||
# Docs
|
# Docs
|
||||||
source_url: "https://gitlab.com/mythic-insight/kindling",
|
source_url: "https://gitlab.com/mythic-insight/kindling",
|
||||||
|
@ -16,9 +17,11 @@ defmodule Kindling.MixProject do
|
||||||
],
|
],
|
||||||
|
|
||||||
# Hex
|
# Hex
|
||||||
licenses: ["MIT"],
|
package: %{
|
||||||
links: %{
|
licenses: ["MIT"],
|
||||||
"source" => "https://gitlab.com/mythic-insight/kindling"
|
links: %{
|
||||||
|
"source" => "https://gitlab.com/mythic-insight/kindling"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue