fiction

Conflic Resolution

The rules for conflicting keys are the same as in Figment

StrategyDictionariesArraysAll Others
joinUnion, RecurseKeep ExistingKeep Existing
concat_joinUnion, RecurseConcatenateKeep Existing
mergeUnion, RecurseUse IncomingUse Incoming
concat_mergeUnion, RecurseConcatenateUse Incoming

Types

pub opaque type Builder
pub type ExtractError {
  Provider(msg: String)
  Decode(inner: List(decode.DecodeError))
}

Constructors

pub type Provider =
  fn() -> Result(List(#(String, Value)), String)
pub type Value {
  String(String)
  Int(Int)
  Float(Float)
  Bool(Bool)
  List(List(Value))
  Dict(dict.Dict(String, Value))
}

Constructors

  • String(String)
  • Int(Int)
  • Float(Float)
  • Bool(Bool)
  • List(List(Value))
  • Dict(dict.Dict(String, Value))

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 new() -> Builder
pub fn to_dynamic(val: Value) -> dynamic.Dynamic
Search Document