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

fix big struct schema rendering #1433

Merged
merged 3 commits into from Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# 0.18.12

* fix issue where schemas for fields of generated big structs (over 22 fields in size) would not be ordered correctly

# 0.18.11

* smithy4s Structure schemas are now retaining the original order of fields, as per the specification.
Expand Down
Expand Up @@ -5,17 +5,18 @@ import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.int
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct

final case class BigStruct(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Int)
final case class BigStruct(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a24: Int, a23: Option[String] = None)

object BigStruct extends ShapeTag.Companion[BigStruct] {
val id: ShapeId = ShapeId("smithy4s.example", "BigStruct")

val hints: Hints = Hints.empty

// constructor using the original order from the spec
private def make(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Int): BigStruct = BigStruct(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23)
private def make(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Option[String], a24: Int): BigStruct = BigStruct(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a24, a23)

implicit val schema: Schema[BigStruct] = struct.genericArity(
int.required[BigStruct]("a1", _.a1),
Expand All @@ -40,7 +41,8 @@ object BigStruct extends ShapeTag.Companion[BigStruct] {
int.required[BigStruct]("a20", _.a20),
int.required[BigStruct]("a21", _.a21),
int.required[BigStruct]("a22", _.a22),
int.required[BigStruct]("a23", _.a23),
string.optional[BigStruct]("a23", _.a23),
int.required[BigStruct]("a24", _.a24),
Copy link
Member

Choose a reason for hiding this comment

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

thought: favorite movie studio

){
arr => make(
arr(0).asInstanceOf[Int],
Expand All @@ -65,7 +67,8 @@ object BigStruct extends ShapeTag.Companion[BigStruct] {
arr(19).asInstanceOf[Int],
arr(20).asInstanceOf[Int],
arr(21).asInstanceOf[Int],
arr(22).asInstanceOf[Int],
arr(22).asInstanceOf[Option[String]],
arr(23).asInstanceOf[Int],
)
}.withId(id).addHints(hints)
}
Expand Up @@ -25,7 +25,7 @@ class BigStructSmokeSpec() extends FunSuite {

test("Big structures do have a schema") {
val expected =
"structure BigStruct(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Int)"
"structure BigStruct(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Option[String], a24: Int)"
val schemaRepr =
smithy4s.example.BigStruct.schema.compile(SchemaDescriptionDetailed)
expect.same(schemaRepr, expected)
Expand Down
Expand Up @@ -747,7 +747,7 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self =>
.args(renderedFields)
.block(
line"arr => make".args(
fields.zipWithIndex.map { case (field, idx) =>
fields.sortBy(_.originalIndex).zipWithIndex.map { case (field, idx) =>
line"arr($idx).asInstanceOf[${Line.fieldType(field)}]"
}
)
Expand Down
3 changes: 2 additions & 1 deletion sampleSpecs/misc.smithy
Expand Up @@ -53,8 +53,9 @@ structure BigStruct {
a21: Integer
@required
a22: Integer
a23: String
@required
a23: Integer
a24: Integer
}

@enum([
Expand Down