From d6abc636986e361374357029389a53febc9b2a41 Mon Sep 17 00:00:00 2001 From: Jason Del Ponte Date: Thu, 12 Nov 2020 10:23:22 -0800 Subject: [PATCH] use GoPointableIndex from knowledge index cache --- .../go/codegen/DocumentMemberSerVisitor.java | 25 ++++++++--------- .../go/codegen/JsonMemberDeserVisitor.java | 27 ++++++++++-------- .../aws/go/codegen/JsonShapeSerVisitor.java | 8 ++---- .../aws/go/codegen/QueryShapeSerVisitor.java | 5 ++-- .../aws/go/codegen/XmlMemberDeserVisitor.java | 28 +++++++++---------- .../aws/go/codegen/XmlProtocolUtils.java | 3 +- 6 files changed, 49 insertions(+), 47 deletions(-) diff --git a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/DocumentMemberSerVisitor.java b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/DocumentMemberSerVisitor.java index fa4f0bafa66..57fdd1851b0 100644 --- a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/DocumentMemberSerVisitor.java +++ b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/DocumentMemberSerVisitor.java @@ -15,7 +15,6 @@ package software.amazon.smithy.aws.go.codegen; -import java.util.function.Consumer; import software.amazon.smithy.codegen.core.CodegenException; import software.amazon.smithy.go.codegen.CodegenUtils; import software.amazon.smithy.go.codegen.GoWriter; @@ -24,7 +23,6 @@ import software.amazon.smithy.go.codegen.integration.ProtocolGenerator.GenerationContext; import software.amazon.smithy.go.codegen.integration.ProtocolUtils; import software.amazon.smithy.go.codegen.knowledge.GoPointableIndex; -import software.amazon.smithy.model.knowledge.NullableIndex; import software.amazon.smithy.model.shapes.BigDecimalShape; import software.amazon.smithy.model.shapes.BigIntegerShape; import software.amazon.smithy.model.shapes.BlobShape; @@ -43,7 +41,6 @@ import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.SetShape; import software.amazon.smithy.model.shapes.Shape; -import software.amazon.smithy.model.shapes.ShapeType; import software.amazon.smithy.model.shapes.ShapeVisitor; import software.amazon.smithy.model.shapes.ShortShape; import software.amazon.smithy.model.shapes.StringShape; @@ -62,6 +59,7 @@ public class DocumentMemberSerVisitor implements ShapeVisitor { private final String dataDest; private final Format timestampFormat; private final MemberShape member; + private final GoPointableIndex pointableIndex; public DocumentMemberSerVisitor( GenerationContext context, @@ -75,67 +73,68 @@ public DocumentMemberSerVisitor( this.dataSource = dataSource; this.dataDest = dataDest; this.timestampFormat = timestampFormat; + this.pointableIndex = GoPointableIndex.of(context.getModel()); } @Override public Void blobShape(BlobShape shape) { - String source = CodegenUtils.getAsValueIfDereferencable(context.getPointableIndex(), member, dataSource); + String source = CodegenUtils.getAsValueIfDereferencable(pointableIndex, member, dataSource); context.getWriter().write("$L.Base64EncodeBytes($L)", dataDest, source); return null; } @Override public Void booleanShape(BooleanShape shape) { - String source = CodegenUtils.getAsValueIfDereferencable(context.getPointableIndex(), member, dataSource); + String source = CodegenUtils.getAsValueIfDereferencable(pointableIndex, member, dataSource); context.getWriter().write("$L.Boolean($L)", dataDest, source); return null; } @Override public Void byteShape(ByteShape shape) { - String source = CodegenUtils.getAsValueIfDereferencable(context.getPointableIndex(), member, dataSource); + String source = CodegenUtils.getAsValueIfDereferencable(pointableIndex, member, dataSource); context.getWriter().write("$L.Byte($L)", dataDest, source); return null; } @Override public Void shortShape(ShortShape shape) { - String source = CodegenUtils.getAsValueIfDereferencable(context.getPointableIndex(), member, dataSource); + String source = CodegenUtils.getAsValueIfDereferencable(pointableIndex, member, dataSource); context.getWriter().write("$L.Short($L)", dataDest, source); return null; } @Override public Void integerShape(IntegerShape shape) { - String source = CodegenUtils.getAsValueIfDereferencable(context.getPointableIndex(), member, dataSource); + String source = CodegenUtils.getAsValueIfDereferencable(pointableIndex, member, dataSource); context.getWriter().write("$L.Integer($L)", dataDest, source); return null; } @Override public Void longShape(LongShape shape) { - String source = CodegenUtils.getAsValueIfDereferencable(context.getPointableIndex(), member, dataSource); + String source = CodegenUtils.getAsValueIfDereferencable(pointableIndex, member, dataSource); context.getWriter().write("$L.Long($L)", dataDest, source); return null; } @Override public Void floatShape(FloatShape shape) { - String source = CodegenUtils.getAsValueIfDereferencable(context.getPointableIndex(), member, dataSource); + String source = CodegenUtils.getAsValueIfDereferencable(pointableIndex, member, dataSource); context.getWriter().write("$L.Float($L)", dataDest, source); return null; } @Override public Void doubleShape(DoubleShape shape) { - String source = CodegenUtils.getAsValueIfDereferencable(context.getPointableIndex(), member, dataSource); + String source = CodegenUtils.getAsValueIfDereferencable(pointableIndex, member, dataSource); context.getWriter().write("$L.Double($L)", dataDest, source); return null; } @Override public Void timestampShape(TimestampShape shape) { - String source = CodegenUtils.getAsValueIfDereferencable(context.getPointableIndex(), member, dataSource); + String source = CodegenUtils.getAsValueIfDereferencable(pointableIndex, member, dataSource); GoWriter writer = context.getWriter(); writer.addUseImports(SmithyGoDependency.SMITHY_TIME); @@ -157,7 +156,7 @@ public Void timestampShape(TimestampShape shape) { @Override public Void stringShape(StringShape shape) { - String source = CodegenUtils.getAsValueIfDereferencable(context.getPointableIndex(), member, dataSource); + String source = CodegenUtils.getAsValueIfDereferencable(pointableIndex, member, dataSource); if (shape.hasTrait(EnumTrait.class)) { source = String.format("string(%s)", source); } diff --git a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/JsonMemberDeserVisitor.java b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/JsonMemberDeserVisitor.java index f3d08164292..85eb018468e 100644 --- a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/JsonMemberDeserVisitor.java +++ b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/JsonMemberDeserVisitor.java @@ -23,6 +23,7 @@ import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; import software.amazon.smithy.go.codegen.integration.ProtocolGenerator.GenerationContext; import software.amazon.smithy.go.codegen.integration.ProtocolUtils; +import software.amazon.smithy.go.codegen.knowledge.GoPointableIndex; import software.amazon.smithy.model.shapes.BigDecimalShape; import software.amazon.smithy.model.shapes.BigIntegerShape; import software.amazon.smithy.model.shapes.BlobShape; @@ -59,12 +60,14 @@ public class JsonMemberDeserVisitor implements ShapeVisitor { private final MemberShape member; private final String dataDest; private final Format timestampFormat; + private final GoPointableIndex pointableIndex; public JsonMemberDeserVisitor(GenerationContext context, MemberShape member, String dataDest, Format timestampFormat) { this.context = context; this.member = member; this.dataDest = dataDest; this.timestampFormat = timestampFormat; + this.pointableIndex = GoPointableIndex.of(context.getModel()); } @Override @@ -86,7 +89,7 @@ public Void blobShape(BlobShape shape) { }); writer.write("$L = $L", dataDest, CodegenUtils.getAsPointerIfPointable(context.getModel(), - context.getWriter(), context.getPointableIndex(), member, "dv")); + context.getWriter(), pointableIndex, member, "dv")); }); return null; } @@ -102,7 +105,7 @@ public Void booleanShape(BooleanShape shape) { shape.getId().getName()); }); writer.write("$L = $L", dataDest, CodegenUtils.getAsPointerIfPointable(context.getModel(), - context.getWriter(), context.getPointableIndex(), member, "jtv")); + context.getWriter(), pointableIndex, member, "jtv")); }); return null; } @@ -112,7 +115,7 @@ public Void byteShape(ByteShape shape) { GoWriter writer = context.getWriter(); // Smithy's byte shape represents a signed 8-bit int, which doesn't line up with Go's unsigned byte handleInteger(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), writer, - context.getPointableIndex(), member, "int8(i64)")); + pointableIndex, member, "int8(i64)")); return null; } @@ -120,7 +123,7 @@ public Void byteShape(ByteShape shape) { public Void shortShape(ShortShape shape) { GoWriter writer = context.getWriter(); handleInteger(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), writer, - context.getPointableIndex(), member, "int16(i64)")); + pointableIndex, member, "int16(i64)")); return null; } @@ -128,14 +131,14 @@ public Void shortShape(ShortShape shape) { public Void integerShape(IntegerShape shape) { GoWriter writer = context.getWriter(); handleInteger(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), writer, - context.getPointableIndex(), member, "int32(i64)")); + pointableIndex, member, "int32(i64)")); return null; } @Override public Void longShape(LongShape shape) { handleInteger(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), context.getWriter(), - context.getPointableIndex(), member, "i64")); + pointableIndex, member, "i64")); return null; } @@ -181,14 +184,14 @@ private void handleNumber(Shape shape, Runnable r) { @Override public Void floatShape(FloatShape shape) { handleFloat(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), context.getWriter(), - context.getPointableIndex(), member, "float32(f64)")); + pointableIndex, member, "float32(f64)")); return null; } @Override public Void doubleShape(DoubleShape shape) { handleFloat(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), context.getWriter(), - context.getPointableIndex(), member, "f64")); + pointableIndex, member, "f64")); return null; } @@ -218,7 +221,7 @@ public Void stringShape(StringShape shape) { handleString(shape, () -> writer.write("$L = $P(jtv)", dataDest, symbol)); } else { handleString(shape, () -> writer.write("$L = $L", dataDest, CodegenUtils.getAsPointerIfPointable( - context.getModel(), context.getWriter(), context.getPointableIndex(), member, "jtv"))); + context.getModel(), context.getWriter(), pointableIndex, member, "jtv"))); } return null; @@ -257,7 +260,7 @@ public Void timestampShape(TimestampShape shape) { writer.write("t, err := smithytime.ParseDateTime(jtv)"); writer.write("if err != nil { return err }"); writer.write("$L = $L", dataDest, CodegenUtils.getAsPointerIfPointable(context.getModel(), - context.getWriter(), context.getPointableIndex(), member, "t")); + context.getWriter(), pointableIndex, member, "t")); }); break; case HTTP_DATE: @@ -265,13 +268,13 @@ public Void timestampShape(TimestampShape shape) { writer.write("t, err := smithytime.ParseHTTPDate(jtv)"); writer.write("if err != nil { return err }"); writer.write("$L = $L", dataDest, CodegenUtils.getAsPointerIfPointable(context.getModel(), - context.getWriter(), context.getPointableIndex(), member, "t")); + context.getWriter(), pointableIndex, member, "t")); }); break; case EPOCH_SECONDS: writer.addUseImports(SmithyGoDependency.SMITHY_PTR); handleFloat(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), context.getWriter(), - context.getPointableIndex(), member, "smithytime.ParseEpochSeconds(f64)")); + pointableIndex, member, "smithytime.ParseEpochSeconds(f64)")); break; default: throw new CodegenException(String.format("Unknown timestamp format %s", timestampFormat)); diff --git a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/JsonShapeSerVisitor.java b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/JsonShapeSerVisitor.java index 6d46eef0829..10ee04b2c6e 100644 --- a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/JsonShapeSerVisitor.java +++ b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/JsonShapeSerVisitor.java @@ -62,7 +62,6 @@ final class JsonShapeSerVisitor extends DocumentShapeSerVisitor { private final Predicate memberFilter; private final GoPointableIndex pointableIndex; - private final NullableIndex nullableIndex; /** * @param context The generation context. @@ -79,8 +78,7 @@ public JsonShapeSerVisitor(GenerationContext context) { public JsonShapeSerVisitor(GenerationContext context, Predicate memberFilter) { super(context); this.memberFilter = NoSerializeTrait.excludeNoSerializeMembers().and(memberFilter); - this.pointableIndex = new GoPointableIndex(context.getModel()); - this.nullableIndex = new NullableIndex(context.getModel()); + this.pointableIndex = GoPointableIndex.of(context.getModel()); } private DocumentMemberSerVisitor getMemberSerVisitor(MemberShape member, String source, String dest) { @@ -109,7 +107,7 @@ protected void serializeCollection(GenerationContext context, CollectionShape sh // Null values in lists should be serialized as such. Enums can't be null, so we don't bother // putting this in for their case. - if (context.getPointableIndex().isNillable(shape.getMember())) { + if (pointableIndex.isNillable(shape.getMember())) { writer.openBlock("if vv := v[i]; vv == nil {", "}", () -> { if (context.getNullableIndex().isNullable(shape.getMember())) { writer.write("av.Null()"); @@ -147,7 +145,7 @@ protected void serializeMap(GenerationContext context, MapShape shape) { // Null values in maps should be serialized as such. Enums can't be null, so we don't bother // putting this in for their case. - if (context.getPointableIndex().isNillable(shape.getValue())) { + if (pointableIndex.isNillable(shape.getValue())) { writer.openBlock("if vv := v[key]; vv == nil {", "}", () -> { if (context.getNullableIndex().isNullable(shape.getValue())) { writer.write("om.Null()"); diff --git a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/QueryShapeSerVisitor.java b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/QueryShapeSerVisitor.java index 029cf68bed5..8db4635bcdd 100644 --- a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/QueryShapeSerVisitor.java +++ b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/QueryShapeSerVisitor.java @@ -15,6 +15,7 @@ import software.amazon.smithy.go.codegen.integration.DocumentShapeSerVisitor; import software.amazon.smithy.go.codegen.integration.ProtocolGenerator.GenerationContext; import software.amazon.smithy.go.codegen.integration.ProtocolUtils; +import software.amazon.smithy.go.codegen.knowledge.GoPointableIndex; import software.amazon.smithy.go.codegen.trait.NoSerializeTrait; import software.amazon.smithy.model.shapes.CollectionShape; import software.amazon.smithy.model.shapes.DocumentShape; @@ -81,7 +82,7 @@ protected void serializeCollection(GenerationContext context, CollectionShape sh writer.openBlock("for i := range v {", "}", () -> { // Null values should be omitted for query. - if (context.getPointableIndex().isNillable(shape.getMember())) { + if (GoPointableIndex.of(context.getModel()).isNillable(shape.getMember())) { writer.openBlock("if vv := v[i]; vv == nil {", "}", () -> { writer.write("continue"); }); @@ -125,7 +126,7 @@ protected void serializeMap(GenerationContext context, MapShape shape) { writer.addUseImports(SmithyGoDependency.FMT); writer.openBlock("for _, key := range keys {", "}", () -> { // Null values should be omitted for query. - if (context.getPointableIndex().isNillable(shape.getValue())) { + if (GoPointableIndex.of(context.getModel()).isNillable(shape.getValue())) { writer.openBlock("if vv := v[key]; vv == nil {", "}", () -> { writer.write("continue"); }); diff --git a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/XmlMemberDeserVisitor.java b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/XmlMemberDeserVisitor.java index c397a670fb9..1166895b2e2 100644 --- a/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/XmlMemberDeserVisitor.java +++ b/codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/XmlMemberDeserVisitor.java @@ -1,6 +1,5 @@ package software.amazon.smithy.aws.go.codegen; -import java.util.function.Consumer; import software.amazon.smithy.codegen.core.CodegenException; import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.go.codegen.CodegenUtils; @@ -47,6 +46,7 @@ public class XmlMemberDeserVisitor implements ShapeVisitor { private final MemberShape member; private final String dataDest; private final Format timestampFormat; + private final GoPointableIndex pointableIndex; // isXmlAttributeMember indicates if member is deserialized from the xml start elements attribute value. private final boolean isXmlAttributeMember; @@ -62,7 +62,7 @@ public XmlMemberDeserVisitor( this.timestampFormat = timestampFormat; this.isXmlAttributeMember = isXmlAttributeMember; this.isFlattened = member.hasTrait(XmlFlattenedTrait.ID); - ; + this.pointableIndex = GoPointableIndex.of(context.getModel()); } @Override @@ -91,7 +91,7 @@ public Void booleanShape(BooleanShape shape) { shape.getId().getName()); }); writer.write("$L = $L", dataDest, CodegenUtils.getAsPointerIfPointable(context.getModel(), - context.getWriter(), context.getPointableIndex(), member, "xtv")); + context.getWriter(), pointableIndex, member, "xtv")); }); return null; } @@ -131,7 +131,7 @@ private void handleDone(Shape shape) { // Handle string shape zero value assignment switch (shape.getType()) { case STRING: - if (context.getPointableIndex().isPointable(member)) { + if (pointableIndex.isPointable(member)) { // assign empty string as zero value if val for string member is nil. writer.addUseImports(SmithyGoDependency.SMITHY_PTR); writer.write("if val == nil { $L = ptr.String(\"\") }", dataDest); @@ -153,28 +153,28 @@ private void handleDone(Shape shape) { public Void byteShape(ByteShape shape) { // Smithy's byte shape represents a signed 8-bit int, which doesn't line up with Go's unsigned byte handleInteger(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), context.getWriter(), - context.getPointableIndex(), member, "int8(i64)")); + pointableIndex, member, "int8(i64)")); return null; } @Override public Void shortShape(ShortShape shape) { handleInteger(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), context.getWriter(), - context.getPointableIndex(), member, "int16(i64)")); + pointableIndex, member, "int16(i64)")); return null; } @Override public Void integerShape(IntegerShape shape) { handleInteger(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), context.getWriter(), - context.getPointableIndex(), member, "int32(i64)")); + pointableIndex, member, "int32(i64)")); return null; } @Override public Void longShape(LongShape shape) { handleInteger(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), context.getWriter(), - context.getPointableIndex(), member, "i64")); + pointableIndex, member, "i64")); return null; } @@ -216,14 +216,14 @@ private void handleNumber(Shape shape, Runnable r) { @Override public Void floatShape(FloatShape shape) { handleFloat(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), context.getWriter(), - context.getPointableIndex(), member, "float32(f64)")); + pointableIndex, member, "float32(f64)")); return null; } @Override public Void doubleShape(DoubleShape shape) { handleFloat(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), context.getWriter(), - context.getPointableIndex(), member, "f64")); + pointableIndex, member, "f64")); return null; } @@ -252,7 +252,7 @@ public Void stringShape(StringShape shape) { handleString(shape, () -> writer.write("$L = $P(xtv)", dataDest, symbol)); } else { handleString(shape, () -> writer.write("$L = $L", dataDest, CodegenUtils.getAsPointerIfPointable( - context.getModel(), context.getWriter(), context.getPointableIndex(), member, "xtv"))); + context.getModel(), context.getWriter(), pointableIndex, member, "xtv"))); } return null; @@ -287,7 +287,7 @@ public Void timestampShape(TimestampShape shape) { writer.write("t, err := smithytime.ParseDateTime(xtv)"); writer.write("if err != nil { return err }"); writer.write("$L = $L", dataDest, CodegenUtils.getAsPointerIfPointable(context.getModel(), - context.getWriter(), context.getPointableIndex(), member, "t")); + context.getWriter(), pointableIndex, member, "t")); }); break; case HTTP_DATE: @@ -295,13 +295,13 @@ public Void timestampShape(TimestampShape shape) { writer.write("t, err := smithytime.ParseHTTPDate(xtv)"); writer.write("if err != nil { return err }"); writer.write("$L = $L", dataDest, CodegenUtils.getAsPointerIfPointable(context.getModel(), - context.getWriter(), context.getPointableIndex(), member, "t")); + context.getWriter(), pointableIndex, member, "t")); }); break; case EPOCH_SECONDS: writer.addUseImports(SmithyGoDependency.SMITHY_PTR); handleFloat(shape, CodegenUtils.getAsPointerIfPointable(context.getModel(), writer, - context.getPointableIndex(), member, "smithytime.ParseEpochSeconds(f64)")); + pointableIndex, member, "smithytime.ParseEpochSeconds(f64)")); break; default: throw new CodegenException(String.format("Unknown timestamp format %s", timestampFormat)); 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 2eef9f3652f..0e037b0a164 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 @@ -13,6 +13,7 @@ import software.amazon.smithy.go.codegen.SyntheticClone; import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; import software.amazon.smithy.go.codegen.integration.ProtocolUtils; +import software.amazon.smithy.go.codegen.knowledge.GoPointableIndex; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.MemberShape; import software.amazon.smithy.model.shapes.ServiceShape; @@ -87,7 +88,7 @@ private static void formatXmlAttributeValueAsString( // Pointable value references need to be dereferenced before being used. String derefSource = src; - if (context.getPointableIndex().isPointable(member)) { + if (GoPointableIndex.of(context.getModel()).isPointable(member)) { derefSource = "*" + src; }