self(auto)- documenting encoders and decoders
Go to file
2024-09-01 16:36:12 +02:00
.github Upgrade to lts 20.23 and nixos 23.05 2023-06-01 17:07:23 +02:00
autodocodec Added boundedEnumCodec 2024-09-01 16:36:12 +02:00
autodocodec-api-usage Added boundedEnumCodec 2024-09-01 16:36:12 +02:00
autodocodec-nix more accurate support for EitherCodec 2024-08-22 01:22:32 +02:00
autodocodec-openapi3 Remove orphan instance for YAML encoder 2024-08-04 18:13:27 +02:00
autodocodec-schema Integer codec 2024-07-26 08:57:47 +02:00
autodocodec-servant-multipart released the missing releases after autodocodec 0.3 2024-07-21 15:15:47 +02:00
autodocodec-swagger2 Remove orphan instance for YAML encoder 2024-08-04 18:13:27 +02:00
autodocodec-yaml Remove orphan instance for YAML encoder 2024-08-04 18:13:27 +02:00
nix Integer codec 2024-07-26 08:57:47 +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 Add 'HasCodec' instances for 'DList' and 'DNonEmpty' from the 'dlist' package 2024-08-06 23:50:10 +10:00
flake.nix fix the nix type of Word 2024-08-20 23:13:47 +02:00
LICENSE Released autodocodec-yaml-0.2.0.3 2023-01-01 21:30:59 +01:00
README.md Update README.md 2024-07-31 15:26:10 -05:00
stack.yaml Add 'HasCodec' instances for 'DList' and 'DNonEmpty' from the 'dlist' package 2024-08-06 23:50:10 +10: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
  <integer> # 64 bit int

Tests

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