use of com.google.api.codegen.viewmodel.SampleFunctionParameterView 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.viewmodel.SampleFunctionParameterView in project toolkit by googleapis.
the class SampleTransformer method paramDocLines.
/**
* Generate parameter descriptions in sample function documentation.
*/
private ImmutableList<List<String>> paramDocLines(MethodContext context, InitCodeView initCodeView) {
SurfaceNamer namer = context.getNamer();
ImmutableList.Builder<List<String>> builder = ImmutableList.builder();
for (SampleFunctionParameterView param : initCodeView.argDefaultParams()) {
if (param.description().isEmpty()) {
continue;
}
List<String> paramDoc = namer.getWrappedDocLines(namer.getParamDocText(param.identifier(), param.typeName(), param.description()), false);
builder.add(paramDoc);
}
return builder.build();
}
Aggregations