fizruk / http-api-data

Converting to/from HTTP API data like URL pieces, headers and query parameters.
http://hackage.haskell.org/package/http-api-data
Other
52 stars 42 forks source link

How to deal with generic deriving for non-flat records #133

Open MangoIV opened 1 year ago

MangoIV commented 1 year ago

Hi, I wonder whether there is some preferred, idiomatic way to deal with things like

data A = MkA 
  { a1 :: Text 
  , a2 :: B 
  } 
  deriving stock Generic 
  deriving anyclass FromForm 

data B = MkB 
  { b1 :: Text 
  , ... }
  deriving stock Generic 
  deriving anyclass FromForm 

fails with "No instance FromHttpApiData B arising from..." because of this instance her:

instance {-# OVERLAPPABLE #-} (Selector s, FromHttpApiData c) => GFromForm t (M1 S s (K1 i c)) where
  gFromForm _ opts form = M1 . K1 <$> parseUnique key form
    where
      key = Text.pack $ fieldLabelModifier opts $ selName (Proxy3 :: Proxy3 s g p)

This is quite unfortunate because I would like to somehow indicate that this should be derived recursively somehow; perhaps we can indicate this somehow by using a newtype wrapper in A's recursive field and then deriving everything newtype for it and then add an {-# OVERLAPPING #-} instance to the generic deriving? Though that would be quite the mess. I wonder what is a good way to do this.

Thanks in advance.