From f6b0309503b0af68acfdea9f0366b5347e3ce771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 16:16:07 +0200 Subject: [PATCH 1/7] Reproduce #1599 --- .../example/HasUnionUnitCaseTrait.scala | 17 +++++ .../example/UnionTraitWithUnitCase.scala | 67 +++++++++++++++++++ .../generated/smithy4s/example/package.scala | 1 + .../codegen/internals/ModelLoader.scala | 2 +- sampleSpecs/misc.smithy | 11 +++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 modules/bootstrapped/src/generated/smithy4s/example/HasUnionUnitCaseTrait.scala create mode 100644 modules/bootstrapped/src/generated/smithy4s/example/UnionTraitWithUnitCase.scala diff --git a/modules/bootstrapped/src/generated/smithy4s/example/HasUnionUnitCaseTrait.scala b/modules/bootstrapped/src/generated/smithy4s/example/HasUnionUnitCaseTrait.scala new file mode 100644 index 000000000..026bf9b37 --- /dev/null +++ b/modules/bootstrapped/src/generated/smithy4s/example/HasUnionUnitCaseTrait.scala @@ -0,0 +1,17 @@ +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 HasUnionUnitCaseTrait extends Newtype[String] { + val id: ShapeId = ShapeId("smithy4s.example", "HasUnionUnitCaseTrait") + val hints: Hints = Hints( + smithy4s.example.UnionTraitWithUnitCase.UCase(()).widen, + ).lazily + val underlyingSchema: Schema[String] = string.withId(id).addHints(hints) + implicit val schema: Schema[HasUnionUnitCaseTrait] = bijection(underlyingSchema, asBijection) +} diff --git a/modules/bootstrapped/src/generated/smithy4s/example/UnionTraitWithUnitCase.scala b/modules/bootstrapped/src/generated/smithy4s/example/UnionTraitWithUnitCase.scala new file mode 100644 index 000000000..b40bb9c3b --- /dev/null +++ b/modules/bootstrapped/src/generated/smithy4s/example/UnionTraitWithUnitCase.scala @@ -0,0 +1,67 @@ +package smithy4s.example + +import UnionTraitWithUnitCase.UCaseAlt +import smithy4s.Hints +import smithy4s.Schema +import smithy4s.ShapeId +import smithy4s.ShapeTag +import smithy4s.schema.Schema.bijection +import smithy4s.schema.Schema.recursive +import smithy4s.schema.Schema.string +import smithy4s.schema.Schema.union + +sealed trait UnionTraitWithUnitCase extends scala.Product with scala.Serializable { self => + @inline final def widen: UnionTraitWithUnitCase = this + def $ordinal: Int + + object project { + def u: Option[UnionTraitWithUnitCase.UCase.type] = UCaseAlt.project.lift(self) + def s: Option[String] = UnionTraitWithUnitCase.SCase.alt.project.lift(self).map(_.s) + } + + def accept[A](visitor: UnionTraitWithUnitCase.Visitor[A]): A = this match { + case value: UnionTraitWithUnitCase.UCase.type => visitor.u(value) + case value: UnionTraitWithUnitCase.SCase => visitor.s(value.s) + } +} +object UnionTraitWithUnitCase extends ShapeTag.Companion[UnionTraitWithUnitCase] { + + def u(): UnionTraitWithUnitCase = UnionTraitWithUnitCase.UCase + def s(s: String): UnionTraitWithUnitCase = SCase(s) + + val id: ShapeId = ShapeId("smithy4s.example", "unionTraitWithUnitCase") + + val hints: Hints = Hints( + smithy.api.Trait(selector = None, structurallyExclusive = None, conflicts = None, breakingChanges = None), + ).lazily + + case object UCase extends UnionTraitWithUnitCase { final def $ordinal: Int = 0 } + private val UCaseAlt = Schema.constant(UnionTraitWithUnitCase.UCase).oneOf[UnionTraitWithUnitCase]("u").addHints(hints) + final case class SCase(s: String) extends UnionTraitWithUnitCase { final def $ordinal: Int = 1 } + + object SCase { + val hints: Hints = Hints.empty + val schema: Schema[UnionTraitWithUnitCase.SCase] = bijection(string.addHints(hints), UnionTraitWithUnitCase.SCase(_), _.s) + val alt = schema.oneOf[UnionTraitWithUnitCase]("s") + } + + trait Visitor[A] { + def u(value: UnionTraitWithUnitCase.UCase.type): A + def s(value: String): A + } + + object Visitor { + trait Default[A] extends Visitor[A] { + def default: A + def u(value: UnionTraitWithUnitCase.UCase.type): A = default + def s(value: String): A = default + } + } + + implicit val schema: Schema[UnionTraitWithUnitCase] = recursive(union( + UCaseAlt, + UnionTraitWithUnitCase.SCase.alt, + ){ + _.$ordinal + }.withId(id).addHints(hints)) +} diff --git a/modules/bootstrapped/src/generated/smithy4s/example/package.scala b/modules/bootstrapped/src/generated/smithy4s/example/package.scala index a914f9cf4..568c337a0 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/package.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/package.scala @@ -66,6 +66,7 @@ package object example { type ExtraData = smithy4s.example.ExtraData.Type type FancyList = smithy4s.example.FancyList.Type type FreeForm = smithy4s.example.FreeForm.Type + type HasUnionUnitCaseTrait = smithy4s.example.HasUnionUnitCaseTrait.Type type Ingredients = smithy4s.example.Ingredients.Type /** @param member * listFoo 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/misc.smithy b/sampleSpecs/misc.smithy index 27f8f3f9d..a5392c2a8 100644 --- a/sampleSpecs/misc.smithy +++ b/sampleSpecs/misc.smithy @@ -110,3 +110,14 @@ structure RangeCheck { // face with sunglasses emoji @pattern("^\\uD83D\\uDE0E$") string UnicodeRegexString + +@trait +union unionTraitWithUnitCase { + u: Unit + s: String +} + +@unionTraitWithUnitCase(u: { + +}) +string HasUnionUnitCaseTrait From 03d0a9d0bb2b7f5ae0b3162f02ca32b485f60b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 16:55:14 +0200 Subject: [PATCH 2/7] dirty fix --- .../smithy4s/example/HasUnionUnitCaseTrait.scala | 2 +- .../codegen/src/smithy4s/codegen/internals/Renderer.scala | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/bootstrapped/src/generated/smithy4s/example/HasUnionUnitCaseTrait.scala b/modules/bootstrapped/src/generated/smithy4s/example/HasUnionUnitCaseTrait.scala index 026bf9b37..f96e4fa4c 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/HasUnionUnitCaseTrait.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/HasUnionUnitCaseTrait.scala @@ -10,7 +10,7 @@ import smithy4s.schema.Schema.string object HasUnionUnitCaseTrait extends Newtype[String] { val id: ShapeId = ShapeId("smithy4s.example", "HasUnionUnitCaseTrait") val hints: Hints = Hints( - smithy4s.example.UnionTraitWithUnitCase.UCase(()).widen, + smithy4s.example.UnionTraitWithUnitCase.UCase.widen, ).lazily val underlyingSchema: Schema[String] = string.withId(id).addHints(hints) implicit val schema: Schema[HasUnionUnitCaseTrait] = bijection(underlyingSchema, asBijection) diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index 1adccaf79..3145a283f 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -1611,7 +1611,13 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => }) case AltTN(ref, altName, AltValueTN.TypeAltTN(alt)) => - line"${ref.show}.${altName.capitalize}Case(${alt.runDefault}).widen".write + val justParens = line"()" + val constructorCall = alt.runDefault match { + case `justParens` => Line.empty + case more => line"($more)" + } + + line"${ref.show}.${altName.capitalize}Case$constructorCall.widen".write case AltTN(_, _, AltValueTN.ProductAltTN(alt)) => alt.runDefault.write From af5e90a6b3ad6b8b7d4e04974acdb2c104a8119a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 17:03:27 +0200 Subject: [PATCH 3/7] Slightly better - keep track of whether we're Unit --- .../internals/CollisionAvoidance.scala | 4 +-- .../src/smithy4s/codegen/internals/IR.scala | 12 ++++--- .../smithy4s/codegen/internals/Renderer.scala | 13 +++----- .../codegen/internals/SmithyToIR.scala | 33 ++++++++++--------- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala b/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala index ac0d50ac0..a3db6f0aa 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala @@ -229,8 +229,8 @@ private[internals] object CollisionAvoidance { NewTypeTN(modRef(ref), target) case ValidatedNewTypeTN(ref, target) => ValidatedNewTypeTN(modRef(ref), target) - case AltTN(ref, altName, alt) => - AltTN(modRef(ref), altName, alt) + case AltTN(ref, altName, alt, isUnit) => + AltTN(modRef(ref), altName, alt, isUnit) case MapTN(values) => MapTN(values) case CollectionTN(collectionType, values) => diff --git a/modules/codegen/src/smithy4s/codegen/internals/IR.scala b/modules/codegen/src/smithy4s/codegen/internals/IR.scala index fbdc20cdc..a34afcaa1 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/IR.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/IR.scala @@ -461,8 +461,8 @@ private[internals] object TypedNode { f(target).map(NewTypeTN(ref, _)) case ValidatedNewTypeTN(ref, target) => f(target).map(ValidatedNewTypeTN(ref, _)) - case AltTN(ref, altName, alt) => - alt.traverse(f).map(AltTN(ref, altName, _)) + case AltTN(ref, altName, alt, isUnit) => + alt.traverse(f).map(AltTN(ref, altName, _, isUnit)) case MapTN(values) => values .traverse { case (k, v) => @@ -493,8 +493,12 @@ private[internals] object TypedNode { case class NewTypeTN[A](ref: Type.Ref, target: A) extends TypedNode[A] case class ValidatedNewTypeTN[A](ref: Type.Ref, target: A) extends TypedNode[A] - case class AltTN[A](ref: Type.Ref, altName: String, alt: AltValueTN[A]) - extends TypedNode[A] + case class AltTN[A]( + ref: Type.Ref, + altName: String, + alt: AltValueTN[A], + isUnit: Boolean + ) extends TypedNode[A] case class MapTN[A](values: List[(A, A)]) extends TypedNode[A] case class CollectionTN[A](collectionType: CollectionType, values: List[A]) extends TypedNode[A] diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index 3145a283f..502cd8874 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -1610,16 +1610,13 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => false -> line"${ref.show}.unsafeApply($text)" }) - case AltTN(ref, altName, AltValueTN.TypeAltTN(alt)) => - val justParens = line"()" - val constructorCall = alt.runDefault match { - case `justParens` => Line.empty - case more => line"($more)" - } + case AltTN(ref, altName, AltValueTN.TypeAltTN(alt), isUnit) => + val applyConstructor = + line"(${alt.runDefault})".when(!isUnit) - line"${ref.show}.${altName.capitalize}Case$constructorCall.widen".write + line"${ref.show}.${altName.capitalize}Case$applyConstructor.widen".write - case AltTN(_, _, AltValueTN.ProductAltTN(alt)) => + case AltTN(_, _, AltValueTN.ProductAltTN(alt), _) => alt.runDefault.write case CollectionTN(collectionType, values) => diff --git a/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala b/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala index bee39fb2e..c763b41c3 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala @@ -1048,7 +1048,12 @@ private[codegen] class SmithyToIR( maybeTypeclassesHint(shape) } - case class AltInfo(name: String, tpe: Type, isAdtMember: Boolean) + case class AltInfo( + name: String, + tpe: Type, + isAdtMember: Boolean, + isUnit: Boolean + ) implicit class ShapeExt(shape: Shape) { def name = shape.getId().getName() @@ -1151,21 +1156,19 @@ private[codegen] class SmithyToIR( shape .members() .asScala - .map { member => - val memberTarget = - model.expectShape(member.getTarget) - if (isPartOfAdt(memberTarget)) { - (member.getMemberName(), member.tpe.map(Left(_))) - } else { - (member.getMemberName(), member.tpe.map(Right(_))) + .flatMap { member => + member.tpe.map { tpe => + val memberTarget = model.expectShape(member.getTarget) + val isUnit = memberTarget.getId() == ShapeId.from("smithy.api#Unit") + + AltInfo( + member.getMemberName(), + tpe, + isAdtMember = isPartOfAdt(memberTarget), + isUnit = isUnit + ) } } - .collect { - case (name, Some(Left(tpe))) => - AltInfo(name, tpe, isAdtMember = true) - case (name, Some(Right(tpe))) => - AltInfo(name, tpe, isAdtMember = false) - } .toList } @@ -1306,7 +1309,7 @@ private[codegen] class SmithyToIR( val t = NodeAndType(node, alt.tpe) TypedNode.AltValueTN.TypeAltTN(t) } - TypedNode.AltTN(ref, name, a) + TypedNode.AltTN(ref, name, a, alt.isUnit) // Alias case (node, Type.Alias(ns, name, tpe, _)) => TypedNode.NewTypeTN(Type.Ref(ns, name), NodeAndType(node, tpe)) From 04e1464424362a96563366952452dd1994ffb7bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 17:07:38 +0200 Subject: [PATCH 4/7] Implement with UnitAltTN --- .../codegen/internals/CollisionAvoidance.scala | 4 ++-- .../src/smithy4s/codegen/internals/IR.scala | 16 ++++++++-------- .../smithy4s/codegen/internals/Renderer.scala | 10 +++++----- .../smithy4s/codegen/internals/SmithyToIR.scala | 4 +++- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala b/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala index a3db6f0aa..ac0d50ac0 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala @@ -229,8 +229,8 @@ private[internals] object CollisionAvoidance { NewTypeTN(modRef(ref), target) case ValidatedNewTypeTN(ref, target) => ValidatedNewTypeTN(modRef(ref), target) - case AltTN(ref, altName, alt, isUnit) => - AltTN(modRef(ref), altName, alt, isUnit) + case AltTN(ref, altName, alt) => + AltTN(modRef(ref), altName, alt) case MapTN(values) => MapTN(values) case CollectionTN(collectionType, values) => diff --git a/modules/codegen/src/smithy4s/codegen/internals/IR.scala b/modules/codegen/src/smithy4s/codegen/internals/IR.scala index a34afcaa1..3519c1d3a 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/IR.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/IR.scala @@ -31,6 +31,7 @@ import TypedNode.FieldTN.OptionalSomeTN import TypedNode.FieldTN.RequiredTN import TypedNode.AltValueTN.ProductAltTN import TypedNode.AltValueTN.TypeAltTN +import TypedNode.AltValueTN.UnitAltTN import UnionMember._ import LineSegment.{NameDef, NameRef} @@ -426,6 +427,7 @@ private[internals] object TypedNode { def map[B](f: A => B): AltValueTN[B] = this match { case ProductAltTN(value) => ProductAltTN(f(value)) case TypeAltTN(value) => TypeAltTN(f(value)) + case UnitAltTN => UnitAltTN } } object AltValueTN { @@ -437,6 +439,7 @@ private[internals] object TypedNode { fa match { case ProductAltTN(value) => f(value).map(ProductAltTN(_)) case TypeAltTN(value) => f(value).map(TypeAltTN(_)) + case UnitAltTN => Applicative[G].pure(UnitAltTN) } def foldLeft[A, B](fa: AltValueTN[A], b: B)(f: (B, A) => B): B = ??? def foldRight[A, B](fa: AltValueTN[A], lb: Eval[B])( @@ -446,6 +449,7 @@ private[internals] object TypedNode { case class ProductAltTN[A](value: A) extends AltValueTN[A] case class TypeAltTN[A](value: A) extends AltValueTN[A] + case object UnitAltTN extends AltValueTN[Nothing] } implicit val typedNodeTraverse: Traverse[TypedNode] = @@ -461,8 +465,8 @@ private[internals] object TypedNode { f(target).map(NewTypeTN(ref, _)) case ValidatedNewTypeTN(ref, target) => f(target).map(ValidatedNewTypeTN(ref, _)) - case AltTN(ref, altName, alt, isUnit) => - alt.traverse(f).map(AltTN(ref, altName, _, isUnit)) + case AltTN(ref, altName, alt) => + alt.traverse(f).map(AltTN(ref, altName, _)) case MapTN(values) => values .traverse { case (k, v) => @@ -493,12 +497,8 @@ private[internals] object TypedNode { case class NewTypeTN[A](ref: Type.Ref, target: A) extends TypedNode[A] case class ValidatedNewTypeTN[A](ref: Type.Ref, target: A) extends TypedNode[A] - case class AltTN[A]( - ref: Type.Ref, - altName: String, - alt: AltValueTN[A], - isUnit: Boolean - ) extends TypedNode[A] + case class AltTN[A](ref: Type.Ref, altName: String, alt: AltValueTN[A]) + extends TypedNode[A] case class MapTN[A](values: List[(A, A)]) extends TypedNode[A] case class CollectionTN[A](collectionType: CollectionType, values: List[A]) extends TypedNode[A] diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index 502cd8874..230e77bb4 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -1610,13 +1610,13 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => false -> line"${ref.show}.unsafeApply($text)" }) - case AltTN(ref, altName, AltValueTN.TypeAltTN(alt), isUnit) => - val applyConstructor = - line"(${alt.runDefault})".when(!isUnit) + case AltTN(ref, altName, AltValueTN.TypeAltTN(alt)) => + line"${ref.show}.${altName.capitalize}Case(${alt.runDefault}).widen".write - line"${ref.show}.${altName.capitalize}Case$applyConstructor.widen".write + case AltTN(ref, altName, AltValueTN.UnitAltTN) => + line"${ref.show}.${altName.capitalize}Case.widen".write - case AltTN(_, _, AltValueTN.ProductAltTN(alt), _) => + case AltTN(_, _, AltValueTN.ProductAltTN(alt)) => alt.runDefault.write case CollectionTN(collectionType, values) => diff --git a/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala b/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala index c763b41c3..5ba1c3144 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala @@ -1305,11 +1305,13 @@ private[codegen] class SmithyToIR( val a = if (alt.isAdtMember) { val t = NodeAndType(node, alt.tpe) TypedNode.AltValueTN.ProductAltTN(t) + } else if (alt.isUnit) { + TypedNode.AltValueTN.UnitAltTN } else { val t = NodeAndType(node, alt.tpe) TypedNode.AltValueTN.TypeAltTN(t) } - TypedNode.AltTN(ref, name, a, alt.isUnit) + TypedNode.AltTN(ref, name, a) // Alias case (node, Type.Alias(ns, name, tpe, _)) => TypedNode.NewTypeTN(Type.Ref(ns, name), NodeAndType(node, tpe)) From e2283694be4ffbbb0b29c3a4e7d2b3d947b5dbfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 17:11:20 +0200 Subject: [PATCH 5/7] Add changelog entry --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 935875af2..8aac752bf 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 an issue in which union members targetting Unit would fail to compile when used as traits (see [#1600](https://github.com/disneystreaming/smithy4s/pull/1600)). + # 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 From 128a4e0cd720c75fbc207d7730db51a4a1e1ce24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 17:16:07 +0200 Subject: [PATCH 6/7] Nicer Unit check --- modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala b/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala index 5ba1c3144..562348fef 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala @@ -1159,7 +1159,7 @@ private[codegen] class SmithyToIR( .flatMap { member => member.tpe.map { tpe => val memberTarget = model.expectShape(member.getTarget) - val isUnit = memberTarget.getId() == ShapeId.from("smithy.api#Unit") + val isUnit = tpe == Type.unit AltInfo( member.getMemberName(), From 5eafc2f247764c7c942cfb2e8e4cbffe36c8d67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Sat, 28 Sep 2024 20:26:58 +0200 Subject: [PATCH 7/7] Make isUnit a def --- .../src/smithy4s/codegen/internals/SmithyToIR.scala | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala b/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala index 562348fef..0ccc14bd2 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/SmithyToIR.scala @@ -1048,12 +1048,9 @@ private[codegen] class SmithyToIR( maybeTypeclassesHint(shape) } - case class AltInfo( - name: String, - tpe: Type, - isAdtMember: Boolean, - isUnit: Boolean - ) + case class AltInfo(name: String, tpe: Type, isAdtMember: Boolean) { + def isUnit: Boolean = tpe == Type.unit + } implicit class ShapeExt(shape: Shape) { def name = shape.getId().getName() @@ -1159,13 +1156,11 @@ private[codegen] class SmithyToIR( .flatMap { member => member.tpe.map { tpe => val memberTarget = model.expectShape(member.getTarget) - val isUnit = tpe == Type.unit AltInfo( member.getMemberName(), tpe, - isAdtMember = isPartOfAdt(memberTarget), - isUnit = isUnit + isAdtMember = isPartOfAdt(memberTarget) ) } }