Skip to content

Commit

Permalink
use GoPointableIndex from knowledge index cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jasdel committed Nov 12, 2020
1 parent 4801e82 commit d6abc63
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -62,6 +59,7 @@ public class DocumentMemberSerVisitor implements ShapeVisitor<Void> {
private final String dataDest;
private final Format timestampFormat;
private final MemberShape member;
private final GoPointableIndex pointableIndex;

public DocumentMemberSerVisitor(
GenerationContext context,
Expand All @@ -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);

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,12 +60,14 @@ public class JsonMemberDeserVisitor implements ShapeVisitor<Void> {
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
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -112,30 +115,30 @@ 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;
}

@Override
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;
}

@Override
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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -257,21 +260,21 @@ 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:
handleString(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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ final class JsonShapeSerVisitor extends DocumentShapeSerVisitor {

private final Predicate<MemberShape> memberFilter;
private final GoPointableIndex pointableIndex;
private final NullableIndex nullableIndex;

/**
* @param context The generation context.
Expand All @@ -79,8 +78,7 @@ public JsonShapeSerVisitor(GenerationContext context) {
public JsonShapeSerVisitor(GenerationContext context, Predicate<MemberShape> 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) {
Expand Down Expand Up @@ -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()");
Expand Down Expand Up @@ -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()");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
});
Expand Down Expand Up @@ -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");
});
Expand Down
Loading

0 comments on commit d6abc63

Please sign in to comment.