git.iko.soy/typeable/http-api-data
on master help sign in
Add notes that Generic-based implementation only works for records
3158b55Nickolay Kudasov
src/Web/Internal/FormUrlEncoded.hs+22−2
@@ -211,7 +211,8 @@ instance IsList Form where
211-- instance 'ToForm' Person
211-- instance 'ToForm' Person
212-- @
212-- @
213--
213--
214+-- The default implementation of 'toForm' is 'genericToForm'.
214−-- The default implementation will use 'toQueryParam' for each field's value.
215+-- It only works for records and it will use 'toQueryParam' for each field's value.
215class ToForm a where
216class ToForm a where
216 -- | Convert a value into 'Form'.
217 -- | Convert a value into 'Form'.
217 toForm :: a -> Form
218 toForm :: a -> Form
@@ -243,6 +244,15 @@ data Proxy3 a b c = Proxy3
243
244
244-- | A 'Generic'-based implementation of 'toForm'.
245-- | A 'Generic'-based implementation of 'toForm'.
245-- This is used as a default implementation in 'ToForm'.
246-- This is used as a default implementation in 'ToForm'.
247+--
248+-- Note that this only works for records (i.e. product data types with named fields):
249+--
250+-- @
251+-- data Person = Person
252+-- { name :: String
253+-- , age :: Int
254+-- } deriving ('Generic')
255+-- @
246genericToForm :: (Generic a, GToForm (Rep a)) => a -> Form
256genericToForm :: (Generic a, GToForm (Rep a)) => a -> Form
247genericToForm = gToForm . from
257genericToForm = gToForm . from
248
258
@@ -296,7 +306,8 @@ instance (Selector s, ToHttpApiData c) => GToForm (M1 S s (K1 i c)) where
296-- instance 'FromForm' Person
306-- instance 'FromForm' Person
297-- @
307-- @
298--
308--
309+-- The default implementation of 'fromForm' is 'genericFromForm'.
299−-- The default implementation will use 'parseQueryParam' for each field's value.
310+-- It only works for records and it will use 'parseQueryParam' for each field's value.
300class FromForm a where
311class FromForm a where
301 -- | Parse 'Form' into a value.
312 -- | Parse 'Form' into a value.
302 fromForm :: Form -> Either Text a
313 fromForm :: Form -> Either Text a
@@ -329,6 +340,15 @@ toEntriesByKey = traverse parseGroup . HashMap.toList . unForm
329
340
330-- | A 'Generic'-based implementation of 'fromForm'.
341-- | A 'Generic'-based implementation of 'fromForm'.
331-- This is used as a default implementation in 'FromForm'.
342-- This is used as a default implementation in 'FromForm'.
343+--
344+-- Note that this only works for records (i.e. product data types with named fields):
345+--
346+-- @
347+-- data Person = Person
348+-- { name :: String
349+-- , age :: Int
350+-- } deriving ('Generic')
351+-- @
332genericFromForm :: (Generic a, GFromForm (Rep a))
352genericFromForm :: (Generic a, GFromForm (Rep a))
333 => Form -> Either Text a
353 => Form -> Either Text a
334genericFromForm f = to <$> gFromForm f
354genericFromForm f = to <$> gFromForm f