Search code examples
scalapattern-matchingcircereserved-words

Pattern matching on a Scala case class with `type` as a field


I have a case class defined for JSON parsing using the Circe library:

  final case class Event(
    source: Option[String],
    nonce: Option[Int],
    `type`: Option[Any],
    tag: Option[String],
    payload: Option[Any],
    blockOrder: Option[Int] = None,
    metadata: ResultMetadata[OperationResult.Event]
  ) extends Operation

Note that one of the fields is called type and I can only use the name using backticks or I get a compiler error. I don't have any choice over the schema used by the source system supplying the data.

Later on, when I try to pattern match on the case class in order to write some information to a database...

private val convertEvent: PartialFunction[
    (Block, OperationHash, Operation),
    Tables.OperationsRow
  ] = {
    case (
      block,
      groupHash,
      Event(source, nonce, `type`, tag, payload, blockOrder, metadata)
      ) =>
    ...

...I get the following error:

not found: value type
      Event(source, nonce, `type`, tag, payload, blockOrder, metadata).

Is there any way I can still use Circe and case classes in general when a field has the same name as a reserved keyword?


Solution

  • In 2.13.8 the error is

    not found: value type
    Identifiers enclosed in backticks are not pattern variables but match the value in scope.
          Event(source, nonce, `type`, tag, payload, blockOrder, metadata)
    

    so just use something different from type in pattern matching as a pattern variable.

    For example

    case (
      block,
      groupHash,
      Event(source, nonce, typ, tag, payload, blockOrder, metadata)
      ) =>