fiction
Conflic Resolution
The rules for conflicting keys are the same as in Figment
| Strategy | Dictionaries | Arrays | All Others |
|---|---|---|---|
join | Union, Recurse | Keep Existing | Keep Existing |
concat_join | Union, Recurse | Concatenate | Keep Existing |
merge | Union, Recurse | Use Incoming | Use Incoming |
concat_merge | Union, Recurse | Concatenate | Use Incoming |
Types
pub type ExtractError {
Provider(msg: String)
Decode(inner: List(decode.DecodeError))
}
Constructors
-
Provider(msg: String) -
Decode(inner: List(decode.DecodeError))
Values
pub fn concat_join(
builder: Builder,
provider: fn() -> Result(List(#(String, Value)), String),
) -> Builder
Add a provider to a builder, keeping existing values and concatenating lists
See the module documentation for details on conflict resolution
Example
import fiction
pub fn main() {
fiction.new()
|> fiction.concat_join(your_cool_provider)
}
fn your_cool_provider() -> Result(List(#(String, fiction.Value), String) {
Ok([#("foo", fiction.List([fiction.String("bar")]))])
}
pub fn concat_merge(
builder: Builder,
provider: fn() -> Result(List(#(String, Value)), String),
) -> Builder
Add a provider to a builder, using incoming values and concatenating lists
See the module documentation for details on conflict resolution
Example
import fiction
pub fn main() {
fiction.new()
|> fiction.concat_merge(your_cool_provider)
}
fn your_cool_provider() -> Result(List(#(String, fiction.Value), String) {
Ok([#("foo", fiction.List([fiction.String("bar")]))])
}
pub fn extract(
builder: Builder,
with decoder: decode.Decoder(a),
) -> Result(a, ExtractError)
Exectute all provider functions and combine the results,
then run the given decoder on the resulting dynamic.properties object
Example
import fiction
import gleam/dynamic/decode
type CoolConfig {
CoolConfig(foo: String)
}
fn cool_config_decoder() {
use foo <- decode.field("foo", decode.string)
decode.success(CoolConfig(foo:))
}
pub fn main() {
let assert Ok(config) =
fiction.new()
|> fiction.join(your_cool_provider)
|> fiction.extract(cool_config_decoder())
assert config.foo == "bar"
}
fn your_cool_provider() -> Result(List(#(String, fiction.Value), String) {
Ok([#("foo", fiction.String("bar"))])
}
pub fn join(
builder: Builder,
provider: fn() -> Result(List(#(String, Value)), String),
) -> Builder
Add a provider to a builder, keeping the existing value in case of a conflict
See the module documentation for details on conflict resolution
Example
import fiction
pub fn main() {
fiction.new()
|> fiction.join(your_cool_provider)
}
fn your_cool_provider() -> Result(List(#(String, fiction.Value), String) {
Ok([#("foo", fiction.String("bar"))])
}
pub fn map(
fun: fn() -> Result(List(#(String, Value)), String),
from from: List(String),
to to: List(String),
) -> fn() -> Result(List(#(String, Value)), String)
Map the results of a provider from one key to another. If mapping to the root (ie. to: []), the mapped value must be a Dict or the created provider will return an error.
If the from keys don’t all exist, the created provider will return an error.
Example
import fiction
import fiction/providers/env
pub fn main() {
let assert Ok(_) =
fiction.new()
|> fiction.join(env.prefixed("APP_") |> fiction.map(from: [], to: ["env"]))
|> fiction.extract(..)
}
pub fn merge(
builder: Builder,
provider: fn() -> Result(List(#(String, Value)), String),
) -> Builder
Add a provider to a builder, using the incoming value in case of a conflict
See the module documentation for details on conflict resolution
Example
import fiction
pub fn main() {
fiction.new()
|> fiction.merge(your_cool_provider)
}
fn your_cool_provider() -> Result(List(#(String, fiction.Value), String) {
Ok([#("foo", fiction.String("bar"))])
}
pub fn to_dynamic(val: Value) -> dynamic.Dynamic