on master help sign in
Provide a clearer message when the required tagKey is missing, #719
commit67772c71350d8fba71903aed2047cacf2d4dfc43 authorgrdvnl <grdvnl@gmail.com> dateWed 2 Oct 2019, 06:10 (7 years ago) parent47604b6
1 file changed+19−8
Data/Aeson/Types/FromJSON.hs+19−8
Data/Aeson/Types/FromJSON.hs+19−8
@@ -911,11 +911,18 @@ type ConName = String
911911contextType :: TypeName -> Parser a -> Parser a
912912contextType = prependContext
913913
914+
915+
914916-- | Add the tagKey that will be looked up while building an ADT
915−contextTag :: Text -> Parser a -> Parser a
916−contextTag tagKey = prependFailure
917− ("Missing required tagKey \"" ++ unpack tagKey ++
918− "\". The parser returned error was: ")
917+-- | Produce the error equivalent to
918+-- | Left "Error in $: parsing T failed, expected an object with keys "tag" and
919+-- | "contents", where "tag" i-- |s associated to one of ["Foo", "Bar"],
920+-- | The parser returned error was: could not find key "tag"
921+contextTag :: Text -> [String] -> Parser a -> Parser a
922+contextTag tagKey cnames = prependFailure
923+ ("expected an object with key \"" ++ unpack tagKey ++
924+ "\" where \"tag\" is associated to one of " ++ show cnames ++
925+ ". The parser returned error was: ")
919926
920927-- | Add the name of the constructor being parsed to a parser's error messages.
921928contextCons :: ConName -> TypeName -> Parser a -> Parser a
@@ -1082,9 +1089,10 @@ instance Constructor c => ConstructorNames (C1 c a) where
10821089 where
10831090 cname = conName (undefined :: M1 _i c _f _p)
10841091
1085−--------------------------------------------------------------------------------
10861092
1087−parseNonAllNullarySum :: ( FromPair arity f
1093+--------------------------------------------------------------------------------
1094+parseNonAllNullarySum :: forall f c arity.
1095+ ( FromPair arity f
10881096 , FromTaggedObject arity f
10891097 , FromUntaggedValue arity f
10901098 , ConstructorNames f
@@ -1094,14 +1102,17 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
10941102 case sumEncoding opts of
10951103 TaggedObject{..} ->
10961104 withObject tname $ \obj -> do
1097− tag <- (contextType tname) . (contextTag tagKey) $ obj .: tagKey
1105+ tag <- (contextType tname) . (contextTag tagKey cnames_) $ obj .: tagKey
10981106 fromMaybe (badTag tag <?> Key tagKey) $
1099− parseFromTaggedObject (tag :* contentsFieldName :* p) obj
1107+ parseFromTaggedObject (tag :* contentsFieldName :* p) obj
11001108 where
11011109 tagKey = pack tagFieldName
11021110 badTag tag = failWith_ $ \cnames ->
11031111 "expected tag field to be one of " ++ show cnames ++
11041112 ", but found tag " ++ show tag
1113+ cnames_ = unTagged2 (constructorTags opts :: Tagged2 f [String])
1114+ --cname_ = conName (undefined :: M1 _i c _f _p)
1115+ -- forall f a. ConstructorNames f
11051116
11061117 ObjectWithSingleField ->
11071118 withObject tname $ \obj -> case H.toList obj of