mdgriffith / elm-codegen / Elm


type alias File =
{ path : String
, contents : String
, warnings : List { declaration : String
, warning : String } 
}

file : List String -> List Declaration -> File

Build a file!

Elm.file [ "My", "Module" ]
    [ Elm.declaration "placeholder"
        (Elm.string "a fancy string!")
    ]

Basics


type alias Expression =
Internal.Compiler.Expression

toString : Expression -> String

See what code this expression would generate!

Note - Check out the Elm.ToString module if this doesn't quite meet your needs!

bool : Basics.Bool -> Expression

int : Basics.Int -> Expression

float : Basics.Float -> Expression

char : Char -> Expression

string : String -> Expression

hex : Basics.Int -> Expression

unit : Expression

maybe : Maybe Expression -> Expression

just : Expression -> Expression

nothing : Expression

list : List Expression -> Expression

tuple : Expression -> Expression -> Expression

triple : Expression -> Expression -> Expression -> Expression

withType : Annotation -> Expression -> Expression

Sometimes you may need to add a manual type annotation.

import Elm.Annotation as Type

Elm.value "myString"
    |> Elm.withType Type.string

Though be sure elm-codegen isn't already doing this automatically for you!

Records

record : List ( String, Expression ) -> Expression

Elm.record
    [ ( "name", Elm.string "Elm" )
    , ( "designation", Elm.string "Pretty fabulous" )
    ]

get : String -> Expression -> Expression

record
    |> Elm.get "field"

results in

record.field

updateRecord : List ( String, Expression ) -> Expression -> Expression

myRecord
    |> updateRecord
        [ ( "designation", Elm.string "Pretty fabulous" )
        ]

Results in

{ myRecord | designation = "Pretty fabulous" }

Flow control

ifThen : Expression -> Expression -> Expression -> Expression

ifThen (Elm.bool True)
    (Elm.string "yes")
    (Elm.string "no")

Will generate

if True then
    "yes"

else
    "no"

If you need more than one branch, then chain them together!

 Elm.ifThen (Elm.bool True)
    (Elm.string "yes")
    (Elm.ifThen (Elm.bool True)
        (Elm.string "maybe")
        (Elm.string "no")
    )

Will generate

if True then
    "yes"

else if True then
    "maybe"

else
    "no"

Declarations