diff --git a/README.md b/README.md index 2df815b..cb0b03b 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/lib/kindling/converter.ex b/lib/kindling/converter.ex index 2e20235..92bf218 100644 --- a/lib/kindling/converter.ex +++ b/lib/kindling/converter.ex @@ -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} -> diff --git a/lib/kindling/embed_template.eex b/lib/kindling/embed_template.eex index 003c833..fa0a88b 100644 --- a/lib/kindling/embed_template.eex +++ b/lib/kindling/embed_template.eex @@ -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 diff --git a/lib/kindling/schema.ex b/lib/kindling/schema.ex index 5a331fa..28bff77 100644 --- a/lib/kindling/schema.ex +++ b/lib/kindling/schema.ex @@ -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 diff --git a/lib/kindling/template.eex b/lib/kindling/template.eex index 51b060b..0d27bf9 100644 --- a/lib/kindling/template.eex +++ b/lib/kindling/template.eex @@ -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 diff --git a/mix.exs b/mix.exs index 34970a0..2d0aa43 100644 --- a/mix.exs +++ b/mix.exs @@ -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,9 +17,11 @@ defmodule Kindling.MixProject do ], # Hex - licenses: ["MIT"], - links: %{ - "source" => "https://gitlab.com/mythic-insight/kindling" + package: %{ + licenses: ["MIT"], + links: %{ + "source" => "https://gitlab.com/mythic-insight/kindling" + } } ] end