Skip to content

Commit

Permalink
Merge pull request #1139 from disneystreaming/int-enum-trait-017
Browse files Browse the repository at this point in the history
Int enum trait rendering for 0.17
  • Loading branch information
Baccata authored Aug 9, 2023
2 parents 727dd80 + 39a13b6 commit f001a5e
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 2 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ lazy val example = projectMatrix
"smithy4s.example.error",
"smithy4s.example.common",
"smithy4s.example.collision",
"smithy4s.example.reservedNameOverride",
"smithy4s.example.reservedNameOverride"
),
smithySpecs := Seq(
(ThisBuild / baseDirectory).value / "sampleSpecs" / "example.smithy",
Expand All @@ -828,6 +828,7 @@ lazy val example = projectMatrix
(ThisBuild / baseDirectory).value / "sampleSpecs" / "quoted_string.smithy",
(ThisBuild / baseDirectory).value / "sampleSpecs" / "numeric.smithy",
(ThisBuild / baseDirectory).value / "sampleSpecs" / "reservedNameOverride.smithy",
(ThisBuild / baseDirectory).value / "sampleSpecs" / "enumTraits.smithy"
),
Compile / resourceDirectory := (ThisBuild / baseDirectory).value / "modules" / "example" / "resources",
isCE3 := true,
Expand Down
22 changes: 21 additions & 1 deletion modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,9 @@ private[codegen] class SmithyToIR(model: Model, namespace: String) {
override def enumShape(x: EnumShape): Option[Type] =
Type.Ref(x.namespace, x.name).some

override def intEnumShape(x: IntEnumShape): Option[Type] =
Type.Ref(x.namespace, x.name).some

def stringShape(x: StringShape): Option[Type] = x match {
case T.enumeration(_) => Type.Ref(x.namespace, x.name).some
case shape if shape.getId() == uuidShapeId =>
Expand Down Expand Up @@ -1138,7 +1141,7 @@ private[codegen] class SmithyToIR(model: Model, namespace: String) {
// Alias
case (node, Type.Alias(ns, name, tpe, _)) =>
TypedNode.NewTypeTN(Type.Ref(ns, name), NodeAndType(node, tpe))
// Enumeration
// Enumeration (Enum Trait)
case (N.StringNode(str), UnRef(shape @ T.enumeration(e))) =>
val (enumDef, index) =
e.getValues().asScala.zipWithIndex.find(_._1.getValue() == str).get
Expand All @@ -1154,6 +1157,7 @@ private[codegen] class SmithyToIR(model: Model, namespace: String) {
index
)
)
// Enumeration
case (N.StringNode(str), UnRef(S.Enumeration(enumeration))) =>
val ((enumName, enumValue), index) =
enumeration
Expand All @@ -1170,6 +1174,22 @@ private[codegen] class SmithyToIR(model: Model, namespace: String) {
index,
enumName
)
// Integer enumeration
case (N.NumberNode(num), UnRef(S.IntEnumeration(enumeration))) =>
val (enumName, enumValue) =
enumeration
.getEnumValues()
.asScala
.find { case (_, value) => value == num.intValue }
.get
val shapeId = enumeration.getId()
val ref = Type.Ref(shapeId.getNamespace(), shapeId.getName())
TypedNode.EnumerationTN(
ref,
enumName,
enumValue,
enumName
)
// List
case (
N.ArrayNode(list),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ package object internals {
object Structure extends ShapeExtractor(_.asStructureShape())
object Union extends ShapeExtractor(_.asUnionShape())
object Enumeration extends ShapeExtractor(_.asEnumShape())
object IntEnumeration extends ShapeExtractor(_.asIntEnumShape())
object Service extends ShapeExtractor(_.asServiceShape())
object Resource extends ShapeExtractor(_.asResourceShape())
object Operation extends ShapeExtractor(_.asOperationShape())
Expand Down
32 changes: 32 additions & 0 deletions modules/example/src/smithy4s/example/LeftRight.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package smithy4s.example

import smithy4s.Enumeration
import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.enumeration

sealed abstract class LeftRight(_value: String, _name: String, _intValue: Int, _hints: Hints) extends Enumeration.Value {
override type EnumType = LeftRight
override val value: String = _value
override val name: String = _name
override val intValue: Int = _intValue
override val hints: Hints = _hints
override def enumeration: Enumeration[EnumType] = LeftRight
@inline final def widen: LeftRight = this
}
object LeftRight extends Enumeration[LeftRight] with ShapeTag.Companion[LeftRight] {
val id: ShapeId = ShapeId("smithy4s.example", "leftRight")

val hints: Hints = Hints.empty

case object LEFT extends LeftRight("left", "LEFT", 0, Hints())
case object RIGHT extends LeftRight("right", "RIGHT", 1, Hints())

val values: List[LeftRight] = List(
LEFT,
RIGHT,
)
implicit val schema: Schema[LeftRight] = enumeration(values).withId(id).addHints(hints)
}
34 changes: 34 additions & 0 deletions modules/example/src/smithy4s/example/OldStyleLeftRight.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package smithy4s.example

import smithy4s.Enumeration
import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.enumeration

sealed abstract class OldStyleLeftRight(_value: String, _name: String, _intValue: Int, _hints: Hints) extends Enumeration.Value {
override type EnumType = OldStyleLeftRight
override val value: String = _value
override val name: String = _name
override val intValue: Int = _intValue
override val hints: Hints = _hints
override def enumeration: Enumeration[EnumType] = OldStyleLeftRight
@inline final def widen: OldStyleLeftRight = this
}
object OldStyleLeftRight extends Enumeration[OldStyleLeftRight] with ShapeTag.Companion[OldStyleLeftRight] {
val id: ShapeId = ShapeId("smithy4s.example", "oldStyleLeftRight")

val hints: Hints = Hints(
smithy.api.Enum(List(smithy.api.EnumDefinition(value = smithy.api.NonEmptyString("left"), name = Some(smithy.api.EnumConstantBodyName("LEFT")), documentation = None, tags = None, deprecated = None), smithy.api.EnumDefinition(value = smithy.api.NonEmptyString("right"), name = Some(smithy.api.EnumConstantBodyName("RIGHT")), documentation = None, tags = None, deprecated = None))),
)

case object LEFT extends OldStyleLeftRight("left", "LEFT", 0, Hints())
case object RIGHT extends OldStyleLeftRight("right", "RIGHT", 1, Hints())

val values: List[OldStyleLeftRight] = List(
LEFT,
RIGHT,
)
implicit val schema: Schema[OldStyleLeftRight] = enumeration(values).withId(id).addHints(hints)
}
35 changes: 35 additions & 0 deletions modules/example/src/smithy4s/example/OneTwo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package smithy4s.example

import smithy4s.Enumeration
import smithy4s.Hints
import smithy4s.IntEnum
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.enumeration

sealed abstract class OneTwo(_value: String, _name: String, _intValue: Int, _hints: Hints) extends Enumeration.Value {
override type EnumType = OneTwo
override val value: String = _value
override val name: String = _name
override val intValue: Int = _intValue
override val hints: Hints = _hints
override def enumeration: Enumeration[EnumType] = OneTwo
@inline final def widen: OneTwo = this
}
object OneTwo extends Enumeration[OneTwo] with ShapeTag.Companion[OneTwo] {
val id: ShapeId = ShapeId("smithy4s.example", "oneTwo")

val hints: Hints = Hints(
IntEnum(),
)

case object ONE extends OneTwo("ONE", "ONE", 1, Hints())
case object TWO extends OneTwo("TWO", "TWO", 2, Hints())

val values: List[OneTwo] = List(
ONE,
TWO,
)
implicit val schema: Schema[OneTwo] = enumeration(values).withId(id).addHints(hints)
}
19 changes: 19 additions & 0 deletions modules/example/src/smithy4s/example/StringWithEnumTraits.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Newtype
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.schema.Schema.bijection
import smithy4s.schema.Schema.string

object StringWithEnumTraits extends Newtype[String] {
val id: ShapeId = ShapeId("smithy4s.example", "StringWithEnumTraits")
val hints: Hints = Hints(
smithy4s.example.OldStyleLeftRight.RIGHT.widen,
smithy4s.example.OneTwo.ONE.widen,
smithy4s.example.LeftRight.LEFT.widen,
)
val underlyingSchema: Schema[String] = string.withId(id).addHints(hints)
implicit val schema: Schema[StringWithEnumTraits] = bijection(underlyingSchema, asBijection)
}
1 change: 1 addition & 0 deletions modules/example/src/smithy4s/example/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package object example {
type NonEmptyMapNumbers = smithy4s.example.NonEmptyMapNumbers.Type
type SomeValue = smithy4s.example.SomeValue.Type
type TestString = smithy4s.example.TestString.Type
type StringWithEnumTraits = smithy4s.example.StringWithEnumTraits.Type
type NonEmptyNames = smithy4s.example.NonEmptyNames.Type
type Age = smithy4s.example.Age.Type
type BucketName = smithy4s.example.BucketName.Type
Expand Down
27 changes: 27 additions & 0 deletions sampleSpecs/enumTraits.smithy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$version: "2.0"

namespace smithy4s.example

intEnum oneTwo {
ONE = 1
TWO = 2
}

enum leftRight {
LEFT = "left"
RIGHT = "right"
}

@enum([{
name: "LEFT"
value: "left"
}, {
name: "RIGHT"
value: "right"
}])
string oldStyleLeftRight

@oneTwo(1)
@leftRight("left")
@oldStyleLeftRight("right")
string StringWithEnumTraits

0 comments on commit f001a5e

Please sign in to comment.