Fix filter score/weight being converted to string

This commit is contained in:
Shadowfacts 2020-07-18 11:03:14 -04:00
parent 6c9247873f
commit daa0c51db5
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 42 additions and 2 deletions

39
lib/frenzy/json_schema.ex Normal file
View File

@ -0,0 +1,39 @@
defmodule Frenzy.JSONSchema do
@enforce_keys [:value, :schema]
defstruct [:value, :schema]
@type t() :: %__MODULE__{
value: map(),
schema: schema()
}
@type schema() ::
:string
| :boolean
| :integer
| {:list, schema()}
| %{required(String.t()) => schema()}
@spec coerce(schema :: schema(), value :: map() | list() | String.t()) :: map()
def coerce(:string, value) when is_binary(value), do: value
def coerce(:boolean, "true"), do: true
def coerce(:boolean, _), do: false
def coerce(:integer, value) when is_binary(value) do
{res, _} = Integer.parse(value)
res
end
def coerce({:list, list_type}, list_value) when is_list(list_value) do
Enum.map(list_value, &coerce(list_type, &1))
end
def coerce(schema, map_value) when is_map(schema) and is_map(map_value) do
Enum.reduce(schema, %{}, fn {key, value_schema}, acc ->
value = Map.fetch!(map_value, key)
Map.put(acc, key, coerce(value_schema, value))
end)
end
end

View File

@ -1,5 +1,6 @@
defmodule FrenzyWeb.ConfigureStage.FilterStageLive do defmodule FrenzyWeb.ConfigureStage.FilterStageLive do
use FrenzyWeb, :live_component use FrenzyWeb, :live_component
alias Frenzy.JSONSchema
@impl true @impl true
def update(assigns, socket) do def update(assigns, socket) do
@ -12,7 +13,7 @@ defmodule FrenzyWeb.ConfigureStage.FilterStageLive do
new_opts = new_opts =
socket.assigns.opts socket.assigns.opts
|> Map.put("mode", mode) |> Map.put("mode", mode)
|> Map.put("score", score) |> Map.put("score", JSONSchema.coerce(:integer, score))
new_stage = Frenzy.Keypath.set(socket.assigns.stage, socket.assigns.keypath, new_opts) new_stage = Frenzy.Keypath.set(socket.assigns.stage, socket.assigns.keypath, new_opts)
@ -63,7 +64,7 @@ defmodule FrenzyWeb.ConfigureStage.FilterStageLive do
|> Map.put("mode", mode) |> Map.put("mode", mode)
|> Map.put("param", param) |> Map.put("param", param)
|> Map.put("property", property) |> Map.put("property", property)
|> Map.put("weight", weight) |> Map.put("weight", JSONSchema.coerce(:integer, weight))
new_stage = new_stage =
Frenzy.Keypath.set( Frenzy.Keypath.set(