Skip to content

Conversation

@lucix-aws
Copy link
Contributor

Issue #

(#123)

Description of changes

Wire in codegen for Document serde.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@lucix-aws lucix-aws requested a review from a team as a code owner June 30, 2022 15:35
@lucix-aws lucix-aws requested review from aajtodd and ianbotsf June 30, 2022 15:36
Copy link
Contributor

@aajtodd aajtodd left a comment

Choose a reason for hiding this comment

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

I wish we could improve the ShapeValueGenerator some but the ugliness there predates this PR.

Looks good overall, one suggestion/improvement to look into.

// TODO - deal with document members
writer
.write("val serializer = #T()", RuntimeTypes.Serde.SerdeJson.JsonSerializer)
.write("serializer.serializeDocument(input.#L)", memberName)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can/should make this part of the StructuredData serializer/parser implementations. More than likely they can just be part of the payloadSerializer/Deserializer implementation and then document can just be added to L450 and be handled in the same branch as ShapeType.STRUCTURE, ShapeType.UNION. This will remove need to have any hard coding on JSON here.

In fact because StructSerializer/DeserializerGenerator has support for documents you probably don't even need to do anything special other than add ShapeType.DOCUMENT to the aforementioned branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ianbotsf Make sure you take a look at the updates for this.

Copy link
Contributor

@ianbotsf ianbotsf left a comment

Choose a reason for hiding this comment

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

Comment: I see there's still an unaddressed TODO in SerializeStructGeneratorTest.kt around tests for document type. We should add some tests there or (if they truly aren't necessary) just delete the TODO.

) : NodeVisitor<Unit> {

override fun objectNode(node: ObjectNode) {
if (currShape.type === ShapeType.DOCUMENT) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: Why ===? Won't == be sufficient here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You have witnessed firsthand my javascript reflex. I didn't even realize I did this!

Comment on lines 232 to 247
writer
.writeInline("#T(\n", RuntimeTypes.Core.Smithy.Document)
.indent()
.writeInline("listOf(\n")
.indent()

node.elements.forEach {
generator.writeShapeValueInline(writer, currShape, it)
writer.writeInline(",\n")
}

writer
.dedent()
.writeInline(")\n")
.dedent()
.writeInline(")")
Copy link
Contributor

Choose a reason for hiding this comment

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

Style: The manual indenting/dedenting is unnecessary here:

writer.withBlock("#T(", ")", RuntimeTypes.Core.Smithy.Document) {
    withBlock("listOf(", ")") {
        node.elements.forEach {
            generator.writeShapeValueInline(writer, currShape, it)
            writeInline(",\n")
        }
    }
}

Copy link
Contributor Author

@lucix-aws lucix-aws Jul 1, 2022

Choose a reason for hiding this comment

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

So this was deliberate -- I had writeBlockified at first, but it doesn't generate output in the spirit of the method writeShapeValueInline - keyword being "inline", you end up with some extra line breaks. The code is still functional but not necessarily reader-friendly and the output isn't truly inline at that point.

It doesn't super matter to me, especially considering that this ShapeValueGenerator is effectively only generating for test code, but I suppose it could be otherwise in the future.

We could (probably should) also expose withInlineBlock on the writer, because the manual *denting is indeed nasty. I could see that getting use elsewhere in codegen.

At the very least, if we do keep it, a comment might be warranted.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see, I missed that the last write doesn't want a newline. withInlineBlock is a great idea and might lead to a simplification of this new block and probably some existing blocks as well. Up to you if you want to tackle it in this PR. Otherwise, my concern is addressed.

@lucix-aws lucix-aws requested a review from ianbotsf July 1, 2022 14:08
@lucix-aws
Copy link
Contributor Author

Comment: I see there's still an unaddressed TODO in SerializeStructGeneratorTest.kt around tests for document type. We should add some tests there or (if they truly aren't necessary) just delete the TODO.

This is a miss on my part. We should have our own tests here, I did in fact add them for the explicit @httpPayload case. The smithy protocol tests technically cover everything but we shouldn't depend on them IMO.

Comment on lines 236 to 238
if (index < node.elements.size - 1) {
writer.writeInline(",\n")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Style: There's no reason to omit the final comma. You could simplify this to the original forEach loop that always adds the trailer.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jul 6, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@lucix-aws lucix-aws merged commit 7da7822 into main Jul 6, 2022
@lucix-aws lucix-aws deleted the feat-document-codegen branch July 6, 2022 23: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.

3 participants