fakemonster/elm-codegen-feature-flags/1.0.0

CodeGen.FeatureFlags

Flags

type Flag

A type setting for a single flag. See the functions below for constructing this type.

Create a feature flag for booleans, with a default value of False. The string parameter here will be your flag name in the generated code, so always provide camelCase.

fromFlags
    [ ( "analyticsV2", bool)
    , ( "showPricingExplainer", bool)
    ]
    |> generate "FeatureFlags"


-- produces a feature flag record definition of
-- { analyticsV2 : Bool
-- , showPricingExplainer : Bool
-- }

Create a feature flag for optional strings, with a default value of Nothing. The string parameter here will be your flag name in the generated code, so always provide camelCase.

fromFlags [ ( "alternateApiUrl", maybeString ) ]
    |> generate "FeatureFlags"
-- produces a feature flag record definition of
-- { alternateApiUrl : Maybe String }
string : String -> Flag

Create a feature flag for strings, with your own default value. The first string parameter here will be your flag name in the generated code, so always provide camelCase.

fromFlags [ ( "welcomeMessage", string "Welcome to my site!" ) ]
    |> generate "FeatureFlags"
-- produces a feature flag record definition of
-- { welcomeMessage : String }

Builder methods

type Config

A ready-to-generate configuration. Pass it along between builders.

fromFlags : List ( String, Flag ) -> Config

Start a builder for your feature flag module.

fromFlags
    [ ( "snazzUpTheLoginButton", bool )
    , ( "alternatePricingExplainer", maybeString )
    ]

Bundle in a JSON decoder and an encoder. Introduces dependencies on elm/json and NoRedInk/elm-json-decode-pipeline. All fields will be optional in decoding, and default values will be omitted on serialization.

fromFlags
    [ ( "loadSlowly", bool )
    , ( "repeatedlyAskForNotificationPermissions", bool )
    ]
    |> withJsonConverters
    |> generate "Flags"


-- Flags will now expose:
--   - `decoder : Json.Decode.Decoder FeatureFlags`
--   - `encode : FeatureFlags -> Json.Encode.Value`
Json.Decode.decodeString
    Flags.decoder
    "{\"repeatedlyAskForNotificationPermissions\":true}"
    == Ok
        { loadSlowly = False
        , repeatedlyAskForNotificationPermissions = True
        }


Json.Encode.encode 0
    (Flags.encode
        { loadSlowly = True
        , repeatedlyAskForNotificationPermissions = False
        }
    )
    == "{\"loadSlowly\":true}"

Bundle in a URL querystring parser and a builder. Introduces a dependency on elm/url. Querystring keys match the fieldname by default. All fields will be optional in decoding, and default values will be omitted on serialization.

fromFlags [ ( "betterNameForUser", maybeString ) ]
    |> withUrlConverters
    |> generate "Flags"


-- Flags will now expose:
--   - `fromUrl : Url.Url -> FeatureFlags`
--   - `toParams : FeatureFlags -> List Url.Builder.QueryParameter`
Maybe.map Flags.fromUrl
    Url.fromString
    "https://trustworthy.com?betterNameForUser=James"
    == Just { betterNameForUser = Just "James" }


Url.Builder.crossOrigin
    "https://trustworthy.com"
    []
    (Url.Flags.toParams { betterNameForUser = Nothing })
    == "https://trustworthy.com/"
withQueryKeyFormatter : (String -> String) -> Config -> Config

Apply your own logic for creating URL query keys. Takes the fieldname as an input. Ignored if you don't call withUrlConverters.

fromFlags [ ( "refreshAtRandom", bool ) ]
    |> withUrlConverters
    |> withQueryKeyFormatter (\fieldName -> "f-" ++ fieldName)
    |> generate "Flags"


Maybe.map Flags.fromUrl
    (Url.fromString "https://trustworthy.com?f-refreshAtRandom=true")
    == Just { refreshAtRandom = True }


Url.Builder.crossOrigin
    "https://trustworthy.com"
    []
    (Url.Flags.toParams { refreshAtRandom = True })
    == "https://trustworthy.com/?f-refreshAtRandom=true"
generate : String -> Config -> File

Generate a file based on the config you've built. elm-codegen will name the module whatever you provide here.

fromFlags
    [ ( "featureA", bool )
    , ( "featureB", maybeString )
    ]
    |> withJsonConverters
    |> withUrlConverters
    |> withQueryKeyFormatter (\fieldName -> "f-" ++ fieldName)
    |> generate "Flags"