Skip to content

Commit

Permalink
Updated GoValueAccessUtils usage
Browse files Browse the repository at this point in the history
  • Loading branch information
skmcgrail committed Nov 17, 2020
1 parent 59a6550 commit b4af135
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ protected void serializeStructure(GenerationContext context, StructureShape shap
Shape target = context.getModel().expectShape(member.getTarget());
String serializedMemberName = getSerializedMemberName(member);

GoValueAccessUtils.writeIfNonZeroValueMember(context, writer, member, "v", (operand) -> {
writer.write("ok := object.Key($S)", serializedMemberName);
target.accept(getMemberSerVisitor(member, operand, "ok"));
});
GoValueAccessUtils.writeIfNonZeroValueMember(context.getModel(), context.getSymbolProvider(), writer,
member, "v", (operand) -> {
writer.write("ok := object.Key($S)", serializedMemberName);
target.accept(getMemberSerVisitor(member, operand, "ok"));
});
writer.write("");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,16 @@ protected void serializeStructure(GenerationContext context, StructureShape shap
}
Shape target = context.getModel().expectShape(member.getTarget());

GoValueAccessUtils.writeIfNonZeroValueMember(context, writer, member, "v", (operand) -> {
String locationName = getSerializedLocationName(member, member.getMemberName());
if (isFlattened(context, member)) {
writer.write("objectKey := object.FlatKey($S)", locationName);
} else {
writer.write("objectKey := object.Key($S)", locationName);
}
target.accept(getMemberSerVisitor(member, operand, "objectKey"));
});
GoValueAccessUtils.writeIfNonZeroValueMember(context.getModel(), context.getSymbolProvider(), writer,
member, "v", (operand) -> {
String locationName = getSerializedLocationName(member, member.getMemberName());
if (isFlattened(context, member)) {
writer.write("objectKey := object.FlatKey($S)", locationName);
} else {
writer.write("objectKey := object.Key($S)", locationName);
}
target.accept(getMemberSerVisitor(member, operand, "objectKey"));
});
writer.write("");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ public static void generateXMLStartElement(
// Traverse member shapes to get attributes
shape.members().stream().forEach(memberShape -> {
if (memberShape.hasTrait(XmlAttributeTrait.class)) {
GoValueAccessUtils.writeIfNonZeroValueMember(context, writer, memberShape, inputSrc, (operand) -> {
// xml attributes should always be string
String dest = "av";
formatXmlAttributeValueAsString(context, memberShape, operand, dest);
writer.write("$L = append($L, smithyxml.NewAttribute($S, $L))",
attrName, attrName, getSerializedXMLMemberName(memberShape), dest);
});
GoValueAccessUtils.writeIfNonZeroValueMember(context.getModel(), context.getSymbolProvider(),
writer, memberShape, inputSrc, (operand) -> {
// xml attributes should always be string
String dest = "av";
formatXmlAttributeValueAsString(context, memberShape, operand, dest);
writer.write("$L = append($L, smithyxml.NewAttribute($S, $L))",
attrName, attrName, getSerializedXMLMemberName(memberShape), dest);
});
}
});

Expand Down Expand Up @@ -225,7 +226,9 @@ public static void initializeXmlDecoder(GoWriter writer, String bodyLocation, St
* @param returnExtras the extra variables to be returned with the wrapped error check statement
* @param returnOnEOF the variable to return in case an EOF error occurs while initializing xml decoder
*/
public static void initializeXmlDecoder(GoWriter writer, String bodyLocation, String returnExtras, String returnOnEOF) {
public static void initializeXmlDecoder(
GoWriter writer, String bodyLocation, String returnExtras, String returnOnEOF
) {
// Use a ring buffer and tee reader to help in pinpointing any deserialization errors.
writer.addUseImports(SmithyGoDependency.SMITHY_IO);
writer.write("var buff [1024]byte");
Expand Down Expand Up @@ -293,12 +296,12 @@ public static void writeXmlErrorMessageCodeDeserializer(ProtocolGenerator.Genera
AwsCustomGoDependency.S3_SHARED_CUSTOMIZATION
).build();

if (isS3Service(service)){
if (isS3Service(service)) {
// s3 service
writer.openBlock("errorComponents, err := $T(errorBody, $T{",
"})", getErrorComponentFunction, errorOptions, () -> {
writer.write("UseStatusCode : true, StatusCode : response.StatusCode,");
});
writer.write("UseStatusCode : true, StatusCode : response.StatusCode,");
});
} else {
// s3 control
writer.openBlock("errorComponents, err := $T(errorBody, $T{",
Expand All @@ -322,7 +325,7 @@ public static void writeXmlErrorMessageCodeDeserializer(ProtocolGenerator.Genera

writer.addUseImports(AwsGoDependency.AWS_MIDDLEWARE);
writer.openBlock("if reqID := errorComponents.RequestID; len(reqID)!=0 {", "}", () -> {
writer.write("awsmiddleware.SetRequestIDMetadata(metadata, reqID)");
writer.write("awsmiddleware.SetRequestIDMetadata(metadata, reqID)");
});
writer.insertTrailingNewline();

Expand All @@ -336,12 +339,12 @@ public static void writeXmlErrorMessageCodeDeserializer(ProtocolGenerator.Genera

// returns true if service is either s3 or s3 control and needs s3 customization
private static boolean requiresS3Customization(ServiceShape service) {
String serviceId= service.expectTrait(ServiceTrait.class).getSdkId();
String serviceId = service.expectTrait(ServiceTrait.class).getSdkId();
return serviceId.equalsIgnoreCase("S3") || serviceId.equalsIgnoreCase("S3 Control");
}

private static boolean isS3Service(ServiceShape service) {
String serviceId= service.expectTrait(ServiceTrait.class).getSdkId();
String serviceId = service.expectTrait(ServiceTrait.class).getSdkId();
return serviceId.equalsIgnoreCase("S3");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected void serializeCollection(GenerationContext context, CollectionShape sh

writer.openBlock("for i := range v {", "}", () -> {
// Serialize zero members as empty values.
GoValueAccessUtils.writeIfZeroValue(context, writer, member, "v[i]", () -> {
GoValueAccessUtils.writeIfZeroValue(context.getModel(), writer, member, "v[i]", () -> {
writer.write("am := array.Member()");
writer.write("am.Close()");
writer.write("continue");
Expand Down Expand Up @@ -105,7 +105,7 @@ protected void serializeMap(GenerationContext context, MapShape shape) {
writer.insertTrailingNewline();

// Serialize zero values as empty values.
GoValueAccessUtils.writeIfZeroValue(context, writer, shape.getValue(), "v[i]", () -> {
GoValueAccessUtils.writeIfZeroValue(context.getModel(), writer, shape.getValue(), "v[i]", () -> {
writer.write("entry.Close()");
writer.write("continue");
});
Expand Down Expand Up @@ -149,17 +149,18 @@ protected void serializeStructure(GenerationContext context, StructureShape shap

writer.addUseImports(SmithyGoDependency.SMITHY_XML);

GoValueAccessUtils.writeIfNonZeroValueMember(context, writer, member, "v", (operand) -> {
XmlProtocolUtils.generateXMLStartElement(context, member, "root", "v");

// check if member shape has flattened trait
if (member.hasTrait(XmlFlattenedTrait.class)) {
writer.write("el := value.FlattenedElement($L)", "root");
} else {
writer.write("el := value.MemberElement($L)", "root");
}
target.accept(getMemberSerVisitor(member, operand, "el"));
});
GoValueAccessUtils.writeIfNonZeroValueMember(context.getModel(), context.getSymbolProvider(), writer,
member, "v", (operand) -> {
XmlProtocolUtils.generateXMLStartElement(context, member, "root", "v");

// check if member shape has flattened trait
if (member.hasTrait(XmlFlattenedTrait.class)) {
writer.write("el := value.FlattenedElement($L)", "root");
} else {
writer.write("el := value.MemberElement($L)", "root");
}
target.accept(getMemberSerVisitor(member, operand, "el"));
});

writer.insertTrailingNewline();
}
Expand Down

0 comments on commit b4af135

Please sign in to comment.