Skip to content

Commit

Permalink
Merge pull request #41 from michelk/compilable-example
Browse files Browse the repository at this point in the history
Adapt example in README to make it compile
  • Loading branch information
NorfairKing committed Jun 1, 2023
2 parents 72033e0 + 032898f commit d0d7b2b
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions README.md
Expand Up @@ -29,16 +29,28 @@ This project is ready to try out!
## Fully featured example

``` haskell
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE TypeApplications #-}
module Main (main) where


import Autodocodec
import Autodocodec.Yaml
import GHC.Generics
import Data.Aeson (FromJSON, ToJSON)
import Data.Text (Text)
import qualified Data.Text as T

data Example = Example
{ exampleTextField :: !Text,
exampleIntField :: !Int
}
deriving stock (Show, Eq, Generic)
deriving
( FromJSON, -- <- FromJSON instance for free.
ToJSON, -- <- ToJSON instance for free.
Swagger.ToSchema, -- <- Swagger schema for free.
OpenAPI.ToSchema -- <- OpenAPI schema for free.
ToJSON -- <- ToJSON instance for free.
)
via (Autodocodec Example)

Expand All @@ -48,7 +60,25 @@ instance HasCodec Example where
Example
<$> requiredField "text" "documentation for the text field" .= exampleTextField
<*> requiredField "int" "documentation for the int field" .= exampleIntField

main :: IO ()
main = do
let schema = T.unpack $ renderColouredSchemaViaCodec @Example
putStrLn schema
```

This will output a nice coloured yaml-schema:

```
# Example
text: # required
# documentation for the text field
<string>
int: # required
# documentation for the int field
<number> # between -9223372036854775808 and 9223372036854775807
```


## Tests

Expand Down

0 comments on commit d0d7b2b

Please sign in to comment.