Conversation
jonatanklosko
left a comment
There was a problem hiding this comment.
Looks good to me! I dropped one suggestion inline :)
| params = Utils.stringify_keys(params) | ||
| existing_config = Utils.stringify_keys(existing_config) | ||
|
|
||
| Map.merge(existing_config, params) | ||
| |> mod.cast_config() | ||
| |> mod.validate_config() |
There was a problem hiding this comment.
For completeness, you could avoid the stringification and merging here. Currently you have:
logflare/lib/logflare/backends/adaptor/webhook_adaptor.ex
Lines 46 to 52 in 4b84eb5
You could change those to accept the existing value and use that instead of the hardcoded %{}:
@impl Logflare.Backends.Adaptor
def cast_config(existing_config, params) do
{existing_config, %{url: :string, headers: :map, http: :string, gzip: :boolean}}
|> Ecto.Changeset.cast(params, [:url, :headers, :http, :gzip])
|> Logflare.Utils.default_field_value(:http, "http2")
|> Logflare.Utils.default_field_value(:gzip, true)
endThen here you would pass both config and params and it should just work. You can always pass %{} as existing params, when creating from scratch. This is similar to usual schema changeset function that accepts the struct and params.
amokan
left a comment
There was a problem hiding this comment.
Looks good if making the change that Jonatan suggested 👍
| nil -> | ||
| changeset | ||
|
|
||
| %{valid?: false, errors: errors} = changeset -> |
There was a problem hiding this comment.
Minor nit. Rebinding changeset here could make things flaky if there was downstream code that inspected Backend-specific fields (doesn't happen today) and expected this to represent a Backend changeset.
Could avoid that by not capturing the failed changeset doing something like:
%{valid?: false, errors: errors} ->
Enum.reduce(errors, changeset, fn {key, {msg, opts}}, cs ->
Changeset.add_error(cs, :"config.#{key}", msg, opts)
end)
Allows partial updating of a backend config. To accomodate PATCH on nested config values.
Part of O11Y-1578