Started on autodocodec-yaml

This commit is contained in:
Tom Sydney Kerckhove 2021-10-23 19:51:31 +02:00
parent cc910db093
commit 949665a190
32 changed files with 229 additions and 3 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2020-2021 Tom Sydney Kerckhove
Copyright (c) 2021 Tom Sydney Kerckhove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

2
autodocodec-yaml/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.stack-work/
*~

21
autodocodec-yaml/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Tom Sydney Kerckhove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,65 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- see: https://github.com/sol/hpack
name: autodocodec-yaml
version: 0.0.0.0
synopsis: Self-documenting encoder and decoder for yaml
homepage: https://github.com/NorfairKing/autodocodec#readme
bug-reports: https://github.com/NorfairKing/autodocodec/issues
author: Tom Sydney Kerckhove
maintainer: syd@cs-syd.eu
copyright: 2021 Tom Sydney Kerckhove
license: MIT
license-file: LICENSE
build-type: Simple
source-repository head
type: git
location: https://github.com/NorfairKing/autodocodec
library
exposed-modules:
Autodocodec.Yaml
Autodocodec.Yaml.Document
other-modules:
Paths_autodocodec_yaml
hs-source-dirs:
src
build-depends:
autodocodec
, base >=4.7 && <5
, safe-coloured-text
, yaml
default-language: Haskell2010
test-suite autodocodec-yaml-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Autodocodec.Yaml.DocumentSpec
Paths_autodocodec_yaml
hs-source-dirs:
test/
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
build-tool-depends:
sydtest-discover:sydtest-discover
build-depends:
aeson
, autodocodec
, autodocodec-aeson
, autodocodec-yaml
, base >=4.7 && <5
, bytestring
, genvalidity
, genvalidity-scientific
, genvalidity-sydtest
, genvalidity-text
, safe-coloured-text
, scientific
, sydtest
, sydtest-aeson
, text
default-language: Haskell2010

View File

@ -0,0 +1,45 @@
name: autodocodec-yaml
version: 0.0.0.0
github: "NorfairKing/autodocodec"
license: MIT
license-file: LICENSE
author: "Tom Sydney Kerckhove"
maintainer: "syd@cs-syd.eu"
copyright: "2021 Tom Sydney Kerckhove"
synopsis: Self-documenting encoder and decoder for yaml
dependencies:
- base >= 4.7 && < 5
library:
source-dirs: src
dependencies:
- autodocodec
- safe-coloured-text
- yaml
tests:
autodocodec-yaml-test:
main: Spec.hs
source-dirs: test/
build-tools: sydtest-discover
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
- -Wall
dependencies:
- aeson
- autodocodec
- autodocodec-aeson
- autodocodec-yaml
- bytestring
- genvalidity
- genvalidity-scientific
- genvalidity-sydtest
- genvalidity-text
- safe-coloured-text
- scientific
- sydtest
- sydtest-aeson
- text

View File

@ -0,0 +1,7 @@
module Autodocodec.Yaml
( -- * To makes sure we definitely export everything.
module Autodocodec.Yaml.Document,
)
where
import Autodocodec.Yaml.Document

View File

@ -0,0 +1,14 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Autodocodec.Yaml.Document where
import Autodocodec
import Text.Colour
schemaChunksViaCodec :: forall a. HasCodec a => [Chunk]
schemaChunksViaCodec = schemaChunksVia (codec @a)
schemaChunksVia :: Codec input output -> [Chunk]
schemaChunksVia _ = []

View File

@ -0,0 +1,71 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Autodocodec.Yaml.DocumentSpec (spec) where
import Autodocodec
import Autodocodec.Yaml
import Data.Data
import Data.GenValidity
import Data.GenValidity.Scientific ()
import Data.GenValidity.Text ()
import Data.Int
import Data.Scientific
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
import Data.Word
import GHC.Generics (Generic)
import Test.Syd
import Test.Syd.Validity.Utils
import Text.Colour
spec :: Spec
spec = do
yamlSchemaSpec @Bool "bool"
yamlSchemaSpec @Char "char"
yamlSchemaSpec @Text "text"
yamlSchemaSpec @LT.Text "lazy-text"
yamlSchemaSpec @String "string"
yamlSchemaSpec @Scientific "scientific"
yamlSchemaSpec @Int "int"
yamlSchemaSpec @Int8 "int8"
yamlSchemaSpec @Int16 "int16"
yamlSchemaSpec @Int32 "int32"
yamlSchemaSpec @Int64 "int64"
yamlSchemaSpec @Word "word"
yamlSchemaSpec @Word8 "word8"
yamlSchemaSpec @Word16 "word16"
yamlSchemaSpec @Word32 "word32"
yamlSchemaSpec @Word64 "word64"
yamlSchemaSpec @(Maybe Text) "maybe-text"
yamlSchemaSpec @(Either Bool Text) "either-bool-text"
yamlSchemaSpec @(Either (Either Bool Scientific) Text) "either-either-bool-scientific-text"
yamlSchemaSpec @[Text] "list-text"
yamlSchemaSpec @Example "example"
data Example = Example
{ exampleText :: !Text,
exampleBool :: !Bool
}
deriving (Show, Eq, Generic)
instance Validity Example
instance GenValid Example where
genValid = genValidStructurally
shrinkValid = shrinkValidStructurally
instance HasCodec Example where
codec =
object $
Example
<$> field "text" .= exampleText
<*> field "bool" .= exampleBool
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)

View File

@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF sydtest-discover #-}

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2020-2021 Tom Sydney Kerckhove
Copyright (c) 2021 Tom Sydney Kerckhove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -2,7 +2,7 @@ resolver: nightly-2021-05-10
packages:
- autodocodec
- autodocodec-aeson
# - autodocodec-yaml
- autodocodec-yaml
extra-deps:
- github: NorfairKing/validity