Env.Decode.Pipeline

Config & IO

Env.Decode.Pipeline

Use the (|>) operator to build environment decoders, with the same required / optional / hardcoded / custom / resolve names as Json.Decode.Pipeline.

There are no requiredAt / optionalAt helpers: the process environment is a flat string map, so nested paths do not apply.

Pipeline.optional uses a fallback when the variable is **unset**. That is different from Env.Decode.optional, which returns Maybe.

Decoding variables

@docs required, optional, hardcoded, custom

Ending pipelines

@docs resolve

Values

required

required : String -> Decoder a -> Decoder (a -> b) -> Decoder b

Decode a required environment variable.

Fails when the variable is unset or the leaf decoder rejects the value.

Examples

import Env
import Env.Decode as Decode exposing (Decoder, int, string)
import Env.Decode.Pipeline exposing (required)

type alias Config =
    { host : String
    , port_ : Int
    }

configDecoder : Decoder Config
configDecoder =
    Decode.succeed Config
        |> required "HOST" string
        |> required "PORT" int

result : Result String Config
result =
    Env.decode configDecoder

optional

optional : String -> Decoder a -> a -> Decoder (a -> b) -> Decoder b

Decode an environment variable, or use fallback when it is unset.

A present but invalid value still fails (for example PORT=abc with int). This is Decode.varOr in pipeline form — not Env.Decode.optional.

Examples

import Env
import Env.Decode as Decode exposing (Decoder, int, string)
import Env.Decode.Pipeline exposing (optional, required)

type alias Config =
    { host : String
    , port_ : Int
    }

configDecoder : Decoder Config
configDecoder =
    Decode.succeed Config
        |> required "HOST" string
        |> optional "PORT" int 3000

result : Result String Config
result =
    Env.decode configDecoder

hardcoded

hardcoded : a -> Decoder (a -> b) -> Decoder b

Rather than reading the environment, use a fixed value for the next step in the pipeline.

Examples

import Env
import Env.Decode as Decode exposing (Decoder, int, string)
import Env.Decode.Pipeline exposing (hardcoded, required)

type alias Config =
    { host : String
    , port_ : Int
    , workers : Int
    }

configDecoder : Decoder Config
configDecoder =
    Decode.succeed Config
        |> required "HOST" string
        |> required "PORT" int
        |> hardcoded 4

result : Result String Config
result =
    Env.decode configDecoder

custom

custom : Decoder a -> Decoder (a -> b) -> Decoder b

Run the given decoder and feed its result into the pipeline at this point.

Examples

import Env
import Env.Decode as Decode exposing (Decoder, int, string)
import Env.Decode.Pipeline exposing (custom, required)

type alias Config =
    { host : String
    , port_ : Int
    }

configDecoder : Decoder Config
configDecoder =
    Decode.succeed Config
        |> required "HOST" string
        |> custom (Decode.varOr "PORT" 3000 Decode.int)

result : Result String Config
result =
    Env.decode configDecoder

resolve

resolve : Decoder (Decoder a) -> Decoder a

Convert a Decoder (Decoder a) into a Decoder a. Useful when you want to perform some custom processing just before completing the decoding operation.

Examples

import Env
import Env.Decode as Decode exposing (Decoder, int, string)
import Env.Decode.Pipeline exposing (required, resolve)

type alias Config =
    { host : String
    , port_ : Int
    }

configDecoder : Decoder Config
configDecoder =
    let
        toDecoder : String -> Int -> Decoder Config
        toDecoder host port_ =
            if port_ > 0 then
                Decode.succeed (Config host port_)

            else
                Decode.fail "PORT must be positive"
    in
    Decode.succeed toDecoder
        |> required "HOST" string
        |> required "PORT" int
        |> resolve

result : Result String Config
result =
    Env.decode configDecoder