Skip to content

Commit

Permalink
fix operation interface generator for incorrect exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
skotambkar committed Dec 5, 2020
1 parent 0ff9d3f commit c4c9a5d
Showing 1 changed file with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

package software.amazon.smithy.go.codegen.integration;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.go.codegen.GoDelegator;
Expand All @@ -38,7 +39,19 @@
*/
public class OperationInterfaceGenerator implements GoIntegration {

private static Set<ShapeId> listOfClientInterfaceOperations = new TreeSet<>();
private static Map<ShapeId, Set<ShapeId>> mapOfClientInterfaceOperations = new HashMap<>();

/**
* Returns name of an API client interface.
*
* @param operationSymbol Symbol of operation shape for which Api client interface is being generated.
* @return name of the interface.
*/
public static String getApiClientInterfaceName(
Symbol operationSymbol
) {
return String.format("%sAPIClient", operationSymbol.getName());
}

@Override
public void processFinalizedModel(
Expand All @@ -47,6 +60,7 @@ public void processFinalizedModel(
) {
ServiceShape serviceShape = settings.getService(model);
TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<ShapeId> listOfClientInterfaceOperations = new TreeSet<>();

// fetch operations for which paginated trait is applied
topDownIndex.getContainedOperations(serviceShape).stream()
Expand All @@ -55,33 +69,37 @@ public void processFinalizedModel(

if (serviceShape.hasTrait(PaginatedTrait.class)) {
topDownIndex.getContainedOperations(serviceShape).stream()
.forEach(operationShape -> listOfClientInterfaceOperations.add(operationShape.getId()));
.forEach(operationShape -> listOfClientInterfaceOperations.add(operationShape.toShapeId()));
}

// fetch operations for which waitable trait is applied
topDownIndex.getContainedOperations(serviceShape).stream()
.filter(operationShape -> operationShape.hasTrait(WaitableTrait.class))
.forEach(operationShape -> listOfClientInterfaceOperations.add(operationShape.getId()));

if (listOfClientInterfaceOperations.isEmpty()) {
throw new CodegenException("empty operations");
if (!listOfClientInterfaceOperations.isEmpty()) {
mapOfClientInterfaceOperations.put(serviceShape.getId(), listOfClientInterfaceOperations);
}
}


@Override
public void writeAdditionalFiles(
GoSettings settings,
Model model,
SymbolProvider symbolProvider,
GoDelegator goDelegator
) {
listOfClientInterfaceOperations.stream().forEach(shapeId -> {
OperationShape operationShape = model.expectShape(shapeId, OperationShape.class);
goDelegator.useShapeWriter(operationShape, writer -> {
generateApiClientInterface(writer, model, symbolProvider, operationShape);
ShapeId serviceId = settings.getService(model).getId();

if (mapOfClientInterfaceOperations.containsKey(serviceId)) {
Set<ShapeId> listOfClientInterfaceOperations = mapOfClientInterfaceOperations.get(serviceId);
listOfClientInterfaceOperations.stream().forEach(shapeId -> {
OperationShape operationShape = model.expectShape(shapeId, OperationShape.class);
goDelegator.useShapeWriter(operationShape, writer -> {
generateApiClientInterface(writer, model, symbolProvider, operationShape);
});
});
});
}
}

private void generateApiClientInterface(
Expand Down Expand Up @@ -111,16 +129,4 @@ private void generateApiClientInterface(
writer.write("var _ $T = (*Client)(nil)", interfaceSymbol);
writer.write("");
}

/**
* Returns name of an API client interface.
*
* @param operationSymbol Symbol of operation shape for which Api client interface is being generated.
* @return name of the interface.
*/
public static String getApiClientInterfaceName(
Symbol operationSymbol
) {
return String.format("%sAPIClient", operationSymbol.getName());
}
}

0 comments on commit c4c9a5d

Please sign in to comment.