-
Notifications
You must be signed in to change notification settings - Fork 73
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
Don't render parens for case objects in union member nodes #1600
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f6b0309
Reproduce #1599
kubukoz 03d0a9d
dirty fix
kubukoz af5e90a
Slightly better - keep track of whether we're Unit
kubukoz 04e1464
Implement with UnitAltTN
kubukoz e228369
Add changelog entry
kubukoz 128a4e0
Nicer Unit check
kubukoz 5eafc2f
Make isUnit a def
kubukoz 8e9c181
Merge branch 'series/0.18' into reproduce-1599
Baccata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
modules/bootstrapped/src/generated/smithy4s/example/HasUnionUnitCaseTrait.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
67 changes: 67 additions & 0 deletions
67
modules/bootstrapped/src/generated/smithy4s/example/UnionTraitWithUnitCase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1048,7 +1048,9 @@ private[codegen] class SmithyToIR( | |
maybeTypeclassesHint(shape) | ||
} | ||
|
||
case class AltInfo(name: String, tpe: Type, isAdtMember: 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() | ||
|
@@ -1151,21 +1153,17 @@ 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 => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: this change is no longer necessary, it's merely a refactor. I left it in because the original seemed needlessly duplicative. |
||
member.tpe.map { tpe => | ||
val memberTarget = model.expectShape(member.getTarget) | ||
|
||
AltInfo( | ||
member.getMemberName(), | ||
tpe, | ||
isAdtMember = isPartOfAdt(memberTarget) | ||
) | ||
} | ||
} | ||
.collect { | ||
case (name, Some(Left(tpe))) => | ||
AltInfo(name, tpe, isAdtMember = true) | ||
case (name, Some(Right(tpe))) => | ||
AltInfo(name, tpe, isAdtMember = false) | ||
} | ||
.toList | ||
} | ||
|
||
|
@@ -1302,6 +1300,8 @@ 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: this is my third implementation of the fix, I think it's the most principled as
AltValueTN
now more accurately mirrors theUnionMember
ADT used on the schema side of things.