fix: Properly case enum values on cast

This commit is contained in:
Robert Prehn 2024-08-09 12:59:58 -05:00
parent dee77ae424
commit a620037f18
No known key found for this signature in database
6 changed files with 34 additions and 13 deletions

View file

@ -9,7 +9,7 @@ Kindling can be installed by adding `kindling` to your list of dependencies in `
```elixir
def deps do
[
{:kindling, "~> 0.1.0"}
{:kindling, "~> 1.0.1"}
]
end
```

View file

@ -10,7 +10,8 @@ defmodule Kindling.Converter do
the module namespace where your resource schema module have been generated (e.g. `FHIR.R4`)
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
resource_module = Module.concat(version_namespace, Resource.class_name(resource_type))
resource_list_module = Module.concat(version_namespace, "ResourceList")
@ -18,6 +19,10 @@ defmodule Kindling.Converter do
structify(resource_module, resource_list_module, resource_json)
end
def convert(_version_namespace, string) when is_binary(string) do
string
end
@doc false
def structify(
resource_module,
@ -108,6 +113,18 @@ defmodule Kindling.Converter do
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
case Ecto.Type.cast(type, value) do
{:ok, v} ->

View file

@ -37,8 +37,11 @@ defmodule <%= @namespace %>.<%= @version %>.<%= class_name(@resource_name) %> do
<%= for {name, choices} <- @choices do %>
def choices(<%= inspect(name) %>) do
[<%= Enum.map_join(choices, ", ", &":#{&1}") %>]
[<%= Enum.map_join(choices, ", ", &":#{name}_#{Recase.to_snake(&1)}") %>]
end
<%= for choice <- choices do %>
def choices(<%= inspect("#{name}#{choice}") %>), do: :error
<% end %>
<% end %>
def choices(_), do: nil

View file

@ -90,13 +90,8 @@ defmodule Kindling.Schema do
other
end
field_names =
Enum.map(types_list, fn type ->
"#{field_name}_#{Recase.to_snake(type)}"
end)
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)
end

View file

@ -41,8 +41,11 @@ defmodule <%= @namespace %>.<%= @version %>.<%= class_name(@resource_name) %> do
<%= for {name, choices} <- @choices do %>
def choices(<%= inspect(name) %>) do
[<%= Enum.map_join(choices, ", ", &":#{&1}") %>]
[<%= Enum.map_join(choices, ", ", &":#{name}_#{Recase.to_snake(&1)}") %>]
end
<%= for choice <- choices do %>
def choices(<%= inspect("#{name}#{choice}") %>), do: :error
<% end %>
<% end %>
def choices(_), do: nil

View file

@ -8,6 +8,7 @@ defmodule Kindling.MixProject do
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
deps: deps(),
description: "Resource generators and API clients for HL7 FHIR.",
# Docs
source_url: "https://gitlab.com/mythic-insight/kindling",
@ -16,10 +17,12 @@ defmodule Kindling.MixProject do
],
# Hex
package: %{
licenses: ["MIT"],
links: %{
"source" => "https://gitlab.com/mythic-insight/kindling"
}
}
]
end