self(auto)- documenting encoders and decoders
Go to file
Tom Sydney Kerckhove 36779707f3 Released
2024-06-23 18:26:48 +02:00
.github Upgrade to lts 20.23 and nixos 23.05 2023-06-02 09:49:23 +02:00
autodocodec Ready for release 2024-06-23 16:32:26 +02:00
autodocodec-api-usage Nicer number bounds 2024-06-15 22:59:08 +02:00
autodocodec-openapi3 Upgrade to check against ghc 9.10 2024-05-30 08:43:51 +02:00
autodocodec-schema Upgrade schema too 2024-06-23 17:08:40 +02:00
autodocodec-servant-multipart Upgrade to check against ghc 9.10 2024-05-30 08:43:51 +02:00
autodocodec-swagger2 Upgrade to check against ghc 9.10 2024-05-30 08:43:51 +02:00
autodocodec-yaml Ready for release 2024-06-23 16:32:26 +02:00
nix forward compatibility 2023-10-09 22:52:26 +02:00
.envrc Nix flake 2022-11-30 14:33:11 +01:00
.gitignore Nix flake 2022-11-30 14:33:11 +01:00
.hlint.yaml autodocodec-servant-multipart 2022-09-20 00:59:49 +02:00
cabal.project Nix flake 2022-11-30 14:33:11 +01:00
flake.lock Released 2024-06-23 18:26:48 +02:00
flake.nix simplify shell 2024-06-23 16:49:34 +02:00
LICENSE Released autodocodec-yaml-0.2.0.3 2023-01-01 21:30:59 +01:00
README.md Adapt example in README to make it compile 2023-06-01 18:08:04 +02:00
stack.yaml Ready for release 2024-06-23 16:32:26 +02:00
TODO.smos autodocodec-nix todo 2022-09-20 00:42:10 +02:00

Autodocodec

Autodocodec is short for "self(auto)- documenting encoder and decoder".

In short: You write a single instance, of the 'Codec' type-class, for your type, and you get:

See the golden test directory directory for example outputs.

Features

  • ✓ Correct-by-construction encoding and decoding, without generating code.
  • ✓ Generate automatically-correct documentation from code.
  • ✓ Support for recursive types.

State of this project

This project is ready to try out!

{-# 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.
    )
    via (Autodocodec Example)

instance HasCodec Example where
  codec =
    object "Example" $
      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

While we don't provide any actual guarantees, we do have tests for the following properties that we would like to maintain: