From 566493a245682b1bd57072d9651fe5eb41a06a55 Mon Sep 17 00:00:00 2001 From: Tom Sydney Kerckhove Date: Sat, 23 Oct 2021 20:09:12 +0200 Subject: [PATCH] basic pretty schema rendering without colour so far --- .../src/Autodocodec/Aeson/Document.hs | 2 ++ autodocodec-yaml/autodocodec-yaml.cabal | 1 + autodocodec-yaml/package.yaml | 1 + .../src/Autodocodec/Yaml/Document.hs | 34 ++++++++++++++++++- .../test/Autodocodec/Yaml/DocumentSpec.hs | 2 +- .../test_resources/schema/bool.json | 0 .../test_resources/schema/bool.txt | 1 + .../test_resources/schema/char.json | 0 .../test_resources/schema/char.txt | 1 + .../schema/either-bool-text.json | 0 .../schema/either-bool-text.txt | 2 ++ .../either-either-bool-scientific-text.json | 0 .../either-either-bool-scientific-text.txt | 3 ++ .../test_resources/schema/example.json | 0 .../test_resources/schema/example.txt | 2 ++ .../test_resources/schema/int.json | 0 .../test_resources/schema/int.txt | 1 + .../test_resources/schema/int16.json | 0 .../test_resources/schema/int16.txt | 1 + .../test_resources/schema/int32.json | 0 .../test_resources/schema/int32.txt | 1 + .../test_resources/schema/int64.json | 0 .../test_resources/schema/int64.txt | 1 + .../test_resources/schema/int8.json | 0 .../test_resources/schema/int8.txt | 1 + .../test_resources/schema/lazy-text.json | 0 .../test_resources/schema/lazy-text.txt | 1 + .../test_resources/schema/list-text.json | 0 .../test_resources/schema/list-text.txt | 1 + .../test_resources/schema/maybe-text.json | 0 .../test_resources/schema/maybe-text.txt | 2 ++ .../test_resources/schema/scientific.json | 0 .../test_resources/schema/scientific.txt | 1 + .../test_resources/schema/string.json | 0 .../test_resources/schema/string.txt | 1 + .../test_resources/schema/text.json | 0 .../test_resources/schema/text.txt | 1 + .../test_resources/schema/word.json | 0 .../test_resources/schema/word.txt | 1 + .../test_resources/schema/word16.json | 0 .../test_resources/schema/word16.txt | 1 + .../test_resources/schema/word32.json | 0 .../test_resources/schema/word32.txt | 1 + .../test_resources/schema/word64.json | 0 .../test_resources/schema/word64.txt | 1 + .../test_resources/schema/word8.json | 0 .../test_resources/schema/word8.txt | 1 + 47 files changed, 64 insertions(+), 2 deletions(-) delete mode 100644 autodocodec-yaml/test_resources/schema/bool.json create mode 100644 autodocodec-yaml/test_resources/schema/bool.txt delete mode 100644 autodocodec-yaml/test_resources/schema/char.json create mode 100644 autodocodec-yaml/test_resources/schema/char.txt delete mode 100644 autodocodec-yaml/test_resources/schema/either-bool-text.json create mode 100644 autodocodec-yaml/test_resources/schema/either-bool-text.txt delete mode 100644 autodocodec-yaml/test_resources/schema/either-either-bool-scientific-text.json create mode 100644 autodocodec-yaml/test_resources/schema/either-either-bool-scientific-text.txt delete mode 100644 autodocodec-yaml/test_resources/schema/example.json create mode 100644 autodocodec-yaml/test_resources/schema/example.txt delete mode 100644 autodocodec-yaml/test_resources/schema/int.json create mode 100644 autodocodec-yaml/test_resources/schema/int.txt delete mode 100644 autodocodec-yaml/test_resources/schema/int16.json create mode 100644 autodocodec-yaml/test_resources/schema/int16.txt delete mode 100644 autodocodec-yaml/test_resources/schema/int32.json create mode 100644 autodocodec-yaml/test_resources/schema/int32.txt delete mode 100644 autodocodec-yaml/test_resources/schema/int64.json create mode 100644 autodocodec-yaml/test_resources/schema/int64.txt delete mode 100644 autodocodec-yaml/test_resources/schema/int8.json create mode 100644 autodocodec-yaml/test_resources/schema/int8.txt delete mode 100644 autodocodec-yaml/test_resources/schema/lazy-text.json create mode 100644 autodocodec-yaml/test_resources/schema/lazy-text.txt delete mode 100644 autodocodec-yaml/test_resources/schema/list-text.json create mode 100644 autodocodec-yaml/test_resources/schema/list-text.txt delete mode 100644 autodocodec-yaml/test_resources/schema/maybe-text.json create mode 100644 autodocodec-yaml/test_resources/schema/maybe-text.txt delete mode 100644 autodocodec-yaml/test_resources/schema/scientific.json create mode 100644 autodocodec-yaml/test_resources/schema/scientific.txt delete mode 100644 autodocodec-yaml/test_resources/schema/string.json create mode 100644 autodocodec-yaml/test_resources/schema/string.txt delete mode 100644 autodocodec-yaml/test_resources/schema/text.json create mode 100644 autodocodec-yaml/test_resources/schema/text.txt delete mode 100644 autodocodec-yaml/test_resources/schema/word.json create mode 100644 autodocodec-yaml/test_resources/schema/word.txt delete mode 100644 autodocodec-yaml/test_resources/schema/word16.json create mode 100644 autodocodec-yaml/test_resources/schema/word16.txt delete mode 100644 autodocodec-yaml/test_resources/schema/word32.json create mode 100644 autodocodec-yaml/test_resources/schema/word32.txt delete mode 100644 autodocodec-yaml/test_resources/schema/word64.json create mode 100644 autodocodec-yaml/test_resources/schema/word64.txt delete mode 100644 autodocodec-yaml/test_resources/schema/word8.json create mode 100644 autodocodec-yaml/test_resources/schema/word8.txt diff --git a/autodocodec-aeson/src/Autodocodec/Aeson/Document.hs b/autodocodec-aeson/src/Autodocodec/Aeson/Document.hs index 7ab4960..7c1adca 100644 --- a/autodocodec-aeson/src/Autodocodec/Aeson/Document.hs +++ b/autodocodec-aeson/src/Autodocodec/Aeson/Document.hs @@ -16,6 +16,8 @@ import qualified Data.List.NonEmpty as NE import Data.Text (Text) 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 data JSONSchema = AnySchema diff --git a/autodocodec-yaml/autodocodec-yaml.cabal b/autodocodec-yaml/autodocodec-yaml.cabal index d6fd2da..0f8c4e0 100644 --- a/autodocodec-yaml/autodocodec-yaml.cabal +++ b/autodocodec-yaml/autodocodec-yaml.cabal @@ -30,6 +30,7 @@ library src build-depends: autodocodec + , autodocodec-aeson , base >=4.7 && <5 , safe-coloured-text , yaml diff --git a/autodocodec-yaml/package.yaml b/autodocodec-yaml/package.yaml index 345c66c..9ace312 100644 --- a/autodocodec-yaml/package.yaml +++ b/autodocodec-yaml/package.yaml @@ -15,6 +15,7 @@ library: source-dirs: src dependencies: - autodocodec + - autodocodec-aeson - safe-coloured-text - yaml diff --git a/autodocodec-yaml/src/Autodocodec/Yaml/Document.hs b/autodocodec-yaml/src/Autodocodec/Yaml/Document.hs index 445412d..87f6178 100644 --- a/autodocodec-yaml/src/Autodocodec/Yaml/Document.hs +++ b/autodocodec-yaml/src/Autodocodec/Yaml/Document.hs @@ -1,14 +1,46 @@ {-# LANGUAGE AllowAmbiguousTypes #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} module Autodocodec.Yaml.Document where import Autodocodec +import Autodocodec.Aeson import Text.Colour schemaChunksViaCodec :: forall a. HasCodec a => [Chunk] schemaChunksViaCodec = schemaChunksVia (codec @a) 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 -> [[""]] + NullSchema -> [["null"]] + BoolSchema -> [[""]] + StringSchema -> [[""]] + NumberSchema -> [[""]] + 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 -> [[""]] + KeySchema k ss -> addInFrontOfFirstInList [chunk k, ":", " "] (go ss) + BothObjectSchema os1 os2 -> goObject os1 ++ goObject os2 diff --git a/autodocodec-yaml/test/Autodocodec/Yaml/DocumentSpec.hs b/autodocodec-yaml/test/Autodocodec/Yaml/DocumentSpec.hs index 61095b9..63090ba 100644 --- a/autodocodec-yaml/test/Autodocodec/Yaml/DocumentSpec.hs +++ b/autodocodec-yaml/test/Autodocodec/Yaml/DocumentSpec.hs @@ -68,4 +68,4 @@ instance HasCodec Example where yamlSchemaSpec :: forall a. (Show a, Eq a, Typeable a, GenValid a, HasCodec a) => FilePath -> Spec yamlSchemaSpec filePath = do 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) diff --git a/autodocodec-yaml/test_resources/schema/bool.json b/autodocodec-yaml/test_resources/schema/bool.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/bool.txt b/autodocodec-yaml/test_resources/schema/bool.txt new file mode 100644 index 0000000..0decf3c --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/bool.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/char.json b/autodocodec-yaml/test_resources/schema/char.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/char.txt b/autodocodec-yaml/test_resources/schema/char.txt new file mode 100644 index 0000000..e92539d --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/char.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/either-bool-text.json b/autodocodec-yaml/test_resources/schema/either-bool-text.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/either-bool-text.txt b/autodocodec-yaml/test_resources/schema/either-bool-text.txt new file mode 100644 index 0000000..070e3e1 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/either-bool-text.txt @@ -0,0 +1,2 @@ +Left: +Right: diff --git a/autodocodec-yaml/test_resources/schema/either-either-bool-scientific-text.json b/autodocodec-yaml/test_resources/schema/either-either-bool-scientific-text.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/either-either-bool-scientific-text.txt b/autodocodec-yaml/test_resources/schema/either-either-bool-scientific-text.txt new file mode 100644 index 0000000..ab7a48d --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/either-either-bool-scientific-text.txt @@ -0,0 +1,3 @@ +Left: Left: + Right: +Right: diff --git a/autodocodec-yaml/test_resources/schema/example.json b/autodocodec-yaml/test_resources/schema/example.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/example.txt b/autodocodec-yaml/test_resources/schema/example.txt new file mode 100644 index 0000000..9c1e23e --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/example.txt @@ -0,0 +1,2 @@ +text: +bool: diff --git a/autodocodec-yaml/test_resources/schema/int.json b/autodocodec-yaml/test_resources/schema/int.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/int.txt b/autodocodec-yaml/test_resources/schema/int.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/int.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/int16.json b/autodocodec-yaml/test_resources/schema/int16.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/int16.txt b/autodocodec-yaml/test_resources/schema/int16.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/int16.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/int32.json b/autodocodec-yaml/test_resources/schema/int32.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/int32.txt b/autodocodec-yaml/test_resources/schema/int32.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/int32.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/int64.json b/autodocodec-yaml/test_resources/schema/int64.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/int64.txt b/autodocodec-yaml/test_resources/schema/int64.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/int64.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/int8.json b/autodocodec-yaml/test_resources/schema/int8.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/int8.txt b/autodocodec-yaml/test_resources/schema/int8.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/int8.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/lazy-text.json b/autodocodec-yaml/test_resources/schema/lazy-text.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/lazy-text.txt b/autodocodec-yaml/test_resources/schema/lazy-text.txt new file mode 100644 index 0000000..e92539d --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/lazy-text.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/list-text.json b/autodocodec-yaml/test_resources/schema/list-text.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/list-text.txt b/autodocodec-yaml/test_resources/schema/list-text.txt new file mode 100644 index 0000000..c4f5b82 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/list-text.txt @@ -0,0 +1 @@ + - diff --git a/autodocodec-yaml/test_resources/schema/maybe-text.json b/autodocodec-yaml/test_resources/schema/maybe-text.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/maybe-text.txt b/autodocodec-yaml/test_resources/schema/maybe-text.txt new file mode 100644 index 0000000..777c0fa --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/maybe-text.txt @@ -0,0 +1,2 @@ +null + diff --git a/autodocodec-yaml/test_resources/schema/scientific.json b/autodocodec-yaml/test_resources/schema/scientific.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/scientific.txt b/autodocodec-yaml/test_resources/schema/scientific.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/scientific.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/string.json b/autodocodec-yaml/test_resources/schema/string.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/string.txt b/autodocodec-yaml/test_resources/schema/string.txt new file mode 100644 index 0000000..e92539d --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/string.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/text.json b/autodocodec-yaml/test_resources/schema/text.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/text.txt b/autodocodec-yaml/test_resources/schema/text.txt new file mode 100644 index 0000000..e92539d --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/text.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/word.json b/autodocodec-yaml/test_resources/schema/word.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/word.txt b/autodocodec-yaml/test_resources/schema/word.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/word.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/word16.json b/autodocodec-yaml/test_resources/schema/word16.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/word16.txt b/autodocodec-yaml/test_resources/schema/word16.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/word16.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/word32.json b/autodocodec-yaml/test_resources/schema/word32.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/word32.txt b/autodocodec-yaml/test_resources/schema/word32.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/word32.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/word64.json b/autodocodec-yaml/test_resources/schema/word64.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/word64.txt b/autodocodec-yaml/test_resources/schema/word64.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/word64.txt @@ -0,0 +1 @@ + diff --git a/autodocodec-yaml/test_resources/schema/word8.json b/autodocodec-yaml/test_resources/schema/word8.json deleted file mode 100644 index e69de29..0000000 diff --git a/autodocodec-yaml/test_resources/schema/word8.txt b/autodocodec-yaml/test_resources/schema/word8.txt new file mode 100644 index 0000000..40fd2b4 --- /dev/null +++ b/autodocodec-yaml/test_resources/schema/word8.txt @@ -0,0 +1 @@ +