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.
Decodes a message.
Decodes a required field.
Decodes an optional field.
Decodes a repeated field.
Decodes a field.
Provides a default value for a field.
Encodes a required field.
Encodes an optional field.
Encodes a repeated field.
Bytes field.
Decodes a bytes field. TODO: Implement.
Encodes a bytes field. TODO: Implement.
Timestamp.
Decodes a Timestamp.
Encodes a Timestamp.
Decodes an IntValue.
Encodes an IntValue.
Decodes a StringValue.
Encodes a StringValue.
Encodes a BoolValue.
Encodes a BoolValue.
Decodes a BytesValue.
Encodes a BytesValue.
Decodes a FloatValue.
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