use of com.google.api.codegen.metacode.InitCodeNode in project toolkit by googleapis.
the class TestCaseTransformer method createMockResponseAdditionalSubTrees.
private List<InitCodeNode> createMockResponseAdditionalSubTrees(MethodContext context) {
List<InitCodeNode> additionalSubTrees = new ArrayList<>();
if (context.getMethodConfig().isPageStreaming()) {
// Initialize one resource element if it is page-streaming.
PageStreamingConfig config = context.getMethodConfig().getPageStreaming();
FieldModel field = config.getResourcesField();
InitCodeNode initCodeNode;
if (field.isRepeated()) {
if (field.isMap()) {
initCodeNode = InitCodeNode.create(field.getNameAsParameter());
} else {
initCodeNode = InitCodeNode.createSingletonList(config.getResourcesFieldName());
}
} else {
initCodeNode = InitCodeNode.create(field.getNameAsParameter());
}
if (config.getResourcesField().isMap()) {
initCodeNode = InitCodeNode.createWithChildren(config.getResourcesField().getNameAsParameter(), MapInitLine, initCodeNode);
}
additionalSubTrees.add(initCodeNode);
// Set the initial value of the page token to empty, in order to indicate that no more pages
// are available
String responseTokenName = config.getResponseTokenField().getSimpleName();
additionalSubTrees.add(InitCodeNode.createWithValue(responseTokenName, InitValueConfig.createWithValue(InitValue.createLiteral(""))));
}
if (context.getMethodConfig().isBatching()) {
// Initialize one batching element if it is batching.
BatchingConfig config = context.getMethodConfig().getBatching();
if (config.getSubresponseField() != null) {
String subResponseFieldName = config.getSubresponseField().getSimpleName();
additionalSubTrees.add(InitCodeNode.createSingletonList(subResponseFieldName));
}
}
if (context.getMethodConfig().isGrpcStreaming()) {
GrpcStreamingConfig streamingConfig = context.getMethodConfig().getGrpcStreaming();
if (streamingConfig.hasResourceField()) {
String resourceFieldName = streamingConfig.getResourcesField().getSimpleName();
additionalSubTrees.add(InitCodeNode.createSingletonList(resourceFieldName));
}
}
return additionalSubTrees;
}
use of com.google.api.codegen.metacode.InitCodeNode in project toolkit by googleapis.
the class InitCodeTransformer method getFieldSettings.
private List<FieldSettingView> getFieldSettings(MethodContext context, Iterable<InitCodeNode> childItems) {
SurfaceNamer namer = context.getNamer();
List<FieldSettingView> allSettings = new ArrayList<>();
for (InitCodeNode item : childItems) {
FieldSettingView.Builder fieldSetting = FieldSettingView.newBuilder();
FieldConfig fieldConfig = item.getFieldConfig();
if (context.getFeatureConfig().useResourceNameProtoAccessor(fieldConfig)) {
fieldSetting.fieldSetFunction(namer.getResourceNameFieldSetFunctionName(fieldConfig.getMessageFieldConfig()));
} else {
fieldSetting.fieldSetFunction(namer.getFieldSetFunctionName(item.getType(), Name.anyLower(item.getVarName())));
}
fieldSetting.fieldAddFunction(namer.getFieldAddFunctionName(item.getType(), Name.anyLower(item.getVarName())));
fieldSetting.fieldGetFunction(namer.getFieldGetFunctionName(item.getType(), Name.anyLower(item.getVarName())));
fieldSetting.identifier(getVariableName(context, item));
fieldSetting.initCodeLine(generateSurfaceInitCodeLine(context, item, allSettings.isEmpty(), false));
fieldSetting.fieldName(context.getNamer().publicFieldName(Name.anyLower(item.getVarName())));
fieldSetting.isMap(item.getType().isMap());
fieldSetting.isArray(!item.getType().isMap() && item.getType().isRepeated());
fieldSetting.elementTypeName(context.getTypeTable().getFullNameFor(item.getType()));
if (item.getOneofConfig() != null) {
fieldSetting.oneofConfig(OneofConfigView.newBuilder().groupName(namer.publicFieldName(item.getOneofConfig().groupName())).variantType(namer.getOneofVariantTypeName(item.getOneofConfig())).build());
}
fieldSetting.required(isRequired(fieldConfig, context));
String formatMethodName = "";
String transformParamFunctionName = "";
// If resource name converters should only be used in the sample, we need to convert the
// resource name to a string before passing it or setting it on the next thing
boolean needsConversion = context.getFeatureConfig().useResourceNameConvertersInSampleOnly(context, fieldConfig);
// flattened method implementation when setting fields on the proto object).
if (context.getFeatureConfig().useResourceNameConverters(fieldConfig) && !context.isFlattenedMethodContext()) {
needsConversion = true;
}
if (needsConversion) {
if (fieldConfig.getField().isRepeated()) {
// TODO (https://github.com/googleapis/toolkit/issues/1806) support repeated one-ofs
transformParamFunctionName = namer.getResourceTypeFormatListMethodName(context.getTypeTable(), fieldConfig);
} else {
formatMethodName = namer.getResourceNameFormatMethodName();
}
}
fieldSetting.transformParamFunctionName(transformParamFunctionName);
fieldSetting.formatMethodName(formatMethodName);
allSettings.add(fieldSetting.build());
}
return allSettings;
}
use of com.google.api.codegen.metacode.InitCodeNode in project toolkit by googleapis.
the class InitCodeTransformer method buildInitCodeView.
/**
* Transform {@code InitCodeNode}s into {@code InitCodeView}.
*
* @param orderedItems These nodes are converted into request-initialization code. It contains all
* initializations regardless of whether they are parameters to the sample function. The
* initialization is "shallow": children nodes are not initialized. If children nodes should
* also be initialized, callers must also include them in the list.
* @param libArguments Used by samples for flattened client lib methods. These nodes contain
* values that become arguments to the method.
* @param sampleFuncParams Subset of {@code orderedItems} containing only items that are function
* parameters. Unlike {@code orderedItems}, the {@code sampleFuncParams} are "deep". The init
* code for these nodes and their children are commented out so that they don't clobber the
* function arguments.
*/
private InitCodeView buildInitCodeView(MethodContext context, List<InitCodeNode> orderedItems, List<InitCodeNode> libArguments, List<InitCodeNode> sampleFuncParams) {
ImportTypeTable typeTable = context.getTypeTable();
SurfaceNamer namer = context.getNamer();
// Initialize the type table with the apiClassName since each sample will be using the
// apiClass.
typeTable.getAndSaveNicknameFor(namer.getFullyQualifiedApiWrapperClassName(context.getInterfaceConfig()));
List<FieldSettingView> fieldSettings = getFieldSettings(context, libArguments);
List<FieldSettingView> optionalFieldSettings = fieldSettings.stream().filter(f -> !f.required()).collect(Collectors.toList());
List<FieldSettingView> requiredFieldSettings = fieldSettings.stream().filter(FieldSettingView::required).collect(Collectors.toList());
List<SampleFunctionParameterView> argDefaultParams = new ArrayList<>();
List<InitCodeLineView> argDefaultLines = new ArrayList<>();
for (InitCodeNode param : sampleFuncParams) {
List<InitCodeNode> paramInits = param.listInInitializationOrder();
argDefaultLines.addAll(generateSurfaceInitCodeLines(context, paramInits));
// The param itself is always at the end.
InitCodeLineView initLine = argDefaultLines.get(argDefaultLines.size() - 1);
checkArgument(initLine.lineType() == InitCodeLineType.SimpleInitLine, "Standalone samples only support primitive types for CLI arguments for now.");
SimpleInitCodeLineView simpleInitLine = (SimpleInitCodeLineView) initLine;
argDefaultParams.add(SampleFunctionParameterView.newBuilder().initValue(simpleInitLine.initValue()).identifier(simpleInitLine.identifier()).upperCamelIdentifier(param.getIdentifier().toUpperCamel()).typeName(simpleInitLine.typeName()).isEnum(simpleInitLine.isEnum()).cliFlagName(param.getIdentifier().toLowerUnderscore()).cliFlagDefaultValue(getCliFlagDefaultValue(param)).description(param.getDescription()).build());
// Since we're going to write the inits for the params here,
// remove so we don't init twice.
orderedItems.removeAll(paramInits);
}
return InitCodeView.newBuilder().argDefaultLines(argDefaultLines).argDefaultParams(argDefaultParams).lines(generateSurfaceInitCodeLines(context, orderedItems)).topLevelLines(generateSurfaceInitCodeLines(context, libArguments)).fieldSettings(fieldSettings).optionalFieldSettings(optionalFieldSettings).requiredFieldSettings(requiredFieldSettings).importSection(importSectionTransformer.generateImportSection(context, orderedItems)).topLevelIndexFileImportName(namer.getTopLevelIndexFileImportName()).build();
}
use of com.google.api.codegen.metacode.InitCodeNode in project toolkit by googleapis.
the class InitCodeTransformer method generateListInitCodeLine.
private InitCodeLineView generateListInitCodeLine(MethodContext context, InitCodeNode item) {
ListInitCodeLineView.Builder surfaceLine = ListInitCodeLineView.newBuilder();
FieldConfig fieldConfig = item.getFieldConfig();
SurfaceNamer namer = context.getNamer();
ImportTypeTable typeTable = context.getTypeTable();
surfaceLine.lineType(InitCodeLineType.ListInitLine);
surfaceLine.identifier(namer.localVarName(item.getIdentifier()));
if (context.getFeatureConfig().useResourceNameFormatOptionInSample(context, fieldConfig)) {
surfaceLine.elementTypeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig));
} else {
surfaceLine.elementTypeName(typeTable.getAndSaveNicknameForElementType(item.getType().makeOptional()));
}
List<String> entries = new ArrayList<>();
List<InitCodeLineView> elements = new ArrayList<>();
for (InitCodeNode child : item.getChildren().values()) {
entries.add(namer.localVarName(child.getIdentifier()));
elements.add(generateSurfaceInitCodeLine(context, child, elements.isEmpty(), false));
}
surfaceLine.elementIdentifiers(entries);
surfaceLine.elements(elements);
surfaceLine.descriptions(context.getNamer().getWrappedDocLines(item.getDescription(), false));
return surfaceLine.build();
}
use of com.google.api.codegen.metacode.InitCodeNode in project toolkit by googleapis.
the class InitCodeTransformer method sampleFuncParams.
/**
* Returns all the nodes to be rendered as sample function parameters.
*
* <p>If path is:
* <li>a normal node, returns that node.
* <li>a ReadFile node, returns the child node of that node.
* <li>a resource path, returns the child node whose key equals the entity name in the path.
*
* @param paramConfigMap the sample parameter configurations derived from {@code InitCodeContext}
*/
private List<InitCodeNode> sampleFuncParams(InitCodeNode root, List<String> paths, Map<String, SampleParameterConfig> paramConfigMap) {
List<InitCodeNode> params = new ArrayList<>();
for (String path : paths) {
Scanner scanner = new Scanner(path);
InitCodeNode node = FieldStructureParser.parsePath(root, scanner);
int token = scanner.lastToken();
if (token == '%') {
scanner.scan();
node = node.getChildren().get(scanner.tokenStr());
node.setDescription(paramConfigMap.get(path).comment());
params.add(node);
} else if (node.getLineType() == InitCodeLineType.ReadFileInitLine) {
node = node.getChildren().get(InitCodeNode.FILE_NAME_KEY);
node.setDescription(paramConfigMap.get(path).comment());
params.add(node);
} else {
node.setDescription(paramConfigMap.get(path).comment());
params.add(node);
}
}
return params;
}
Aggregations