use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.
the class GoGapicSurfaceTransformer method generateApiMethods.
@VisibleForTesting
List<StaticLangApiMethodView> generateApiMethods(InterfaceContext context, Iterable<? extends MethodModel> methods) {
List<StaticLangApiMethodView> apiMethods = new ArrayList<>();
for (MethodModel method : methods) {
MethodConfig methodConfig = context.getMethodConfig(method);
MethodContext methodContext = context.asRequestMethodContext(method);
if (method.getRequestStreaming() || method.getResponseStreaming()) {
apiMethods.add(apiMethodTransformer.generateGrpcStreamingRequestObjectMethod(methodContext, null));
} else if (methodConfig.isPageStreaming()) {
apiMethods.add(apiMethodTransformer.generatePagedRequestObjectMethod(methodContext, null));
} else if (methodContext.isLongRunningMethodContext()) {
apiMethods.add(apiMethodTransformer.generateOperationRequestObjectMethod(methodContext, null));
} else {
apiMethods.add(apiMethodTransformer.generateRequestObjectMethod(methodContext, null));
}
}
return apiMethods;
}
use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.
the class CSharpGapicSnippetsTransformer method generateMethods.
private List<StaticLangApiMethodSnippetView> generateMethods(InterfaceContext context) {
List<StaticLangApiMethodSnippetView> methods = new ArrayList<>();
for (MethodModel method : csharpCommonTransformer.getSupportedMethods(context)) {
MethodConfig methodConfig = context.getMethodConfig(method);
MethodContext methodContext = context.asRequestMethodContext(method);
if (methodConfig.isGrpcStreaming()) {
methods.add(generateGrpcStreamingRequestMethod(methodContext));
} else if (methodContext.isLongRunningMethodContext()) {
if (methodConfig.isFlattening()) {
List<FlatteningConfig> flatteningGroups = FlatteningConfigs.getRepresentativeFlatteningConfigs(methodConfig.getFlatteningConfigs());
boolean requiresNameSuffix = flatteningGroups.size() > 1;
for (int i = 0; i < flatteningGroups.size(); i++) {
FlatteningConfig flatteningGroup = flatteningGroups.get(i);
String nameSuffix = requiresNameSuffix ? Integer.toString(i + 1) : "";
MethodContext methodContextFlat = context.asFlattenedMethodContext(methodContext, flatteningGroup);
methods.add(generateOperationFlattenedAsyncMethod(methodContextFlat, nameSuffix));
methods.add(generateOperationFlattenedMethod(methodContextFlat, nameSuffix));
}
}
methods.add(generateOperationRequestAsyncMethod(methodContext));
methods.add(generateOperationRequestMethod(methodContext));
} else if (methodConfig.isPageStreaming()) {
if (methodConfig.isFlattening()) {
List<FlatteningConfig> flatteningGroups = FlatteningConfigs.getRepresentativeFlatteningConfigs(methodConfig.getFlatteningConfigs());
// Find flattenings that have ambiguous parameters, and mark them to use named arguments.
// Ambiguity occurs in a page-stream flattening that has one or two extra string
// parameters (that are not resource-names) compared to any other flattening of this same
// method.
// Create a string for each flattening, encoding which parameters are strings and
// not-strings. Each character in the string refers to a parameter. Each string refers
// to a flattening.
String[] stringParams = flatteningGroups.stream().map(flat -> flat.getFlattenedFieldConfigs().values().stream().map(field -> field.getField().getType().isStringType() && field.getResourceNameConfig() == null ? 's' : '.').collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString()).toArray(String[]::new);
// Array of which flattenings need to use named arguments.
// Each array entry refers to the correspondingly indexed flattening.
Boolean[] requiresNamedParameters = Arrays.stream(stringParams).map(a -> Arrays.stream(stringParams).anyMatch(b -> a.startsWith(b + "s") || a.startsWith(b + "ss"))).toArray(Boolean[]::new);
boolean requiresNameSuffix = flatteningGroups.size() > 1;
// Build method list.
for (int i = 0; i < flatteningGroups.size(); i++) {
FlatteningConfig flatteningGroup = flatteningGroups.get(i);
String nameSuffix = requiresNameSuffix ? Integer.toString(i + 1) : "";
MethodContext methodContextFlat = context.asFlattenedMethodContext(methodContext, flatteningGroup);
methods.add(generatePagedFlattenedAsyncMethod(methodContextFlat, nameSuffix, requiresNamedParameters[i]));
methods.add(generatePagedFlattenedMethod(methodContextFlat, nameSuffix, requiresNamedParameters[i]));
}
}
methods.add(generatePagedRequestAsyncMethod(methodContext));
methods.add(generatePagedRequestMethod(methodContext));
} else {
if (methodConfig.isFlattening()) {
List<FlatteningConfig> flatteningGroups = FlatteningConfigs.getRepresentativeFlatteningConfigs(methodConfig.getFlatteningConfigs());
boolean requiresNameSuffix = flatteningGroups.size() > 1;
for (int i = 0; i < flatteningGroups.size(); i++) {
FlatteningConfig flatteningGroup = flatteningGroups.get(i);
String nameSuffix = requiresNameSuffix ? Integer.toString(i + 1) : "";
MethodContext methodContextFlat = context.asFlattenedMethodContext(methodContext, flatteningGroup);
methods.add(generateFlattenedAsyncMethod(methodContextFlat, nameSuffix));
methods.add(generateFlattenedMethod(methodContextFlat, nameSuffix));
}
}
methods.add(generateRequestAsyncMethod(methodContext));
methods.add(generateRequestMethod(methodContext));
}
}
return methods;
}
use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.
the class JavaSurfaceTestTransformer method createSmokeTestClassViewBuilder.
/**
* Package-private
*
* <p>A helper method that creates a partially initialized builder that can be customized and
* build the smoke test class view later.
*/
SmokeTestClassView.Builder createSmokeTestClassViewBuilder(InterfaceContext context) {
addSmokeTestImports(context);
MethodModel method = context.getInterfaceConfig().getSmokeTestConfig().getMethod();
MethodContext defaultMethodContext = context.asRequestMethodContext(method);
SurfaceNamer namer = context.getNamer();
FlatteningConfig flatteningGroup = testCaseTransformer.getSmokeTestFlatteningGroup(context.getMethodConfig(method));
MethodContext methodContext = context.asFlattenedMethodContext(defaultMethodContext, flatteningGroup);
if (FlatteningConfig.hasAnyRepeatedResourceNameParameter(flatteningGroup)) {
methodContext = methodContext.withResourceNamesInSamplesOnly();
}
SmokeTestClassView.Builder testClass = SmokeTestClassView.newBuilder();
StaticLangApiMethodView apiMethodView = createSmokeTestCaseApiMethodView(methodContext);
testClass.apiSettingsClassName(namer.getApiSettingsClassName(context.getInterfaceConfig()));
testClass.apiClassName(namer.getApiWrapperClassName(context.getInterfaceConfig()));
testClass.templateFileName(SMOKE_TEST_TEMPLATE_FILE);
testClass.apiMethod(apiMethodView);
testClass.requireProjectId(testCaseTransformer.requireProjectIdInSmokeTest(apiMethodView.initCode(), context.getNamer()));
// Imports must be done as the last step to catch all imports.
FileHeaderView fileHeader = fileHeaderTransformer.generateFileHeader(context);
testClass.fileHeader(fileHeader);
return testClass;
}
use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.
the class CSharpApiMethodTransformer method generateApiMethods.
@Override
public List<StaticLangApiMethodView> generateApiMethods(InterfaceContext interfaceContext) {
Preconditions.checkArgument(interfaceContext instanceof GapicInterfaceContext, "Only applicable for protobuf-based API in CSharp.");
GapicInterfaceContext context = (GapicInterfaceContext) interfaceContext;
List<ParamWithSimpleDoc> pagedMethodAdditionalParams = new ImmutableList.Builder<ParamWithSimpleDoc>().addAll(csharpCommonTransformer.pagedMethodAdditionalParams()).addAll(csharpCommonTransformer.callSettingsParam()).build();
List<StaticLangApiMethodView> apiMethods = new ArrayList<>();
// gRPC streaming methods.
for (MethodModel method : csharpCommonTransformer.getSupportedMethods(context)) {
MethodConfig methodConfig = context.getMethodConfig(method);
MethodContext requestMethodContext = context.asRequestMethodContext(method);
if (methodConfig.isGrpcStreaming()) {
if (methodConfig.isFlattening()) {
for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
MethodContext methodContext = context.asFlattenedMethodContext(requestMethodContext, flatteningGroup).withCallingForms(ImmutableList.of(CallingForm.FlattenedStreamingBidi, CallingForm.FlattenedStreamingServer));
apiMethods.add(generateGrpcStreamingFlattenedMethod(methodContext, csharpCommonTransformer.callSettingsParam(), null));
}
}
requestMethodContext = requestMethodContext.withCallingForms(ImmutableList.of(CallingForm.RequestStreamingBidi, CallingForm.RequestStreamingServer));
apiMethods.add(generateGrpcStreamingRequestObjectMethod(requestMethodContext, null));
} else if (requestMethodContext.isLongRunningMethodContext()) {
// LRO methods.
if (methodConfig.isFlattening()) {
for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
GapicMethodContext methodContext = context.asFlattenedMethodContext(requestMethodContext, flatteningGroup);
apiMethods.addAll(generateFlattenedLroMethods(methodContext));
}
}
apiMethods.add(generateAsyncOperationRequestObjectMethod(requestMethodContext.withCallingForms(ImmutableList.of(CallingForm.LongRunningRequestAsyncPollUntilComplete)), csharpCommonTransformer.callSettingsParam(), true, null));
apiMethods.add(generateOperationRequestObjectMethod(requestMethodContext.withCallingForms(ImmutableList.of(CallingForm.LongRunningRequestPollUntilComplete)), csharpCommonTransformer.callSettingsParam(), null));
} else if (methodConfig.isPageStreaming()) {
// Paged streaming methods.
if (methodConfig.isFlattening()) {
for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
MethodContext methodContext = context.asFlattenedMethodContext(requestMethodContext, flatteningGroup);
apiMethods.addAll(generatePageStreamingFlattenedMethods(methodContext, pagedMethodAdditionalParams));
}
}
apiMethods.add(generatePagedRequestObjectAsyncMethod(requestMethodContext.withCallingForms(ImmutableList.of(CallingForm.RequestAsyncPaged, CallingForm.RequestAsyncPagedAll, CallingForm.RequestAsyncPagedPageSize)), csharpCommonTransformer.callSettingsParam(), null));
apiMethods.add(generatePagedRequestObjectMethod(requestMethodContext.withCallingForms(ImmutableList.of(CallingForm.RequestPaged, CallingForm.RequestPagedAll, CallingForm.RequestPagedPageSize)), csharpCommonTransformer.callSettingsParam(), null));
} else {
// Unary methods.
if (methodConfig.isFlattening()) {
for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
GapicMethodContext methodContext = context.asFlattenedMethodContext(requestMethodContext, flatteningGroup);
apiMethods.addAll(generateNormalFlattenedMethods(methodContext));
}
}
apiMethods.add(generateRequestObjectAsyncMethod(requestMethodContext.withCallingForms(Collections.singletonList(CallingForm.RequestAsync)), csharpCommonTransformer.callSettingsParam(), ClientMethodType.AsyncRequestObjectCallSettingsMethod, null));
apiMethods.add(generateRequestObjectAsyncMethod(requestMethodContext.withCallingForms(Collections.singletonList(CallingForm.RequestAsync)), csharpCommonTransformer.cancellationTokenParam(), ClientMethodType.AsyncRequestObjectCancellationMethod, null));
apiMethods.add(generateRequestObjectMethod(requestMethodContext.withCallingForms(Collections.singletonList(CallingForm.Request)), csharpCommonTransformer.callSettingsParam(), null));
}
}
return apiMethods;
}
use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.
the class StaticLangGapicSamplesTransformer method getSampleContexts.
public List<SampleContext> getSampleContexts(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig) {
SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
ImmutableTable<String, String, ImmutableList<SampleConfig>> sampleConfigTable = productConfig.getSampleConfigTable();
ImmutableList.Builder<SampleContext> sampleContexts = ImmutableList.builder();
// Loop through sample configs and and map each sample ID to its matching calling forms.
// We need this information when we need to create, in a language-specific way, unique
// sample ids when one sample id has multiple matching calling forms
Map<String, List<CallingForm>> configsAndMatchingForms = new HashMap<>();
for (InterfaceContext interfaceContext : interfaceContexts) {
for (MethodModel method : interfaceContext.getSupportedMethods()) {
MethodContext methodContext = interfaceContext.asRequestMethodContext(method);
String interfaceName = interfaceContext.getInterfaceConfig().getInterfaceModel().getFullName();
String methodName = method.getSimpleName();
List<SampleConfig> sampleConfigs = sampleConfigTable.get(interfaceName, methodName);
sampleConfigs = firstNonNull(sampleConfigs, ImmutableList.<SampleConfig>of());
for (SampleConfig config : sampleConfigs) {
List<CallingForm> allMatchingCallingForms = namer.getMatchingCallingForms(methodContext, config.callingPattern());
List<CallingForm> existingForms = configsAndMatchingForms.get(config.id());
if (existingForms == null) {
existingForms = new ArrayList<>();
configsAndMatchingForms.put(config.id(), existingForms);
}
existingForms.addAll(allMatchingCallingForms);
}
}
}
SampleFileRegistry registry = new SampleFileRegistry(namer, configsAndMatchingForms);
for (InterfaceContext interfaceContext : interfaceContexts) {
for (MethodModel method : interfaceContext.getSupportedMethods()) {
MethodContext methodContext = interfaceContext.asRequestMethodContext(method);
String interfaceName = interfaceContext.getInterfaceConfig().getInterfaceModel().getFullName();
String methodName = method.getSimpleName();
ImmutableList<SampleConfig> sampleConfigs = sampleConfigTable.get(interfaceName, methodName);
if (sampleConfigs == null) {
continue;
}
for (SampleConfig sampleConfig : sampleConfigs) {
List<CallingForm> allMatchingCallingForms = configsAndMatchingForms.get(sampleConfig.id());
for (CallingForm form : allMatchingCallingForms) {
InitCodeOutputType initCodeOutputType = InitCodeOutputType.SingleObject;
// In Java and C#, we need to handle flattening.
if (CallingForm.isFlattened(form)) {
if (!methodContext.getMethodConfig().isFlattening()) {
continue;
}
initCodeOutputType = InitCodeOutputType.FieldList;
methodContext = methodContext.getSurfaceInterfaceContext().asFlattenedMethodContext(methodContext, methodContext.getMethodConfig().getFlatteningConfigs().get(0));
}
SampleContext sampleContext = SampleContext.newBuilder().uniqueSampleId(registry.getUniqueSampleId(sampleConfig, form)).sampleType(SampleSpec.SampleType.STANDALONE).callingForm(form).clientMethodType(fromCallingForm(form)).sampleConfig(sampleConfig).initCodeOutputType(initCodeOutputType).methodContext(methodContext).build();
sampleContexts.add(sampleContext);
}
}
}
}
return sampleContexts.build();
}
Aggregations