Skip to content

Commit

Permalink
Support open enums
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Oct 5, 2023
1 parent f878c18 commit ffb8595
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
7 changes: 7 additions & 0 deletions modules/core/src/main/scala/playground/CompilationError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ sealed trait CompilationErrorDetails extends Product with Serializable {
case EmptyStruct(possibleValues) =>
s"found empty struct, expected one of: ${possibleValues.mkString_(", ")}."

case InvalidIntEnumValue(value) =>
s"Invalid value for open int enum: $value - must be an integer."

case UnknownEnumValue(name, possibleValues) =>
s"Unknown enum value: $name. Available values: ${possibleValues.mkString(", ")}"

Expand Down Expand Up @@ -227,6 +230,10 @@ object CompilationErrorDetails {
possibleValues: NonEmptyList[String]
) extends CompilationErrorDetails

final case class InvalidIntEnumValue(
value: String
) extends CompilationErrorDetails

final case class UnknownEnumValue(
value: String,
possibleValues: List[String],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import smithy4s.schema.CollectionTag.ListTag
import smithy4s.schema.CollectionTag.SetTag
import smithy4s.schema.CollectionTag.VectorTag
import smithy4s.schema.EnumTag
import smithy4s.schema.EnumTag._
import smithy4s.schema.EnumValue
import smithy4s.schema.Field
import smithy4s.schema.Primitive
Expand Down Expand Up @@ -172,7 +173,12 @@ object NodeEncoderVisitor extends SchemaVisitor[NodeEncoder] { self =>
tag: EnumTag[E],
values: List[EnumValue[E]],
total: E => EnumValue[E],
): NodeEncoder[E] = string.contramap(total(_).name)
): NodeEncoder[E] =
tag match {
case ClosedIntEnum | ClosedStringEnum => string.contramap(total(_).name)
// todo: test
case OpenIntEnum(_) | OpenStringEnum(_) => string.contramap(total(_).stringValue)
}

def struct[S](
shapeId: ShapeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import smithy4s.schema.CollectionTag.ListTag
import smithy4s.schema.CollectionTag.SetTag
import smithy4s.schema.CollectionTag.VectorTag
import smithy4s.schema.EnumTag
import smithy4s.schema.EnumTag.OpenIntEnum
import smithy4s.schema.EnumTag.OpenStringEnum
import smithy4s.schema.EnumValue
import smithy4s.schema.Field
import smithy4s.schema.Primitive
Expand Down Expand Up @@ -490,7 +492,20 @@ object QueryCompilerVisitorInternal extends SchemaVisitor[QueryCompiler] {
Ior.bothNec(CompilationError.warning(EnumFallback(v.name), range).deprecated, v.value)

case (None, None) =>
Ior.leftNec(CompilationError.error(UnknownEnumValue(name, values.map(_.name)), range))
tag match {
// todo: test
case OpenIntEnum(unknown) =>
name
.toIntOption
.toRightIor(CompilationError.error(InvalidIntEnumValue(name), range))
.toIorNec
.map(unknown)

// todo: test
case OpenStringEnum(unknown) => unknown(name).pure[QueryCompiler.Result]
case _ =>
Ior.leftNec(CompilationError.error(UnknownEnumValue(name, values.map(_.name)), range))
}
}

}
Expand Down
9 changes: 6 additions & 3 deletions modules/core/src/test/smithy/demo.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ $version: "2"
namespace demo.smithy

use alloy#UUID
use alloy#openEnum
use alloy#simpleRestJson
use smithy4s.meta#indexedSeq
use smithy4s.meta#refinement
Expand Down Expand Up @@ -67,9 +68,10 @@ structure CreateHeroInput {
friendSet: FriendSet
hasNewtypes: HasNewtypes
hasDeprecations: HasDeprecations
doc: Document,
sparse: SampleSparseList,
doc: Document
sparse: SampleSparseList
sparseMap: SampleSparseMap
tier: PrivacyTier
}

@uniqueItems
Expand Down Expand Up @@ -141,13 +143,15 @@ map PowerMap {
value: Hero
}

@openEnum
enum Power {
ICE = "Ice"
FIRE = "Fire"
LIGHTNING = "Lightning"
WIND = "Wind"
}

@openEnum
intEnum PrivacyTier {
PUBLIC = 0
PRIVATE = 1
Expand Down Expand Up @@ -265,7 +269,6 @@ structure HasMixin with [SampleMixin] {
name: String
}


@sparse
list SampleSparseList {
member: Integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import smithy4s.dynamic.DynamicSchemaIndex
import smithy4s.schema.Alt
import smithy4s.schema.CollectionTag
import smithy4s.schema.EnumTag
import smithy4s.schema.EnumTag.IntEnum
import smithy4s.schema.EnumTag._
import smithy4s.schema.EnumValue
import smithy4s.schema.Field
import smithy4s.schema.Primitive
Expand Down Expand Up @@ -261,8 +261,10 @@ object CompletionItem {

case e @ EnumerationSchema(_, _, _, _, _) =>
e.tag match {
case IntEnum() => now(s"intEnum ${e.shapeId.name}")
case _ => now(s"enum ${e.shapeId.name}")
case ClosedIntEnum => now(s"intEnum ${e.shapeId.name}")
case OpenIntEnum(_) => now(s"intEnum(open) ${e.shapeId.name}")
case OpenStringEnum(_) => now(s"enum(open) ${e.shapeId.name}")
case ClosedStringEnum => now(s"enum ${e.shapeId.name}")
}

case MapSchema(shapeId, _, key, value) =>
Expand Down
4 changes: 1 addition & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
ThisBuild / libraryDependencySchemes += "org.scala-lang.modules" %% "scala-xml" % VersionScheme.Always

addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.11")
addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.4.3")

// try to keep in sync with smithy-build.json
addSbtPlugin("com.disneystreaming.smithy4s" % "smithy4s-sbt-codegen" % "0.18.0")
addSbtPlugin("com.disneystreaming.smithy4s" % "smithy4s-sbt-codegen" % "dev-SNAPSHOT")

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0")
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.1")
Expand Down

0 comments on commit ffb8595

Please sign in to comment.