From 66020fa31ad17ad0ded0d17dbd4e6cb3bc0d09d4 Mon Sep 17 00:00:00 2001 From: skotambkar Date: Thu, 10 Dec 2020 23:58:09 -0800 Subject: [PATCH] update xml name generation logic for payload as document shape --- .../go/codegen/RestXmlProtocolGenerator.java | 3 +- .../aws/go/codegen/XmlProtocolUtils.java | 73 ++++++++++++++++--- service/s3/serializers.go | 2 +- 3 files changed, 66 insertions(+), 12 deletions(-) diff --git a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/RestXmlProtocolGenerator.java b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/RestXmlProtocolGenerator.java index c798a67378d..684a8bc9282 100644 --- a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/RestXmlProtocolGenerator.java +++ b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/RestXmlProtocolGenerator.java @@ -6,6 +6,7 @@ import static software.amazon.smithy.aws.go.codegen.XmlProtocolUtils.handleDecodeError; import static software.amazon.smithy.aws.go.codegen.XmlProtocolUtils.writeXmlErrorMessageCodeDeserializer; import static software.amazon.smithy.aws.go.codegen.XmlProtocolUtils.generateXMLStartElement; +import static software.amazon.smithy.aws.go.codegen.XmlProtocolUtils.generatePayloadAsDocumentXMLStartElement; import java.util.Optional; import java.util.Set; @@ -132,7 +133,7 @@ protected void writeMiddlewarePayloadAsDocumentSerializerDelegator( writer.addUseImports(SmithyGoDependency.BYTES); writer.write("xmlEncoder := smithyxml.NewEncoder(bytes.NewBuffer(nil))"); - generateXMLStartElement(context, payloadShape, "payloadRoot", operand); + generatePayloadAsDocumentXMLStartElement(context, memberShape, "payloadRoot", operand); // check if service shape is bound by xmlNameSpace Trait Optional xmlNamespaceTrait = context.getService().getTrait(XmlNamespaceTrait.class); diff --git a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/XmlProtocolUtils.java b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/XmlProtocolUtils.java index cabdfa45a81..4be0852ce5e 100644 --- a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/XmlProtocolUtils.java +++ b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/XmlProtocolUtils.java @@ -42,13 +42,71 @@ public static void generateXMLStartElement( ) { GoWriter writer = context.getWriter(); String attrName = dst + "Attr"; - writer.write("$L := []smithyxml.Attr{}", attrName); + generateXmlNamespaceAndAttributes(context, shape, attrName, inputSrc); + + writer.openBlock("$L := smithyxml.StartElement{ ", "}", dst, () -> { + writer.openBlock("Name:smithyxml.Name{", "},", () -> { + writer.write("Local: $S,", getSerializedXMLShapeName(context, shape)); + }); + writer.write("Attr : $L,", attrName); + }); + } + + /** + * Generates XML Start element for a document shape marked as a payload. + * + * @param context is the generation context. + * @param memberShape is the payload as document member shape + * @param dst is the operand name which holds the generated start element. + * @param inputSrc is the input variable for the shape with values to be serialized. + */ + public static void generatePayloadAsDocumentXMLStartElement( + ProtocolGenerator.GenerationContext context, MemberShape memberShape, String dst, String inputSrc + ) { + GoWriter writer = context.getWriter(); + String attrName = dst + "Attr"; + Shape targetShape = context.getModel().expectShape(memberShape.getTarget()); + + generateXmlNamespaceAndAttributes(context, targetShape, attrName, inputSrc); + + writer.openBlock("$L := smithyxml.StartElement{ ", "}", dst, () -> { + writer.openBlock("Name:smithyxml.Name{", "},", () -> { + String name = memberShape.getMemberName(); + if (targetShape.isStructureShape()) { + if (memberShape.hasTrait(XmlNameTrait.class)) { + name = getSerializedXMLMemberName(memberShape); + } else { + name = getSerializedXMLShapeName(context, targetShape); + } + } + + writer.write("Local: $S,", name); + + }); + writer.write("Attr : $L,", attrName); + }); + } + + + /** + * Generates XML Attributes as per xmlNamespace and xmlAttribute traits. + * + * @param context is the generation context. + * @param shape is the shape that is decorated with XmlNamespace, XmlAttribute trait. + * @param dst is the operand name which holds the generated xml Attribute value. + * @param inputSrc is the input variable for the shape with values to be put as xml attributes. + */ + private static void generateXmlNamespaceAndAttributes( + ProtocolGenerator.GenerationContext context, Shape shape, String dst, String inputSrc + ) { + GoWriter writer = context.getWriter(); + writer.write("$L := []smithyxml.Attr{}", dst); Optional xmlNamespaceTrait = shape.getTrait(XmlNamespaceTrait.class); if (xmlNamespaceTrait.isPresent()) { XmlNamespaceTrait namespace = xmlNamespaceTrait.get(); writer.write("$L = append($L, smithyxml.NewNamespaceAttribute($S, $S))", - attrName, attrName, + dst, dst, namespace.getPrefix().isPresent() ? namespace.getPrefix().get() : "", namespace.getUri() ); } @@ -62,19 +120,14 @@ public static void generateXMLStartElement( String dest = "av"; formatXmlAttributeValueAsString(context, memberShape, operand, dest); writer.write("$L = append($L, smithyxml.NewAttribute($S, $L))", - attrName, attrName, getSerializedXMLMemberName(memberShape), dest); + dst, dst, getSerializedXMLMemberName(memberShape), dest); }); } }); - - writer.openBlock("$L := smithyxml.StartElement{ ", "}", dst, () -> { - writer.openBlock("Name:smithyxml.Name{", "},", () -> { - writer.write("Local: $S,", getSerializedXMLShapeName(context, shape)); - }); - writer.write("Attr : $L,", attrName); - }); } + + // generates code to format xml attributes. If a shape type is timestamp, number, or boolean // it will be formatted into a string. private static void formatXmlAttributeValueAsString( diff --git a/service/s3/serializers.go b/service/s3/serializers.go index 0e07b067480..46e1a82936e 100644 --- a/service/s3/serializers.go +++ b/service/s3/serializers.go @@ -150,7 +150,7 @@ func (m *awsRestxml_serializeOpCompleteMultipartUpload) HandleSerialize(ctx cont payloadRootAttr := []smithyxml.Attr{} payloadRoot := smithyxml.StartElement{ Name: smithyxml.Name{ - Local: "CompletedMultipartUpload", + Local: "CompleteMultipartUpload", }, Attr: payloadRootAttr, }