Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Go): always generate code in an order #758

Merged
merged 17 commits into from
Jan 9, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import static software.amazon.polymorph.smithygo.utils.Constants.DAFNY_RUNTIME_GO_LIBRARY_MODULE;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
import software.amazon.polymorph.smithygo.awssdk.shapevisitor.AwsSdkToDafnyShapeVisitor;
import software.amazon.polymorph.smithygo.awssdk.shapevisitor.DafnyToAwsSdkShapeVisitor;
import software.amazon.polymorph.smithygo.awssdk.shapevisitor.ShapeVisitorHelper;
Expand Down Expand Up @@ -62,6 +64,8 @@ public void generateSerializers(final GenerationContext context) {
final var writerDelegator = context.writerDelegator();
serviceShape
.getOperations()
.stream()
.sorted()
.forEach(eachOperation -> {
final var awsNormalizedOperation = awsNormalizedModel.expectShape(
eachOperation,
Expand Down Expand Up @@ -213,6 +217,8 @@ public void generateDeserializers(final GenerationContext context) {

serviceShape
.getOperations()
.stream()
.sorted()
.forEach(eachOperation -> {
final var awsNormalizedOperationShape = awsNormalizedModel.expectShape(
eachOperation,
Expand Down Expand Up @@ -487,9 +493,11 @@ private void generateResponseDeserializer(

private void generateErrorSerializer(final GenerationContext context) {
final Set<ShapeId> alreadyVisited = new HashSet<>();
final var errorShapes = awsNormalizedModel.getShapesWithTrait(
ErrorTrait.class
);
final var errorShapes = awsNormalizedModel
.getShapesWithTrait(ErrorTrait.class)
.stream()
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));

for (final var errorShape : errorShapes) {
if (
Expand Down Expand Up @@ -634,9 +642,11 @@ func Error_ToDafny(err error)($L.Error) {

private void generateErrorDeserializer(final GenerationContext context) {
final Set<ShapeId> alreadyVisited = new HashSet<>();
final var errorShapes = awsNormalizedModel.getShapesWithTrait(
ErrorTrait.class
);
final var errorShapes = awsNormalizedModel
.getShapesWithTrait(ErrorTrait.class)
.stream()
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));
for (final var errorShape : errorShapes) {
if (
!errorShape
Expand Down Expand Up @@ -749,9 +759,7 @@ func Error_FromDafny(err $L.Error)(error) {
""",
DafnyNameResolver.dafnyTypesNamespace(serviceShape),
writer.consumer(w -> {
for (final var error : awsNormalizedModel.getShapesWithTrait(
ErrorTrait.class
)) {
for (final var error : errorShapes) {
w.write(
"""
if err.Is_$L() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static software.amazon.polymorph.smithygo.localservice.nameresolver.Constants.DOT;
import static software.amazon.polymorph.smithygo.utils.Constants.DAFNY_RUNTIME_GO_LIBRARY_MODULE;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import software.amazon.polymorph.smithygo.awssdk.AwsSdkGoPointableIndex;
Expand Down Expand Up @@ -48,7 +48,7 @@ public class AwsSdkToDafnyShapeVisitor extends ShapeVisitor.Default<String> {
protected boolean isPointerType;
//TODO: Ideally this shouldn't be static but with current design we need to access this across instances.
private static final Map<MemberShape, String> memberShapeConversionFuncMap =
new HashMap<>();
new LinkedHashMap<>();

public AwsSdkToDafnyShapeVisitor(
final GenerationContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class DafnyToAwsSdkShapeVisitor extends ShapeVisitor.Default<String> {

//TODO: Ideally this shouldn't be static but with current design we need to access this across instances.
private static final Map<MemberShape, String> memberShapeConversionFuncMap =
new HashMap<>();
new LinkedHashMap<>();

public DafnyToAwsSdkShapeVisitor(
final GenerationContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ public void generateUnion(GoWriter writer) {
.values()
.stream()
.filter(memberShape -> !isEventStreamErrorMember(memberShape))
.sorted()
.collect(Collectors.toCollection(TreeSet::new));

memberShapes
.stream()
.map(symbolProvider::toMemberName)
.sorted()
.forEach(name -> {
writer.write("// " + name);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import software.amazon.polymorph.smithygo.localservice.nameresolver.SmithyNameResolver;
Expand Down Expand Up @@ -45,9 +46,9 @@ public class ValidationGenerator {
}
""";
private static final Map<MemberShape, String> validationFuncMap =
new HashMap<>();
new LinkedHashMap<>();
private static final Map<MemberShape, String> validationFuncInputTypeMap =
new HashMap<>();
new LinkedHashMap<>();

public ValidationGenerator(
final GenerationContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public GoUsageIndex(Model model) {
)
.stream()
.map(Shape::toShapeId)
.sorted()
.collect(Collectors.toList())
);

Expand All @@ -84,6 +85,7 @@ public GoUsageIndex(Model model) {
)
.stream()
.map(Shape::toShapeId)
.sorted()
.collect(Collectors.toList())
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public GoValidationIndex(Model model) {
.values()
.stream()
.map(OperationShape::toShapeId)
.sorted()
.collect(Collectors.toSet())
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import static software.amazon.polymorph.smithygo.codegen.SymbolUtils.POINTABLE;
import static software.amazon.polymorph.smithygo.utils.Constants.DAFNY_RUNTIME_GO_LIBRARY_MODULE;

import java.util.LinkedHashSet;
import java.util.stream.Collectors;
import software.amazon.polymorph.smithygo.codegen.GenerationContext;
import software.amazon.polymorph.smithygo.codegen.GoDelegator;
import software.amazon.polymorph.smithygo.codegen.GoWriter;
Expand All @@ -28,10 +30,8 @@
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ResourceShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.UnitTypeTrait;
import software.amazon.smithy.utils.StringUtils;

public class DafnyLocalServiceGenerator implements Runnable {

Expand Down Expand Up @@ -90,6 +90,7 @@ void generateClient(GoWriter writer) {
.getNamespace()
.equals(service.getId().getNamespace())
)
.sorted()
.forEach(unionShape -> {
new UnionGenerator(model, symbolProvider, unionShape)
.generateUnion(writer1);
Expand Down Expand Up @@ -623,7 +624,11 @@ void generateShim() {
}

void shimErrors(GoWriter writer) {
for (final var error : model.getShapesWithTrait(ErrorTrait.class)) {
for (final var error : model
.getShapesWithTrait(ErrorTrait.class)
.stream()
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new))) {
writer.write(
"""
case $L.$L:
Expand All @@ -641,7 +646,11 @@ void shimErrors(GoWriter writer) {
}

void resourceErrors(GoWriter writer) {
for (final var error : model.getShapesWithTrait(ErrorTrait.class)) {
for (final var error : model
.getShapesWithTrait(ErrorTrait.class)
.stream()
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new))) {
writer.write(
"""
case $L:
Expand Down Expand Up @@ -710,7 +719,11 @@ void generateUnmodelledErrors(GenerationContext context) {
}

void generateReferencedResources(final GenerationContext context) {
final var refResources = model.getShapesWithTrait(ReferenceTrait.class);
final var refResources = model
.getShapesWithTrait(ReferenceTrait.class)
.stream()
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));
for (final var refResource : refResources) {
if (!refResource.expectTrait(ReferenceTrait.class).isService()) {
final var resource = refResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -65,6 +66,8 @@ public void generateSerializers(final GenerationContext context) {
final var writerDelegator = context.writerDelegator();
serviceShape
.getOperations()
.stream()
.sorted()
.forEach(eachOperation -> {
final var operation = model.expectShape(
eachOperation,
Expand Down Expand Up @@ -203,7 +206,10 @@ public void generateSerializers(final GenerationContext context) {

final var refResources = context
.model()
.getShapesWithTrait(ReferenceTrait.class);
.getShapesWithTrait(ReferenceTrait.class)
.stream()
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));
for (final var refResource : refResources) {
final var resource = refResource
.expectTrait(ReferenceTrait.class)
Expand All @@ -215,6 +221,8 @@ public void generateSerializers(final GenerationContext context) {
);
resourceShape
.getOperations()
.stream()
.sorted()
.forEach(eachOperation -> {
final var operation = model.expectShape(
eachOperation,
Expand Down Expand Up @@ -425,6 +433,8 @@ public void generateDeserializers(final GenerationContext context) {

serviceShape
.getOperations()
.stream()
.sorted()
.forEach(eachOperation -> {
final var operation = context
.model()
Expand Down Expand Up @@ -578,7 +588,10 @@ public void generateDeserializers(final GenerationContext context) {

final var refResources = context
.model()
.getShapesWithTrait(ReferenceTrait.class);
.getShapesWithTrait(ReferenceTrait.class)
.stream()
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));
for (final var refResource : refResources) {
final var resource = refResource
.expectTrait(ReferenceTrait.class)
Expand All @@ -590,6 +603,8 @@ public void generateDeserializers(final GenerationContext context) {
.expectShape(resource, ResourceShape.class);
resourceShape
.getOperations()
.stream()
.sorted()
.forEach(eachOperation -> {
final var operation = context
.model()
Expand Down Expand Up @@ -976,7 +991,10 @@ private void generateErrorSerializer(final GenerationContext context) {
final var serviceShape = context.settings().getService(context.model());
final var errorShapes = context
.model()
.getShapesWithTrait(ErrorTrait.class);
.getShapesWithTrait(ErrorTrait.class)
.stream()
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));

for (final var errorShape : errorShapes) {
if (
Expand Down Expand Up @@ -1076,7 +1094,8 @@ func OpaqueError_Input_ToDafny(nativeInput $L.OpaqueError)($L.Error) {
.filter(shape ->
ModelUtils.isInServiceNamespace(shape.getId(), serviceShape)
)
.collect(Collectors.toSet());
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));

context
.writerDelegator()
Expand Down Expand Up @@ -1332,7 +1351,10 @@ private void generateErrorDeserializer(final GenerationContext context) {
final var serviceShape = context.settings().getService(context.model());
final var errorShapes = context
.model()
.getShapesWithTrait(ErrorTrait.class);
.getShapesWithTrait(ErrorTrait.class)
.stream()
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));
for (final var errorShape : errorShapes) {
if (
!errorShape
Expand Down Expand Up @@ -1457,7 +1479,8 @@ func OpaqueError_Output_FromDafny(dafnyOutput $L.Error)($L.OpaqueError) {
.filter(shape ->
ModelUtils.isInServiceNamespace(shape.getId(), serviceShape)
)
.collect(Collectors.toSet());
.sorted()
.collect(Collectors.toCollection(LinkedHashSet::new));

context
.writerDelegator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static software.amazon.polymorph.smithygo.utils.Constants.DAFNY_RUNTIME_GO_LIBRARY_MODULE;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import software.amazon.polymorph.smithygo.codegen.GenerationContext;
Expand Down Expand Up @@ -52,7 +52,7 @@ public class DafnyToSmithyShapeVisitor extends ShapeVisitor.Default<String> {
private final boolean isOptional;
//TODO: Ideally this shouldn't be static but with current design we need to access this across instances.
private static final Map<MemberShape, String> memberShapeConversionFuncMap =
new HashMap<>();
new LinkedHashMap<>();

public DafnyToSmithyShapeVisitor(
final GenerationContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static software.amazon.polymorph.smithygo.codegen.SymbolUtils.POINTABLE;
import static software.amazon.polymorph.smithygo.utils.Constants.DAFNY_RUNTIME_GO_LIBRARY_MODULE;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import software.amazon.polymorph.smithygo.codegen.GenerationContext;
Expand Down Expand Up @@ -52,7 +52,7 @@ public class SmithyToDafnyShapeVisitor extends ShapeVisitor.Default<String> {
protected boolean isPointerType;
//TODO: Ideally this shouldn't be static but with current design we need to access this across instances.
private static final Map<MemberShape, String> memberShapeConversionFuncMap =
new HashMap<>();
new LinkedHashMap<>();

public SmithyToDafnyShapeVisitor(
final GenerationContext context,
Expand Down
Loading