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
Changes from 1 commit
7137905
e8bd027
831c750
f0b79af
5bc7c6d
ee99933
ad23001
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -698,9 +698,19 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => | |
renderProtocol(product.nameRef, hints), | ||
newline, | ||
renderLenses(product, hints), | ||
locally { | ||
val params = | ||
fields.sortBy(_.originalIndex).map(fieldToRenderLine(_, noDefault = true)).intercalate(Line.comma) | ||
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)" | ||
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. 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? 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. 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 I do not however want to expose this |
||
).when(fields.nonEmpty) | ||
}, | ||
newline, | ||
if (fields.nonEmpty) { | ||
val renderedFields = | ||
fields.map { case Field(fieldName, realName, tpe, modifier, hints) => | ||
fields.sortBy(_.originalIndex).map { case Field(fieldName, realName, tpe, modifier, _, hints) => | ||
val fieldBuilder = modifier.typeMod match { | ||
case Field.TypeModification.None if modifier.required => "required" | ||
case Field.TypeModification.None => "field" | ||
|
@@ -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") | ||
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. nitpick: I'd try to avoid the block here and pass this in the same line 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. |
||
.appendToLast(".withId(id).addHints(hints)") | ||
.appendToLast(if (recursive) ")" else "") | ||
} else { | ||
|
@@ -737,7 +747,7 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self => | |
line"${schemaImplicit}val schema: $Schema_[${product.nameRef}] = $definition" | ||
.args(renderedFields) | ||
.block( | ||
line"arr => new ${product.nameRef}".args( | ||
line"arr => make".args( | ||
fields.zipWithIndex.map { case (field, idx) => | ||
line"arr($idx).asInstanceOf[${Line.fieldType(field)}]" | ||
} | ||
|
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: named params