parent
a036372335
commit
0a75805e1a
@ -0,0 +1,63 @@
|
||||
module WsMessage exposing (..)
|
||||
|
||||
import Json.Decode exposing (Decoder, andThen, decodeString, fail, field, map2, map3, string, succeed)
|
||||
|
||||
|
||||
type Action
|
||||
= Publish
|
||||
|
||||
|
||||
strToAction : String -> Decoder Action
|
||||
strToAction str =
|
||||
if str == "Publish" then
|
||||
succeed Publish
|
||||
|
||||
else
|
||||
fail ("invalid action '" ++ str ++ "'")
|
||||
|
||||
|
||||
type alias Publisher =
|
||||
{ uuid : String
|
||||
, name : String
|
||||
}
|
||||
|
||||
|
||||
type alias WsMessage =
|
||||
{ action : Action
|
||||
, publisher : Publisher
|
||||
, payload : String
|
||||
}
|
||||
|
||||
|
||||
|
||||
-- DECODERS
|
||||
|
||||
|
||||
publisherDecoder : Decoder Publisher
|
||||
publisherDecoder =
|
||||
map2 Publisher
|
||||
(field "uuid" string)
|
||||
(field "name" string)
|
||||
|
||||
|
||||
msgDecoder : Decoder WsMessage
|
||||
msgDecoder =
|
||||
map3 WsMessage
|
||||
actionDecoder
|
||||
(field "publisher" publisherDecoder)
|
||||
(field "payload" string)
|
||||
|
||||
|
||||
actionDecoder : Decoder Action
|
||||
actionDecoder =
|
||||
field "action" string |> andThen strToAction
|
||||
|
||||
|
||||
decode : String -> Maybe WsMessage
|
||||
decode str =
|
||||
case decodeString msgDecoder str of
|
||||
Ok msg ->
|
||||
Just msg
|
||||
|
||||
Err _ ->
|
||||
Nothing
|
Loading…
Reference in new issue