Skip to content

Commit

Permalink
Merging the latest
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalend committed Oct 9, 2024
2 parents 45d6ce7 + ae5b9de commit 0a735a0
Show file tree
Hide file tree
Showing 38 changed files with 547 additions and 133 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Thank you!

# 0.18.25

* Add A flag to allow for numerics to be decoded from JSON strings (in smithy4s-json).
* 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)).
* Fixes an issue in which refinements wouldn't work on custom simple shapes (newtypes) (see [#1595](https://github.com/disneystreaming/smithy4s/pull/1595))
* Fixes a regression from 0.18.4 which incorrectly rendered default values for certain types (see [#1593](https://github.com/disneystreaming/smithy4s/pull/1593))
* 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)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ 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(
smithy.api.Deprecated(message = None, since = None),
).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")
}
Expand All @@ -68,15 +68,15 @@ 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(
smithy.api.Deprecated(message = None, since = None),
).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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package smithy4s.example
package object _package {

type MyPackageString = smithy4s.example._package.MyPackageString.Type
type MyPackageStringTrait = smithy4s.example._package.MyPackageStringTrait.Type

}
Original file line number Diff line number Diff line change
@@ -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 {
val id: ShapeId = ShapeId("smithy4s.example.collision", "AdtStruct")

val hints: Hints = Hints.empty


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))
}
Original file line number Diff line number Diff line change
@@ -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().widen,
).lazily
val underlyingSchema: Schema[java.lang.String] = string.withId(id).addHints(hints)
implicit val schema: Schema[ReservedNameUnionTrait] = bijection(underlyingSchema, asBijection)
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
26 changes: 26 additions & 0 deletions modules/bootstrapped/test/src-js/smithy4s/TimestampSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,30 @@ class TimestampSpec() extends munit.FunSuite with munit.ScalaCheckSuite {
expect.same(tsFromEpochMilli, ts)
}
}

property("Truncate to milliseconds precision") {
forAll { (d: Date) =>
val epochMilli = d.valueOf().toLong
val ts = Timestamp.fromDate(d).truncateToMillis

val strippedDate = new Date(0)
strippedDate.setUTCMilliseconds(epochMilli)
val tsFromStrippedDate = Timestamp.fromDate(strippedDate)

expect.same(ts, tsFromStrippedDate)
}
}

property("Truncate to seconds precision") {
forAll { (d: Date) =>
val epochSecond = (d.valueOf() / 1000).toLong
val ts = Timestamp.fromDate(d).truncateToMillis

val strippedDate = new Date(0)
strippedDate.setUTCSeconds(epochSecond)
val tsFromStrippedDate = Timestamp.fromDate(strippedDate)

expect.same(ts, tsFromStrippedDate)
}
}
}
19 changes: 19 additions & 0 deletions modules/bootstrapped/test/src-jvm/smithy4s/TimestampSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,23 @@ class TimestampSpec() extends munit.FunSuite with munit.ScalaCheckSuite {
expect.same(tsFromEpochMilli, tsFromStrippedInstant)
}
}

property("Truncate to milliseconds precision") {
forAll { (i: Instant) =>
val ts = Timestamp.fromInstant(i).truncateToMillis
val strippedInstant = Instant.ofEpochMilli(i.toEpochMilli)
val tsFromStrippedInstant = Timestamp.fromInstant(strippedInstant)

expect.same(ts, tsFromStrippedInstant)
}
}

property("Truncate to seconds precision") {
forAll { (i: Instant) =>
val ts = Timestamp.fromInstant(i).truncateToSeconds
val tsFromStrippedInstant = Timestamp.fromEpochSecond(i.getEpochSecond)

expect.same(ts, tsFromStrippedInstant)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
lazy val root = (project in file("."))
.enablePlugins(Smithy4sCodegenPlugin)
.settings(
crossScalaVersions := Seq("2.13.15", "3.3.3"),
libraryDependencies ++= Seq(
"com.disneystreaming.smithy4s" %% "smithy4s-core" % smithy4sVersion.value
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.8.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sys.props.get("plugin.version") match {
case Some(x) =>
addSbtPlugin("com.disneystreaming.smithy4s" % "smithy4s-sbt-codegen" % x)
case _ =>
sys.error(
"""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace smithy4s.example

structure Simple {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# generate for Scala 2.13
> ++ 2.13
> compile

$ exists target/scala-2.13/src_managed/main/smithy/generated-metadata.smithy
$ exists target/scala-2.13/src_managed/main/smithy4s/smithy4s/example/Simple.scala

> ++ 3
> compile

$ exists target/scala-3.3.3/src_managed/main/smithy/generated-metadata.smithy
$ exists target/scala-3.3.3/src_managed/main/smithy4s/smithy4s/example/Simple.scala

# switch back to Scala 2.13 and compile again; codegen should not happen a second time
> ++ 2.13
> compile

$ newer target/scala-3.3.3/src_managed/main/smithy/generated-metadata.smithy target/scala-2.13/src_managed/main/smithy/generated-metadata.smithy
$ newer target/scala-3.3.3/src_managed/main/smithy4s/smithy4s/example/Simple.scala target/scala-2.13/src_managed/main/smithy4s/smithy4s/example/Simple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
(config / sourceManaged).value / "smithy" / "generated-metadata.smithy"
},
config / smithy4sGeneratedSmithyFiles := {
val cacheFactory = (config / streams).value.cacheStoreFactory
val cacheFactory =
(config / streams).value.cacheStoreFactory.sub(scalaVersion.value)
val cached = Tracked.inputChanged[(String, Boolean), Seq[File]](
cacheFactory.make("smithy4sGeneratedSmithyFilesInput")
) { case (changed, (wildcardArg, shouldGenerateOptics)) =>
Expand Down Expand Up @@ -454,14 +455,15 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
smithyBuild = smithyBuildValue
)

val cacheStoreFactory = s.cacheStoreFactory.sub(scalaVersion.value)
val cached =
CachedTask.inputChanged[CodegenArgs, Seq[File]](
s.cacheStoreFactory.make("input"),
cacheStoreFactory.make("input"),
s.log
) {
Function.untupled {
Tracked.lastOutput[(Boolean, CodegenArgs), Seq[File]](
s.cacheStoreFactory.make("output")
cacheStoreFactory.make("output")
) { case ((inputChanged, args), outputs) =>
if (inputChanged || outputs.isEmpty) {
s.log.debug(s"[smithy4s] Input changed: $inputChanged")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,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)
Expand Down
Loading

0 comments on commit 0a735a0

Please sign in to comment.