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

Protobuf

Runtime library for Google Protocol Buffers.

This is mostly useless on its own, it is meant to support the code generated by the Elm Protocol Buffer compiler.

Decoder Helpers

decode : a -> JD.Decoder a

Decodes a message.

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

Decodes a required field.

optional : String -> JD.Decoder a -> JD.Decoder (Maybe a -> b) -> JD.Decoder b

Decodes an optional field.

repeated : String -> JD.Decoder a -> JD.Decoder (List a -> b) -> JD.Decoder b

Decodes a repeated field.

field : JD.Decoder a -> JD.Decoder (a -> b) -> JD.Decoder b

Decodes a field.

withDefault : a -> JD.Decoder a -> JD.Decoder a

Provides a default value for a field.

Encoder Helpers

requiredFieldEncoder : String -> (a -> JE.Value) -> a -> a -> Maybe ( String, JE.Value )

Encodes a required field.

optionalEncoder : String -> (a -> JE.Value) -> Maybe a -> Maybe ( String, JE.Value )

Encodes an optional field.

repeatedFieldEncoder : String -> (a -> JE.Value) -> List a -> Maybe ( String, JE.Value )

Encodes a repeated field.

Bytes

type alias Bytes = List Int

Bytes field.

bytesFieldDecoder : JD.Decoder Bytes

Decodes a bytes field. TODO: Implement.

bytesFieldEncoder : Bytes -> JE.Value

Encodes a bytes field. TODO: Implement.

Well Known Types

type alias Timestamp = Date.Date

Timestamp.

timestampDecoder : JD.Decoder Timestamp

Decodes a Timestamp.

timestampEncoder : Timestamp -> JE.Value

Encodes a Timestamp.

intValueDecoder : JD.Decoder Int

Decodes an IntValue.

intValueEncoder : Int -> JE.Value

Encodes an IntValue.

stringValueDecoder : JD.Decoder String

Decodes a StringValue.

stringValueEncoder : String -> JE.Value

Encodes a StringValue.

boolValueDecoder : JD.Decoder Bool

Encodes a BoolValue.

boolValueEncoder : Bool -> JE.Value

Encodes a BoolValue.

bytesValueDecoder : JD.Decoder Bytes

Decodes a BytesValue.

bytesValueEncoder : Bytes -> JE.Value

Encodes a BytesValue.

floatValueDecoder : JD.Decoder Float

Decodes a FloatValue.

floatValueEncoder : Float -> JE.Value

Encodes a FloatValue.

module Protobuf exposing (..)

{-| Runtime library for Google Protocol Buffers.

This is mostly useless on its own, it is meant to support the code generated by the [Elm Protocol
Buffer compiler](https://github.com/tiziano88/elm-protobuf).

# Decoder Helpers

@docs decode, required, optional, repeated, field

@docs withDefault

# Encoder Helpers

@docs requiredFieldEncoder, optionalEncoder, repeatedFieldEncoder

# Bytes

@docs Bytes, bytesFieldDecoder, bytesFieldEncoder

# Well Known Types

@docs Timestamp, timestampDecoder, timestampEncoder

@docs intValueDecoder, intValueEncoder

@docs stringValueDecoder, stringValueEncoder

@docs boolValueDecoder, boolValueEncoder

@docs bytesValueDecoder, bytesValueEncoder

@docs floatValueDecoder, floatValueEncoder
-}

import Date
import Time
import Json.Decode as JD
import Json.Encode as JE
import ISO8601


{-| Decodes a message.
-}
decode : a -> JD.Decoder a
decode =
    JD.succeed


{-| Decodes a required field.
-}
required : String -> JD.Decoder a -> a -> JD.Decoder (a -> b) -> JD.Decoder b
required name decoder default d =
    field (withDefault default <| JD.field name decoder) d


{-| Decodes an optional field.
-}
optional : String -> JD.Decoder a -> JD.Decoder (Maybe a -> b) -> JD.Decoder b
optional name decoder d =
    field (JD.maybe <| JD.field name decoder) d


{-| Decodes a repeated field.
-}
repeated : String -> JD.Decoder a -> JD.Decoder (List a -> b) -> JD.Decoder b
repeated name decoder d =
    field (withDefault [] <| JD.field name <| JD.list decoder) d


{-| Decodes a field.
-}
field : JD.Decoder a -> JD.Decoder (a -> b) -> JD.Decoder b
field =
    JD.map2 (|>)


{-| Provides a default value for a field.
-}
withDefault : a -> JD.Decoder a -> JD.Decoder a
withDefault default decoder =
    JD.oneOf
        [ decoder
        , JD.succeed default
        ]


{-| Encodes an optional field.
-}
optionalEncoder : String -> (a -> JE.Value) -> Maybe a -> Maybe ( String, JE.Value )
optionalEncoder name encoder v =
    case v of
        Just x ->
            Just ( name, encoder x )

        Nothing ->
            Nothing


{-| Encodes a required field.
-}
requiredFieldEncoder : String -> (a -> JE.Value) -> a -> a -> Maybe ( String, JE.Value )
requiredFieldEncoder name encoder default v =
    if v == default then
        Nothing
    else
        Just ( name, encoder v )


{-| Encodes a repeated field.
-}
repeatedFieldEncoder : String -> (a -> JE.Value) -> List a -> Maybe ( String, JE.Value )
repeatedFieldEncoder name encoder v =
    case v of
        [] ->
            Nothing

        _ ->
            Just ( name, JE.list <| List.map encoder v )


{-| Bytes field.
-}
type alias Bytes =
    List Int


{-| Decodes a bytes field.
TODO: Implement.
-}
bytesFieldDecoder : JD.Decoder Bytes
bytesFieldDecoder =
    JD.succeed []


{-| Encodes a bytes field.
TODO: Implement.
-}
bytesFieldEncoder : Bytes -> JE.Value
bytesFieldEncoder v =
    JE.list []



-- Well Known Types.


{-| Timestamp.
-}
type alias Timestamp =
    Date.Date


{-| Decodes a Timestamp.
-}
timestampDecoder : JD.Decoder Timestamp
timestampDecoder =
    JD.map ISO8601.fromString JD.string
        |> JD.andThen
            (\v ->
                case v of
                    Ok v ->
                        JD.succeed <| Date.fromTime <| Time.millisecond * (toFloat (ISO8601.toTime v))

                    Err e ->
                        JD.fail e
            )


{-| Encodes a Timestamp.
-}
timestampEncoder : Timestamp -> JE.Value
timestampEncoder v =
    JE.string <| ISO8601.toString <| ISO8601.fromTime <| round <| Time.inMilliseconds <| Date.toTime <| v


{-| Decodes an IntValue.
-}
intValueDecoder : JD.Decoder Int
intValueDecoder =
    JD.int


{-| Encodes an IntValue.
-}
intValueEncoder : Int -> JE.Value
intValueEncoder =
    JE.int


{-| Decodes a StringValue.
-}
stringValueDecoder : JD.Decoder String
stringValueDecoder =
    JD.string


{-| Encodes a StringValue.
-}
stringValueEncoder : String -> JE.Value
stringValueEncoder =
    JE.string


{-| Encodes a BoolValue.
-}
boolValueDecoder : JD.Decoder Bool
boolValueDecoder =
    JD.bool


{-| Encodes a BoolValue.
-}
boolValueEncoder : Bool -> JE.Value
boolValueEncoder =
    JE.bool


{-| Decodes a BytesValue.
-}
bytesValueDecoder : JD.Decoder Bytes
bytesValueDecoder =
    bytesFieldDecoder


{-| Encodes a BytesValue.
-}
bytesValueEncoder : Bytes -> JE.Value
bytesValueEncoder =
    bytesFieldEncoder


{-| Decodes a FloatValue.
-}
floatValueDecoder : JD.Decoder Float
floatValueDecoder =
    JD.float


{-| Encodes a FloatValue.
-}
floatValueEncoder : Float -> JE.Value
floatValueEncoder =
    JE.float