use of com.google.api.codegen.util.SymbolTable in project toolkit by googleapis.
the class JavaDiscoGapicSchemaToViewTransformer method generateSchemaClasses.
private StaticLangApiMessageView generateSchemaClasses(Map<SchemaTransformationContext, StaticLangApiMessageView> messageViewAccumulator, DiscoGapicInterfaceContext documentContext, Schema schema) {
FieldModel schemaModel = DiscoveryField.create(schema, documentContext.getApiModel());
SchemaTypeTable schemaTypeTable = documentContext.getSchemaTypeTable().cloneEmpty();
SchemaTransformationContext context = SchemaTransformationContext.create(schema.getIdentifier(), schemaTypeTable, documentContext);
StaticLangApiMessageView.Builder schemaView = StaticLangApiMessageView.newBuilder();
boolean hasRequiredProperties = false;
// Child schemas cannot have the same symbols as parent schemas, but sibling schemas can have
// the same symbols.
SymbolTable symbolTableCopy = SymbolTable.fromSeed(reservedKeywords);
String schemaId = Name.anyCamel(schema.getIdentifier()).toLowerCamel();
String schemaName = nameFormatter.privateFieldName(Name.anyCamel(symbolTableCopy.getNewSymbol(schemaId)));
schemaView.name(schemaName);
schemaView.defaultValue(schema.defaultValue());
schemaView.description(schema.description());
schemaView.fieldGetFunction(context.getNamer().getFieldGetFunctionName(schemaModel));
schemaView.fieldSetFunction(context.getNamer().getFieldSetFunctionName(schemaModel));
schemaView.fieldAddFunction(context.getNamer().getFieldAddFunctionName(schemaModel));
String schemaTypeName = schemaTypeTable.getAndSaveNicknameFor(schema);
schemaView.typeName(schemaTypeName);
if (schema.repeated() || schema.type() == Type.ARRAY) {
schemaView.innerTypeName(schemaTypeTable.getInnerTypeNameFor(schema));
} else {
schemaView.innerTypeName(schemaTypeName);
}
// Generate a Schema view from each property.
List<StaticLangApiMessageView> viewProperties = new LinkedList<>();
List<Schema> schemaProperties = new LinkedList<>();
schemaProperties.addAll(schema.properties().values());
if (schema.items() != null) {
schemaProperties.addAll(schema.items().properties().values());
}
for (Schema property : schemaProperties) {
viewProperties.add(generateSchemaClasses(messageViewAccumulator, documentContext, property));
if (!property.properties().isEmpty() || (property.items() != null)) {
// Add non-primitive-type property to imports.
schemaTypeTable.getAndSaveNicknameFor(property);
}
if (property.required()) {
hasRequiredProperties = true;
}
}
Collections.sort(viewProperties);
schemaView.properties(viewProperties);
schemaView.canRepeat(schema.repeated() || schema.type().equals(Type.ARRAY));
schemaView.isRequired(schema.required());
schemaView.isRequestMessage(false);
schemaView.hasRequiredProperties(hasRequiredProperties);
if (!schema.properties().isEmpty() || (schema.items() != null && !schema.items().properties().isEmpty())) {
// This is a top-level Schema, so add it to list of file ViewModels for rendering.
messageViewAccumulator.put(context, schemaView.build());
}
return schemaView.build();
}
use of com.google.api.codegen.util.SymbolTable in project toolkit by googleapis.
the class JavaSurfaceTestTransformer method createTestCaseViews.
private List<TestCaseView> createTestCaseViews(InterfaceContext context) {
ArrayList<TestCaseView> testCaseViews = new ArrayList<>();
SymbolTable testNameTable = new SymbolTable();
for (MethodModel method : context.getSupportedMethods()) {
MethodConfig methodConfig = context.getMethodConfig(method);
if (methodConfig.isGrpcStreaming()) {
if (methodConfig.getGrpcStreamingType() == GrpcStreamingType.ClientStreaming) {
// Issue: https://github.com/googleapis/toolkit/issues/946
continue;
}
addGrpcStreamingTestImports(context, methodConfig.getGrpcStreamingType());
MethodContext methodContext = context.asRequestMethodContext(method);
InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext(methodContext, new SymbolTable(), methodConfig.getRequiredFieldConfigs(), InitCodeOutputType.SingleObject, valueGenerator);
testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, ClientMethodType.CallableMethod));
} else if (methodConfig.isFlattening()) {
ClientMethodType clientMethodType;
if (methodConfig.isPageStreaming()) {
clientMethodType = ClientMethodType.PagedFlattenedMethod;
} else if (methodConfig.isLongRunningOperation()) {
clientMethodType = ClientMethodType.AsyncOperationFlattenedMethod;
} else {
clientMethodType = ClientMethodType.FlattenedMethod;
}
for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
MethodContext methodContext = context.asFlattenedMethodContext(method, flatteningGroup);
InitCodeContext initCodeContext = initCodeTransformer.createRequestInitCodeContext(methodContext, new SymbolTable(), flatteningGroup.getFlattenedFieldConfigs().values(), InitCodeOutputType.FieldList, valueGenerator);
testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType));
}
} else {
// TODO: Add support of non-flattening method
// Github issue: https://github.com/googleapis/toolkit/issues/393
System.err.println("Non-flattening method test is not supported yet for " + method.getSimpleName());
}
}
return testCaseViews;
}
use of com.google.api.codegen.util.SymbolTable in project toolkit by googleapis.
the class NodeJSGapicSurfaceTestTransformer method createTestCaseViews.
private List<TestCaseView> createTestCaseViews(GapicInterfaceContext context) {
ArrayList<TestCaseView> testCaseViews = new ArrayList<>();
SymbolTable testNameTable = new SymbolTable();
for (MethodModel method : context.getSupportedMethods()) {
GapicMethodContext methodContext = context.asRequestMethodContext(method);
if (methodContext.getMethodConfig().getGrpcStreamingType() == GrpcStreamingType.ClientStreaming) {
// Issue: https://github.com/googleapis/toolkit/issues/946
continue;
}
Iterable<FieldConfig> fieldConfigs = methodContext.getMethodConfig().getRequiredFieldConfigs();
InitCodeContext initCodeContext = InitCodeContext.newBuilder().initObjectType(methodContext.getMethodModel().getInputType()).suggestedName(Name.from("request")).initFieldConfigStrings(methodContext.getMethodConfig().getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(methodContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).outputType(InitCodeOutputType.SingleObject).fieldConfigMap(FieldConfig.toFieldConfigMap(fieldConfigs)).valueGenerator(valueGenerator).build();
testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, getMethodType(methodContext.getMethodConfig())));
}
return testCaseViews;
}
use of com.google.api.codegen.util.SymbolTable in project toolkit by googleapis.
the class PhpGapicSurfaceTestTransformer method createTestCaseViews.
private List<TestCaseView> createTestCaseViews(GapicInterfaceContext context) {
ArrayList<TestCaseView> testCaseViews = new ArrayList<>();
SymbolTable testNameTable = new SymbolTable();
for (MethodModel method : context.getSupportedMethods()) {
GapicMethodContext methodContext = context.asRequestMethodContext(method);
if (methodContext.getMethodConfig().getGrpcStreamingType() == GrpcStreamingType.ClientStreaming) {
// Issue: https://github.com/googleapis/toolkit/issues/946
continue;
}
InitCodeOutputType initCodeOutputType = InitCodeOutputType.FieldList;
if (methodContext.getMethodConfig().getGrpcStreamingType() == GrpcStreamingType.BidiStreaming) {
initCodeOutputType = InitCodeOutputType.SingleObject;
}
ClientMethodType clientMethodType = ClientMethodType.OptionalArrayMethod;
if (methodContext.getMethodConfig().isLongRunningOperation()) {
clientMethodType = ClientMethodType.OperationOptionalArrayMethod;
} else if (methodContext.getMethodConfig().isPageStreaming()) {
clientMethodType = ClientMethodType.PagedOptionalArrayMethod;
}
Iterable<FieldConfig> fieldConfigs = methodContext.getMethodConfig().getRequiredFieldConfigs();
InitCodeContext initCodeContext = InitCodeContext.newBuilder().initObjectType(methodContext.getMethodModel().getInputType()).suggestedName(Name.from("request")).initFieldConfigStrings(methodContext.getMethodConfig().getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(methodContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).outputType(initCodeOutputType).fieldConfigMap(FieldConfig.toFieldConfigMap(fieldConfigs)).valueGenerator(valueGenerator).build();
testCaseViews.add(testCaseTransformer.createTestCaseView(methodContext, testNameTable, initCodeContext, clientMethodType));
}
return testCaseViews;
}
Aggregations