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

add default support in several locations #358

Merged
merged 11 commits into from
Aug 18, 2022
Merged

add default support in several locations #358

merged 11 commits into from
Aug 18, 2022

Conversation

lewisjkl
Copy link
Contributor

@lewisjkl lewisjkl commented Aug 9, 2022

Adding support for default values in the various codecs (document, metadata, json).

Metadata and json are still in progress as of the time of writing this description. Tests are in place, but not yet all passing.

Notice this PR is created against a snapshot version of smithy 2 that I published locally. This is because 1.21.0-rc1 doesn't handle default traits properly/completely.

@@ -390,6 +392,7 @@ lazy val protocol = projectMatrix
.filterNot { case (file, path) =>
path.equalsIgnoreCase("META-INF/smithy/manifest")
},
resolvers += Resolver.mavenLocal,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove when changing to use official smithy 2 release

val streamedOutput : smithy4s.StreamingSchema[StreamedBlob] = smithy4s.StreamingSchema("GetStreamedObjectOutput", StreamedBlob.schema)
val streamedOutput : smithy4s.StreamingSchema[StreamedBlob] = smithy4s.StreamingSchema("GetStreamedObjectOutput", StreamedBlob.schema.addHints(smithy.api.Default(smithy4s.Document.fromString(""))))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Afaik AWS automatically adds this default since the blob shape used to be boxed and now isn't, this would go away if we updated streaming.smithy to be $version 2.0

Copy link
Contributor

Choose a reason for hiding this comment

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

interesting

Comment on lines 1224 to 1227
Option(value).orElse {
Option(defaults.get(f.label))
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if orElse is as efficient here as other methods

Copy link
Contributor

Choose a reason for hiding this comment

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

it's not, because of the cost of the Option allocation. Also you're only leveraging defaults values on optional fields there.

It should be :

fieldsWithDefaults.foreach { case (f, default) => 
 ....
  if (f.isRequired) {
      if(default == null) {
      ... 
      }  else {
        default 
      }
  } 
} 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason I put it in the optional side is that smithy doesn't allow required fields to have default values (required and default traits are mutually exclusive). But I do get your broader point and will make the update

Copy link
Contributor

Choose a reason for hiding this comment

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

@lewisjkl actually that's an interesting notion : in the smithy4s level, I think the presence of @default should translate to a required field, and in the case class, the field should not be wrapped in Option. Essentially @default means that an instance of the case class always has a value for the corresponding field, which means in the Schema, the field should be considered 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.

Ah, I see what you're saying. I will make that change 👍


import smithy4s.schema.Schema._

case class DefaultTest(one: Int, two: String, three: List[String])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Guessing we are going to want this to be:

Suggested change
case class DefaultTest(one: Int, two: String, three: List[String])
case class DefaultTest(one: Int = 1, two: String = "test", three: List[String] = List.empty)

If so, I'll make the changes to facilitate that

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah. But also we need to make sure that the ordering of the fields goes as such :

  1. fully required fields first
  2. required fields with default second
  3. optional fields last.

The fields are already getting sorted to ensure that optional fields come AFTER required ones. Just change the ordering there .

Baccata and others added 4 commits August 17, 2022 10:35
* Add the logic to render default parameters

* Handle unwrapped parameters, add comments

* Drop Hint.* import, makes code clearer

* Protect ref name

Co-authored-by: David Francoeur <dfrancoeur04@gmail.com>
@Baccata Baccata marked this pull request as ready for review August 18, 2022 08:16
@Baccata Baccata merged commit 5de6d23 into 0.0.15 Aug 18, 2022
@Baccata Baccata deleted the smithy2-defaults branch August 18, 2022 08:30
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.

None yet

3 participants