Skip to content

Commit

Permalink
Merge pull request #1432 from disneystreaming/enable-alloy.proto
Browse files Browse the repository at this point in the history
Enable code generation of alloy.proto in core
  • Loading branch information
Baccata authored Mar 6, 2024
2 parents 173b6bb + 1b88980 commit 816ac1e
Show file tree
Hide file tree
Showing 35 changed files with 1,037 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* smithy4s Structure schemas are now retaining the original order of fields, as per the specification.
* Added a utility method, `Schema.transformTransitivelyK`, to help in recursively transforming schemas.
In addition, the semantics of `transformHintsTransitively` have been changed: the transformation no longer modifies the hints on the result of the `total` function.
* smithy4s-core now contains the generated code for the alloy.proto namespace

# 0.18.10

Expand Down
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ lazy val core = projectMatrix
"smithy.api",
"smithy.waiters",
"alloy",
"alloy.common"
"alloy.common",
"alloy.proto"
),
smithy4sDependencies ++= Seq(
Dependencies.Smithy.waiters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.bigdecimal
import smithy4s.schema.Schema.struct

final case class BigDecimalWrapper(bigDecimal: BigDecimal)

object BigDecimalWrapper extends ShapeTag.Companion[BigDecimalWrapper] {
val id: ShapeId = ShapeId("smithy4s.example", "BigDecimalWrapper")

val hints: Hints = Hints(
alloy.proto.ProtoEnabled(),
).lazily

// constructor using the original order from the spec
private def make(bigDecimal: BigDecimal): BigDecimalWrapper = BigDecimalWrapper(bigDecimal)

implicit val schema: Schema[BigDecimalWrapper] = struct(
bigdecimal.required[BigDecimalWrapper]("bigDecimal", _.bigDecimal).addHints(alloy.proto.ProtoIndex(1)),
)(make).withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package smithy4s.example

import smithy4s.Enumeration
import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.EnumTag
import smithy4s.schema.Schema.enumeration

sealed abstract class ClosedInt(_value: String, _name: String, _intValue: Int, _hints: Hints) extends Enumeration.Value {
override type EnumType = ClosedInt
override val value: String = _value
override val name: String = _name
override val intValue: Int = _intValue
override val hints: Hints = _hints
override def enumeration: Enumeration[EnumType] = ClosedInt
@inline final def widen: ClosedInt = this
}
object ClosedInt extends Enumeration[ClosedInt] with ShapeTag.Companion[ClosedInt] {
val id: ShapeId = ShapeId("smithy4s.example", "ClosedInt")

val hints: Hints = Hints.empty

case object FOO extends ClosedInt("FOO", "FOO", 0, Hints.empty) {
override val hints: Hints = Hints(alloy.proto.ProtoIndex(0)).lazily
}
case object BAR extends ClosedInt("BAR", "BAR", 1, Hints.empty) {
override val hints: Hints = Hints(alloy.proto.ProtoIndex(1)).lazily
}

val values: List[ClosedInt] = List(
FOO,
BAR,
)
val tag: EnumTag[ClosedInt] = EnumTag.ClosedIntEnum
implicit val schema: Schema[ClosedInt] = enumeration(tag, values).withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package smithy4s.example

import smithy4s.Enumeration
import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.EnumTag
import smithy4s.schema.Schema.enumeration

sealed abstract class ClosedString(_value: String, _name: String, _intValue: Int, _hints: Hints) extends Enumeration.Value {
override type EnumType = ClosedString
override val value: String = _value
override val name: String = _name
override val intValue: Int = _intValue
override val hints: Hints = _hints
override def enumeration: Enumeration[EnumType] = ClosedString
@inline final def widen: ClosedString = this
}
object ClosedString extends Enumeration[ClosedString] with ShapeTag.Companion[ClosedString] {
val id: ShapeId = ShapeId("smithy4s.example", "ClosedString")

val hints: Hints = Hints.empty

case object FOO extends ClosedString("FOO", "FOO", 0, Hints.empty) {
override val hints: Hints = Hints(alloy.proto.ProtoIndex(0)).lazily
}
case object BAR extends ClosedString("BAR", "BAR", 1, Hints.empty) {
override val hints: Hints = Hints(alloy.proto.ProtoIndex(1)).lazily
}

val values: List[ClosedString] = List(
FOO,
BAR,
)
val tag: EnumTag[ClosedString] = EnumTag.ClosedStringEnum
implicit val schema: Schema[ClosedString] = enumeration(tag, values).withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ object DummyServiceOperation {
val schema: OperationSchema[HostLabelInput, Nothing, Unit, Nothing, Nothing] = Schema.operation(ShapeId("smithy4s.example", "DummyHostPrefix"))
.withInput(HostLabelInput.schema)
.withOutput(unit)
.withHints(smithy.api.Http(method = smithy.api.NonEmptyString("GET"), uri = smithy.api.NonEmptyString("/dummy"), code = 200), smithy.api.Endpoint(hostPrefix = smithy.api.NonEmptyString("foo.{label1}--abc{label2}.{label3}.secure.")))
.withHints(smithy.api.Http(method = smithy.api.NonEmptyString("POST"), uri = smithy.api.NonEmptyString("/dummy"), code = 200), smithy.api.Endpoint(hostPrefix = smithy.api.NonEmptyString("foo.{label1}--abc{label2}.{label3}.secure.")))
def wrap(input: HostLabelInput): DummyHostPrefix = DummyHostPrefix(input)
}
final case class DummyPath(input: PathParams) extends DummyServiceOperation[PathParams, Nothing, Unit, Nothing, Nothing] {
Expand Down
27 changes: 27 additions & 0 deletions modules/bootstrapped/src/generated/smithy4s/example/Enums.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.struct

final case class Enums(closedString: ClosedString, openString: OpenString, closedInt: ClosedInt, openInt: OpenInt)

object Enums extends ShapeTag.Companion[Enums] {
val id: ShapeId = ShapeId("smithy4s.example", "Enums")

val hints: Hints = Hints(
alloy.proto.ProtoEnabled(),
).lazily

// constructor using the original order from the spec
private def make(closedString: ClosedString, openString: OpenString, closedInt: ClosedInt, openInt: OpenInt): Enums = Enums(closedString, openString, closedInt, openInt)

implicit val schema: Schema[Enums] = struct(
ClosedString.schema.required[Enums]("closedString", _.closedString).addHints(alloy.proto.ProtoIndex(1)),
OpenString.schema.required[Enums]("openString", _.openString).addHints(alloy.proto.ProtoIndex(2)),
ClosedInt.schema.required[Enums]("closedInt", _.closedInt).addHints(alloy.proto.ProtoIndex(3)),
OpenInt.schema.required[Enums]("openInt", _.openInt).addHints(alloy.proto.ProtoIndex(4)),
)(make).withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.struct

final case class InlinedUnionWrapper(myInlinedUnion: Option[MyInlinedUnion] = None)

object InlinedUnionWrapper extends ShapeTag.Companion[InlinedUnionWrapper] {
val id: ShapeId = ShapeId("smithy4s.example", "InlinedUnionWrapper")

val hints: Hints = Hints(
alloy.proto.ProtoEnabled(),
).lazily

// constructor using the original order from the spec
private def make(myInlinedUnion: Option[MyInlinedUnion]): InlinedUnionWrapper = InlinedUnionWrapper(myInlinedUnion)

implicit val schema: Schema[InlinedUnionWrapper] = struct(
MyInlinedUnion.schema.optional[InlinedUnionWrapper]("myInlinedUnion", _.myInlinedUnion),
)(make).withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.struct

final case class IntListWrapper(ints: IntList)

object IntListWrapper extends ShapeTag.Companion[IntListWrapper] {
val id: ShapeId = ShapeId("smithy4s.example", "IntListWrapper")

val hints: Hints = Hints(
alloy.proto.ProtoEnabled(),
).lazily

// constructor using the original order from the spec
private def make(ints: IntList): IntListWrapper = IntListWrapper(ints)

implicit val schema: Schema[IntListWrapper] = struct(
IntList.schema.required[IntListWrapper]("ints", _.ints).addHints(alloy.proto.ProtoIndex(1)),
)(make).withId(id).addHints(hints)
}
29 changes: 29 additions & 0 deletions modules/bootstrapped/src/generated/smithy4s/example/Integers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.int
import smithy4s.schema.Schema.struct

final case class Integers(int: Int, sint: Int, uint: Int, fixedUint: Int, fixedSint: Int)

object Integers extends ShapeTag.Companion[Integers] {
val id: ShapeId = ShapeId("smithy4s.example", "Integers")

val hints: Hints = Hints(
alloy.proto.ProtoEnabled(),
).lazily

// constructor using the original order from the spec
private def make(int: Int, sint: Int, uint: Int, fixedUint: Int, fixedSint: Int): Integers = Integers(int, sint, uint, fixedUint, fixedSint)

implicit val schema: Schema[Integers] = struct(
int.required[Integers]("int", _.int).addHints(alloy.proto.ProtoIndex(1)),
int.required[Integers]("sint", _.sint).addHints(alloy.proto.ProtoNumType.SIGNED.widen, alloy.proto.ProtoIndex(2)),
int.required[Integers]("uint", _.uint).addHints(alloy.proto.ProtoNumType.UNSIGNED.widen, alloy.proto.ProtoIndex(3)),
int.required[Integers]("fixedUint", _.fixedUint).addHints(alloy.proto.ProtoNumType.FIXED.widen, alloy.proto.ProtoIndex(4)),
int.required[Integers]("fixedSint", _.fixedSint).addHints(alloy.proto.ProtoNumType.FIXED_SIGNED.widen, alloy.proto.ProtoIndex(5)),
)(make).withId(id).addHints(hints)
}
29 changes: 29 additions & 0 deletions modules/bootstrapped/src/generated/smithy4s/example/Longs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.long
import smithy4s.schema.Schema.struct

final case class Longs(long: Long, slong: Long, ulong: Long, fixedLong: Long, fixedSlong: Long)

object Longs extends ShapeTag.Companion[Longs] {
val id: ShapeId = ShapeId("smithy4s.example", "Longs")

val hints: Hints = Hints(
alloy.proto.ProtoEnabled(),
).lazily

// constructor using the original order from the spec
private def make(long: Long, slong: Long, ulong: Long, fixedLong: Long, fixedSlong: Long): Longs = Longs(long, slong, ulong, fixedLong, fixedSlong)

implicit val schema: Schema[Longs] = struct(
long.required[Longs]("long", _.long).addHints(alloy.proto.ProtoIndex(1)),
long.required[Longs]("slong", _.slong).addHints(alloy.proto.ProtoNumType.SIGNED.widen, alloy.proto.ProtoIndex(2)),
long.required[Longs]("ulong", _.ulong).addHints(alloy.proto.ProtoNumType.UNSIGNED.widen, alloy.proto.ProtoIndex(3)),
long.required[Longs]("fixedLong", _.fixedLong).addHints(alloy.proto.ProtoNumType.FIXED.widen, alloy.proto.ProtoIndex(4)),
long.required[Longs]("fixedSlong", _.fixedSlong).addHints(alloy.proto.ProtoNumType.FIXED_SIGNED.widen, alloy.proto.ProtoIndex(5)),
)(make).withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.struct

final case class MessageWrapper(message: Integers)

object MessageWrapper extends ShapeTag.Companion[MessageWrapper] {
val id: ShapeId = ShapeId("smithy4s.example", "MessageWrapper")

val hints: Hints = Hints(
alloy.proto.ProtoEnabled(),
).lazily

// constructor using the original order from the spec
private def make(message: Integers): MessageWrapper = MessageWrapper(message)

implicit val schema: Schema[MessageWrapper] = struct(
Integers.schema.required[MessageWrapper]("message", _.message).addHints(alloy.proto.ProtoIndex(1)),
)(make).withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.bijection
import smithy4s.schema.Schema.boolean
import smithy4s.schema.Schema.union

sealed trait MyInlinedUnion extends scala.Product with scala.Serializable { self =>
@inline final def widen: MyInlinedUnion = this
def $ordinal: Int

object project {
def int: Option[Int] = MyInlinedUnion.IntCase.alt.project.lift(self).map(_.int)
def bool: Option[Boolean] = MyInlinedUnion.BoolCase.alt.project.lift(self).map(_.bool)
}

def accept[A](visitor: MyInlinedUnion.Visitor[A]): A = this match {
case value: MyInlinedUnion.IntCase => visitor.int(value.int)
case value: MyInlinedUnion.BoolCase => visitor.bool(value.bool)
}
}
object MyInlinedUnion extends ShapeTag.Companion[MyInlinedUnion] {

def int(int: Int): MyInlinedUnion = IntCase(int)
def bool(bool: Boolean): MyInlinedUnion = BoolCase(bool)

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

val hints: Hints = Hints(
alloy.proto.ProtoInlinedOneOf(),
).lazily

final case class IntCase(int: Int) extends MyInlinedUnion { final def $ordinal: Int = 0 }
final case class BoolCase(bool: Boolean) extends MyInlinedUnion { final def $ordinal: Int = 1 }

object IntCase {
val hints: Hints = Hints(
alloy.proto.ProtoIndex(1),
).lazily
val schema: Schema[MyInlinedUnion.IntCase] = bijection(smithy4s.schema.Schema.int.addHints(hints), MyInlinedUnion.IntCase(_), _.int)
val alt = schema.oneOf[MyInlinedUnion]("int")
}
object BoolCase {
val hints: Hints = Hints(
alloy.proto.ProtoIndex(2),
).lazily
val schema: Schema[MyInlinedUnion.BoolCase] = bijection(boolean.addHints(hints), MyInlinedUnion.BoolCase(_), _.bool)
val alt = schema.oneOf[MyInlinedUnion]("bool")
}

trait Visitor[A] {
def int(value: Int): A
def bool(value: Boolean): A
}

object Visitor {
trait Default[A] extends Visitor[A] {
def default: A
def int(value: Int): A = default
def bool(value: Boolean): A = default
}
}

implicit val schema: Schema[MyInlinedUnion] = union(
MyInlinedUnion.IntCase.alt,
MyInlinedUnion.BoolCase.alt,
){
_.$ordinal
}.withId(id).addHints(hints)
}
15 changes: 15 additions & 0 deletions modules/bootstrapped/src/generated/smithy4s/example/MyInt.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Newtype
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.schema.Schema.bijection
import smithy4s.schema.Schema.int

object MyInt extends Newtype[Int] {
val id: ShapeId = ShapeId("smithy4s.example", "MyInt")
val hints: Hints = Hints.empty
val underlyingSchema: Schema[Int] = int.withId(id).addHints(hints)
implicit val schema: Schema[MyInt] = bijection(underlyingSchema, asBijection)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package smithy4s.example

import smithy4s.Hints
import smithy4s.Newtype
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.schema.Schema.bijection
import smithy4s.schema.Schema.list

object MyIntList extends Newtype[List[MyInt]] {
val id: ShapeId = ShapeId("smithy4s.example", "MyIntList")
val hints: Hints = Hints.empty
val underlyingSchema: Schema[List[MyInt]] = list(MyInt.schema).withId(id).addHints(hints)
implicit val schema: Schema[MyIntList] = bijection(underlyingSchema, asBijection)
}
Loading

0 comments on commit 816ac1e

Please sign in to comment.