Skip to content
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 2 commits into from
Nov 22, 2023

Conversation

Baccata
Copy link
Contributor

@Baccata Baccata commented Nov 17, 2023

This PR aims at moving towards a more principled way of handling required and defaults. The notion of "required" is now more strictly coupled to the presence of the smithy.api#required trait.

  • Update codegen to not use the "required" terminology, as in smithy it is explicitly tied to the presence of the required trait.
  • Makes sure that the smithy.api.Required hint is only generated when the smithy.api#required is applied at the Smithy level
  • Updates getUnlessDefault or foreachUnlessDefault to return value/ apply lambda when the smithy.api#required is applied.
  • Update default Json hint mask to have smithy.api.Required explicitly
  • Adds relevant unit-tests for Document and Json encodings

Closes #1289

PR Checklist (not all items are relevant to all PRs)

  • Added unit-tests (for runtime code)
  • Added bootstrapped code + smoke tests (when the rendering logic is modified)
  • Updated changelog

* Update codegen to not use the "required" terminology, as in smithy
it is explicitly tied to the presence of the `required` trait.
* Makes sure that the `smithy.api.Required` hint is only generated when
the `smithy.api#required` is applied at the Smithy level
* Updates `getUnlessDefault` or `foreachUnlessDefault` to return value/
apply lambda when the `smithy.api#required` is applied.
* Update default Json hint mask to have `smithy.api.Required` explicitly
* Adds relevant unit-tests for Document and Json encodings
@Baccata Baccata requested review from lewisjkl, kubukoz and daddykotex and removed request for lewisjkl and kubukoz November 17, 2023 14:11
one: String = "test"
}

structure DefaultVariants {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, until I saw this structure I was unsure what use case we were not covering properly but now it's clearer

a field not marked with required with a default would use required before, now it use field

@@ -141,13 +141,37 @@ private[internals] case class StreamingField(

private[internals] object Field {

sealed trait Modifier {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great improvement

Copy link
Contributor

@daddykotex daddykotex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work

) {

val document = Document.encode(
DefaultVariants("default", "default", "default", Some("default"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: named parameters would be very helpful

@@ -584,4 +584,36 @@ class SchemaVisitorJCodecTests() extends FunSuite {
}
}

test(
"fields marked with @required and @default should always be encoded"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: good candidate for a property test?

Comment on lines +18 to +21
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),
Copy link
Contributor

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 than optional and it is also used in other places where required was used prior. Is field only used when a default value is present and the field is not required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is field only used when a default value is present and the field is not required

Pretty much.

  • optional forces the type of the getter to be S => Option[A]
  • required lets you do S => A, but automatically enriches the hints with smithy.api.Required
  • field lets you do S => A, and doesn't enrich the hints with anything

Copy link
Contributor

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

@kubukoz
Copy link
Member

kubukoz commented Nov 20, 2023

This isn't going to affect Dynamic, right?

edit: I suppose getUnlessDefault and friends will be affected but that's about it

@Baccata Baccata merged commit 533afe9 into series/0.18 Nov 22, 2023
11 checks passed
@Baccata Baccata deleted the always-serialise-required branch November 22, 2023 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Required defaults aren't written
4 participants