View Source CharonOauth2.Types.SeparatedStringOrdset (CharonOauth2 v0.0.5)
Ecto type for casting a string with a pattern-separated list of values to an ordered-set string array.
All pattern types of String.split/3
are supported.
Empty strings are trimmed from the result by default.
example
Example
schema "separated_things" do
field :comma_separated, SeparatedStringOrdset, pattern: ","
field :comma_or_semicolon_separated, SeparatedStringOrdset, pattern: ~w(, ;)
field :regex_separated, SeparatedStringOrdset, pattern: ~r/,/
field :comma_separated_with_empty_strings, SeparatedStringOrdset, pattern: ",", split_opts: []
end
doctests
Doctests
@opts SeparatedStringOrdset.init(pattern: ",")
iex> {:ok, ~w(bar baz foo)} = SeparatedStringOrdset.cast("foo,bar,baz,bar", @opts)
iex> :error = SeparatedStringOrdset.cast([1, 2, "3"], @opts)
iex> {:ok, ~w(1 2 true)} = SeparatedStringOrdset.cast("1,2,true", @opts)
iex> {:ok, ~w(a b c)} = SeparatedStringOrdset.cast(~w(a b c), @opts)
iex> opts = SeparatedStringOrdset.init(pattern: ",", split_opts: [])
iex> {:ok, ["", "bar", "foo"]} = SeparatedStringOrdset.cast("foo,bar,", opts)