-
Notifications
You must be signed in to change notification settings - Fork 68
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
Update meaning of required
in both codegen and runtime.
#1301
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
25 changes: 25 additions & 0 deletions
25
modules/bootstrapped/src/generated/smithy4s/example/DefaultVariants.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,25 @@ | ||
package smithy4s.example | ||
|
||
import smithy4s.Hints | ||
import smithy4s.Schema | ||
import smithy4s.ShapeId | ||
import smithy4s.ShapeTag | ||
import smithy4s.schema.Schema.string | ||
import smithy4s.schema.Schema.struct | ||
|
||
final case class DefaultVariants(req: String, reqDef: String = "default", optDef: String = "default", opt: Option[String] = None) | ||
|
||
object DefaultVariants extends ShapeTag.Companion[DefaultVariants] { | ||
val id: ShapeId = ShapeId("smithy4s.example", "DefaultVariants") | ||
|
||
val hints: Hints = Hints.empty | ||
|
||
implicit val schema: Schema[DefaultVariants] = struct( | ||
string.required[DefaultVariants]("req", _.req), | ||
string.required[DefaultVariants]("reqDef", _.reqDef).addHints(smithy.api.Default(smithy4s.Document.fromString("default"))), | ||
string.field[DefaultVariants]("optDef", _.optDef).addHints(smithy.api.Default(smithy4s.Document.fromString("default"))), | ||
string.optional[DefaultVariants]("opt", _.opt), | ||
){ | ||
DefaultVariants.apply | ||
}.withId(id).addHints(hints) | ||
} |
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
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
44 changes: 44 additions & 0 deletions
44
modules/bootstrapped/test/src/smithy4s/DefaultSmokeSpec.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,44 @@ | ||
/* | ||
* 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 munit._ | ||
import smithy4s.example._ | ||
|
||
class DefaultSmokeSpec() extends FunSuite { | ||
|
||
test( | ||
"Fields with defaults do not get implicitly `smithy.api.Required` hints" | ||
) { | ||
|
||
val document = Document.encode( | ||
DefaultVariants("default", "default", "default", Some("default")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: named parameters would be very helpful |
||
) | ||
|
||
import Document._ | ||
// optDef field gets eluded because it matches the default value | ||
val expected = obj( | ||
"req" -> fromString("default"), | ||
"reqDef" -> fromString("default"), | ||
"opt" -> fromString("default") | ||
) | ||
|
||
assertEquals(document, expected) | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,7 +129,7 @@ private[internals] case class Field( | |
name: String, | ||
realName: String, | ||
tpe: Type, | ||
required: Boolean, | ||
modifier: Field.Modifier, | ||
hints: List[Hint] | ||
) | ||
|
||
|
@@ -141,13 +141,37 @@ private[internals] case class StreamingField( | |
|
||
private[internals] object Field { | ||
|
||
sealed trait Modifier { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great improvement |
||
def default: Option[Node] = this match { | ||
case Modifier.RequiredDefaultMod(node, _) => Some(node) | ||
case Modifier.DefaultMod(node, _) => Some(node) | ||
case Modifier.RequiredMod => None | ||
case Modifier.NoModifier => None | ||
} | ||
|
||
def none: Boolean = this match { | ||
case Modifier.RequiredDefaultMod(_, _) => false | ||
case Modifier.DefaultMod(_, _) => false | ||
case Modifier.RequiredMod => false | ||
case Modifier.NoModifier => true | ||
} | ||
} | ||
object Modifier { | ||
case object NoModifier extends Modifier | ||
case object RequiredMod extends Modifier | ||
case class RequiredDefaultMod(node: Node, typedNode: Option[Fix[TypedNode]]) | ||
extends Modifier | ||
case class DefaultMod(node: Node, typedNode: Option[Fix[TypedNode]]) | ||
extends Modifier | ||
} | ||
|
||
def apply( | ||
name: String, | ||
tpe: Type, | ||
required: Boolean = true, | ||
modifier: Modifier, | ||
hints: List[Hint] = Nil | ||
): Field = | ||
Field(name, name, tpe, required, hints) | ||
Field(name, name, tpe, modifier, hints) | ||
|
||
} | ||
|
||
|
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.
I'm not sure I fully understand why
field
is used here rather thanoptional
and it is also used in other places whererequired
was used prior. Isfield
only used when a default value is present and the field is not required?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.
Pretty much.
optional
forces the type of the getter to beS => Option[A]
required
lets you doS => A
, but automatically enriches the hints withsmithy.api.Required
field
lets you doS => A
, and doesn't enrich the hints with anythingThere 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.
Got it yeah that makes sense