From d93784b3f3ce37a148840b8bde465df3e10c20e2 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Tue, 26 Jun 2018 18:09:29 +0200 Subject: [PATCH] [CO-319] Correctly format (jsend) newtype errors This is rather ugly and could probably be achieved nicely with a better understanding of the Generics.SOP library. As far as I could tell, there's no easy way to retrieve 'Tag' for single constructor (cf: 'For a datatype with a single constructor we do not need to tag values with their constructor; but for a datatype with multiple constructors we do. ') --- wallet-new/src/Cardano/Wallet/API/V1/Generic.hs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/wallet-new/src/Cardano/Wallet/API/V1/Generic.hs b/wallet-new/src/Cardano/Wallet/API/V1/Generic.hs index a75549e7ac9..3cc0f1a3705 100644 --- a/wallet-new/src/Cardano/Wallet/API/V1/Generic.hs +++ b/wallet-new/src/Cardano/Wallet/API/V1/Generic.hs @@ -57,21 +57,21 @@ gtoJsend :: forall a. (Generic a, HasDatatypeInfo a, All2 ToJSON (Code a)) => ResponseStatus -> a -> Value gtoJsend rs a = hcollapse $ - hcliftA2 allpt (gtoJsend' rs) + hcliftA2 allpt (gtoJsend' rs (toString $ gconsName a)) (jsendInfo (Proxy :: Proxy a)) (unSOP $ from a) gtoJsend' :: All ToJSON xs - => ResponseStatus -> JsonInfo xs -> NP I xs -> K Value xs -gtoJsend' rs (JsonZero n) Nil = + => ResponseStatus -> String -> JsonInfo xs -> NP I xs -> K Value xs +gtoJsend' rs _ (JsonZero n) Nil = jsendValue rs (Tag n) (Object mempty) -gtoJsend' rs (JsonOne tag) (I a :* Nil) = - jsendValue rs tag (toJSON a) -gtoJsend' rs (JsonMultiple tag) cs = +gtoJsend' rs consName (JsonOne _) (I a :* Nil) = + jsendValue rs (Tag consName) (toJSON a) +gtoJsend' rs _ (JsonMultiple tag) cs = jsendValue rs tag . Array . V.fromList . hcollapse $ hcliftA pt (K . toJSON . unI) cs -gtoJsend' rs (JsonRecord tag fields) cs = +gtoJsend' rs _ (JsonRecord tag fields) cs = jsendValue rs tag . Object . HM.fromList . hcollapse $ hcliftA2 pt (\(K field) (I a) -> K (toText field, toJSON a)) fields cs