Skip to content

Commit

Permalink
update xml name generation logic for payload as document shape
Browse files Browse the repository at this point in the history
  • Loading branch information
skotambkar committed Dec 11, 2020
1 parent 4e5e79c commit 66020fa
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> xmlNamespaceTrait = context.getService().getTrait(XmlNamespaceTrait.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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()
);
}
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion service/s3/serializers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 66020fa

Please sign in to comment.