From de30891e2d561f62789adbd33ceb482c6bb0012c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 18:53:47 +0200 Subject: [PATCH 01/10] Reproduce issue(s) --- .../_package/MyPackageStringTrait.scala | 18 +++++ .../smithy4s/example/_package/package.scala | 1 + .../smithy4s/example/collision/Class.scala | 65 +++++++++++++++++++ .../collision/ReservedNameUnionTrait.scala | 17 +++++ .../TestReservedNamespaceTrait.scala | 17 +++++ .../smithy4s/example/collision/package.scala | 2 + .../codegen/internals/ModelLoader.scala | 2 +- sampleSpecs/reservedNamespace.smithy | 3 + sampleSpecs/reservednames.smithy | 18 +++++ 9 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 modules/bootstrapped/src/generated/smithy4s/example/_package/MyPackageStringTrait.scala create mode 100644 modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala create mode 100644 modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala create mode 100644 modules/bootstrapped/src/generated/smithy4s/example/collision/TestReservedNamespaceTrait.scala diff --git a/modules/bootstrapped/src/generated/smithy4s/example/_package/MyPackageStringTrait.scala b/modules/bootstrapped/src/generated/smithy4s/example/_package/MyPackageStringTrait.scala new file mode 100644 index 000000000..b715f4b33 --- /dev/null +++ b/modules/bootstrapped/src/generated/smithy4s/example/_package/MyPackageStringTrait.scala @@ -0,0 +1,18 @@ +package smithy4s.example._package + +import smithy4s.Hints +import smithy4s.Newtype +import smithy4s.Schema +import smithy4s.ShapeId +import smithy4s.schema.Schema.bijection +import smithy4s.schema.Schema.recursive +import smithy4s.schema.Schema.string + +object MyPackageStringTrait extends Newtype[String] { + val id: ShapeId = ShapeId("smithy4s.example.package", "MyPackageStringTrait") + val hints: Hints = Hints( + smithy.api.Trait(selector = None, structurallyExclusive = None, conflicts = None, breakingChanges = None), + ).lazily + val underlyingSchema: Schema[String] = string.withId(id).addHints(hints) + implicit val schema: Schema[MyPackageStringTrait] = recursive(bijection(underlyingSchema, asBijection)) +} diff --git a/modules/bootstrapped/src/generated/smithy4s/example/_package/package.scala b/modules/bootstrapped/src/generated/smithy4s/example/_package/package.scala index 28a781875..fffb0cbb9 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/_package/package.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/_package/package.scala @@ -3,5 +3,6 @@ package smithy4s.example package object _package { type MyPackageString = smithy4s.example._package.MyPackageString.Type + type MyPackageStringTrait = smithy4s.example._package.MyPackageStringTrait.Type } \ No newline at end of file diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala new file mode 100644 index 000000000..6276380e8 --- /dev/null +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala @@ -0,0 +1,65 @@ +package smithy4s.example.collision + +import smithy4s.Hints +import smithy4s.Schema +import smithy4s.ShapeId +import smithy4s.ShapeTag +import smithy4s.schema.Schema.constant +import smithy4s.schema.Schema.recursive +import smithy4s.schema.Schema.union + +sealed trait Class extends scala.Product with scala.Serializable { self => + @inline final def widen: Class = this + def $ordinal: Int + + object project { + def _package: Option[Class.AdtStruct] = Class.AdtStruct.alt.project.lift(self) + } + + def accept[A](visitor: Class.Visitor[A]): A = this match { + case value: Class.AdtStruct => visitor._package(value) + } +} +object Class extends ShapeTag.Companion[Class] { + + def adtStruct():AdtStruct = AdtStruct() + + val id: ShapeId = ShapeId("smithy4s.example.collision", "class") + + val hints: Hints = Hints( + smithy.api.Trait(selector = None, structurallyExclusive = None, conflicts = None, breakingChanges = None), + ).lazily + + final case class AdtStruct() extends Class { + def $ordinal: Int = 0 + } + + object AdtStruct extends ShapeTag.Companion[AdtStruct] { + val id: ShapeId = ShapeId("smithy4s.example.collision", "AdtStruct") + + val hints: Hints = Hints.empty + + + implicit val schema: Schema[AdtStruct] = constant(AdtStruct()).withId(id).addHints(hints) + + val alt = schema.oneOf[Class]("package") + } + + + trait Visitor[A] { + def _package(value: Class.AdtStruct): A + } + + object Visitor { + trait Default[A] extends Visitor[A] { + def default: A + def _package(value: Class.AdtStruct): A = default + } + } + + implicit val schema: Schema[Class] = recursive(union( + Class.AdtStruct.alt, + ){ + _.$ordinal + }.withId(id).addHints(hints)) +} diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala new file mode 100644 index 000000000..ee7d0d7bd --- /dev/null +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala @@ -0,0 +1,17 @@ +package smithy4s.example.collision + +import smithy4s.Hints +import smithy4s.Newtype +import smithy4s.Schema +import smithy4s.ShapeId +import smithy4s.schema.Schema.bijection +import smithy4s.schema.Schema.string + +object ReservedNameUnionTrait extends Newtype[java.lang.String] { + val id: ShapeId = ShapeId("smithy4s.example.collision", "ReservedNameUnionTrait") + val hints: Hints = Hints( + smithy4s.example.collision.class.AdtStruct(), + ).lazily + val underlyingSchema: Schema[java.lang.String] = string.withId(id).addHints(hints) + implicit val schema: Schema[ReservedNameUnionTrait] = bijection(underlyingSchema, asBijection) +} diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/TestReservedNamespaceTrait.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/TestReservedNamespaceTrait.scala new file mode 100644 index 000000000..06056d3ef --- /dev/null +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/TestReservedNamespaceTrait.scala @@ -0,0 +1,17 @@ +package smithy4s.example.collision + +import smithy4s.Hints +import smithy4s.Newtype +import smithy4s.Schema +import smithy4s.ShapeId +import smithy4s.schema.Schema.bijection +import smithy4s.schema.Schema.string + +object TestReservedNamespaceTrait extends Newtype[java.lang.String] { + val id: ShapeId = ShapeId("smithy4s.example.collision", "TestReservedNamespaceTrait") + val hints: Hints = Hints( + smithy4s.example.package.MyPackageStringTrait("test"), + ).lazily + val underlyingSchema: Schema[java.lang.String] = string.withId(id).addHints(hints) + implicit val schema: Schema[TestReservedNamespaceTrait] = bijection(underlyingSchema, asBijection) +} diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/package.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/package.scala index f7f33c849..bde7a66bd 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/collision/package.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/package.scala @@ -9,6 +9,8 @@ package object collision { type MySet = smithy4s.example.collision.MySet.Type type ReservedKeywordTraitExampleCollection = smithy4s.example.collision.ReservedKeywordTraitExampleCollection.Type type ReservedKeywordTraitExamplePrimitive = smithy4s.example.collision.ReservedKeywordTraitExamplePrimitive.Type + type ReservedNameUnionTrait = smithy4s.example.collision.ReservedNameUnionTrait.Type type String = smithy4s.example.collision.String.Type + type TestReservedNamespaceTrait = smithy4s.example.collision.TestReservedNamespaceTrait.Type } \ No newline at end of file diff --git a/modules/codegen/src/smithy4s/codegen/internals/ModelLoader.scala b/modules/codegen/src/smithy4s/codegen/internals/ModelLoader.scala index 9958dc94c..c2cb7fcf1 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/ModelLoader.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/ModelLoader.scala @@ -51,7 +51,7 @@ private[codegen] object ModelLoader { val modelsInJars = deps.flatMap { file => Using.resource( // Note: On JDK13+, the second parameter is redundant. - FileSystems.newFileSystem(file.toPath(), null) + FileSystems.newFileSystem(file.toPath(), null: ClassLoader) ) { jarFS => val p = jarFS.getPath("META-INF", "smithy", "manifest") diff --git a/sampleSpecs/reservedNamespace.smithy b/sampleSpecs/reservedNamespace.smithy index af20e2088..d263b1488 100644 --- a/sampleSpecs/reservedNamespace.smithy +++ b/sampleSpecs/reservedNamespace.smithy @@ -3,3 +3,6 @@ $version: "2.0" namespace smithy4s.example.package string MyPackageString + +@trait +string MyPackageStringTrait diff --git a/sampleSpecs/reservednames.smithy b/sampleSpecs/reservednames.smithy index 9991f479d..6b35398cc 100644 --- a/sampleSpecs/reservednames.smithy +++ b/sampleSpecs/reservednames.smithy @@ -4,6 +4,7 @@ namespace smithy4s.example.collision use alloy#simpleRestJson use smithy4s.example.package#MyPackageString +use smithy4s.example.package#MyPackageStringTrait @simpleRestJson service ReservedNameService { @@ -67,6 +68,9 @@ structure TestReservedNamespaceImport { package: MyPackageString } +@MyPackageStringTrait("test") +string TestReservedNamespaceTrait + // trait def @trait structure reservedKeywordStructTrait { @@ -146,3 +150,17 @@ structure Scala3ReservedKeywords { export: String enum: String } + +@smithy4s.meta#adt +@trait +union class { + package: AdtStruct +} + +structure AdtStruct {} + +// Regression test for https://github.com/disneystreaming/smithy4s/issues/1601 +@class(package: { + +}) +string ReservedNameUnionTrait From e9b65b774b4a5b914ffcced810c05607bf23e895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 18:59:23 +0200 Subject: [PATCH 02/10] Fix issues --- .../example/collision/ReservedNameUnionTrait.scala | 2 +- .../example/collision/TestReservedNamespaceTrait.scala | 2 +- .../smithy4s/codegen/internals/CollisionAvoidance.scala | 5 +++++ modules/codegen/src/smithy4s/codegen/internals/IR.scala | 5 ++++- .../src/smithy4s/codegen/internals/LineSegment.scala | 8 ++++++-- .../codegen/src/smithy4s/codegen/internals/Renderer.scala | 2 +- .../src/smithy4s/codegen/internals/SmithyToIR.scala | 2 +- 7 files changed, 19 insertions(+), 7 deletions(-) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala index ee7d0d7bd..037b6257c 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala @@ -10,7 +10,7 @@ import smithy4s.schema.Schema.string object ReservedNameUnionTrait extends Newtype[java.lang.String] { val id: ShapeId = ShapeId("smithy4s.example.collision", "ReservedNameUnionTrait") val hints: Hints = Hints( - smithy4s.example.collision.class.AdtStruct(), + smithy4s.example.collision.Class.AdtStruct(), ).lazily val underlyingSchema: Schema[java.lang.String] = string.withId(id).addHints(hints) implicit val schema: Schema[ReservedNameUnionTrait] = bijection(underlyingSchema, asBijection) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/TestReservedNamespaceTrait.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/TestReservedNamespaceTrait.scala index 06056d3ef..58cbc0b3f 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/collision/TestReservedNamespaceTrait.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/TestReservedNamespaceTrait.scala @@ -10,7 +10,7 @@ import smithy4s.schema.Schema.string object TestReservedNamespaceTrait extends Newtype[java.lang.String] { val id: ShapeId = ShapeId("smithy4s.example.collision", "TestReservedNamespaceTrait") val hints: Hints = Hints( - smithy4s.example.package.MyPackageStringTrait("test"), + smithy4s.example._package.MyPackageStringTrait("test"), ).lazily val underlyingSchema: Schema[java.lang.String] = string.withId(id).addHints(hints) implicit val schema: Schema[TestReservedNamespaceTrait] = bijection(underlyingSchema, asBijection) diff --git a/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala b/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala index ac0d50ac0..780c09f8f 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala @@ -230,6 +230,11 @@ private[internals] object CollisionAvoidance { case ValidatedNewTypeTN(ref, target) => ValidatedNewTypeTN(modRef(ref), target) case AltTN(ref, altName, alt) => + // note: technically we should probably escape altName here + // but it'd only really break if it matched a capitalized keyword, + // and Scala has none of those, so it's impossible to write a failing test. + // Alt names in this context are always capitalized before being printed + // (Renderer.scala:1614 at the time of writing). AltTN(modRef(ref), altName, alt) case MapTN(values) => MapTN(values) diff --git a/modules/codegen/src/smithy4s/codegen/internals/IR.scala b/modules/codegen/src/smithy4s/codegen/internals/IR.scala index fbdc20cdc..4f23fc652 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/IR.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/IR.scala @@ -304,7 +304,10 @@ private[internals] object Type { valueHints: List[Hint] ) extends Type case class Ref(namespace: String, name: String) extends Type { - def show = namespace + "." + name + def show: String = NameRef + .splitPath(namespace) + .map(CollisionAvoidance.protectKeyword) + .mkString(".") + "." + name } case class Alias( namespace: String, diff --git a/modules/codegen/src/smithy4s/codegen/internals/LineSegment.scala b/modules/codegen/src/smithy4s/codegen/internals/LineSegment.scala index 1e69019e0..bdcfb1375 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/LineSegment.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/LineSegment.scala @@ -95,13 +95,17 @@ private[codegen] object LineSegment { ) implicit val nameRefShow: Show[NameRef] = Show.show[NameRef](_.asImport) def apply(pkg: String, name: String): NameRef = - NameRef(pkg.split("\\.").toList, name, List.empty) + NameRef(splitPath(pkg), name, List.empty) def apply(fqn: String): NameRef = { - val parts = fqn.split("\\.").toList.toNel.get + val parts = + splitPath(fqn).toNel.getOrElse(sys.error(s"Invalid FQN: $fqn")) NameRef(parts.toList.dropRight(1), parts.last, List.empty) } def apply(fqn: String, typeParams: List[NameRef]): NameRef = apply(fqn).copy(typeParams = typeParams) + + private[internals] def splitPath(pkg: String): List[String] = + pkg.split("\\.").toList } implicit val lineSegmentShow: Show[LineSegment] = Show.show { diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index 1adccaf79..b4c04a916 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -1583,7 +1583,7 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => private def renderTypedNode(tn: TypedNode[CString]): CString = tn match { case EnumerationTN(ref, _, _, name) => - line"${ref.show + "." + name + ".widen"}".write + line"${ref.show}.$name.widen".write case StructureTN(ref, fields) => val fieldStrings = fields.map { case (name, FieldTN.RequiredTN(value)) => diff --git a/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala b/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala index bee39fb2e..246154ad9 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala @@ -1239,7 +1239,7 @@ private[codegen] class SmithyToIR( case Some(parent) => val cId = shape.getId val newNs = - cId.getNamespace + "." + parent.getName + cId.getNamespace + "." + parent.getName.capitalize val error = new Exception( s"Shapes annotated with the adtMemberTrait must be structures. $cId is not a structure." ) From 04964a2736fd8f7af49cd67d12e68b594a8ebf13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 19:04:23 +0200 Subject: [PATCH 03/10] Add changelog and issue link --- CHANGELOG.md | 6 +++++- sampleSpecs/reservednames.smithy | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 935875af2..347ccb304 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,14 @@ When adding entries, please treat them as if they could end up in a release any Thank you! +# 0.18.25 + +* Fixes issues in which applications of some Smithy traits would be incorrectly rendered in Scala code (see [#1602](https://github.com/disneystreaming/smithy4s/pull/1602)). + # 0.18.24 * Adds missing nanoseconds in Document encoding of EPOCH_SECOND timestamps -* Add support for `alloy#jsonUnknown`, allowing structures to capture unknown JSON fields in one of their members. +* Add support for `alloy#jsonUnknown`, allowing structures to capture unknown JSON fields in one of their members. * Add `getMessage` implementation in `Smithy4sThrowable` which will be overridden in cases where the error structure contains a message field, but otherwise will be used to prevent a useless `null` result when `getMessage` is called. # 0.18.23 diff --git a/sampleSpecs/reservednames.smithy b/sampleSpecs/reservednames.smithy index 6b35398cc..ec39da06c 100644 --- a/sampleSpecs/reservednames.smithy +++ b/sampleSpecs/reservednames.smithy @@ -68,6 +68,7 @@ structure TestReservedNamespaceImport { package: MyPackageString } +// Regression test for https://github.com/disneystreaming/smithy4s/issues/1601 @MyPackageStringTrait("test") string TestReservedNamespaceTrait From 7367301b301b7abbcef7eb15b81afc617aaa3681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 20:20:15 +0200 Subject: [PATCH 04/10] Widen ADT hints to avoid ambiguous implicits in Scala 3 --- .../src/generated/smithy4s/example/TestString.scala | 2 +- .../smithy4s/example/collision/ReservedNameUnionTrait.scala | 2 +- .../codegen/src/smithy4s/codegen/internals/Renderer.scala | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala b/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala index 152c04113..b9c3a417b 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala @@ -10,7 +10,7 @@ import smithy4s.schema.Schema.string object TestString extends Newtype[String] { val id: ShapeId = ShapeId("smithy4s.example", "TestString") val hints: Hints = Hints( - smithy4s.example.TestTrait(orderType = Some(smithy4s.example.OrderType.InStoreOrder(id = smithy4s.example.OrderNumber(100), locationId = Some("someLocation")))), + smithy4s.example.TestTrait(orderType = Some(smithy4s.example.OrderType.InStoreOrder(id = smithy4s.example.OrderNumber(100), locationId = Some("someLocation")).widen)), ).lazily val underlyingSchema: Schema[String] = string.withId(id).addHints(hints) implicit val schema: Schema[TestString] = bijection(underlyingSchema, asBijection) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala index 037b6257c..d1d388b4d 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala @@ -10,7 +10,7 @@ import smithy4s.schema.Schema.string object ReservedNameUnionTrait extends Newtype[java.lang.String] { val id: ShapeId = ShapeId("smithy4s.example.collision", "ReservedNameUnionTrait") val hints: Hints = Hints( - smithy4s.example.collision.Class.AdtStruct(), + smithy4s.example.collision.Class.AdtStruct().widen, ).lazily val underlyingSchema: Schema[java.lang.String] = string.withId(id).addHints(hints) implicit val schema: Schema[ReservedNameUnionTrait] = bijection(underlyingSchema, asBijection) diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index b4c04a916..4bbb6a4a2 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -1614,7 +1614,10 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => line"${ref.show}.${altName.capitalize}Case(${alt.runDefault}).widen".write case AltTN(_, _, AltValueTN.ProductAltTN(alt)) => - alt.runDefault.write + // The `widen` is necessary in Scala 3. + // Without it, there are two schemas (the struct and the union) that match the type + // for the conversion to Hints.Binding. + line"${alt.runDefault}.widen".write case CollectionTN(collectionType, values) => val col = collectionType.tpe From 04f9e8339676804fe6bf1a11916a2032d28fe10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Thu, 3 Oct 2024 15:53:52 +0200 Subject: [PATCH 05/10] Apply suggestion: remove `implicit` from 0-struct alt schemas --- .../src/generated/smithy4s/example/DeprecatedUnion.scala | 4 ++-- .../src/generated/smithy4s/example/collision/Class.scala | 2 +- modules/codegen/src/smithy4s/codegen/internals/Renderer.scala | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/DeprecatedUnion.scala b/modules/bootstrapped/src/generated/smithy4s/example/DeprecatedUnion.scala index 4e8080a68..0f24a5e0a 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/DeprecatedUnion.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/DeprecatedUnion.scala @@ -59,7 +59,7 @@ object DeprecatedUnion extends ShapeTag.Companion[DeprecatedUnion] { ).lazily - implicit val schema: Schema[DeprecatedUnionProductCase] = constant(DeprecatedUnionProductCase()).withId(id).addHints(hints) + val schema: Schema[DeprecatedUnionProductCase] = constant(DeprecatedUnionProductCase()).withId(id).addHints(hints) val alt = schema.oneOf[DeprecatedUnion]("p") } @@ -76,7 +76,7 @@ object DeprecatedUnion extends ShapeTag.Companion[DeprecatedUnion] { ).lazily - implicit val schema: Schema[UnionProductCaseDeprecatedAtCallSite] = constant(UnionProductCaseDeprecatedAtCallSite()).withId(id).addHints(hints) + val schema: Schema[UnionProductCaseDeprecatedAtCallSite] = constant(UnionProductCaseDeprecatedAtCallSite()).withId(id).addHints(hints) val alt = schema.oneOf[DeprecatedUnion]("p2") } diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala index 6276380e8..0ed9f11ae 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala @@ -40,7 +40,7 @@ object Class extends ShapeTag.Companion[Class] { val hints: Hints = Hints.empty - implicit val schema: Schema[AdtStruct] = constant(AdtStruct()).withId(id).addHints(hints) + val schema: Schema[AdtStruct] = constant(AdtStruct()).withId(id).addHints(hints) val alt = schema.oneOf[Class]("package") } diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index e3eecd5b2..f33b081c7 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -776,7 +776,7 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => .appendToLast(if (recursive) ")" else "") } } else { - line"implicit val schema: $Schema_[${product.nameRef}] = $constant_(${product.nameRef}()).withId(id).addHints(hints)" + line"${schemaImplicit}val schema: $Schema_[${product.nameRef}] = $constant_(${product.nameRef}()).withId(id).addHints(hints)" }, renderTypeclasses(product.hints, product.nameRef), additionalLines From e143b46f20b0c0e371b3ab479db56145156a98f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Thu, 3 Oct 2024 15:56:28 +0200 Subject: [PATCH 06/10] Remove extra widen --- .../src/generated/smithy4s/example/TestString.scala | 2 +- .../smithy4s/example/collision/ReservedNameUnionTrait.scala | 2 +- .../codegen/src/smithy4s/codegen/internals/Renderer.scala | 5 +---- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala b/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala index b9c3a417b..152c04113 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala @@ -10,7 +10,7 @@ import smithy4s.schema.Schema.string object TestString extends Newtype[String] { val id: ShapeId = ShapeId("smithy4s.example", "TestString") val hints: Hints = Hints( - smithy4s.example.TestTrait(orderType = Some(smithy4s.example.OrderType.InStoreOrder(id = smithy4s.example.OrderNumber(100), locationId = Some("someLocation")).widen)), + smithy4s.example.TestTrait(orderType = Some(smithy4s.example.OrderType.InStoreOrder(id = smithy4s.example.OrderNumber(100), locationId = Some("someLocation")))), ).lazily val underlyingSchema: Schema[String] = string.withId(id).addHints(hints) implicit val schema: Schema[TestString] = bijection(underlyingSchema, asBijection) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala index d1d388b4d..037b6257c 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala @@ -10,7 +10,7 @@ import smithy4s.schema.Schema.string object ReservedNameUnionTrait extends Newtype[java.lang.String] { val id: ShapeId = ShapeId("smithy4s.example.collision", "ReservedNameUnionTrait") val hints: Hints = Hints( - smithy4s.example.collision.Class.AdtStruct().widen, + smithy4s.example.collision.Class.AdtStruct(), ).lazily val underlyingSchema: Schema[java.lang.String] = string.withId(id).addHints(hints) implicit val schema: Schema[ReservedNameUnionTrait] = bijection(underlyingSchema, asBijection) diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index f33b081c7..f3347d352 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -1617,10 +1617,7 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => line"${ref.show}.${altName.capitalize}Case.widen".write case AltTN(_, _, AltValueTN.ProductAltTN(alt)) => - // The `widen` is necessary in Scala 3. - // Without it, there are two schemas (the struct and the union) that match the type - // for the conversion to Hints.Binding. - line"${alt.runDefault}.widen".write + alt.runDefault.write case CollectionTN(collectionType, values) => val col = collectionType.tpe From 12f51383b08b759be56779a4326bb90cacfce405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Thu, 3 Oct 2024 16:08:15 +0200 Subject: [PATCH 07/10] Remove ShapeTag from alt product members --- .../src/generated/smithy4s/example/DeprecatedUnion.scala | 4 ++-- .../src/generated/smithy4s/example/OrderType.scala | 2 +- .../src/generated/smithy4s/example/PersonUnion.scala | 2 +- .../bootstrapped/src/generated/smithy4s/example/Podcast.scala | 4 ++-- .../bootstrapped/src/generated/smithy4s/example/TestAdt.scala | 4 ++-- .../src/generated/smithy4s/example/TestMixinAdt.scala | 2 +- .../src/generated/smithy4s/example/collision/Class.scala | 2 +- modules/codegen/src/smithy4s/codegen/internals/Renderer.scala | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/DeprecatedUnion.scala b/modules/bootstrapped/src/generated/smithy4s/example/DeprecatedUnion.scala index 0f24a5e0a..aa3006fd3 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/DeprecatedUnion.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/DeprecatedUnion.scala @@ -51,7 +51,7 @@ object DeprecatedUnion extends ShapeTag.Companion[DeprecatedUnion] { def $ordinal: Int = 2 } - object DeprecatedUnionProductCase extends ShapeTag.Companion[DeprecatedUnionProductCase] { + object DeprecatedUnionProductCase { val id: ShapeId = ShapeId("smithy4s.example", "DeprecatedUnionProductCase") val hints: Hints = Hints( @@ -68,7 +68,7 @@ object DeprecatedUnion extends ShapeTag.Companion[DeprecatedUnion] { def $ordinal: Int = 3 } - object UnionProductCaseDeprecatedAtCallSite extends ShapeTag.Companion[UnionProductCaseDeprecatedAtCallSite] { + object UnionProductCaseDeprecatedAtCallSite { val id: ShapeId = ShapeId("smithy4s.example", "UnionProductCaseDeprecatedAtCallSite") val hints: Hints = Hints( diff --git a/modules/bootstrapped/src/generated/smithy4s/example/OrderType.scala b/modules/bootstrapped/src/generated/smithy4s/example/OrderType.scala index 5a2dcdd22..ec7071bbf 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/OrderType.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/OrderType.scala @@ -48,7 +48,7 @@ object OrderType extends ShapeTag.Companion[OrderType] { def $ordinal: Int = 1 } - object InStoreOrder extends ShapeTag.Companion[InStoreOrder] { + object InStoreOrder { val id: ShapeId = ShapeId("smithy4s.example", "InStoreOrder") val hints: Hints = Hints( diff --git a/modules/bootstrapped/src/generated/smithy4s/example/PersonUnion.scala b/modules/bootstrapped/src/generated/smithy4s/example/PersonUnion.scala index 830ce2d56..5e3e3b402 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/PersonUnion.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/PersonUnion.scala @@ -32,7 +32,7 @@ object PersonUnion extends ShapeTag.Companion[PersonUnion] { def $ordinal: Int = 0 } - object OtherPerson extends ShapeTag.Companion[OtherPerson] { + object OtherPerson { val id: ShapeId = ShapeId("smithy4s.example", "OtherPerson") val hints: Hints = Hints.empty diff --git a/modules/bootstrapped/src/generated/smithy4s/example/Podcast.scala b/modules/bootstrapped/src/generated/smithy4s/example/Podcast.scala index f5a10d5a3..08baea638 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/Podcast.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/Podcast.scala @@ -43,7 +43,7 @@ object Podcast extends ShapeTag.Companion[Podcast] { def $ordinal: Int = 0 } - object Video extends ShapeTag.Companion[Video] { + object Video { val id: ShapeId = ShapeId("smithy4s.example", "Video") val hints: Hints = Hints.empty @@ -69,7 +69,7 @@ object Podcast extends ShapeTag.Companion[Podcast] { def $ordinal: Int = 1 } - object Audio extends ShapeTag.Companion[Audio] { + object Audio { val id: ShapeId = ShapeId("smithy4s.example", "Audio") val hints: Hints = Hints.empty diff --git a/modules/bootstrapped/src/generated/smithy4s/example/TestAdt.scala b/modules/bootstrapped/src/generated/smithy4s/example/TestAdt.scala index 42b173c7a..d6287ac67 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/TestAdt.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/TestAdt.scala @@ -40,7 +40,7 @@ object TestAdt extends ShapeTag.Companion[TestAdt] { def $ordinal: Int = 0 } - object AdtOne extends ShapeTag.Companion[AdtOne] { + object AdtOne { val id: ShapeId = ShapeId("smithy4s.example", "AdtOne") val hints: Hints = Hints.empty @@ -61,7 +61,7 @@ object TestAdt extends ShapeTag.Companion[TestAdt] { def $ordinal: Int = 1 } - object AdtTwo extends ShapeTag.Companion[AdtTwo] { + object AdtTwo { val id: ShapeId = ShapeId("smithy4s.example", "AdtTwo") val hints: Hints = Hints.empty diff --git a/modules/bootstrapped/src/generated/smithy4s/example/TestMixinAdt.scala b/modules/bootstrapped/src/generated/smithy4s/example/TestMixinAdt.scala index 3bcbf9854..aab179c86 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/TestMixinAdt.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/TestMixinAdt.scala @@ -33,7 +33,7 @@ object TestMixinAdt extends ShapeTag.Companion[TestMixinAdt] { def $ordinal: Int = 0 } - object TestAdtMemberWithMixin extends ShapeTag.Companion[TestAdtMemberWithMixin] { + object TestAdtMemberWithMixin { val id: ShapeId = ShapeId("smithy4s.example", "TestAdtMemberWithMixin") val hints: Hints = Hints.empty diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala index 0ed9f11ae..8064d82cb 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/Class.scala @@ -34,7 +34,7 @@ object Class extends ShapeTag.Companion[Class] { def $ordinal: Int = 0 } - object AdtStruct extends ShapeTag.Companion[AdtStruct] { + object AdtStruct { val id: ShapeId = ShapeId("smithy4s.example.collision", "AdtStruct") val hints: Hints = Hints.empty diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index f3347d352..11bd20285 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -711,7 +711,7 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => } }, newline, - obj(product.nameRef, shapeTag(product.nameRef))( + obj(product.nameRef, if (adtParent.isEmpty) shapeTag(product.nameRef) else Line.empty)( renderId(shapeId), newline, renderHintsVal(hints), From 1d8f67d995cd90f4e65f6c20ce7ac8c651efb8f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Thu, 3 Oct 2024 16:08:31 +0200 Subject: [PATCH 08/10] Revert "Remove extra widen" This reverts commit e143b46f20b0c0e371b3ab479db56145156a98f9. --- .../src/generated/smithy4s/example/TestString.scala | 2 +- .../smithy4s/example/collision/ReservedNameUnionTrait.scala | 2 +- .../codegen/src/smithy4s/codegen/internals/Renderer.scala | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala b/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala index 152c04113..b9c3a417b 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/TestString.scala @@ -10,7 +10,7 @@ import smithy4s.schema.Schema.string object TestString extends Newtype[String] { val id: ShapeId = ShapeId("smithy4s.example", "TestString") val hints: Hints = Hints( - smithy4s.example.TestTrait(orderType = Some(smithy4s.example.OrderType.InStoreOrder(id = smithy4s.example.OrderNumber(100), locationId = Some("someLocation")))), + smithy4s.example.TestTrait(orderType = Some(smithy4s.example.OrderType.InStoreOrder(id = smithy4s.example.OrderNumber(100), locationId = Some("someLocation")).widen)), ).lazily val underlyingSchema: Schema[String] = string.withId(id).addHints(hints) implicit val schema: Schema[TestString] = bijection(underlyingSchema, asBijection) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala b/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala index 037b6257c..d1d388b4d 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/collision/ReservedNameUnionTrait.scala @@ -10,7 +10,7 @@ import smithy4s.schema.Schema.string object ReservedNameUnionTrait extends Newtype[java.lang.String] { val id: ShapeId = ShapeId("smithy4s.example.collision", "ReservedNameUnionTrait") val hints: Hints = Hints( - smithy4s.example.collision.Class.AdtStruct(), + smithy4s.example.collision.Class.AdtStruct().widen, ).lazily val underlyingSchema: Schema[java.lang.String] = string.withId(id).addHints(hints) implicit val schema: Schema[ReservedNameUnionTrait] = bijection(underlyingSchema, asBijection) diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index 11bd20285..a2989f41a 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -1617,7 +1617,10 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => line"${ref.show}.${altName.capitalize}Case.widen".write case AltTN(_, _, AltValueTN.ProductAltTN(alt)) => - alt.runDefault.write + // The `widen` is necessary in Scala 3. + // Without it, there are two schemas (the struct and the union) that match the type + // for the conversion to Hints.Binding. + line"${alt.runDefault}.widen".write case CollectionTN(collectionType, values) => val col = collectionType.tpe From 5890c659b248fdc1d406d601a9504e02f7f650c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Thu, 3 Oct 2024 16:10:33 +0200 Subject: [PATCH 09/10] remove outdated file --- .../example/HasConstrainedNewtype.scala | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 modules/bootstrapped/src/generated/smithy4s/example/HasConstrainedNewtype.scala diff --git a/modules/bootstrapped/src/generated/smithy4s/example/HasConstrainedNewtype.scala b/modules/bootstrapped/src/generated/smithy4s/example/HasConstrainedNewtype.scala deleted file mode 100644 index 3139eb114..000000000 --- a/modules/bootstrapped/src/generated/smithy4s/example/HasConstrainedNewtype.scala +++ /dev/null @@ -1,22 +0,0 @@ -package smithy4s.example - -import smithy4s.Hints -import smithy4s.Schema -import smithy4s.ShapeId -import smithy4s.ShapeTag -import smithy4s.schema.Schema.struct - -final case class HasConstrainedNewtype(s: CityId) - -object HasConstrainedNewtype extends ShapeTag.Companion[HasConstrainedNewtype] { - val id: ShapeId = ShapeId("smithy4s.example", "HasConstrainedNewtype") - - val hints: Hints = Hints.empty - - // constructor using the original order from the spec - private def make(s: CityId): HasConstrainedNewtype = HasConstrainedNewtype(s) - - implicit val schema: Schema[HasConstrainedNewtype] = struct( - CityId.schema.validated(smithy.api.Length(min = Some(1L), max = None)).required[HasConstrainedNewtype]("s", _.s), - )(make).withId(id).addHints(hints) -} From fdb9540e3d6f9e5c072ef7e5aaabf921d72d5555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Thu, 3 Oct 2024 16:11:20 +0200 Subject: [PATCH 10/10] fix comment --- .../codegen/src/smithy4s/codegen/internals/Renderer.scala | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index a2989f41a..0fd3b734b 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -1617,9 +1617,8 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => line"${ref.show}.${altName.capitalize}Case.widen".write case AltTN(_, _, AltValueTN.ProductAltTN(alt)) => - // The `widen` is necessary in Scala 3. - // Without it, there are two schemas (the struct and the union) that match the type - // for the conversion to Hints.Binding. + // The `widen` is necessary in Scala 2. + // Without it, there is no ShapeTag to use for the conversion to Hints.Binding. line"${alt.runDefault}.widen".write case CollectionTN(collectionType, values) =>