Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper method for creating custom enum schemas #1953

Merged
merged 4 commits into from
Oct 22, 2023

Conversation

kyri-petrou
Copy link
Collaborator

@kyri-petrou kyri-petrou commented Oct 22, 2023

Currently, the only way for a user to create a custom enum schema is a bit verbose, and requires the user to look up the enum schema derivation in order to implement one:

trait Foo
object Foo {
  case object A extends Foo
  case object B extends Foo
}

implicit val fooSchema: Schema[Any, Foo] = new Schema[Any, Foo] {
  def toType(isInput: Boolean = false, isSubscription: Boolean = false): __Type =
    Types.makeEnum(
      Some("Foo"),
      None,
      List(__EnumValue("FooB", None, false, None, None),  __EnumValue("FooA", None, false, None, None)),
      None,
    )

  override def resolve(value: Foo): Step[R] = PureStep(StringValue("Foo" + value.toString))
}

With this PR, we add a helper method in the Schema companion object to make it easier to create one:

import Schema._

implicit val fooSchema: Schema[Any, Foo] = enumSchema[Foo](
  "Foo",
  values = List(enumValue("FooA"), enumValue("FooB")),
  repr = v => "Foo" + v.toString
)

@kyri-petrou kyri-petrou merged commit 82e2ff6 into series/2.x Oct 22, 2023
@kyri-petrou kyri-petrou deleted the enum-schema-helper-method branch October 22, 2023 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants