Skip to content

Instantly share code, notes, and snippets.

@parsonsmatt
Last active April 3, 2017 17:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsonsmatt/fc017f3296e7b830fdcc5926d98297c0 to your computer and use it in GitHub Desktop.
Save parsonsmatt/fc017f3296e7b830fdcc5926d98297c0 to your computer and use it in GitHub Desktop.
i love this
instance ToSchema (Book' '[]) where
declareNamedSchema _ =
pure . NamedSchema Nothing $ mempty
& type_ .~ SwaggerObject
instance (KnownSymbol k, ToSchema v, ToSchema (Book' xs)) => ToSchema (Book' ( k :=> v ': xs )) where
declareNamedSchema _ = do
valRef <- declareSchemaRef (Proxy @v)
let key = Text.pack (symbolVal (Proxy @k))
rest = toNamedSchema (Proxy @(Book' xs))
pure $ rest
& schema . properties %~ (at key ?~ valRef)
{-
λ> encode $ toSchema (Proxy @(Book '["foo" :=> Bool, "bar" :=> Int]))
"{\"properties\":{\"foo\":{\"type\":\"boolean\"},\"bar\":{\"maximum\":9223372036854775807,\"minimum\":-9223372036854775808,\"type\":\"integer\"}},\"type\":\"object\"}"
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment