on master help sign in
Merge pull request #730 from gdevanla/master

Provide a clearer message when the required tagKey is missing, #719

commitb23d35107e6b162eda9f820c7a3d177d59b22192 authorAdam Bergmark <adam@bergmark.nl> dateWed 16 Oct 2019, 13:48 (7 years ago) parentf26e93ddd46e22
2 files changed+18−7
Data/Aeson/Types/FromJSON.hs+15−4 tests/golden/generic.expected+3−3
Data/Aeson/Types/FromJSON.hs+15−4
@@ -892,7 +892,7 @@ parseFieldMaybe' = (.:!)
892892-- E.g. @'explicitParseField' 'parseJSON1' :: ('FromJSON1' f, 'FromJSON' a) -> 'Object' -> 'Text' -> 'Parser' (f a)@
893893explicitParseField :: (Value -> Parser a) -> Object -> Text -> Parser a
894894explicitParseField p obj key = case H.lookup key obj of
895− Nothing -> fail $ "key " ++ show key ++ " not present"
895+ Nothing -> fail $ "key " ++ show key ++ " not found"
896896 Just v -> p v <?> Key key
897897{-# INLINE explicitParseField #-}
898898
@@ -951,6 +951,16 @@ type ConName = String
951951contextType :: TypeName -> Parser a -> Parser a
952952contextType = prependContext
953953
954+-- | Add the tagKey that will be looked up while building an ADT
955+-- | Produce the error equivalent to
956+-- | Left "Error in $: parsing T failed, expected an object with keys "tag" and
957+-- | "contents", where "tag" i-- |s associated to one of ["Foo", "Bar"],
958+-- | The parser returned error was: could not find key "tag"
959+contextTag :: Text -> [String] -> Parser a -> Parser a
960+contextTag tagKey cnames = prependFailure
961+ ("expected Object with key \"" ++ unpack tagKey ++ "\"" ++
962+ " containing one of " ++ show cnames ++ ", ")
963+
954964-- | Add the name of the constructor being parsed to a parser's error messages.
955965contextCons :: ConName -> TypeName -> Parser a -> Parser a
956966contextCons cname tname = prependContext (showCons cname tname)
@@ -1129,8 +1139,8 @@ instance ConstructorNames a => ConstructorNames (D1 d a) where
11291139 retag (Tagged2 x) = Tagged2 x
11301140
11311141--------------------------------------------------------------------------------
1132−
1133−parseNonAllNullarySum :: ( FromPair arity f
1142+parseNonAllNullarySum :: forall f c arity.
1143+ ( FromPair arity f
11341144 , FromTaggedObject arity f
11351145 , FromUntaggedValue arity f
11361146 , ConstructorNames f
@@ -1140,7 +1150,7 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
11401150 case sumEncoding opts of
11411151 TaggedObject{..} ->
11421152 withObject tname $ \obj -> do
1143− tag <- contextType tname $ obj .: tagKey
1153+ tag <- contextType tname . contextTag tagKey cnames_ $ obj .: tagKey
11441154 fromMaybe (badTag tag <?> Key tagKey) $
11451155 parseFromTaggedObject (tag :* contentsFieldName :* p) obj
11461156 where
@@ -1148,6 +1158,7 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
11481158 badTag tag = failWith_ $ \cnames ->
11491159 "expected tag field to be one of " ++ show cnames ++
11501160 ", but found tag " ++ show tag
1161+ cnames_ = unTagged2 (constructorTags (constructorTagModifier opts) :: Tagged2 f [String])
11511162
11521163 ObjectWithSingleField ->
11531164 withObject tname $ \obj -> case H.toList obj of
tests/golden/generic.expected+3−3
@@ -6,11 +6,11 @@ Error in $: parsing Types.Nullary failed, expected one of the tags ["C1","C2","C
66Error in $: parsing Types.Nullary failed, expected String, but encountered Array
77SomeType (tagged)
88Error in $.contents: parsing Int failed, expected Number, but encountered Boolean
9−Error in $: parsing Types.SomeType(Unary) failed, key "contents" not present
10−Error in $: parsing Types.SomeType(Record) failed, key "testone" not present
9+Error in $: parsing Types.SomeType(Unary) failed, key "contents" not found
10+Error in $: parsing Types.SomeType(Record) failed, key "testone" not found
1111Error in $.testone: parsing Double failed, unexpected Boolean
1212Error in $.tag: parsing Types.SomeType failed, expected tag field to be one of ["nullary","unary","product","record","list"], but found tag "X"
13−Error in $: parsing Types.SomeType failed, key "tag" not present
13+Error in $: parsing Types.SomeType failed, expected Object with key "tag" containing one of ["nullary","unary","product","record","list"], key "tag" not found
1414Error in $: parsing Types.SomeType failed, expected Object, but encountered Array
1515SomeType (single-field)
1616Error in $.unary: parsing Int failed, expected Number, but encountered Object