use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.
the class OutputTransformer method mapLoopView.
private OutputView.MapLoopView mapLoopView(ResponseStatementProto.LoopStatement loop, MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext) {
outputContext.mapSpecs().add(loop);
ScopeTable scope = outputContext.scopeTable();
String key = loop.getKey();
String value = loop.getValue();
OutputView.VariableView mapVar = accessor(new Scanner(loop.getMap()), methodContext, sampleContext, scope);
TypeModel keyType = mapVar.type().getMapKeyType();
TypeModel valueType = mapVar.type().getMapValueType();
String keyTypeName = methodContext.getTypeTable().getNicknameFor(keyType);
String valueTypeName = methodContext.getTypeTable().getNicknameFor(valueType);
if (!key.isEmpty()) {
assertIdentifierNotUsed(key, methodContext, sampleContext);
scope.put(key, keyType, keyTypeName);
}
if (!value.isEmpty()) {
assertIdentifierNotUsed(value, methodContext, sampleContext);
scope.put(value, valueType, valueTypeName);
}
return OutputView.MapLoopView.newBuilder().keyVariableName(methodContext.getNamer().localVarName(Name.anyLower(key))).keyType(keyTypeName).valueVariableName(methodContext.getNamer().localVarName(Name.anyLower(value))).valueType(valueTypeName).map(mapVar).body(loop.getBodyList().stream().map(body -> toView(body, methodContext, sampleContext, outputContext)).collect(ImmutableList.toImmutableList())).build();
}
use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.
the class ApiCallableTransformer method generateHttpFields.
private HttpMethodView generateHttpFields(MethodContext context) {
if (context.getProductConfig().getTransportProtocol().equals(TransportProtocol.HTTP)) {
if (context.getMethodModel() instanceof DiscoveryMethodModel) {
// This section is only for DiscoGapic and will be deleted once the generator stops
// ingesting Discovery files.
Method method = ((DiscoveryMethodModel) context.getMethodModel()).getDiscoMethod();
HttpMethodView.Builder httpMethodView = HttpMethodView.newBuilder();
httpMethodView.fullMethodName(method.id());
httpMethodView.httpMethod(method.httpMethod());
List<String> pathParams = new ArrayList<>(method.pathParams().keySet());
List<String> queryParams = new ArrayList<>(method.queryParams().keySet());
Collections.sort(pathParams);
Collections.sort(queryParams);
httpMethodView.pathParams(pathParams);
httpMethodView.queryParams(queryParams);
httpMethodView.pathTemplate(method.path());
// TODO(andrealin): handle multiple resource names.
DiscoGapicInterfaceConfig interfaceConfig = (DiscoGapicInterfaceConfig) context.getSurfaceInterfaceContext().getInterfaceConfig();
SingleResourceNameConfig nameConfig = interfaceConfig.methodToResourceNameMap().get(context.getMethodConfig());
httpMethodView.resourceNameTypeName(context.getNamer().publicClassName(DiscoGapicParser.getResourceNameName(nameConfig)));
// Find the field with the resource name config.
for (FieldConfig fieldConfig : context.getMethodConfig().getRequiredFieldConfigs()) {
if (fieldConfig.getResourceNameConfig() != null && fieldConfig.getResourceNameConfig().equals(nameConfig)) {
httpMethodView.resourceNameFieldName(context.getNamer().privateFieldName(Name.anyCamel(fieldConfig.getField().getNameAsParameter())));
}
}
return httpMethodView.build();
} else if (context.getMethodModel() instanceof ProtoMethodModel) {
com.google.api.tools.framework.model.Method method = ((ProtoMethodModel) context.getMethodModel()).getProtoMethod();
HttpAttribute httpAttr = method.getAttribute(HttpAttribute.KEY);
HttpMethodView.Builder httpMethodView = HttpMethodView.newBuilder();
httpMethodView.httpMethod(httpAttr.getMethodKind().toString());
httpMethodView.fullMethodName(httpAttr.getRestMethod().getFullName());
SurfaceNamer namer = context.getNamer();
httpMethodView.pathTemplate(httpAttr.getPath().stream().map(pathSegment -> normalizePathSegment(pathSegment.toString())).collect(Collectors.joining("/", "/", "")));
httpMethodView.pathParamSelectors(populateMethodSelectors(namer, httpAttr.getPathSelectors()));
httpMethodView.queryParamSelectors(populateMethodSelectors(namer, httpAttr.getParamSelectors()));
httpMethodView.bodySelectors(populateMethodSelectors(namer, httpAttr.getBodySelectors()));
return httpMethodView.build();
}
}
return null;
}
use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.
the class JavaDiscoGapicRequestToViewTransformer method getRequestObjectParams.
private RequestObjectParamView getRequestObjectParams(DiscoGapicInterfaceContext context, MethodModel method) {
MethodConfig methodConfig = context.getMethodConfig(method);
// Generate the ResourceName methods.
if (methodConfig.isFlattening()) {
for (FlatteningConfig flatteningGroup : methodConfig.getFlatteningConfigs()) {
DiscoGapicMethodContext defaultMethodContext = context.asRequestMethodContext(method);
MethodContext flattenedMethodContext = context.asFlattenedMethodContext(defaultMethodContext, flatteningGroup);
if (FlatteningConfig.hasAnyRepeatedResourceNameParameter(flatteningGroup)) {
flattenedMethodContext = flattenedMethodContext.withResourceNamesInSamplesOnly();
}
Iterable<FieldConfig> fieldConfigs = flattenedMethodContext.getFlatteningConfig().getFlattenedFieldConfigs().values();
for (FieldConfig fieldConfig : fieldConfigs) {
if (context.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
return resourceObjectTransformer.generateRequestObjectParam(flattenedMethodContext, fieldConfig);
}
}
}
}
return null;
}
use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.
the class RubyGapicSurfaceTestTransformer method createUnitTestCaseInitCodeContext.
private InitCodeContext createUnitTestCaseInitCodeContext(GapicInterfaceContext context, MethodModel method) {
MethodContext requestMethodContext = context.asRequestMethodContext(method);
MethodContext dynamicMethodContext = context.asRequestMethodContext(method);
MethodConfig methodConfig = requestMethodContext.getMethodConfig();
Collection<FieldConfig> fieldConfigs = methodConfig.getRequiredFieldConfigs();
InitCodeOutputType outputType = method.getRequestStreaming() ? InitCodeOutputType.SingleObject : InitCodeOutputType.FieldList;
return InitCodeContext.newBuilder().initObjectType(method.getInputType()).suggestedName(Name.from("request")).initFieldConfigStrings(methodConfig.getSampleCodeInitFields()).initValueConfigMap(InitCodeTransformer.createCollectionMap(dynamicMethodContext)).initFields(FieldConfig.toFieldTypeIterable(fieldConfigs)).outputType(outputType).fieldConfigMap(FieldConfig.toFieldConfigMap(fieldConfigs)).build();
}
use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.
the class RubyGapicSurfaceTestTransformer method createSmokeTestClassView.
private SmokeTestClassView createSmokeTestClassView(GapicInterfaceContext context) {
boolean packageHasMultipleServices = context.getApiModel().hasMultipleServices();
String outputPath = smokeTestPathMapper.getOutputPath(context.getInterfaceModel().getFullName(), context.getProductConfig());
SurfaceNamer namer = context.getNamer();
RubyPackageMetadataNamer metadataNamer = new RubyPackageMetadataNamer(context.getNamer().getPackageName());
String name = namer.getSmokeTestClassName(context.getInterfaceConfig());
MethodModel method = context.getInterfaceConfig().getSmokeTestConfig().getMethod();
MethodContext methodContext = context.asRequestMethodContext(method);
TestCaseTransformer testCaseTransformer = new TestCaseTransformer(valueProducer, packageHasMultipleServices);
FlatteningConfig flatteningGroup = testCaseTransformer.getSmokeTestFlatteningGroup(methodContext.getMethodConfig());
GapicMethodContext flattenedMethodContext = context.asFlattenedMethodContext(methodContext, flatteningGroup);
SmokeTestClassView.Builder testClass = SmokeTestClassView.newBuilder();
OptionalArrayMethodView apiMethodView = createSmokeTestCaseApiMethodView(flattenedMethodContext, packageHasMultipleServices);
testClass.apiSettingsClassName(namer.getApiSettingsClassName(context.getInterfaceConfig()));
testClass.apiClassName(namer.getApiWrapperClassName(context.getInterfaceConfig()));
testClass.name(name);
testClass.outputPath(namer.getSourceFilePath(outputPath, name));
testClass.templateFileName(SMOKE_TEST_TEMPLATE_FILE);
testClass.apiMethod(apiMethodView);
testClass.requireProjectId(testCaseTransformer.requireProjectIdInSmokeTest(apiMethodView.initCode(), context.getNamer()));
testClass.projectIdVariableName(metadataNamer.getProjectVariable(true));
testClass.apiVersion(packageConfig.apiVersion());
FileHeaderView fileHeader = fileHeaderTransformer.generateFileHeader(context.getProductConfig(), importSectionTransformer.generateSmokeTestImportSection(context), namer);
testClass.fileHeader(fileHeader);
return testClass.build();
}
Aggregations