Skip to content

Conversation

@dontgitit
Copy link
Contributor

I didn't make it implicit like all the other ArgBuilders because its type conflicts with the existing ArgBuilder[String]

case BooleanValue(value) => Right(value)
case other => Left(InvalidInputArgument("Boolean", other))
}
lazy val enum: ArgBuilder[String] = {
Copy link
Contributor Author

@dontgitit dontgitit Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can also potentially provide something like this for Java enums:

  implicit def enumArgBuilder[T <: java.lang.Enum[T]: ClassTag]: ArgBuilder[T] =
    enum.map(v => java.lang.Enum.valueOf[T](summon[ClassTag[T]].runtimeClass.asInstanceOf[Class[T]], v))

not entirely sure how to do that for Scala enums since scala.reflect.Enum doesn't provide valueOf (but I think actual subclasses do?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe something like this:

implicit def enumArgBuilder[T <: java.lang.Enum[T]: ClassTag]: ArgBuilder[T] =
  `enum`.flatMap { value =>
    val cls = summon[ClassTag[T]].runtimeClass.asInstanceOf[Class[T]]
    Try {
      java.lang.Enum.valueOf[T](cls, value)
    }.toEither.left.map { t =>
      ExecutionError(s"'$value' is not a valid value of ${cls.getSimpleName}", innerThrowable = Some(t))
    }
  }

case BooleanValue(value) => Right(value)
case other => Left(InvalidInputArgument("Boolean", other))
}
lazy val `enum`: ArgBuilder[String] = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the goal to use this + map for enums?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's correct, that was the intention!

i guess maybe to make that clearer, it should be something like the following...?

private lazy val enumBuilder: ArgBuilder[String]                  = {
   case EnumValue(value) => Right(value)
   case other            => Left(InvalidInputArgument("Enum", other))
}

def `enum`[A](f: T => A): ArgBuilder[A] = enumBuilder.map(f)

def `enum`[A](f: T => Either[ExecutionError, A]): ArgBuilder[A] = enumBuilder.flatMap(f)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be f: String => ..., right? Maybe we can call it enumString and drop the backticks.

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