-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP: validated newtypes squashed Co-Authored-By: Denis Rosca <[email protected]> * Common supertype * Fix mima * Fix formatting * Fix bincompat issue in 2.1x * ber precise about mima baseline * Add dummy change to retrigger build --------- Co-authored-by: Jakub Kozłowski <[email protected]>
- Loading branch information
1 parent
8e1ee57
commit 5cee547
Showing
42 changed files
with
1,152 additions
and
70 deletions.
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
15 changes: 15 additions & 0 deletions
15
modules/bootstrapped/src/generated/smithy4s/example/NonValidatedString.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,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.string | ||
|
||
object NonValidatedString extends Newtype[String] { | ||
val id: ShapeId = ShapeId("smithy4s.example", "NonValidatedString") | ||
val hints: Hints = Hints.empty | ||
val underlyingSchema: Schema[String] = string.withId(id).addHints(hints).validated(smithy.api.Length(min = Some(1L), max = None)).validated(smithy.api.Pattern("[a-zA-Z0-9]+")) | ||
implicit val schema: Schema[NonValidatedString] = bijection(underlyingSchema, asBijection) | ||
} |
22 changes: 22 additions & 0 deletions
22
modules/bootstrapped/src/generated/smithy4s/example/ValidatedFoo.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,22 @@ | ||
package smithy4s.example | ||
|
||
import smithy4s.Hints | ||
import smithy4s.Schema | ||
import smithy4s.ShapeId | ||
import smithy4s.ShapeTag | ||
import smithy4s.schema.Schema.struct | ||
|
||
final case class ValidatedFoo(name: ValidatedString = smithy4s.example.ValidatedString.unsafeApply("abc")) | ||
|
||
object ValidatedFoo extends ShapeTag.Companion[ValidatedFoo] { | ||
val id: ShapeId = ShapeId("smithy4s.example", "ValidatedFoo") | ||
|
||
val hints: Hints = Hints.empty | ||
|
||
// constructor using the original order from the spec | ||
private def make(name: ValidatedString): ValidatedFoo = ValidatedFoo(name) | ||
|
||
implicit val schema: Schema[ValidatedFoo] = struct( | ||
ValidatedString.schema.field[ValidatedFoo]("name", _.name).addHints(smithy.api.Default(smithy4s.Document.fromString("abc"))), | ||
)(make).withId(id).addHints(hints) | ||
} |
18 changes: 18 additions & 0 deletions
18
modules/bootstrapped/src/generated/smithy4s/example/ValidatedString.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,18 @@ | ||
package smithy4s.example | ||
|
||
import smithy4s.Bijection | ||
import smithy4s.Hints | ||
import smithy4s.Schema | ||
import smithy4s.ShapeId | ||
import smithy4s.ValidatedNewtype | ||
import smithy4s.Validator | ||
import smithy4s.schema.Schema.string | ||
|
||
object ValidatedString extends ValidatedNewtype[String] { | ||
val id: ShapeId = ShapeId("smithy4s.example", "ValidatedString") | ||
val hints: Hints = Hints.empty | ||
val underlyingSchema: Schema[String] = string.withId(id).addHints(hints).validated(smithy.api.Length(min = Some(1L), max = None)).validated(smithy.api.Pattern("[a-zA-Z0-9]+")) | ||
val validator: Validator[String, ValidatedString] = Validator.of[String, ValidatedString](Bijection[String, ValidatedString](_.asInstanceOf[ValidatedString], value(_))).validating(smithy.api.Length(min = Some(1L), max = None)).alsoValidating(smithy.api.Pattern("[a-zA-Z0-9]+")) | ||
implicit val schema: Schema[ValidatedString] = validator.toSchema(underlyingSchema) | ||
@inline def apply(a: String): Either[String, ValidatedString] = validator.validate(a) | ||
} |
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
109 changes: 109 additions & 0 deletions
109
modules/bootstrapped/test/src/smithy4s/ValidatedNewtypesSpec.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,109 @@ | ||
/* | ||
* Copyright 2021-2023 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smithy4s | ||
|
||
import smithy4s.schema.Schema.string | ||
import munit.Assertions | ||
|
||
class ValidatedNewtypesSpec() extends munit.FunSuite { | ||
val id1 = "id1" | ||
val id2 = "id2" | ||
|
||
test("Validated newtypes are consistent") { | ||
expect.same(AccountId.unsafeApply(id1).value, id1) | ||
expect.different( | ||
AccountId.unsafeApply(id1).value, | ||
AccountId.unsafeApply(id2).value | ||
) | ||
expect.different( | ||
implicitly[ShapeTag[AccountId]], | ||
implicitly[ShapeTag[DeviceId]] | ||
) | ||
expect.same(AccountId.unapply(AccountId.unsafeApply(id1)), Some(id1)) | ||
} | ||
|
||
test("Newtypes have well defined unapply") { | ||
val aid = AccountId.unsafeApply(id1) | ||
aid match { | ||
case AccountId(id) => expect(id == id1) | ||
} | ||
} | ||
|
||
test("Validated newtypes unsafeApply throws exception") { | ||
val e = Assertions.intercept[IllegalArgumentException] { | ||
AccountId.unsafeApply("!^%&") | ||
} | ||
|
||
expect.same( | ||
e.getMessage(), | ||
"String '!^%&' does not match pattern '[a-zA-Z0-9]+'" | ||
) | ||
} | ||
|
||
type DeviceId = DeviceId.Type | ||
object DeviceId extends ValidatedNewtype[String] { | ||
|
||
val id: ShapeId = ShapeId("foo", "DeviceId") | ||
val hints: Hints = Hints.empty | ||
|
||
val underlyingSchema: Schema[String] = string | ||
.withId(id) | ||
.addHints(hints) | ||
.validated(smithy.api.Length(min = Some(1L), max = None)) | ||
|
||
val validator: Validator[String, DeviceId] = Validator | ||
.of[String, DeviceId]( | ||
Bijection[String, DeviceId](_.asInstanceOf[DeviceId], value(_)) | ||
) | ||
.validating(smithy.api.Length(min = Some(1L), max = None)) | ||
|
||
implicit val schema: Schema[DeviceId] = | ||
validator.toSchema(underlyingSchema) | ||
|
||
@inline def apply(a: String): Either[String, DeviceId] = | ||
validator.validate(a) | ||
|
||
} | ||
|
||
type AccountId = AccountId.Type | ||
|
||
object AccountId extends ValidatedNewtype[String] { | ||
def id: smithy4s.ShapeId = ShapeId("foo", "AccountId") | ||
val hints: Hints = Hints.empty | ||
|
||
val underlyingSchema: Schema[String] = string | ||
.withId(id) | ||
.addHints(hints) | ||
.validated(smithy.api.Length(min = Some(1L), max = None)) | ||
.validated(smithy.api.Pattern("[a-zA-Z0-9]+")) | ||
|
||
val validator: Validator[String, AccountId] = Validator | ||
.of[String, AccountId]( | ||
Bijection[String, AccountId](_.asInstanceOf[AccountId], value(_)) | ||
) | ||
.validating(smithy.api.Length(min = Some(1L), max = None)) | ||
.alsoValidating(smithy.api.Pattern("[a-zA-Z0-9]+")) | ||
|
||
implicit val schema: Schema[AccountId] = | ||
validator.toSchema(underlyingSchema) | ||
|
||
@inline def apply(a: String): Either[String, AccountId] = | ||
validator.validate(a) | ||
|
||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
modules/codegen-plugin/src/sbt-test/codegen-plugin/render-validated-newtypes/build.sbt
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,10 @@ | ||
lazy val root = (project in file(".")) | ||
.enablePlugins(Smithy4sCodegenPlugin) | ||
.settings( | ||
scalaVersion := "2.13.10", | ||
libraryDependencies ++= Seq( | ||
"com.disneystreaming.smithy4s" %% "smithy4s-core" % smithy4sVersion.value, | ||
"com.disneystreaming.smithy4s" %% "smithy4s-dynamic" % smithy4sVersion.value, | ||
"com.disneystreaming.alloy" % "alloy-core" % "0.3.4", | ||
) | ||
) |
1 change: 1 addition & 0 deletions
1
...gen-plugin/src/sbt-test/codegen-plugin/render-validated-newtypes/project/build.properties
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 @@ | ||
sbt.version=1.8.3 |
9 changes: 9 additions & 0 deletions
9
.../codegen-plugin/src/sbt-test/codegen-plugin/render-validated-newtypes/project/plugins.sbt
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,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 | ||
) | ||
} |
38 changes: 38 additions & 0 deletions
38
...en-plugin/src/sbt-test/codegen-plugin/render-validated-newtypes/src/main/scala/Main.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,38 @@ | ||
/* | ||
* Copyright 2021-2024 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package newtypes.validated | ||
|
||
import newtypes.validated._ | ||
|
||
object Main extends App { | ||
try { | ||
val cityOrError: Either[String, ValidatedCity] = ValidatedCity("test-city") | ||
val nameOrError: Either[String, ValidatedName] = ValidatedName("test-name") | ||
val country: String = "test-country" | ||
|
||
println( | ||
(nameOrError, cityOrError) match { | ||
case (Right(name), Right(city)) => s"Success: ${Person(name, Some(city), Some(country))}" | ||
case _ => s"Error" | ||
} | ||
) | ||
} catch { | ||
case _: java.lang.ExceptionInInitializerError => | ||
println("failed") | ||
sys.exit(1) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...t-test/codegen-plugin/render-validated-newtypes/src/main/smithy/validated-newtypes.smithy
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,30 @@ | ||
$version: "2.0" | ||
|
||
metadata smithy4sRenderValidatedNewtypes = true | ||
|
||
namespace newtypes.validated | ||
|
||
use smithy4s.meta#unwrap | ||
use alloy#simpleRestJson | ||
|
||
@length(min: 1, max: 10) | ||
string ValidatedCity | ||
|
||
@length(min: 1, max: 10) | ||
string ValidatedName | ||
|
||
@unwrap | ||
@length(min: 1, max: 10) | ||
string ValidatedCountry | ||
|
||
structure Person { | ||
@httpLabel | ||
@required | ||
name: ValidatedName | ||
|
||
@httpQuery("town") | ||
town: ValidatedCity | ||
|
||
@httpQuery("country") | ||
country: ValidatedCountry | ||
} |
2 changes: 2 additions & 0 deletions
2
modules/codegen-plugin/src/sbt-test/codegen-plugin/render-validated-newtypes/test
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,2 @@ | ||
# check if smithy4sCodegen works and everything compiles | ||
> compile |
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
Oops, something went wrong.