Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
[CO-319] Correctly format (jsend) newtype errors
Browse files Browse the repository at this point in the history
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.  ')
  • Loading branch information
KtorZ committed Jun 26, 2018
1 parent 82116ff commit d93784b
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions wallet-new/src/Cardano/Wallet/API/V1/Generic.hs
Expand Up @@ -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

Expand Down

0 comments on commit d93784b

Please sign in to comment.