Commit 994a66dd authored by Steffen Michels's avatar Steffen Michels

JSONDecode of Testing.TestEvents.StartEvent: check whether 'event' field is 'start'

parent fa8bcd9c
Pipeline #9343 passed with stage
in 3 minutes and 10 seconds
......@@ -11,8 +11,8 @@ from Text.JSON import generic JSONEncode, generic JSONDecode, :: JSONNode, :: Ma
/**
* Event emitted when a test is started.
* A Specialised JSONEncode instance is used for this type, which
* has to be adapted in case the type definition is changed!
* Specialised JSONEncode/JSONDecode instances are used for this type, which
* have to be adapted in case the type definition is changed!
*/
:: StartEvent = { name :: !String //* The test's name
}
......
implementation module Testing.TestEvents
import Text.JSON
import Text.JSON, Control.Monad, StdFunc, StdTuple, StdList, Data.Maybe, Control.Applicative
JSONEncode{|StartEvent|} _ startEvent = [ JSONObject [ ("name", JSONString startEvent.StartEvent.name)
, ("event", JSONString "start")
]
]
JSONDecode{|StartEvent|} _ [JSONObject objFields : rest] = (mbEvent, rest)
where
mbEvent :: Maybe StartEvent
mbEvent = getField "name" >>= \name ->
getField "event" >>= \event ->
if (event == "start")
(pure {StartEvent | name = name})
mzero
getField :: String -> Maybe a | JSONDecode{|*|} a
getField fieldName = case filter ((==) fieldName o fst) objFields of
[(_, jsonNode)] -> fromJSON jsonNode
_ -> mzero
JSONDecode{|StartEvent|} _ _ = (Nothing, [])
JSONEncode{|EndEventType|} _ eType = [JSONString eTypeStr]
where
eTypeStr = case eType of
......@@ -23,5 +39,5 @@ where
JSONDecode{|EndEventType|} _ nodes = (Nothing, nodes)
derive JSONEncode EndEvent
derive JSONDecode StartEvent, EndEvent
derive JSONDecode EndEvent
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment