catapult icon indicating copy to clipboard operation
catapult copied to clipboard

Add a `simple` ContextEncoder for quick definitions of simple encoders

Open averymcnab opened this issue 2 years ago • 0 comments

This is largely a quality-of-life suggestion to make encoder definitions less painful, especially when they have only one or two fields.

Add a def simple() method on the ContextEncoder companion object, such as:

def simple[Ctx](_kind: String)(_key: Ctx => String): ContextEncoder[Ctx] = new ContextEncoder[Ctx] {
  ...
}

Alongside this define methods to allow additional attributes, e.g.

def withStringAttribute(attributeName: String)(f: Ctx => String): ContextEncoder[Ctx] = ...

def withBooleanAttribute(attributeName: String)(f: Ctx => Boolean): ContextEncoder[Ctx] = ...

These can then be used together, for example:

implicit val anEncoder: ContextEncoder[Type] =
  ContextEncoder
    .simple[Type]("kind")(key = type.someField)
    .withStringAttribute("otherKind")(_.someOtherField)

averymcnab avatar Nov 02 '23 17:11 averymcnab