Search in sources :

Example 6 with MethodContext

use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.

the class JavaApiMethodTransformer method generateLongRunningMethods.

private List<StaticLangApiMethodView> generateLongRunningMethods(MethodContext methodContext, @Nullable SampleContext sampleContext) {
    List<StaticLangApiMethodView> apiMethods = new ArrayList<>();
    InterfaceContext interfaceContext = methodContext.getSurfaceInterfaceContext();
    interfaceContext.getImportTypeTable().saveNicknameFor("com.google.api.gax.rpc.OperationCallable");
    MethodConfig methodConfig = methodContext.getMethodConfig();
    if (methodConfig.isFlattening()) {
        List<FlatteningConfig> flatteningConfigs = FlatteningConfig.withRepeatedResourceInSampleOnly(methodConfig.getFlatteningConfigs());
        for (FlatteningConfig flatteningGroup : flatteningConfigs) {
            MethodContext flattenedMethodContext = interfaceContext.asFlattenedMethodContext(methodContext, flatteningGroup).withCallingForms(Collections.singletonList(CallingForm.LongRunningFlattenedAsync));
            apiMethods.add(generateAsyncOperationFlattenedMethod(flattenedMethodContext, sampleContext));
        }
    }
    apiMethods.add(generateAsyncOperationRequestObjectMethod(methodContext.withCallingForms(Collections.singletonList(CallingForm.LongRunningRequestAsync)), sampleContext));
    apiMethods.add(generateOperationCallableMethod(methodContext.withCallingForms(Collections.singletonList(CallingForm.LongRunningCallable)), sampleContext));
    apiMethods.add(generateCallableMethod(methodContext.withCallingForms(Collections.singletonList(CallingForm.Callable)), sampleContext));
    return apiMethods;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.config.MethodContext) InterfaceContext(com.google.api.codegen.config.InterfaceContext) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig)

Example 7 with MethodContext

use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.

the class JavaApiMethodTransformer method generateUnaryMethods.

private List<StaticLangApiMethodView> generateUnaryMethods(MethodContext methodContext, @Nullable SampleContext sampleContext) {
    List<StaticLangApiMethodView> apiMethods = new ArrayList<>();
    InterfaceContext interfaceContext = methodContext.getSurfaceInterfaceContext();
    MethodConfig methodConfig = methodContext.getMethodConfig();
    if (methodConfig.isFlattening()) {
        List<FlatteningConfig> flatteningConfigs = FlatteningConfig.withRepeatedResourceInSampleOnly(methodConfig.getFlatteningConfigs());
        for (FlatteningConfig flatteningGroup : flatteningConfigs) {
            MethodContext flattenedMethodContext = interfaceContext.asFlattenedMethodContext(methodContext, flatteningGroup);
            apiMethods.add(generateFlattenedMethod(flattenedMethodContext.withCallingForms(Collections.singletonList(CallingForm.Flattened)), sampleContext));
        }
    }
    apiMethods.add(generateRequestObjectMethod(methodContext.withCallingForms(Collections.singletonList(CallingForm.Request)), sampleContext));
    apiMethods.add(generateCallableMethod(methodContext.withCallingForms(Collections.singletonList(CallingForm.Callable)), sampleContext));
    return apiMethods;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.config.MethodContext) InterfaceContext(com.google.api.codegen.config.InterfaceContext) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig)

Example 8 with MethodContext

use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.

the class JavaApiMethodTransformer method generatePagedStreamingMethods.

private List<StaticLangApiMethodView> generatePagedStreamingMethods(MethodContext methodContext, @Nullable SampleContext sampleContext) {
    List<StaticLangApiMethodView> apiMethods = new ArrayList<>();
    InterfaceContext interfaceContext = methodContext.getSurfaceInterfaceContext();
    MethodConfig methodConfig = methodContext.getMethodConfig();
    if (methodConfig.isFlattening()) {
        List<FlatteningConfig> flatteningConfigs = FlatteningConfig.withRepeatedResourceInSampleOnly(methodConfig.getFlatteningConfigs());
        for (FlatteningConfig flatteningGroup : flatteningConfigs) {
            MethodContext flattenedMethodContext = interfaceContext.asFlattenedMethodContext(methodContext, flatteningGroup).withCallingForms(ImmutableList.of(CallingForm.FlattenedPaged));
            apiMethods.add(generatePagedFlattenedMethod(flattenedMethodContext, sampleContext));
        }
    }
    apiMethods.add(generatePagedRequestObjectMethod(methodContext.withCallingForms(ImmutableList.of(CallingForm.RequestPaged)), sampleContext));
    apiMethods.add(generatePagedCallableMethod(methodContext.withCallingForms(ImmutableList.of(CallingForm.CallablePaged)), sampleContext));
    apiMethods.add(generateUnpagedListCallableMethod(methodContext.withCallingForms(Collections.singletonList(CallingForm.CallableList)), sampleContext));
    return apiMethods;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.config.MethodContext) InterfaceContext(com.google.api.codegen.config.InterfaceContext) FlatteningConfig(com.google.api.codegen.config.FlatteningConfig)

Example 9 with MethodContext

use of com.google.api.codegen.config.MethodContext 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 10 with MethodContext

use of com.google.api.codegen.config.MethodContext in project toolkit by googleapis.

the class PathTemplateTransformer method getResourceNameConfigsUsedByInterface.

private static List<ResourceNameConfig> getResourceNameConfigsUsedByInterface(InterfaceContext context) {
    InterfaceConfig interfaceConfig = context.getInterfaceConfig();
    Set<String> foundSet = new HashSet<>();
    List<ResourceNameConfig> resourceNameConfigs = new ArrayList<>();
    for (SingleResourceNameConfig config : interfaceConfig.getSingleResourceNameConfigs()) {
        resourceNameConfigs.add(config);
        foundSet.add(config.getEntityId());
    }
    for (MethodConfig methodConfig : interfaceConfig.getMethodConfigs()) {
        MethodContext methodContext = context.asRequestMethodContext(methodConfig.getMethodModel());
        for (String fieldNamePattern : methodConfig.getFieldNamePatterns().values()) {
            addSingleResourceNameConfigsUsedByInterface(methodContext, fieldNamePattern, foundSet, resourceNameConfigs);
            addResourceNameOneofConfigsUsedByInterface(context, fieldNamePattern, foundSet, resourceNameConfigs);
        }
    }
    return resourceNameConfigs;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) ArrayList(java.util.ArrayList) MethodContext(com.google.api.codegen.config.MethodContext) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) HashSet(java.util.HashSet) SingleResourceNameConfig(com.google.api.codegen.config.SingleResourceNameConfig) ResourceNameConfig(com.google.api.codegen.config.ResourceNameConfig) FixedResourceNameConfig(com.google.api.codegen.config.FixedResourceNameConfig)

Aggregations

MethodContext (com.google.api.codegen.config.MethodContext)21 ArrayList (java.util.ArrayList)14 MethodConfig (com.google.api.codegen.config.MethodConfig)13 MethodModel (com.google.api.codegen.config.MethodModel)12 FlatteningConfig (com.google.api.codegen.config.FlatteningConfig)9 StaticLangApiMethodView (com.google.api.codegen.viewmodel.StaticLangApiMethodView)8 InterfaceContext (com.google.api.codegen.config.InterfaceContext)7 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)7 ImmutableList (com.google.common.collect.ImmutableList)7 List (java.util.List)7 HashMap (java.util.HashMap)6 FieldConfig (com.google.api.codegen.config.FieldConfig)5 GapicMethodContext (com.google.api.codegen.config.GapicMethodContext)5 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)5 CallingForm (com.google.api.codegen.viewmodel.CallingForm)5 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)4 SampleConfig (com.google.api.codegen.config.SampleConfig)4 SampleContext (com.google.api.codegen.config.SampleContext)4 Name (com.google.api.codegen.util.Name)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4