Skip to content
Snippets Groups Projects
Commit f2f06e2e authored by Iustin Pop's avatar Iustin Pop
Browse files

Handle better 'null' values in optional fields


While testing Haskell⇔Python interoperability for opcode
serialisation, I found this bug: the Haskell code doesn't treat
optional fields with 'null' values as missing, which the Python code
does, leading to differences.

Investigating all uses of 'maybeFromObj' and the single use of
'fromObjWithDefault' shows that these are only used in cases where we
the rules are indeed "null == missing", so let's update the functions
and their docstrings accordingly.

Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarAgata Murawska <agatamurawska@google.com>
parent 9990c068
No related branches found
No related tags found
No related merge requests found
......@@ -84,15 +84,26 @@ fromObj o k =
k (show (map fst o))
Just val -> fromKeyValue k val
-- | Reads the value of an optional key in a JSON object.
-- | Reads the value of an optional key in a JSON object. Missing
-- keys, or keys that have a \'null\' value, will be returned as
-- 'Nothing', otherwise we attempt deserialisation and return a 'Just'
-- value.
maybeFromObj :: (J.JSON a, Monad m) =>
JSRecord -> String -> m (Maybe a)
maybeFromObj o k =
case lookup k o of
Nothing -> return Nothing
-- a optional key with value JSNull is the same as missing, since
-- we can't convert it meaningfully anyway to a Haskell type, and
-- the Python code can emit 'null' for optional values (depending
-- on usage), and finally our encoding rules treat 'null' values
-- as 'missing'
Just J.JSNull -> return Nothing
Just val -> liftM Just (fromKeyValue k val)
-- | Reads the value of a key in a JSON object with a default if missing.
-- | Reads the value of a key in a JSON object with a default if
-- missing. Note that both missing keys and keys with value \'null\'
-- will case the default value to be returned.
fromObjWithDefault :: (J.JSON a, Monad m) =>
JSRecord -> String -> a -> m a
fromObjWithDefault o k d = liftM (fromMaybe d) $ maybeFromObj o k
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment