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
Validated newtypes #1454
Open
denisrosca
wants to merge
27
commits into
disneystreaming:series/0.19
Choose a base branch
from
denisrosca:validated-newtypes
base: series/0.19
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Validated newtypes #1454
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
dffd10d
Add NewtypeValidated
denisrosca c477541
Fix formatting
denisrosca 468d54c
Add simple renderer tests
denisrosca c600b1f
Fix validated newtype usage
denisrosca ba9b81b
Render validated newtypes only if smithy4sRenderValidatedNewtypes=true
denisrosca 301ed39
Fix formatting again
denisrosca cf9ec8e
Don't validate types from smithy.api namespace
denisrosca de82030
Minor fixup of sbt plugin test
denisrosca cd48f63
Add validated newtypes usage to generated metadata
denisrosca 89f3911
Merge remote-tracking branch 'upstream/series/0.19' into validated-ne…
denisrosca 043c859
Fix newline
denisrosca be7b3e1
Fix missing license header
denisrosca 7ec86d6
Add ValidatedNewtypesTransformer and switch to meta trait
denisrosca e6db5e1
Add default value to bootstrapped sample
denisrosca 4f7b313
Fix renderer spec
denisrosca 54762fe
Add transformer spec
denisrosca c00ed39
Fix formatting
denisrosca 5ff681c
Add Validator interface and refactor constraint validation
denisrosca 5f27413
Rename trait
denisrosca 382b96a
Fix scala3 encoding
denisrosca a09a568
Fix formatting, again
denisrosca 9fe555a
Add doc entry
denisrosca e378c0a
Make mdoc compile only
denisrosca 1e2b2ea
Format mandatory transformers
denisrosca c98a563
Make Validator trait sealed
denisrosca e7ca985
Add changelog entry
denisrosca d38247d
Change unsafeApply to throw exceptions
denisrosca File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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) | ||
} |
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
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 | ||
|
||
implicit val schema: Schema[ValidatedFoo] = struct( | ||
ValidatedString.schema.field[ValidatedFoo]("name", _.name).addHints(smithy.api.Default(smithy4s.Document.fromString("abc"))), | ||
){ | ||
ValidatedFoo.apply | ||
}.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.Hints | ||
import smithy4s.NewtypeValidated | ||
import smithy4s.Schema | ||
import smithy4s.ShapeId | ||
import smithy4s.Validator | ||
import smithy4s.schema.Schema.bijection | ||
import smithy4s.schema.Schema.string | ||
|
||
object ValidatedString extends NewtypeValidated[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]+")) | ||
implicit val schema: Schema[ValidatedString] = bijection(underlyingSchema, asBijectionUnsafe) | ||
val validator: Validator[String, ValidatedString] = Validator.of[String, ValidatedString].validating(smithy.api.Length(min = Some(1L), max = None)).validating(smithy.api.Pattern("[a-zA-Z0-9]+")).build | ||
@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
96 changes: 96 additions & 0 deletions
96
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,96 @@ | ||
/* | ||
* 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.bijection | ||
import smithy4s.schema.Schema.string | ||
|
||
class ValidatedNewtypesSpec() extends munit.FunSuite { | ||
val id1 = "id-1" | ||
val id2 = "id-2" | ||
|
||
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) | ||
} | ||
} | ||
|
||
type DeviceId = DeviceId.Type | ||
object DeviceId extends NewtypeValidated[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)) | ||
|
||
implicit val schema: Schema[DeviceId] = | ||
bijection(underlyingSchema, asBijectionUnsafe) | ||
|
||
val validator: Validator[String, DeviceId] = Validator | ||
.of[String, DeviceId] | ||
.validating(smithy.api.Length(min = Some(1L), max = None)) | ||
.build | ||
|
||
@inline def apply(a: String): Either[String, DeviceId] = | ||
validator.validate(a) | ||
|
||
} | ||
|
||
type AccountId = AccountId.Type | ||
|
||
object AccountId extends NewtypeValidated[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]+")) | ||
|
||
implicit val schema: Schema[AccountId] = | ||
bijection(underlyingSchema, asBijectionUnsafe) | ||
|
||
val validator: Validator[String, AccountId] = Validator | ||
.of[String, AccountId] | ||
.validating(smithy.api.Length(min = Some(1L), max = None)) | ||
.validating(smithy.api.Pattern("[a-zA-Z0-9]+")) | ||
.build | ||
|
||
@inline def apply(a: String): Either[String, AccountId] = | ||
validator.validate(a) | ||
|
||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
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,11 @@ | ||
lazy val root = (project in file(".")) | ||
.enablePlugins(Smithy4sCodegenPlugin) | ||
.settings( | ||
scalaVersion := "2.13.10", | ||
Compile / smithy4sRenderValidatedNewtypes := true, | ||
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) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...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,26 @@ | ||
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
1 change: 1 addition & 0 deletions
1
...es/codegen/resources/META-INF/services/software.amazon.smithy.build.ProjectionTransformer
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
smithy4s.codegen.transformers.AwsStandardTypesTransformer | ||
smithy4s.codegen.transformers.AwsConstraintsRemover | ||
smithy4s.codegen.transformers.OpenEnumTransformer | ||
smithy4s.codegen.transformers.ValidatedNewtypesTransformer |
Oops, something went wrong.
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.
suggestion: I'd extract some of this to avoid the current nesting level, which is becoming a bit unwieldy