Skip to content

Commit

Permalink
Union rendering tweaks (#1126)
Browse files Browse the repository at this point in the history
* Move union-smart-constructors to be at the top
* Replace `_ordinal` by `$ordinal`, to ensure non-collision
  • Loading branch information
Baccata authored Jul 28, 2023
1 parent c27107b commit 9d39efc
Show file tree
Hide file tree
Showing 29 changed files with 310 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,19 @@ object DynamoDBOperation {
}
sealed trait ListTablesError extends scala.Product with scala.Serializable {
@inline final def widen: ListTablesError = this
def _ordinal: Int
def $ordinal: Int
}
object ListTablesError extends ShapeTag.Companion[ListTablesError] {

def internalServerError(internalServerError:InternalServerError): ListTablesError = InternalServerErrorCase(internalServerError)
def invalidEndpointException(invalidEndpointException:InvalidEndpointException): ListTablesError = InvalidEndpointExceptionCase(invalidEndpointException)

val id: ShapeId = ShapeId("com.amazonaws.dynamodb", "ListTablesError")

val hints: Hints = Hints.empty

final case class InternalServerErrorCase(internalServerError: InternalServerError) extends ListTablesError { final def _ordinal: Int = 0 }
def internalServerError(internalServerError:InternalServerError): ListTablesError = InternalServerErrorCase(internalServerError)
final case class InvalidEndpointExceptionCase(invalidEndpointException: InvalidEndpointException) extends ListTablesError { final def _ordinal: Int = 1 }
def invalidEndpointException(invalidEndpointException:InvalidEndpointException): ListTablesError = InvalidEndpointExceptionCase(invalidEndpointException)
final case class InternalServerErrorCase(internalServerError: InternalServerError) extends ListTablesError { final def $ordinal: Int = 0 }
final case class InvalidEndpointExceptionCase(invalidEndpointException: InvalidEndpointException) extends ListTablesError { final def $ordinal: Int = 1 }

object InternalServerErrorCase {
val hints: Hints = Hints.empty
Expand All @@ -191,7 +193,7 @@ object DynamoDBOperation {
InternalServerErrorCase.alt,
InvalidEndpointExceptionCase.alt,
){
_._ordinal
_.$ordinal
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ import smithy4s.schema.Schema.union

sealed trait CheckedOrUnchecked extends scala.Product with scala.Serializable {
@inline final def widen: CheckedOrUnchecked = this
def _ordinal: Int
def $ordinal: Int
}
object CheckedOrUnchecked extends ShapeTag.Companion[CheckedOrUnchecked] {

def checked(checked:String): CheckedOrUnchecked = CheckedCase(checked)
def raw(raw:String): CheckedOrUnchecked = RawCase(raw)

val id: ShapeId = ShapeId("smithy4s.example", "CheckedOrUnchecked")

val hints: Hints = Hints.empty

final case class CheckedCase(checked: String) extends CheckedOrUnchecked { final def _ordinal: Int = 0 }
def checked(checked:String): CheckedOrUnchecked = CheckedCase(checked)
final case class RawCase(raw: String) extends CheckedOrUnchecked { final def _ordinal: Int = 1 }
def raw(raw:String): CheckedOrUnchecked = RawCase(raw)
final case class CheckedCase(checked: String) extends CheckedOrUnchecked { final def $ordinal: Int = 0 }
final case class RawCase(raw: String) extends CheckedOrUnchecked { final def $ordinal: Int = 1 }

object CheckedCase {
val hints: Hints = Hints.empty
Expand All @@ -37,6 +39,6 @@ object CheckedOrUnchecked extends ShapeTag.Companion[CheckedOrUnchecked] {
CheckedCase.alt,
RawCase.alt,
){
_._ordinal
_.$ordinal
}.withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import smithy4s.schema.Schema.union

sealed trait CheckedOrUnchecked2 extends scala.Product with scala.Serializable {
@inline final def widen: CheckedOrUnchecked2 = this
def _ordinal: Int
def $ordinal: Int
}
object CheckedOrUnchecked2 extends ShapeTag.Companion[CheckedOrUnchecked2] {

def checked(checked:String): CheckedOrUnchecked2 = CheckedCase(checked)
def raw(raw:String): CheckedOrUnchecked2 = RawCase(raw)

val id: ShapeId = ShapeId("smithy4s.example", "CheckedOrUnchecked2")

val hints: Hints = Hints(
alloy.Untagged(),
)

final case class CheckedCase(checked: String) extends CheckedOrUnchecked2 { final def _ordinal: Int = 0 }
def checked(checked:String): CheckedOrUnchecked2 = CheckedCase(checked)
final case class RawCase(raw: String) extends CheckedOrUnchecked2 { final def _ordinal: Int = 1 }
def raw(raw:String): CheckedOrUnchecked2 = RawCase(raw)
final case class CheckedCase(checked: String) extends CheckedOrUnchecked2 { final def $ordinal: Int = 0 }
final case class RawCase(raw: String) extends CheckedOrUnchecked2 { final def $ordinal: Int = 1 }

object CheckedCase {
val hints: Hints = Hints.empty
Expand All @@ -39,6 +41,6 @@ object CheckedOrUnchecked2 extends ShapeTag.Companion[CheckedOrUnchecked2] {
CheckedCase.alt,
RawCase.alt,
){
_._ordinal
_.$ordinal
}.withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,29 @@ import smithy4s.schema.Schema.union
@deprecated(message = "A compelling reason", since = "0.0.1")
sealed trait DeprecatedUnion extends scala.Product with scala.Serializable {
@inline final def widen: DeprecatedUnion = this
def _ordinal: Int
def $ordinal: Int
}
object DeprecatedUnion extends ShapeTag.Companion[DeprecatedUnion] {

@deprecated(message = "N/A", since = "N/A")
def s(s:String): DeprecatedUnion = SCase(s)
def s_V2(s_V2:String): DeprecatedUnion = S_V2Case(s_V2)
def deprecatedUnionProductCase():DeprecatedUnionProductCase = DeprecatedUnionProductCase()
@deprecated(message = "N/A", since = "N/A")
def unionProductCaseDeprecatedAtCallSite():UnionProductCaseDeprecatedAtCallSite = UnionProductCaseDeprecatedAtCallSite()

val id: ShapeId = ShapeId("smithy4s.example", "DeprecatedUnion")

val hints: Hints = Hints(
smithy.api.Deprecated(message = Some("A compelling reason"), since = Some("0.0.1")),
)

@deprecated(message = "N/A", since = "N/A")
final case class SCase(s: String) extends DeprecatedUnion { final def _ordinal: Int = 0 }
def s(s:String): DeprecatedUnion = SCase(s)
final case class S_V2Case(s_V2: String) extends DeprecatedUnion { final def _ordinal: Int = 1 }
def s_V2(s_V2:String): DeprecatedUnion = S_V2Case(s_V2)
final case class SCase(s: String) extends DeprecatedUnion { final def $ordinal: Int = 0 }
final case class S_V2Case(s_V2: String) extends DeprecatedUnion { final def $ordinal: Int = 1 }
@deprecated(message = "N/A", since = "N/A")
final case class DeprecatedUnionProductCase() extends DeprecatedUnion {
def _ordinal: Int = 2
def $ordinal: Int = 2
}
object DeprecatedUnionProductCase extends ShapeTag.Companion[DeprecatedUnionProductCase] {
val id: ShapeId = ShapeId("smithy4s.example", "DeprecatedUnionProductCase")
Expand All @@ -43,7 +49,7 @@ object DeprecatedUnion extends ShapeTag.Companion[DeprecatedUnion] {
}
@deprecated(message = "N/A", since = "N/A")
final case class UnionProductCaseDeprecatedAtCallSite() extends DeprecatedUnion {
def _ordinal: Int = 3
def $ordinal: Int = 3
}
object UnionProductCaseDeprecatedAtCallSite extends ShapeTag.Companion[UnionProductCaseDeprecatedAtCallSite] {
val id: ShapeId = ShapeId("smithy4s.example", "UnionProductCaseDeprecatedAtCallSite")
Expand Down Expand Up @@ -76,6 +82,6 @@ object DeprecatedUnion extends ShapeTag.Companion[DeprecatedUnion] {
DeprecatedUnionProductCase.alt,
UnionProductCaseDeprecatedAtCallSite.alt,
){
_._ordinal
_.$ordinal
}.withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,24 @@ object ErrorHandlingServiceOperation {
}
sealed trait ErrorHandlingOperationError extends scala.Product with scala.Serializable {
@inline final def widen: ErrorHandlingOperationError = this
def _ordinal: Int
def $ordinal: Int
}
object ErrorHandlingOperationError extends ShapeTag.Companion[ErrorHandlingOperationError] {
val id: ShapeId = ShapeId("smithy4s.example", "ErrorHandlingOperationError")

val hints: Hints = Hints.empty

final case class EHFallbackClientErrorCase(eHFallbackClientError: EHFallbackClientError) extends ErrorHandlingOperationError { final def _ordinal: Int = 0 }
def eHFallbackClientError(eHFallbackClientError:EHFallbackClientError): ErrorHandlingOperationError = EHFallbackClientErrorCase(eHFallbackClientError)
final case class EHServiceUnavailableCase(eHServiceUnavailable: EHServiceUnavailable) extends ErrorHandlingOperationError { final def _ordinal: Int = 1 }
def eHServiceUnavailable(eHServiceUnavailable:EHServiceUnavailable): ErrorHandlingOperationError = EHServiceUnavailableCase(eHServiceUnavailable)
final case class EHNotFoundCase(eHNotFound: EHNotFound) extends ErrorHandlingOperationError { final def _ordinal: Int = 2 }
def eHNotFound(eHNotFound:EHNotFound): ErrorHandlingOperationError = EHNotFoundCase(eHNotFound)
final case class EHFallbackServerErrorCase(eHFallbackServerError: EHFallbackServerError) extends ErrorHandlingOperationError { final def _ordinal: Int = 3 }
def eHFallbackServerError(eHFallbackServerError:EHFallbackServerError): ErrorHandlingOperationError = EHFallbackServerErrorCase(eHFallbackServerError)

val id: ShapeId = ShapeId("smithy4s.example", "ErrorHandlingOperationError")

val hints: Hints = Hints.empty

final case class EHFallbackClientErrorCase(eHFallbackClientError: EHFallbackClientError) extends ErrorHandlingOperationError { final def $ordinal: Int = 0 }
final case class EHServiceUnavailableCase(eHServiceUnavailable: EHServiceUnavailable) extends ErrorHandlingOperationError { final def $ordinal: Int = 1 }
final case class EHNotFoundCase(eHNotFound: EHNotFound) extends ErrorHandlingOperationError { final def $ordinal: Int = 2 }
final case class EHFallbackServerErrorCase(eHFallbackServerError: EHFallbackServerError) extends ErrorHandlingOperationError { final def $ordinal: Int = 3 }

object EHFallbackClientErrorCase {
val hints: Hints = Hints.empty
val schema: Schema[EHFallbackClientErrorCase] = bijection(EHFallbackClientError.schema.addHints(hints), EHFallbackClientErrorCase(_), _.eHFallbackClientError)
Expand Down Expand Up @@ -148,7 +150,7 @@ object ErrorHandlingServiceOperation {
EHNotFoundCase.alt,
EHFallbackServerErrorCase.alt,
){
_._ordinal
_.$ordinal
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,24 @@ object ErrorHandlingServiceExtraErrorsOperation {
}
sealed trait ExtraErrorOperationError extends scala.Product with scala.Serializable {
@inline final def widen: ExtraErrorOperationError = this
def _ordinal: Int
def $ordinal: Int
}
object ExtraErrorOperationError extends ShapeTag.Companion[ExtraErrorOperationError] {
val id: ShapeId = ShapeId("smithy4s.example", "ExtraErrorOperationError")

val hints: Hints = Hints.empty

final case class RandomOtherClientErrorCase(randomOtherClientError: RandomOtherClientError) extends ExtraErrorOperationError { final def _ordinal: Int = 0 }
def randomOtherClientError(randomOtherClientError:RandomOtherClientError): ExtraErrorOperationError = RandomOtherClientErrorCase(randomOtherClientError)
final case class RandomOtherServerErrorCase(randomOtherServerError: RandomOtherServerError) extends ExtraErrorOperationError { final def _ordinal: Int = 1 }
def randomOtherServerError(randomOtherServerError:RandomOtherServerError): ExtraErrorOperationError = RandomOtherServerErrorCase(randomOtherServerError)
final case class RandomOtherClientErrorWithCodeCase(randomOtherClientErrorWithCode: RandomOtherClientErrorWithCode) extends ExtraErrorOperationError { final def _ordinal: Int = 2 }
def randomOtherClientErrorWithCode(randomOtherClientErrorWithCode:RandomOtherClientErrorWithCode): ExtraErrorOperationError = RandomOtherClientErrorWithCodeCase(randomOtherClientErrorWithCode)
final case class RandomOtherServerErrorWithCodeCase(randomOtherServerErrorWithCode: RandomOtherServerErrorWithCode) extends ExtraErrorOperationError { final def _ordinal: Int = 3 }
def randomOtherServerErrorWithCode(randomOtherServerErrorWithCode:RandomOtherServerErrorWithCode): ExtraErrorOperationError = RandomOtherServerErrorWithCodeCase(randomOtherServerErrorWithCode)

val id: ShapeId = ShapeId("smithy4s.example", "ExtraErrorOperationError")

val hints: Hints = Hints.empty

final case class RandomOtherClientErrorCase(randomOtherClientError: RandomOtherClientError) extends ExtraErrorOperationError { final def $ordinal: Int = 0 }
final case class RandomOtherServerErrorCase(randomOtherServerError: RandomOtherServerError) extends ExtraErrorOperationError { final def $ordinal: Int = 1 }
final case class RandomOtherClientErrorWithCodeCase(randomOtherClientErrorWithCode: RandomOtherClientErrorWithCode) extends ExtraErrorOperationError { final def $ordinal: Int = 2 }
final case class RandomOtherServerErrorWithCodeCase(randomOtherServerErrorWithCode: RandomOtherServerErrorWithCode) extends ExtraErrorOperationError { final def $ordinal: Int = 3 }

object RandomOtherClientErrorCase {
val hints: Hints = Hints.empty
val schema: Schema[RandomOtherClientErrorCase] = bijection(RandomOtherClientError.schema.addHints(hints), RandomOtherClientErrorCase(_), _.randomOtherClientError)
Expand Down Expand Up @@ -149,7 +151,7 @@ object ErrorHandlingServiceExtraErrorsOperation {
RandomOtherClientErrorWithCodeCase.alt,
RandomOtherServerErrorWithCodeCase.alt,
){
_._ordinal
_.$ordinal
}
}
}
Expand Down
25 changes: 15 additions & 10 deletions modules/bootstrapped/src/generated/smithy4s/example/Foo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,31 @@ import smithy4s.schema.Schema.union
*/
sealed trait Foo extends scala.Product with scala.Serializable {
@inline final def widen: Foo = this
def _ordinal: Int
def $ordinal: Int
}
object Foo extends ShapeTag.Companion[Foo] {

def int(int:Int): Foo = IntCase(int)
/** this is a comment saying you should be careful for this case
* you never know what lies ahead with Strings like this
*/
def str(str:String): Foo = StrCase(str)
def bInt(bInt:BigInt): Foo = BIntCase(bInt)
def bDec(bDec:BigDecimal): Foo = BDecCase(bDec)

val id: ShapeId = ShapeId("smithy4s.example", "Foo")

val hints: Hints = Hints(
smithy.api.Documentation("Helpful information for Foo\nint, bigInt and bDec are useful number constructs\nThe string case is there because."),
)

final case class IntCase(int: Int) extends Foo { final def _ordinal: Int = 0 }
def int(int:Int): Foo = IntCase(int)
final case class IntCase(int: Int) extends Foo { final def $ordinal: Int = 0 }
/** this is a comment saying you should be careful for this case
* you never know what lies ahead with Strings like this
*/
final case class StrCase(str: String) extends Foo { final def _ordinal: Int = 1 }
def str(str:String): Foo = StrCase(str)
final case class BIntCase(bInt: BigInt) extends Foo { final def _ordinal: Int = 2 }
def bInt(bInt:BigInt): Foo = BIntCase(bInt)
final case class BDecCase(bDec: BigDecimal) extends Foo { final def _ordinal: Int = 3 }
def bDec(bDec:BigDecimal): Foo = BDecCase(bDec)
final case class StrCase(str: String) extends Foo { final def $ordinal: Int = 1 }
final case class BIntCase(bInt: BigInt) extends Foo { final def $ordinal: Int = 2 }
final case class BDecCase(bDec: BigDecimal) extends Foo { final def $ordinal: Int = 3 }

object IntCase {
val hints: Hints = Hints.empty
Expand Down Expand Up @@ -66,6 +71,6 @@ object Foo extends ShapeTag.Companion[Foo] {
BIntCase.alt,
BDecCase.alt,
){
_._ordinal
_.$ordinal
}.withId(id).addHints(hints)
}
14 changes: 8 additions & 6 deletions modules/bootstrapped/src/generated/smithy4s/example/Food.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import smithy4s.schema.Schema.union

sealed trait Food extends scala.Product with scala.Serializable {
@inline final def widen: Food = this
def _ordinal: Int
def $ordinal: Int
}
object Food extends ShapeTag.Companion[Food] {

def pizza(pizza:Pizza): Food = PizzaCase(pizza)
def salad(salad:Salad): Food = SaladCase(salad)

val id: ShapeId = ShapeId("smithy4s.example", "Food")

val hints: Hints = Hints.empty

final case class PizzaCase(pizza: Pizza) extends Food { final def _ordinal: Int = 0 }
def pizza(pizza:Pizza): Food = PizzaCase(pizza)
final case class SaladCase(salad: Salad) extends Food { final def _ordinal: Int = 1 }
def salad(salad:Salad): Food = SaladCase(salad)
final case class PizzaCase(pizza: Pizza) extends Food { final def $ordinal: Int = 0 }
final case class SaladCase(salad: Salad) extends Food { final def $ordinal: Int = 1 }

object PizzaCase {
val hints: Hints = Hints.empty
Expand All @@ -36,6 +38,6 @@ object Food extends ShapeTag.Companion[Food] {
PizzaCase.alt,
SaladCase.alt,
){
_._ordinal
_.$ordinal
}.withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import smithy4s.schema.Schema.union

sealed trait ForecastResult extends scala.Product with scala.Serializable {
@inline final def widen: ForecastResult = this
def _ordinal: Int
def $ordinal: Int
}
object ForecastResult extends ShapeTag.Companion[ForecastResult] {

def rain(rain:ChanceOfRain): ForecastResult = RainCase(rain)
def sun(sun:UVIndex): ForecastResult = SunCase(sun)

val id: ShapeId = ShapeId("smithy4s.example", "ForecastResult")

val hints: Hints = Hints.empty
Expand All @@ -22,10 +26,8 @@ object ForecastResult extends ShapeTag.Companion[ForecastResult] {
val sun: Prism[ForecastResult, UVIndex] = Prism.partial[ForecastResult, UVIndex]{ case SunCase(t) => t }(SunCase.apply)
}

final case class RainCase(rain: ChanceOfRain) extends ForecastResult { final def _ordinal: Int = 0 }
def rain(rain:ChanceOfRain): ForecastResult = RainCase(rain)
final case class SunCase(sun: UVIndex) extends ForecastResult { final def _ordinal: Int = 1 }
def sun(sun:UVIndex): ForecastResult = SunCase(sun)
final case class RainCase(rain: ChanceOfRain) extends ForecastResult { final def $ordinal: Int = 0 }
final case class SunCase(sun: UVIndex) extends ForecastResult { final def $ordinal: Int = 1 }

object RainCase {
val hints: Hints = Hints.empty
Expand All @@ -42,6 +44,6 @@ object ForecastResult extends ShapeTag.Companion[ForecastResult] {
RainCase.alt,
SunCase.alt,
){
_._ordinal
_.$ordinal
}.withId(id).addHints(hints)
}
Loading

0 comments on commit 9d39efc

Please sign in to comment.