Search in sources :

Example 1 with InitCodeLineView

use of com.google.api.codegen.viewmodel.InitCodeLineView 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();
}
Also used : SimpleInitValueView(com.google.api.codegen.viewmodel.SimpleInitValueView) TypeRef(com.google.api.tools.framework.model.TypeRef) EscaperFactory(com.google.api.codegen.util.EscaperFactory) Arrays(java.util.Arrays) ProtoFile(com.google.api.tools.framework.model.ProtoFile) InitValueView(com.google.api.codegen.viewmodel.InitValueView) PathTemplate(com.google.api.pathtemplate.PathTemplate) RepeatedResourceNameInitValueView(com.google.api.codegen.viewmodel.RepeatedResourceNameInitValueView) StructureInitCodeLineView(com.google.api.codegen.viewmodel.StructureInitCodeLineView) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) FieldSettingView(com.google.api.codegen.viewmodel.FieldSettingView) HashMultimap(com.google.common.collect.HashMultimap) MethodContext(com.google.api.codegen.config.MethodContext) InitValue(com.google.api.codegen.metacode.InitValue) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext) Map(java.util.Map) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) ListInitCodeLineView(com.google.api.codegen.viewmodel.ListInitCodeLineView) ResourceNameType(com.google.api.codegen.config.ResourceNameType) FieldStructureParser(com.google.api.codegen.metacode.FieldStructureParser) InitCodeView(com.google.api.codegen.viewmodel.InitCodeView) ResourceNameOneofInitValueView(com.google.api.codegen.viewmodel.ResourceNameOneofInitValueView) SimpleInitCodeLineView(com.google.api.codegen.viewmodel.SimpleInitCodeLineView) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) InitCodeNode(com.google.api.codegen.metacode.InitCodeNode) Collectors(java.util.stream.Collectors) ProtoTypeRef(com.google.api.codegen.config.ProtoTypeRef) InitValueConfig(com.google.api.codegen.metacode.InitValueConfig) Scanner(com.google.api.codegen.util.Scanner) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Name(com.google.api.codegen.util.Name) ResourceNameInitValueView(com.google.api.codegen.viewmodel.ResourceNameInitValueView) List(java.util.List) InitCodeLineView(com.google.api.codegen.viewmodel.InitCodeLineView) InterfaceModel(com.google.api.codegen.config.InterfaceModel) Optional(java.util.Optional) ReadFileInitCodeLineView(com.google.api.codegen.viewmodel.ReadFileInitCodeLineView) ResourceNameOneofConfig(com.google.api.codegen.config.ResourceNameOneofConfig) TestValueGenerator(com.google.api.codegen.util.testing.TestValueGenerator) Iterables(com.google.common.collect.Iterables) FormattedInitValueView(com.google.api.codegen.viewmodel.FormattedInitValueView) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) InitCodeLineType(com.google.api.codegen.metacode.InitCodeLineType) SampleParameterConfig(com.google.api.codegen.config.SampleParameterConfig) OneofConfigView(com.google.api.codegen.viewmodel.OneofConfigView) ResourceNamePatternConfig(com.google.api.codegen.config.ResourceNamePatternConfig) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleFunctionParameterView(com.google.api.codegen.viewmodel.SampleFunctionParameterView) SymbolTable(com.google.api.codegen.util.SymbolTable) ResourceNameConfig(com.google.api.codegen.config.ResourceNameConfig) TypeModel(com.google.api.codegen.config.TypeModel) ProtoInterfaceModel(com.google.api.codegen.config.ProtoInterfaceModel) MapEntryView(com.google.api.codegen.viewmodel.MapEntryView) ClientTestAssertView(com.google.api.codegen.viewmodel.testing.ClientTestAssertView) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ArrayDeque(java.util.ArrayDeque) FieldConfig(com.google.api.codegen.config.FieldConfig) MapInitCodeLineView(com.google.api.codegen.viewmodel.MapInitCodeLineView) ArrayList(java.util.ArrayList) SampleFunctionParameterView(com.google.api.codegen.viewmodel.SampleFunctionParameterView) StructureInitCodeLineView(com.google.api.codegen.viewmodel.StructureInitCodeLineView) ListInitCodeLineView(com.google.api.codegen.viewmodel.ListInitCodeLineView) SimpleInitCodeLineView(com.google.api.codegen.viewmodel.SimpleInitCodeLineView) InitCodeLineView(com.google.api.codegen.viewmodel.InitCodeLineView) ReadFileInitCodeLineView(com.google.api.codegen.viewmodel.ReadFileInitCodeLineView) MapInitCodeLineView(com.google.api.codegen.viewmodel.MapInitCodeLineView) FieldSettingView(com.google.api.codegen.viewmodel.FieldSettingView) InitCodeNode(com.google.api.codegen.metacode.InitCodeNode) SimpleInitCodeLineView(com.google.api.codegen.viewmodel.SimpleInitCodeLineView)

Example 2 with InitCodeLineView

use of com.google.api.codegen.viewmodel.InitCodeLineView 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();
}
Also used : StructureInitCodeLineView(com.google.api.codegen.viewmodel.StructureInitCodeLineView) ListInitCodeLineView(com.google.api.codegen.viewmodel.ListInitCodeLineView) SimpleInitCodeLineView(com.google.api.codegen.viewmodel.SimpleInitCodeLineView) InitCodeLineView(com.google.api.codegen.viewmodel.InitCodeLineView) ReadFileInitCodeLineView(com.google.api.codegen.viewmodel.ReadFileInitCodeLineView) MapInitCodeLineView(com.google.api.codegen.viewmodel.MapInitCodeLineView) ListInitCodeLineView(com.google.api.codegen.viewmodel.ListInitCodeLineView) InitCodeNode(com.google.api.codegen.metacode.InitCodeNode) FieldConfig(com.google.api.codegen.config.FieldConfig) ArrayList(java.util.ArrayList)

Example 3 with InitCodeLineView

use of com.google.api.codegen.viewmodel.InitCodeLineView in project toolkit by googleapis.

the class InitCodeTransformer method generateSurfaceInitCodeLines.

private List<InitCodeLineView> generateSurfaceInitCodeLines(MethodContext context, Iterable<InitCodeNode> specItemNode) {
    boolean isFirstReadFileView = true;
    List<InitCodeLineView> surfaceLines = new ArrayList<>();
    for (InitCodeNode item : specItemNode) {
        surfaceLines.add(generateSurfaceInitCodeLine(context, item, surfaceLines.isEmpty(), isFirstReadFileView));
        isFirstReadFileView = isFirstReadFileView && item.getLineType() != InitCodeLineType.ReadFileInitLine;
    }
    return surfaceLines;
}
Also used : StructureInitCodeLineView(com.google.api.codegen.viewmodel.StructureInitCodeLineView) ListInitCodeLineView(com.google.api.codegen.viewmodel.ListInitCodeLineView) SimpleInitCodeLineView(com.google.api.codegen.viewmodel.SimpleInitCodeLineView) InitCodeLineView(com.google.api.codegen.viewmodel.InitCodeLineView) ReadFileInitCodeLineView(com.google.api.codegen.viewmodel.ReadFileInitCodeLineView) MapInitCodeLineView(com.google.api.codegen.viewmodel.MapInitCodeLineView) InitCodeNode(com.google.api.codegen.metacode.InitCodeNode) ArrayList(java.util.ArrayList)

Example 4 with InitCodeLineView

use of com.google.api.codegen.viewmodel.InitCodeLineView in project toolkit by googleapis.

the class TestCaseTransformer method requireProjectIdInSmokeTest.

private boolean requireProjectIdInSmokeTest(FieldSettingView settingsView, SurfaceNamer namer) {
    InitCodeLineView line = settingsView.initCodeLine();
    if (line.lineType() == InitCodeLineType.SimpleInitLine) {
        SimpleInitCodeLineView simpleLine = (SimpleInitCodeLineView) line;
        String projectVarName = namer.localVarReference(Name.from(InitFieldConfig.PROJECT_ID_VARIABLE_NAME));
        if (simpleLine.initValue() instanceof ResourceNameInitValueView) {
            ResourceNameInitValueView initValue = (ResourceNameInitValueView) simpleLine.initValue();
            return initValue.formatArgs().contains(projectVarName);
        } else if (simpleLine.initValue() instanceof ResourceNameOneofInitValueView) {
            ResourceNameOneofInitValueView initValue = (ResourceNameOneofInitValueView) simpleLine.initValue();
            ResourceNameInitValueView subValue = initValue.specificResourceNameView();
            return subValue == null ? initValue.formatArgs().contains(projectVarName) : subValue.formatArgs().contains(projectVarName);
        } else if (simpleLine.initValue() instanceof SimpleInitValueView) {
            SimpleInitValueView initValue = (SimpleInitValueView) simpleLine.initValue();
            return initValue.initialValue().equals(projectVarName);
        } else if (simpleLine.initValue() instanceof FormattedInitValueView) {
            FormattedInitValueView initValue = (FormattedInitValueView) simpleLine.initValue();
            return initValue.formatArgs().contains(projectVarName);
        }
    }
    return false;
}
Also used : ResourceNameInitValueView(com.google.api.codegen.viewmodel.ResourceNameInitValueView) SimpleInitCodeLineView(com.google.api.codegen.viewmodel.SimpleInitCodeLineView) InitCodeLineView(com.google.api.codegen.viewmodel.InitCodeLineView) SimpleInitCodeLineView(com.google.api.codegen.viewmodel.SimpleInitCodeLineView) SimpleInitValueView(com.google.api.codegen.viewmodel.SimpleInitValueView) FormattedInitValueView(com.google.api.codegen.viewmodel.FormattedInitValueView) ResourceNameOneofInitValueView(com.google.api.codegen.viewmodel.ResourceNameOneofInitValueView)

Aggregations

InitCodeLineView (com.google.api.codegen.viewmodel.InitCodeLineView)4 SimpleInitCodeLineView (com.google.api.codegen.viewmodel.SimpleInitCodeLineView)4 InitCodeNode (com.google.api.codegen.metacode.InitCodeNode)3 ListInitCodeLineView (com.google.api.codegen.viewmodel.ListInitCodeLineView)3 MapInitCodeLineView (com.google.api.codegen.viewmodel.MapInitCodeLineView)3 ReadFileInitCodeLineView (com.google.api.codegen.viewmodel.ReadFileInitCodeLineView)3 StructureInitCodeLineView (com.google.api.codegen.viewmodel.StructureInitCodeLineView)3 ArrayList (java.util.ArrayList)3 FieldConfig (com.google.api.codegen.config.FieldConfig)2 FormattedInitValueView (com.google.api.codegen.viewmodel.FormattedInitValueView)2 ResourceNameInitValueView (com.google.api.codegen.viewmodel.ResourceNameInitValueView)2 ResourceNameOneofInitValueView (com.google.api.codegen.viewmodel.ResourceNameOneofInitValueView)2 InterfaceModel (com.google.api.codegen.config.InterfaceModel)1 MethodContext (com.google.api.codegen.config.MethodContext)1 ProtoInterfaceModel (com.google.api.codegen.config.ProtoInterfaceModel)1 ProtoTypeRef (com.google.api.codegen.config.ProtoTypeRef)1 ResourceNameConfig (com.google.api.codegen.config.ResourceNameConfig)1 ResourceNameOneofConfig (com.google.api.codegen.config.ResourceNameOneofConfig)1 ResourceNamePatternConfig (com.google.api.codegen.config.ResourceNamePatternConfig)1 ResourceNameType (com.google.api.codegen.config.ResourceNameType)1