mirror of
https://github.com/NorfairKing/autodocodec.git
synced 2024-11-26 08:01:33 +03:00
basic pretty schema rendering without colour so far
This commit is contained in:
parent
949665a190
commit
566493a245
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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)
|
||||||
|
1
autodocodec-yaml/test_resources/schema/bool.txt
Normal file
1
autodocodec-yaml/test_resources/schema/bool.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<boolean>
|
1
autodocodec-yaml/test_resources/schema/char.txt
Normal file
1
autodocodec-yaml/test_resources/schema/char.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<string>
|
@ -0,0 +1,2 @@
|
|||||||
|
Left: <boolean>
|
||||||
|
Right: <string>
|
@ -0,0 +1,3 @@
|
|||||||
|
Left: Left: <boolean>
|
||||||
|
Right: <number>
|
||||||
|
Right: <string>
|
2
autodocodec-yaml/test_resources/schema/example.txt
Normal file
2
autodocodec-yaml/test_resources/schema/example.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
text: <string>
|
||||||
|
bool: <boolean>
|
1
autodocodec-yaml/test_resources/schema/int.txt
Normal file
1
autodocodec-yaml/test_resources/schema/int.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
1
autodocodec-yaml/test_resources/schema/int16.txt
Normal file
1
autodocodec-yaml/test_resources/schema/int16.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
1
autodocodec-yaml/test_resources/schema/int32.txt
Normal file
1
autodocodec-yaml/test_resources/schema/int32.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
1
autodocodec-yaml/test_resources/schema/int64.txt
Normal file
1
autodocodec-yaml/test_resources/schema/int64.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
1
autodocodec-yaml/test_resources/schema/int8.txt
Normal file
1
autodocodec-yaml/test_resources/schema/int8.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
1
autodocodec-yaml/test_resources/schema/lazy-text.txt
Normal file
1
autodocodec-yaml/test_resources/schema/lazy-text.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<string>
|
1
autodocodec-yaml/test_resources/schema/list-text.txt
Normal file
1
autodocodec-yaml/test_resources/schema/list-text.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
- <string>
|
2
autodocodec-yaml/test_resources/schema/maybe-text.txt
Normal file
2
autodocodec-yaml/test_resources/schema/maybe-text.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
null
|
||||||
|
<string>
|
1
autodocodec-yaml/test_resources/schema/scientific.txt
Normal file
1
autodocodec-yaml/test_resources/schema/scientific.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
1
autodocodec-yaml/test_resources/schema/string.txt
Normal file
1
autodocodec-yaml/test_resources/schema/string.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<string>
|
1
autodocodec-yaml/test_resources/schema/text.txt
Normal file
1
autodocodec-yaml/test_resources/schema/text.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<string>
|
1
autodocodec-yaml/test_resources/schema/word.txt
Normal file
1
autodocodec-yaml/test_resources/schema/word.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
1
autodocodec-yaml/test_resources/schema/word16.txt
Normal file
1
autodocodec-yaml/test_resources/schema/word16.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
1
autodocodec-yaml/test_resources/schema/word32.txt
Normal file
1
autodocodec-yaml/test_resources/schema/word32.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
1
autodocodec-yaml/test_resources/schema/word64.txt
Normal file
1
autodocodec-yaml/test_resources/schema/word64.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
1
autodocodec-yaml/test_resources/schema/word8.txt
Normal file
1
autodocodec-yaml/test_resources/schema/word8.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<number>
|
Loading…
Reference in New Issue
Block a user