-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validate options with NimbleOptions
- Loading branch information
Showing
7 changed files
with
108 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,56 @@ | ||
defmodule PlugLocale.Header.Config do | ||
@moduledoc false | ||
|
||
defstruct [ | ||
:default_locale, | ||
:locales, | ||
:cast_locale_by, | ||
:header_name, | ||
:assign_key | ||
] | ||
|
||
@doc false | ||
def new!(opts) when is_list(opts) do | ||
default_locale = Keyword.get(opts, :default_locale) | ||
locales = Keyword.get(opts, :locales, []) | ||
cast_locale_by = Keyword.get(opts, :cast_locale_by, nil) | ||
header_name = Keyword.get(opts, :header_name, "x-client-locale") | ||
assign_key = Keyword.get(opts, :assign_key, :locale) | ||
|
||
[ | ||
default_locale: default_locale, | ||
locales: locales, | ||
cast_locale_by: cast_locale_by, | ||
header_name: header_name, | ||
assign_key: assign_key | ||
@opts_schema [ | ||
default_locale: [ | ||
type: :string, | ||
required: true | ||
], | ||
locales: [ | ||
type: {:list, :string}, | ||
default: [] | ||
], | ||
cast_locale_by: [ | ||
type: {:or, [nil, {:fun, 1}]}, | ||
default: nil | ||
], | ||
header_name: [ | ||
type: :string, | ||
default: "x-client-locale" | ||
], | ||
assign_key: [ | ||
type: :atom, | ||
default: :locale | ||
] | ||
|> as_map!() | ||
|> as_struct!() | ||
end | ||
|
||
defp validate!(opts) do | ||
keys = Keyword.keys(opts) | ||
unset_keys = Enum.filter(keys, fn key -> Keyword.get(opts, key) == nil end) | ||
] | ||
|
||
if unset_keys != [] do | ||
raise RuntimeError, "following keys #{inspect(unset_keys)} should be set" | ||
end | ||
defstruct Keyword.keys(@opts_schema) | ||
|
||
@doc false | ||
def new!(opts) do | ||
opts | ||
|> NimbleOptions.validate!(@opts_schema) | ||
|> finalize() | ||
|> as_struct!() | ||
end | ||
|
||
defp as_map!(opts) do | ||
defp finalize(opts) do | ||
default_locale = Keyword.fetch!(opts, :default_locale) | ||
locales = Keyword.fetch!(opts, :locales) | ||
cast_locale_by = Keyword.fetch!(opts, :cast_locale_by) | ||
header_name = Keyword.fetch!(opts, :header_name) | ||
assign_key = Keyword.fetch!(opts, :assign_key) | ||
locales = Enum.uniq([default_locale | locales]) | ||
|
||
%{ | ||
default_locale: default_locale, | ||
locales: Enum.uniq([default_locale | locales]), | ||
cast_locale_by: cast_locale_by, | ||
header_name: header_name, | ||
assign_key: assign_key | ||
} | ||
Keyword.merge(opts, locales: locales) | ||
end | ||
|
||
defp as_struct!(config) do | ||
defp as_struct!(opts) do | ||
default_struct = __MODULE__.__struct__() | ||
valid_keys = Map.keys(default_struct) | ||
config = Map.take(config, valid_keys) | ||
|
||
config = | ||
opts | ||
|> Enum.into(%{}) | ||
|> Map.take(valid_keys) | ||
|
||
Map.merge(default_struct, config) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,74 @@ | ||
defmodule PlugLocale.WebBrowser.Config do | ||
@moduledoc false | ||
|
||
defstruct [ | ||
:default_locale, | ||
:locales, | ||
:detect_locale_from, | ||
:cast_locale_by, | ||
:route_identifier, | ||
:path_param_key, | ||
:assign_key, | ||
:query_key, | ||
:cookie_key | ||
] | ||
|
||
@doc false | ||
def new!(opts) when is_list(opts) do | ||
default_locale = Keyword.get(opts, :default_locale) | ||
locales = Keyword.get(opts, :locales, []) | ||
|
||
detect_locale_from = | ||
Keyword.get(opts, :detect_locale_from, [:cookie, :referrer, :accept_language]) | ||
|
||
cast_locale_by = Keyword.get(opts, :cast_locale_by, nil) | ||
route_identifier = Keyword.get(opts, :route_identifier, :locale) | ||
assign_key = Keyword.get(opts, :assign_key, route_identifier) | ||
query_key = Keyword.get(opts, :query_key, to_string(route_identifier)) | ||
cookie_key = Keyword.get(opts, :cookie_key, "preferred_locale") | ||
|
||
[ | ||
default_locale: default_locale, | ||
locales: locales, | ||
detect_locale_from: detect_locale_from, | ||
cast_locale_by: cast_locale_by, | ||
route_identifier: route_identifier, | ||
assign_key: assign_key, | ||
query_key: query_key, | ||
cookie_key: cookie_key | ||
@opts_schema [ | ||
default_locale: [ | ||
type: :string, | ||
required: true | ||
], | ||
locales: [ | ||
type: {:list, :string}, | ||
default: [] | ||
], | ||
detect_locale_from: [ | ||
type: {:list, :atom}, | ||
default: [:cookie, :referrer, :accept_language] | ||
], | ||
cast_locale_by: [ | ||
type: {:or, [nil, {:fun, 1}]}, | ||
default: nil | ||
], | ||
route_identifier: [ | ||
type: :atom, | ||
default: :locale | ||
], | ||
assign_key: [ | ||
type: :atom, | ||
default: :locale | ||
], | ||
query_key: [ | ||
type: :string, | ||
default: "locale" | ||
], | ||
cookie_key: [ | ||
type: :string, | ||
default: "locale" | ||
] | ||
|> as_map!() | ||
|> as_struct!() | ||
end | ||
] | ||
|
||
defp validate!(opts) do | ||
keys = Keyword.keys(opts) | ||
unset_keys = Enum.filter(keys, fn key -> Keyword.get(opts, key) == nil end) | ||
@direct_fields Keyword.keys(@opts_schema) | ||
@derived_fields [:path_param_key] | ||
|
||
if unset_keys != [] do | ||
raise RuntimeError, "following keys #{inspect(unset_keys)} should be set" | ||
end | ||
defstruct @direct_fields ++ @derived_fields | ||
|
||
@doc false | ||
def new!(opts) do | ||
opts | ||
|> NimbleOptions.validate!(@opts_schema) | ||
|> finalize() | ||
|> as_struct!() | ||
end | ||
|
||
defp as_map!(opts) do | ||
defp finalize(opts) do | ||
default_locale = Keyword.fetch!(opts, :default_locale) | ||
locales = Keyword.fetch!(opts, :locales) | ||
detect_locale_from = Keyword.fetch!(opts, :detect_locale_from) | ||
cast_locale_by = Keyword.fetch!(opts, :cast_locale_by) | ||
locales = Enum.uniq([default_locale | locales]) | ||
|
||
route_identifier = Keyword.fetch!(opts, :route_identifier) | ||
assign_key = Keyword.fetch!(opts, :assign_key) | ||
query_key = Keyword.fetch!(opts, :query_key) | ||
cookie_key = Keyword.fetch!(opts, :cookie_key) | ||
path_param_key = to_string(route_identifier) | ||
|
||
%{ | ||
default_locale: default_locale, | ||
locales: Enum.uniq([default_locale | locales]), | ||
detect_locale_from: detect_locale_from, | ||
cast_locale_by: cast_locale_by, | ||
route_identifier: route_identifier, | ||
path_param_key: to_string(route_identifier), | ||
assign_key: assign_key, | ||
query_key: query_key, | ||
cookie_key: cookie_key | ||
} | ||
Keyword.merge(opts, locales: locales, path_param_key: path_param_key) | ||
end | ||
|
||
defp as_struct!(config) do | ||
defp as_struct!(opts) do | ||
default_struct = __MODULE__.__struct__() | ||
valid_keys = Map.keys(default_struct) | ||
config = Map.take(config, valid_keys) | ||
|
||
config = | ||
opts | ||
|> Enum.into(%{}) | ||
|> Map.take(valid_keys) | ||
|
||
Map.merge(default_struct, config) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters