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

Render deprecation hints #599

Merged
merged 13 commits into from
Nov 20, 2022
4 changes: 2 additions & 2 deletions modules/codegen/src/smithy4s/codegen/SmithyToIR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,6 @@ private[codegen] class SmithyToIR(model: Model, namespace: String) {
.map { case ((name, value), index) =>
val member = shape.getMember(name).get()

// random note: any other traits that are allowed on enum values? cuz they don't generate hints.
// known: @deprecated, @documentation
EnumValue(value, index, name, hints(member))
}
.toList
Expand Down Expand Up @@ -742,6 +740,8 @@ private[codegen] class SmithyToIR(model: Model, namespace: String) {
// traits from the synthetic namespace, e.g. smithy.synthetic.enum
// don't have shapes in the model - so we can't generate hints for them.
.filterNot(_.toShapeId().getNamespace() == "smithy.synthetic")
kubukoz marked this conversation as resolved.
Show resolved Hide resolved
// enumValue can be derived from enum schemas anyway, so we're removing it from hints
.filterNot(_.toShapeId().toString() == "smithy.api#enumValue")
Copy link
Contributor

Choose a reason for hiding this comment

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

thy shall not use .toString when there is an alternative (in this case, ShapeId.fromParts("smithy.api", "enumValue") or probably even EnumValueTrait.ID or something

Copy link
Member Author

Choose a reason for hiding this comment

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

now you're just nitpicking :P

Copy link
Contributor

Choose a reason for hiding this comment

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

Nah, just .toString trauma 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

:heavy-nodding:


val nonConstraintNonMetaTraits = nonMetaTraits.collect {
case t if ConstraintTrait.unapply(t).isEmpty => t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,16 @@ private[dynamic] object Compiler {
)
}

private def allHints(traits: Option[Map[IdRef, Document]]): Hints =
private def allHints(traits: Option[Map[IdRef, Document]]): Hints = {
val ignoredHints = List(IdRef("smithy.api#enumValue"))

Hints.fromSeq {
traits
.getOrElse(Map.empty)
.collect { case (ValidIdRef(k), v) =>
(traits.getOrElse(Map.empty) -- ignoredHints).collect {
case (ValidIdRef(k), v) =>
toHint(k, v)
}
.toSeq
}.toSeq
}
}

private def update[A](
shapeId: ShapeId,
Expand Down
25 changes: 5 additions & 20 deletions modules/dynamic/test/src-jvm/smithy4s/dynamic/EnumSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,14 @@ class EnumSpec extends FunSuite {
intValue = 0,
value = 0,
name = "FIRE",
hints = Hints(
enumValueHint("Fire")
)
hints = Hints.empty
),
EnumValue(
stringValue = "Ice",
intValue = 1,
value = 1,
name = "ICE",
hints = Hints(
enumValueHint("Ice")
)
hints = Hints.empty
)
)
)
Expand All @@ -169,18 +165,14 @@ class EnumSpec extends FunSuite {
intValue = 10,
value = 10,
name = "FIRE",
hints = Hints(
enumValueHintInt(10)
)
hints = Hints.empty
),
EnumValue(
stringValue = "ICE",
intValue = 42,
value = 42,
name = "ICE",
hints = Hints(
enumValueHintInt(42)
)
hints = Hints.empty
)
)
)
Expand Down Expand Up @@ -230,12 +222,6 @@ class EnumSpec extends FunSuite {
}
}

private def enumValueHint(value: String) =
ShapeId("smithy.api", "enumValue") -> Document.fromString(value)

private def enumValueHintInt(value: Int) =
ShapeId("smithy.api", "enumValue") -> Document.fromInt(value)

test("Smithy 2.0 enum members get their hints compiled") {
assertEnum(
ShapeId("example", "EnumWithTraits"),
Expand All @@ -245,15 +231,14 @@ class EnumSpec extends FunSuite {
intValue = 0,
value = 0,
name = "FIRE",
hints = Hints(enumValueHint("FIRE"))
hints = Hints.empty
),
EnumValue(
stringValue = "ICE",
intValue = 1,
value = 1,
name = "ICE",
hints = Hints(
enumValueHint("ICE"),
ShapeId("smithy.api", "deprecated") -> Document.obj()
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ object EnumWithDeprecations extends Enumeration[EnumWithDeprecations] with Shape
)

@deprecated
case object OLD extends EnumWithDeprecations("OLD", "OLD", 0, Hints(smithy.api.Deprecated(message = None, since = None), smithy.api.EnumValue(smithy4s.Document.fromString("OLD"))))
case object NEW extends EnumWithDeprecations("NEW", "NEW", 1, Hints(smithy.api.EnumValue(smithy4s.Document.fromString("NEW"))))
case object OLD extends EnumWithDeprecations("OLD", "OLD", 0, Hints(smithy.api.Deprecated(message = None, since = None)))
case object NEW extends EnumWithDeprecations("NEW", "NEW", 1, Hints())

val values: List[EnumWithDeprecations] = List(
OLD,
Expand Down
10 changes: 5 additions & 5 deletions modules/example/src/smithy4s/example/FaceCard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ object FaceCard extends Enumeration[FaceCard] with ShapeTag.Companion[FaceCard]
IntEnum(),
)

case object JACK extends FaceCard("JACK", "JACK", 1, Hints(smithy.api.EnumValue(smithy4s.Document.fromDouble(1.0))))
case object QUEEN extends FaceCard("QUEEN", "QUEEN", 2, Hints(smithy.api.EnumValue(smithy4s.Document.fromDouble(2.0))))
case object KING extends FaceCard("KING", "KING", 3, Hints(smithy.api.EnumValue(smithy4s.Document.fromDouble(3.0))))
case object ACE extends FaceCard("ACE", "ACE", 4, Hints(smithy.api.EnumValue(smithy4s.Document.fromDouble(4.0))))
case object JOKER extends FaceCard("JOKER", "JOKER", 5, Hints(smithy.api.EnumValue(smithy4s.Document.fromDouble(5.0))))
case object JACK extends FaceCard("JACK", "JACK", 1, Hints())
case object QUEEN extends FaceCard("QUEEN", "QUEEN", 2, Hints())
case object KING extends FaceCard("KING", "KING", 3, Hints())
case object ACE extends FaceCard("ACE", "ACE", 4, Hints())
case object JOKER extends FaceCard("JOKER", "JOKER", 5, Hints())

val values: List[FaceCard] = List(
JACK,
Expand Down
6 changes: 3 additions & 3 deletions modules/example/src/smithy4s/example/Letters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ object Letters extends Enumeration[Letters] with ShapeTag.Companion[Letters] {

val hints : Hints = Hints.empty

case object A extends Letters("a", "A", 0, Hints(smithy.api.EnumValue(smithy4s.Document.fromString("a"))))
case object B extends Letters("b", "B", 1, Hints(smithy.api.EnumValue(smithy4s.Document.fromString("b"))))
case object C extends Letters("c", "C", 2, Hints(smithy.api.EnumValue(smithy4s.Document.fromString("c"))))
case object A extends Letters("a", "A", 0, Hints())
case object B extends Letters("b", "B", 1, Hints())
case object C extends Letters("c", "C", 2, Hints())

val values: List[Letters] = List(
A,
Expand Down
4 changes: 2 additions & 2 deletions modules/example/src/smithy4s/example/SwitchState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ object SwitchState extends Enumeration[SwitchState] with ShapeTag.Companion[Swit

val hints : Hints = Hints.empty

case object ON extends SwitchState("ON", "ON", 0, Hints(smithy.api.EnumValue(smithy4s.Document.fromString("ON"))))
case object OFF extends SwitchState("OFF", "OFF", 1, Hints(smithy.api.EnumValue(smithy4s.Document.fromString("OFF"))))
case object ON extends SwitchState("ON", "ON", 0, Hints())
case object OFF extends SwitchState("OFF", "OFF", 1, Hints())

val values: List[SwitchState] = List(
ON,
Expand Down