Skip to content

Commit

Permalink
codegen fixes with cas
Browse files Browse the repository at this point in the history
  • Loading branch information
sbera87 committed Oct 21, 2024
1 parent f16a2c1 commit b8bc77f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4,093 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ else if (isMap(value) && isMapShape(shape))
String mapKey = entry.getKey();
DATA mapValue = entry.getValue();
SHAPE fieldShape = fieldShapeMap.get(mapKey);

//set elements of the variable
blockWriter.addCode(String.format("%s.Set%s( %s );\n", varName, mapKey,
blockWriter.addCode(String.format("%s.Set%s( %s );\n", varName, convertSnakeToPascal(mapKey),
GenerateCppSetters(mapKey,
mapValue,
fieldShape,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.amazonaws.util.awsclientsmithygenerator.generators;

import java.lang.reflect.Type;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class SmithyC2JNamespaceMap {
private static SmithyC2JNamespaceMap instance = null;
private static Map<String, String> SMITHY_C2J_SERVICE_NAME_MAP;
private static boolean isInitialized = false;


private SmithyC2JNamespaceMap(String jsonString){

Type mapType = new TypeToken<Map<String, String>>(){}.getType();
Gson gson = new Gson();
SMITHY_C2J_SERVICE_NAME_MAP = gson.fromJson(jsonString, mapType);
}

//This method must be called once to construct an internal mapping of smithy to c2j namespace
public static SmithyC2JNamespaceMap getInstance(String jsonString)
{
//double check locking
if(instance == null)
{
synchronized (SmithyC2JNamespaceMap.class) {
if (instance == null) {
instance = new SmithyC2JNamespaceMap(jsonString);
isInitialized = true;
}
}
}
return instance;
}

public static SmithyC2JNamespaceMap getInstance()
{
if (!isInitialized) {
// If accessed without prior initialization, throw an exception
throw new IllegalStateException("Singleton not initialized. Call getInstance(json string) first.");
}
return instance;
}

public static String getC2JServiceName(String smithyServiceName)
{
if(SMITHY_C2J_SERVICE_NAME_MAP.containsKey(smithyServiceName))
{
return SMITHY_C2J_SERVICE_NAME_MAP.get(smithyServiceName);
}
return smithyServiceName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public final class SmokeTestsCMakeWriter extends SymbolWriter<SmokeTestsCMakeWri
//protected CppBlockWriter blockWriter;
public SmokeTestsCMakeWriter(String namespace) {
super(new CppImportContainer(namespace));
this.folderNamespace = SmokeTestsParser.toKebabCase(namespace);
this.folderNamespace = namespace;
}

public String generate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
import software.amazon.smithy.aws.smoketests.model.AwsVendorParams;
import software.amazon.smithy.aws.smoketests.model.S3VendorParams;
import software.amazon.smithy.aws.smoketests.model.AwsSmokeTestModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;


//import com.amazonaws.util.awsclientsmithygenerator.generators.common.CppSymbolVisitor;
Expand All @@ -43,7 +40,6 @@ public class SmokeTestsParser implements Runnable{
final private Map<ShapeId, String> operationToServiceMap;
final private Map<String, ServiceShape> serviceShapeMap;
// Static member to store the flipped map
private Map<String, String> SMITHY_C2J_SERVICE_NAME_MAP;
final private Set<String> serviceFilter;

public SmokeTestsParser(PluginContext context)
Expand All @@ -55,7 +51,6 @@ public SmokeTestsParser(PluginContext context)
this.operationToServiceMap = new HashMap<>();
this.serviceShapeMap = new HashMap<>();
this.serviceFilter = new HashSet<>();
this.SMITHY_C2J_SERVICE_NAME_MAP = new HashMap<>();
//parse the filter
Map<String, Node> settings = context.getSettings().getStringMap();
if(settings.containsKey("serviceFilter"))
Expand All @@ -73,14 +68,12 @@ public SmokeTestsParser(PluginContext context)
}
}
//parse mapping and store
Type mapType = new TypeToken<Map<String, String>>(){}.getType();
if(settings.containsKey("c2jMap"))
{
Node value = settings.get("c2jMap");
if(value.isStringNode())
{
Gson gson = new Gson();
SMITHY_C2J_SERVICE_NAME_MAP = gson.fromJson(value.asStringNode().get().getValue(), mapType);
SmithyC2JNamespaceMap.getInstance(value.asStringNode().get().getValue());
}
}

Expand Down Expand Up @@ -140,15 +133,6 @@ private String getServiceName(ServiceShape serviceShape) throws Exception
return clientName;
}

public String getC2JServiceName(String smithyServiceName)
{
if(SMITHY_C2J_SERVICE_NAME_MAP.containsKey(smithyServiceName))
{
return SMITHY_C2J_SERVICE_NAME_MAP.get(smithyServiceName);
}
return smithyServiceName;
}

//for given smoke test trait in an operation for a service, parse smoke tests
private List<SmokeTestData> parseSmokeTests(
SmokeTestsTrait smokeTestsTrait,
Expand Down Expand Up @@ -203,7 +187,7 @@ private List<SmokeTestData> parseSmokeTests(

Node value = paramEntry.getValue();
try {
blockWriter.addCode(String.format("input.Set%s(%s);\n", key,
blockWriter.addCode(String.format("input.Set%s(%s);\n", GenericCodegenAdapter.convertSnakeToPascal(key),

codegenAdapter.GenerateCppSetters(
test.getOperationName().toLowerCase() + "_elem",
Expand Down Expand Up @@ -343,22 +327,24 @@ public void run(){
SmokeTestsSourceDelegator delegator = new SmokeTestsSourceDelegator(this.context.getFileManifest(), this.symbolProvider);
SmokeTestsCMakeDelegator cmakedelegator = new SmokeTestsCMakeDelegator(this.context.getFileManifest(), this.symbolProvider);
Map<ServiceShape, List<SmokeTestData> > smoketests = extractServiceSmokeTests();

//make service specific folder
smoketests.entrySet().stream().forEach(entry -> {
ServiceShape serviceShape = entry.getKey();
try{
String client = getServiceName(serviceShape);
String c2jClientname = getC2JServiceName(toKebabCase(client));
String c2jClientname = SmithyC2JNamespaceMap.getC2JServiceName(toKebabCase(client));

Path relativePath = Paths.get( c2jClientname );
System.out.println(toKebabCase(client) + " mapped to " + c2jClientname);

delegator.useFileWriter( relativePath.toString() + "/"+ removeSpaces(client) + "SmokeTests.cpp", client, writer -> {
System.out.println(String.format("generating smoke test source code=%s",relativePath.toString() + "/"+ removeSpaces(client) + "SmokeTests.cpp"));
writer.generate(entry.getValue());
});

cmakedelegator.useFileWriter( relativePath.toString() + "/"+ "CMakeLists.txt", client, writer -> {
System.out.println(String.format("generating smoke test source code=%s",relativePath.toString() + "/"+ "CMakeLists.txt"));
cmakedelegator.useFileWriter( relativePath.toString() + "/"+ "CMakeLists.txt", c2jClientname, writer -> {
System.out.println(String.format("generating smoke test cmake code=%s",relativePath.toString() + "/"+ "CMakeLists.txt"));
writer.generate();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import java.util.List;

public final class SmokeTestsSourceWriter extends SymbolWriter<SmokeTestsSourceWriter, CppImportContainer>{
private String clientNamespace;
private String folderNamespace;
private final String clientNamespace;
private final String folderNamespace;
private final String c2jNamespace;
//protected CppBlockWriter blockWriter;
public SmokeTestsSourceWriter(String namespace) {
super(new CppImportContainer(namespace));
this.clientNamespace = SmokeTestsParser.removeSpaces(namespace);
this.folderNamespace = SmokeTestsParser.toKebabCase(namespace);
this.c2jNamespace = SmithyC2JNamespaceMap.getC2JServiceName(this.folderNamespace);
}

protected void useNamespaces()
Expand Down Expand Up @@ -48,12 +50,12 @@ protected void addHeaderBlock()

protected void addClientHeader()
{
write("#include <aws/$L/$LClient.h>", folderNamespace, clientNamespace );
write("#include <aws/$L/$LClient.h>", c2jNamespace, clientNamespace );
};

protected void addRequestHeader(SmokeTestData test)
{
write("#include <aws/$L/model/$LRequest.h>", folderNamespace, test.getOperationName());
write("#include <aws/$L/model/$LRequest.h>", c2jNamespace, test.getOperationName());
};

protected void defineTestFixture()
Expand Down
Loading

0 comments on commit b8bc77f

Please sign in to comment.