basic pretty schema rendering without colour so far

This commit is contained in:
Tom Sydney Kerckhove 2021-10-23 20:09:12 +02:00
parent 949665a190
commit 566493a245
47 changed files with 64 additions and 2 deletions

View File

@ -16,6 +16,8 @@ import qualified Data.List.NonEmpty as NE
import Data.Text (Text) import Data.Text (Text)
import GHC.Generics (Generic) import GHC.Generics (Generic)
-- TODO think about putting this value in a separate package or directly in autodocodec
--
-- http://json-schema.org/understanding-json-schema/reference/index.html -- http://json-schema.org/understanding-json-schema/reference/index.html
data JSONSchema data JSONSchema
= AnySchema = AnySchema

View File

@ -30,6 +30,7 @@ library
src src
build-depends: build-depends:
autodocodec autodocodec
, autodocodec-aeson
, base >=4.7 && <5 , base >=4.7 && <5
, safe-coloured-text , safe-coloured-text
, yaml , yaml

View File

@ -15,6 +15,7 @@ library:
source-dirs: src source-dirs: src
dependencies: dependencies:
- autodocodec - autodocodec
- autodocodec-aeson
- safe-coloured-text - safe-coloured-text
- yaml - yaml

View File

@ -1,14 +1,46 @@
{-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeApplications #-}
module Autodocodec.Yaml.Document where module Autodocodec.Yaml.Document where
import Autodocodec import Autodocodec
import Autodocodec.Aeson
import Text.Colour import Text.Colour
schemaChunksViaCodec :: forall a. HasCodec a => [Chunk] schemaChunksViaCodec :: forall a. HasCodec a => [Chunk]
schemaChunksViaCodec = schemaChunksVia (codec @a) schemaChunksViaCodec = schemaChunksVia (codec @a)
schemaChunksVia :: Codec input output -> [Chunk] schemaChunksVia :: Codec input output -> [Chunk]
schemaChunksVia _ = [] schemaChunksVia = jsonSchemaChunks . jsonSchemaVia
jsonSchemaChunks :: JSONSchema -> [Chunk]
jsonSchemaChunks = concatMap (\l -> l ++ ["\n"]) . go
where
indent :: [[Chunk]] -> [[Chunk]]
indent = map (" " :)
addInFrontOfFirstInList :: [Chunk] -> [[Chunk]] -> [[Chunk]]
addInFrontOfFirstInList cs = \case
[] -> [cs] -- Shouldn't happen, but fine if it doesn't
(l : ls) -> (cs ++ l) : indent ls
go :: JSONSchema -> [[Chunk]]
go = \case
AnySchema -> [["<any>"]]
NullSchema -> [["null"]]
BoolSchema -> [["<boolean>"]]
StringSchema -> [["<string>"]]
NumberSchema -> [["<number>"]]
ArraySchema s ->
let addListMarker = addInFrontOfFirstInList ["- "]
in indent $ addListMarker $ go s -- TODO add the dash
ObjectSchema s -> goObject s
ChoiceSchema s -> concatMap go s -- TODO add the list
goObject :: JSONObjectSchema -> [[Chunk]]
goObject = \case
AnyObjectSchema -> [["<object>"]]
KeySchema k ss -> addInFrontOfFirstInList [chunk k, ":", " "] (go ss)
BothObjectSchema os1 os2 -> goObject os1 ++ goObject os2

View File

@ -68,4 +68,4 @@ instance HasCodec Example where
yamlSchemaSpec :: forall a. (Show a, Eq a, Typeable a, GenValid a, HasCodec a) => FilePath -> Spec yamlSchemaSpec :: forall a. (Show a, Eq a, Typeable a, GenValid a, HasCodec a) => FilePath -> Spec
yamlSchemaSpec filePath = do yamlSchemaSpec filePath = do
it ("outputs the same schema as before for " <> nameOf @a) $ it ("outputs the same schema as before for " <> nameOf @a) $
pureGoldenByteStringFile ("test_resources/schema/" <> filePath <> ".json") (renderChunksBS With24BitColours $ schemaChunksViaCodec @a) pureGoldenByteStringFile ("test_resources/schema/" <> filePath <> ".txt") (renderChunksBS With24BitColours $ schemaChunksViaCodec @a)

View File

@ -0,0 +1 @@
<boolean>

View File

@ -0,0 +1 @@
<string>

View File

@ -0,0 +1,2 @@
Left: <boolean>
Right: <string>

View File

@ -0,0 +1,3 @@
Left: Left: <boolean>
Right: <number>
Right: <string>

View File

@ -0,0 +1,2 @@
text: <string>
bool: <boolean>

View File

@ -0,0 +1 @@
<number>

View File

@ -0,0 +1 @@
<number>

View File

@ -0,0 +1 @@
<number>

View File

@ -0,0 +1 @@
<number>

View File

@ -0,0 +1 @@
<number>

View File

@ -0,0 +1 @@
<string>

View File

@ -0,0 +1 @@
- <string>

View File

@ -0,0 +1,2 @@
null
<string>

View File

@ -0,0 +1 @@
<number>

View File

@ -0,0 +1 @@
<string>

View File

@ -0,0 +1 @@
<string>

View File

@ -0,0 +1 @@
<number>

View File

@ -0,0 +1 @@
<number>

View File

@ -0,0 +1 @@
<number>

View File

@ -0,0 +1 @@
<number>

View File

@ -0,0 +1 @@
<number>