This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

Graphqelm.Internal.Encode

WARNING Graphqelm.Interal modules are used by the graphqelm command line code generator tool. They should not be consumed through hand-written code.

Graphqelm.Internal.Encode.Values are low-level details used by generated code. They are only used by the code generated by the graphqelm CLI tool.

null : Value

Encode null

bool : Bool -> Value

Encode a bool

enum : (a -> String) -> a -> Value

Encode an enum. The first argument is the toString function for that enum.

int : Int -> Value

Encode an int

list : (a -> Value) -> List a -> Value

Encode a list of Values

string : String -> Value

Encode a string

object : List ( String, Value ) -> Value

Encode a list of key-value pairs into an object

maybe : (a -> Value) -> Maybe a -> Value

Encode a Maybe. Uses encoder for Just, or Encode.null for Nothing.

maybeObject : List ( String, Maybe Value ) -> Value

Encode a list of key-value pairs into an object

optional : OptionalArgument a -> (a -> Value) -> Maybe Value

Encode a list of key-value pairs into an object

float : Float -> Value

Encode a float

serialize : Value -> String

Low-level function for serializing a Graphqelm.Internal.Encode.Values.

type Value = EnumValue String | Json Json.Encode.Value | List (List Value) | Object (List ( String, Value ))

Represents an encoded Value

module Graphqelm.Internal.Encode exposing (Value, bool, enum, float, int, list, maybe, maybeObject, null, object, optional, serialize, string)

{-| **WARNING** `Graphqelm.Interal` modules are used by the `graphqelm` command line
code generator tool. They should not be consumed through hand-written code.

`Graphqelm.Internal.Encode.Value`s are low-level details used by generated code.
They are only used by the code generated by the `graphqelm` CLI tool.

@docs null, bool, enum, int, list, string, object, maybe, maybeObject, optional, float
@docs serialize
@docs Value

-}

import Graphqelm.OptionalArgument as OptionalArgument exposing (OptionalArgument)
import Json.Encode


{-| Represents an encoded Value
-}
type Value
    = EnumValue String
    | Json Json.Encode.Value
    | List (List Value)
    | Object (List ( String, Value ))


{-| Encode a list of key-value pairs into an object
-}
object : List ( String, Value ) -> Value
object value =
    Object value


{-| Encode a list of key-value pairs into an object
-}
maybeObject : List ( String, Maybe Value ) -> Value
maybeObject maybeValues =
    maybeValues
        |> List.filterMap
            (\( key, value ) ->
                case value of
                    Just actualValue ->
                        Just ( key, actualValue )

                    Nothing ->
                        Nothing
            )
        |> Object


{-| Encode a list of key-value pairs into an object
-}
optional : OptionalArgument a -> (a -> Value) -> Maybe Value
optional optionalValue toValue =
    case optionalValue of
        OptionalArgument.Present value ->
            toValue value
                |> Just

        OptionalArgument.Absent ->
            Nothing

        OptionalArgument.Null ->
            Just null


{-| Encode an int
-}
int : Int -> Value
int value =
    Json.Encode.int value
        |> Json


{-| Encode a float
-}
float : Float -> Value
float value =
    Json.Encode.float value
        |> Json


{-| Encode null
-}
null : Value
null =
    Json.Encode.null
        |> Json


{-| Encode a bool
-}
bool : Bool -> Value
bool bool =
    Json.Encode.bool bool
        |> Json


{-| Encode a string
-}
string : String -> Value
string string =
    Json.Encode.string string
        |> Json


{-| Encode an enum. The first argument is the toString function for that enum.
-}
enum : (a -> String) -> a -> Value
enum enumToString enum =
    EnumValue (enumToString enum)


{-| Encode a list of Values
-}
list : (a -> Value) -> List a -> Value
list toValue list =
    list
        |> List.map toValue
        |> List


{-| Encode a Maybe. Uses encoder for `Just`, or `Encode.null` for `Nothing`.
-}
maybe : (a -> Value) -> Maybe a -> Value
maybe encoder =
    Maybe.map encoder >> Maybe.withDefault null


{-| Low-level function for serializing a `Graphqelm.Internal.Encode.Value`s.
-}
serialize : Value -> String
serialize value =
    case value of
        EnumValue enumValue ->
            enumValue

        Json json ->
            Json.Encode.encode 0 json

        List values ->
            "["
                ++ (List.map serialize values |> String.join ", ")
                ++ "]"

        Object keyValuePairs ->
            "{"
                ++ (List.map (\( key, objectValue ) -> key ++ ": " ++ serialize objectValue)
                        keyValuePairs
                        |> String.join ", "
                   )
                ++ "}"