View Source SopsConfigProvider.State (SopsConfigProvider v0.1.1)
Define data structure for state
Link to this section Summary
Functions
Ensures that struct conforms to its t()
type and all preconditions
are fulfilled.
Ensures that struct conforms to its t()
type and all preconditions
are fulfilled.
Creates a struct validating type conformance and preconditions.
Creates a struct validating type conformance and preconditions.
Returns the list of struct's fields having type others then nil
or any()
.
Returns the list of struct's fields defined with explicit types in its t()
type spec.
Link to this section Types
@type available_file_type() :: :json | :yaml | nil
@type t() :: %SopsConfigProvider.State{ app_name: atom(), file_type: available_file_type(), secret_file_path: binary(), sops_binary_path: binary(), sops_content: binary() | nil }
Link to this section Functions
Ensures that struct conforms to its t()
type and all preconditions
are fulfilled.
Returns struct when it's valid in the shape of {:ok, struct}
.
Otherwise returns the error in the shape of {:error, message_by_field}
.
Takes the same options as new/2
.
Useful for struct validation when its fields changed with map syntax
or with Map
module functions.
Options are the same as for new/2
.
examples
Examples
alias SopsConfigProvider.State
struct = State.new!(first_field: value1, second_field: value2, ...)
{:ok, _updated_struct} =
State.ensure_type(%{struct | first_field: new_value})
{:ok, _updated_struct} =
struct
|> Map.put(:first_field, new_value1)
|> Map.put(:second_field, new_value2)
|> State.ensure_type()
Ensures that struct conforms to its t()
type and all preconditions
are fulfilled.
Returns struct when it's valid. Raises an ArgumentError
otherwise.
Useful for struct validation when its fields changed with map syntax
or with Map
module functions.
examples
Examples
alias SopsConfigProvider.State
struct = State.new!(first_field: value1, second_field: value2, ...)
State.ensure_type!(%{struct | first_field: new_value})
struct
|> Map.put(:first_field, new_value1)
|> Map.put(:second_field, new_value2)
|> State.ensure_type!()
Creates a struct validating type conformance and preconditions.
The argument is any Enumerable
that emits two-element tuples
(key-value pairs) during enumeration.
Returns the instance of the struct built from the given enumerable
in the shape of {:ok, struct_value}
. Does so only if struct's
field values conform to its t()
type and all field's type and struct's
type precondition functions return ok.
If conditions described above are not fulfilled, the function
returns an appropriate error in the shape of {:error, message_by_field}
.
message_by_field
is a keyword list where the key is the name of
the field and value is the string with the error message.
Keys in the enumerable
that don't exist in the struct
are automatically discarded.
Options
maybe_filter_precond_errors
- when set totrue
, the values inmessage_by_field
instead of string become a list of error messages from precondition functions. If there are no error messages from precondition functions for a field's type, then all errors are returned unfiltered. Helpful in taking one of the custom errors after executing precondition functions in a deeply nested type to communicate back to the user. F.e. when the field's type is another struct. Default isfalse
.
examples
Examples
alias SopsConfigProvider.State
State.new(first_field: value1, second_field: value2, ...)
Creates a struct validating type conformance and preconditions.
The argument is any Enumerable
that emits two-element tuples
(key-value pairs) during enumeration.
Returns the instance of the struct built from the given enumerable
.
Does so only if struct's field values conform to its t()
type
and all field's type and struct's type precondition functions return ok.
Raises an ArgumentError
if conditions described above are not fulfilled.
This function will check if every given key-value belongs to the struct
and raise KeyError
otherwise.
examples
Examples
alias SopsConfigProvider.State
State.new!(first_field: value1, second_field: value2, ...)
Returns the list of struct's fields having type others then nil
or any()
.
Does not return meta fields with __underscored__
names.
Useful for validation of the required fields for emptiness.
F.e. with validate_required/2
call in the Ecto
changeset.
Options
:include_meta
- when set totrue
, adds fields with__underscored__
names to the return list. Default isfalse
.
Returns the list of struct's fields defined with explicit types in its t()
type spec.
Does not return meta fields with __underscored__
names and fields
having any()
type by default.
Includes fields that have nil
type into the return list.
Options
:include_any_typed
- when set totrue
, adds fields withany()
type to the return list. Default isfalse
.:include_meta
- when set totrue
, adds fields with__underscored__
names to the return list. Default isfalse
.