NorfairKing / autodocodec

self(auto)- documenting encoders and decoders

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generic HasCodec

dmjio opened this issue · comments

What do you think about adding a Generic deriving for HasCodec, with optional type level info to guide the instance creation.

data Example = Example
  { exampleTextField :: !Text,
    exampleIntField :: !Int
  }
  deriving stock (Show, Eq, Generic)
  deriving
    ( FromJSON, -- <- FromJSON instance for free.
      ToJSON, -- <- ToJSON instance for free.
      Swagger.ToSchema, -- <- Swagger schema for free.
      OpenAPI.ToSchema -- <- OpenAPI schema for free.
    )
    via (Autodocodec Example)
  deriving
    HasCodec via (Codec CodecExample Example)

type CodecExample =
  '[ Field "exampleTextField" "sample text field description" Required
   , Field "exampleIntField" "sample int field description" Required
   ]

I've done something similar to this on a project that is very similar to autodocodec.

I'm fine with this in theory but I'm not convinced that this will be any simpler than just writing a HasCodec instance. If it is then great, otherwise I'm hesitant.
This is an example of where code will win the argument.

That's fair, I think it'd be close to equivalent in this case.