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

Fix rendering of IntEnum traits #1138

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.EnumTag
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,
)
val tag: EnumTag = EnumTag.StringEnum
implicit val schema: Schema[LeftRight] = enumeration(tag, values).withId(id).addHints(hints)
}
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.EnumTag
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.empty

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,
)
val tag: EnumTag = EnumTag.StringEnum
implicit val schema: Schema[OldStyleLeftRight] = enumeration(tag, values).withId(id).addHints(hints)
}
34 changes: 34 additions & 0 deletions modules/bootstrapped/src/generated/smithy4s/example/OneTwo.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.EnumTag
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.empty

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,
)
val tag: EnumTag = EnumTag.IntEnum
implicit val schema: Schema[OneTwo] = enumeration(tag, values).withId(id).addHints(hints)
}
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ package object example {
type StreamedBlob = smithy4s.example.StreamedBlob.Type
type TestIdRefValueMap = smithy4s.example.TestIdRefValueMap.Type
type PNG = smithy4s.example.PNG.Type
type StringWithEnumTraits = smithy4s.example.StringWithEnumTraits.Type
type Name = smithy4s.example.Name.Type
type ObjectKey = smithy4s.example.ObjectKey.Type
type UnwrappedFancyList = smithy4s.example.UnwrappedFancyList.Type
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 @@ -787,6 +787,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 @@ -1277,7 +1280,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 @@ -1293,6 +1296,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 @@ -1309,6 +1313,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
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