git.iko.soy/typeable/servant
on master help sign in
Move toSample method out of ToSample class
98af812Nickolay Kudasov
servant-docs/example/greet.hs+1−3
@@ -54,15 +54,13 @@ instance ToParam (MatrixParam "lang" String) where
54 Normal
54 Normal
55
55
56instance ToSample Greet Greet where
56instance ToSample Greet Greet where
57− toSample _ = Just $ Greet "Hello, haskeller!"
58−
59 toSamples _ =
57 toSamples _ =
60 [ ("If you use ?capital=true", Greet "HELLO, HASKELLER")
58 [ ("If you use ?capital=true", Greet "HELLO, HASKELLER")
61 , ("If you use ?capital=false", Greet "Hello, haskeller")
59 , ("If you use ?capital=false", Greet "Hello, haskeller")
62 ]
60 ]
63
61
64instance ToSample Int Int where
62instance ToSample Int Int where
65− toSample _ = Just 1729
63+ toSamples _ = singleSample 1729
66
64
67-- We define some introductory sections, these will appear at the top of the
65-- We define some introductory sections, these will appear at the top of the
68-- documentation.
66-- documentation.
servant-docs/src/Servant/Docs.hs+3
@@ -148,6 +148,9 @@ module Servant.Docs
148
148
149 , -- * Classes you need to implement for your types
149 , -- * Classes you need to implement for your types
150 ToSample(..)
150 ToSample(..)
151+ , toSample
152+ , noSamples
153+ , singleSample
151 , sampleByteString
154 , sampleByteString
152 , sampleByteStrings
155 , sampleByteStrings
153 , ToParam(..)
156 , ToParam(..)
servant-docs/src/Servant/Docs/Internal.hs+13−16
@@ -374,7 +374,7 @@ class HasDocs layout where
374-- > instance ToJSON Greet
374-- > instance ToJSON Greet
375-- >
375-- >
376-- > instance ToSample Greet Greet where
376-- > instance ToSample Greet Greet where
377−-- > toSample _ = Just g
377+-- > toSamples _ = singleSample g
378-- >
378-- >
379-- > where g = Greet "Hello, haskeller!"
379-- > where g = Greet "Hello, haskeller!"
380--
380--
@@ -383,34 +383,34 @@ class HasDocs layout where
383-- some context (as 'Text') that explains when you're supposed to
383-- some context (as 'Text') that explains when you're supposed to
384-- get the corresponding response.
384-- get the corresponding response.
385class ToSample a b | a -> b where
385class ToSample a b | a -> b where
386− toSample :: Proxy a -> Maybe b
387− toSample _ = snd <$> listToMaybe samples
388− where samples = toSamples (Proxy :: Proxy a)
389−
390 toSamples :: Proxy a -> [(Text, b)]
386 toSamples :: Proxy a -> [(Text, b)]
391 default toSamples :: (Generic a, Generic b, GToSample (Rep a) (Rep b)) => Proxy a -> [(Text, b)]
387 default toSamples :: (Generic a, Generic b, GToSample (Rep a) (Rep b)) => Proxy a -> [(Text, b)]
392 toSamples = defaultSamples
388 toSamples = defaultSamples
393
389
394−defaultSample :: forall a b. (Generic a, Generic b, GToSample (Rep a) (Rep b)) => Proxy a -> Maybe b
390+toSample :: forall a b. ToSample a b => Proxy a -> Maybe b
395−defaultSample _ = to <$> gtoSample (Proxy :: Proxy (Rep a))
391+toSample _ = snd <$> listToMaybe (toSamples (Proxy :: Proxy a))
392+
393+-- | No samples.
394+noSamples :: [(Text, a)]
395+noSamples = empty
396+
397+-- | Single sample without description.
398+singleSample :: a -> [(Text, a)]
399+singleSample x = [("", x)]
396
400
397defaultSamples :: forall a b. (Generic a, Generic b, GToSample (Rep a) (Rep b)) => Proxy a -> [(Text, b)]
401defaultSamples :: forall a b. (Generic a, Generic b, GToSample (Rep a) (Rep b)) => Proxy a -> [(Text, b)]
398defaultSamples _ = Omega.runOmega $ second to <$> gtoSamples (Proxy :: Proxy (Rep a))
402defaultSamples _ = Omega.runOmega $ second to <$> gtoSamples (Proxy :: Proxy (Rep a))
399
403
400class GToSample t s where
404class GToSample t s where
401− gtoSample :: proxy t -> Maybe (s x)
402− gtoSample _ = snd <$> listToMaybe (Omega.runOmega (gtoSamples (Proxy :: Proxy t)))
403 gtoSamples :: proxy t -> Omega.Omega (Text, s x)
405 gtoSamples :: proxy t -> Omega.Omega (Text, s x)
404− gtoSamples _ = maybe empty (pure . ("",)) (gtoSample (Proxy :: Proxy t))
405
406
406instance GToSample U1 U1 where
407instance GToSample U1 U1 where
407− gtoSample _ = Just U1
408+ gtoSamples _ = Omega.each (singleSample U1)
408
409
409instance GToSample V1 V1 where
410instance GToSample V1 V1 where
410− gtoSample _ = Nothing
411+ gtoSamples _ = empty
411
412
412instance (GToSample p p', GToSample q q') => GToSample (p :*: q) (p' :*: q') where
413instance (GToSample p p', GToSample q q') => GToSample (p :*: q) (p' :*: q') where
413− gtoSample _ = (:*:) <$> gtoSample (Proxy :: Proxy p) <*> gtoSample (Proxy :: Proxy q)
414 gtoSamples _ = render <$> ps <*> qs
414 gtoSamples _ = render <$> ps <*> qs
415 where
415 where
416 ps = gtoSamples (Proxy :: Proxy p)
416 ps = gtoSamples (Proxy :: Proxy p)
@@ -426,15 +426,12 @@ instance (GToSample p p', GToSample q q') => GToSample (p :+: q) (p' :+: q') whe
426 rights = second R1 <$> gtoSamples (Proxy :: Proxy q)
426 rights = second R1 <$> gtoSamples (Proxy :: Proxy q)
427
427
428instance ToSample a b => GToSample (K1 i a) (K1 i b) where
428instance ToSample a b => GToSample (K1 i a) (K1 i b) where
429− gtoSample _ = K1 <$> toSample (Proxy :: Proxy a)
430 gtoSamples _ = second K1 <$> Omega.each (toSamples (Proxy :: Proxy a))
429 gtoSamples _ = second K1 <$> Omega.each (toSamples (Proxy :: Proxy a))
431
430
432instance (GToSample f g) => GToSample (M1 i a f) (M1 i a g) where
431instance (GToSample f g) => GToSample (M1 i a f) (M1 i a g) where
433− gtoSample _ = M1 <$> gtoSample (Proxy :: Proxy f)
434 gtoSamples _ = second M1 <$> gtoSamples (Proxy :: Proxy f)
432 gtoSamples _ = second M1 <$> gtoSamples (Proxy :: Proxy f)
435
433
436instance ToSample a b => ToSample (Headers ls a) b where
434instance ToSample a b => ToSample (Headers ls a) b where
437− toSample _ = toSample (Proxy :: Proxy a)
438 toSamples _ = toSamples (Proxy :: Proxy a)
435 toSamples _ = toSamples (Proxy :: Proxy a)
439
436
440
437
servant-examples/tutorial/T10.hs+3−3
@@ -24,7 +24,7 @@ instance ToCapture (Capture "y" Int) where
24 toCapture _ = DocCapture "y" "(integer) position on the y axis"
24 toCapture _ = DocCapture "y" "(integer) position on the y axis"
25
25
26instance ToSample T3.Position T3.Position where
26instance ToSample T3.Position T3.Position where
27− toSample _ = Just (T3.Position 3 14)
27+ toSamples _ = singleSample (T3.Position 3 14)
28
28
29instance ToParam (QueryParam "name" String) where
29instance ToParam (QueryParam "name" String) where
30 toParam _ =
30 toParam _ =
@@ -43,10 +43,10 @@ ci :: T3.ClientInfo
43ci = T3.ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"]
43ci = T3.ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"]
44
44
45instance ToSample T3.ClientInfo T3.ClientInfo where
45instance ToSample T3.ClientInfo T3.ClientInfo where
46− toSample _ = Just ci
46+ toSamples _ = singleSample ci
47
47
48instance ToSample T3.Email T3.Email where
48instance ToSample T3.Email T3.Email where
49− toSample _ = Just (T3.emailForClient ci)
49+ toSamples _ = singleSample (T3.emailForClient ci)
50
50
51api :: Proxy DocsAPI
51api :: Proxy DocsAPI
52api = Proxy
52api = Proxy