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

Retain original field ordering in schemas #1427

Merged
merged 7 commits into from Mar 4, 2024

Conversation

Baccata
Copy link
Contributor

@Baccata Baccata commented Mar 1, 2024

This changes the rendering logic to retain the original ordering of fields in Structure schemas, thus reflecting the spec more accurately.

The original ordering of fields is important in some protocols, when positional information has semantic importance.

NB for reviewers : the commits can be reviewed individually (in particular the first one)

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

val args = fields.map(f => Line(f.name)).intercalate(Line.comma)
lines(
line"// constructor using the original order from the spec",
line"private def make($params): ${product.nameRef} = ${product.nameRef}($args)"
Copy link
Member

Choose a reason for hiding this comment

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

question: I think it'd be valuable to have this in the public API, e.g. if someone really really wants to avoid getting bit by a default parameter (I've seen it happen). WDYT?

Copy link
Contributor Author

@Baccata Baccata Mar 4, 2024

Choose a reason for hiding this comment

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

If people care about it, we have rendering options to prevent default parameters from being generated : https://disneystreaming.github.io/smithy4s/docs/codegen/customisation/default-rendering

I think the "correct" course of action would be to generate the case-class without default parameters, retaining the original order, and to create a custom apply method that would have the default parameters. The problem is that it'd also impact extractors, so I don't want to pull that trigger just yet. We could potentially roll it in 0.19, as people should be more careful about reading release notes.

I do not however want to expose this make method. I've toyed with the idea of actually turning it into a IndexedSeq[Any] => A, as it's general and could be captured in a potential GeneratedStructureCompanion interface, which could potentially help the Spark usecase. I don't want to make the generated make public because we may change it later on.

hints: List[Hint] = Nil
): Field =
Field(name, name, tpe, modifier, hints)
Field(name, name, tpe, modifier, originalIndex, hints)
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 params

@@ -727,7 +737,7 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self =>
if (recursive) line"$recursive_($struct_" else line"$struct_"
line"${schemaImplicit}val schema: $Schema_[${product.nameRef}] = $definition"
.args(renderedFields)
.block(line"${product.nameRef}.apply")
.block(line"make")
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: I'd try to avoid the block here and pass this in the same line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Baccata Baccata merged commit 173b6bb into series/0.18 Mar 4, 2024
11 checks passed
@Baccata Baccata deleted the retain-original-field-ordering branch March 4, 2024 11:00
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

2 participants