question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

For interoperation with poorly specced or flexible downstream APIs, clients should be able to represent a branch of an enumeration that isn’t known. This permits accepting unknown branches and doing work with them without rejecting the response from client calls (currently, enumeration lookup failures result in a decoder failure for clients).

Currently, generated enumerations look like this:

sealed abstract class Foo(val value: String) { override def toString: String = value.toString }
object Foo {
  object members {
    case object Bar extends Foo("Bar")
  }
  val Bar: Foo = members.Bar
  val values = Vector(Bar)
  def parse(value: String): Option[Foo] = values.find(_.value == value)
  implicit val encodeFoo: Encoder[Foo] = Encoder[String].contramap(_.value)
  implicit val decodeFoo: Decoder[Foo] = Decoder[String].emap(value => parse(value).toRight(s"$value not a member of Foo"))
  implicit val addPathFoo: AddPath[Foo] = AddPath.build(_.value)
  implicit val showFoo: Show[Foo] = Show.build(_.value)
}

A possible alteration to accept unknown members (currently unsure if this should be opt-in or opt-out, definitely needs more thought):

@@ -2,10 +2,12 @@
 object Foo {
   object members {
     case object Bar extends Foo("Bar")
+    class Unknown private[Foo] (value: String) extends Foo(value)
   }
   val Bar: Foo = members.Bar
+  def Unknown(value: String): Foo = new members.Unknown(value)
   val values = Vector(Bar)
-  def parse(value: String): Option[Foo] = values.find(_.value == value)
+  def parse(value: String): Option[Foo] = values.find(_.value == value).orElse(new members.Unknown(value))
   implicit val encodeFoo: Encoder[Foo] = Encoder[String].contramap(_.value)
   implicit val decodeFoo: Decoder[Foo] = Decoder[String].emap(value => parse(value).toRight(s"$value not a member of Foo"))
   implicit val addPathFoo: AddPath[Foo] = AddPath.build(_.value)

Initially I thought the Unknown branch should not expose a constructor, as it would prevent invalid data from being supplied by the clients into downstream services. This has the unfortunate side-effect of not allowing services to round-trip to persistence layers; consider receiving values of an array via client API call, writing those values into a database, then attempting to use those values to make future API calls.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
sulliscommented, Mar 24, 2021

fyi, I am aware of prior art in two open source projects:

  1. ApiBuilder
  2. AWS SDK for Java v2

ApiBuilder uses UNDEFINED as the marker for unknown enumeration values:

https://github.com/apicollective/apibuilder-generator/blob/main/scala-generator/src/main/scala/models/generator/ScalaEnums.scala

Here is example output from the ApiBuilder Scala generator:

https://app.apibuilder.io/flow/delta/0.8.36/scala_models/FlowDeltaV0Models.scala

AWS SDK for Java (version 2) defines a special Java enum value called UNKNOWN_TO_SDK_VERSION

https://github.com/aws/aws-sdk-java-v2/blob/master/codegen/src/main/java/software/amazon/awssdk/codegen/poet/common/AbstractEnumClass.java#L56

0reactions
blast-hardcheesecommented, Mar 25, 2021

Right, but that’s opt-in behavior

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extensible Enums - Business Central - Microsoft Learn
An enumeration type, also known as an enum in programming, is a keyword used to declare a type that consists of a set...
Read more >
Extending Enums in Java - Baeldung
We've learned that we can't create a subclass of an existing enum. However, an interface is extensible. Therefore, we can emulate extensible ......
Read more >
A .Net standard library providing extendable class based enums.
This library is available from NuGet.org. A .NET Standard class library that provides base classes for creating enumerations that can be extended with ......
Read more >
Extensible Enums in Dynamics 365 Business Central
Extensible Enums in Dynamics 365 Business Central ... Enums are object types in their own right, not merely data types you can assign...
Read more >
Extensible Enums - theDenSter
The AL language has an object type called 'enum'. This object type defines a list of possible values in the form of a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found