Search in sources :

Example 1 with CallingForm

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

the class StaticLangGapicSamplesTransformer method getSampleContexts.

public List<SampleContext> getSampleContexts(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig) {
    SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
    ImmutableTable<String, String, ImmutableList<SampleConfig>> sampleConfigTable = productConfig.getSampleConfigTable();
    ImmutableList.Builder<SampleContext> sampleContexts = ImmutableList.builder();
    // Loop through sample configs and and map each sample ID to its matching calling forms.
    // We need this information when we need to create, in a language-specific way, unique
    // sample ids when one sample id has multiple matching calling forms
    Map<String, List<CallingForm>> configsAndMatchingForms = new HashMap<>();
    for (InterfaceContext interfaceContext : interfaceContexts) {
        for (MethodModel method : interfaceContext.getSupportedMethods()) {
            MethodContext methodContext = interfaceContext.asRequestMethodContext(method);
            String interfaceName = interfaceContext.getInterfaceConfig().getInterfaceModel().getFullName();
            String methodName = method.getSimpleName();
            List<SampleConfig> sampleConfigs = sampleConfigTable.get(interfaceName, methodName);
            sampleConfigs = firstNonNull(sampleConfigs, ImmutableList.<SampleConfig>of());
            for (SampleConfig config : sampleConfigs) {
                List<CallingForm> allMatchingCallingForms = namer.getMatchingCallingForms(methodContext, config.callingPattern());
                List<CallingForm> existingForms = configsAndMatchingForms.get(config.id());
                if (existingForms == null) {
                    existingForms = new ArrayList<>();
                    configsAndMatchingForms.put(config.id(), existingForms);
                }
                existingForms.addAll(allMatchingCallingForms);
            }
        }
    }
    SampleFileRegistry registry = new SampleFileRegistry(namer, configsAndMatchingForms);
    for (InterfaceContext interfaceContext : interfaceContexts) {
        for (MethodModel method : interfaceContext.getSupportedMethods()) {
            MethodContext methodContext = interfaceContext.asRequestMethodContext(method);
            String interfaceName = interfaceContext.getInterfaceConfig().getInterfaceModel().getFullName();
            String methodName = method.getSimpleName();
            ImmutableList<SampleConfig> sampleConfigs = sampleConfigTable.get(interfaceName, methodName);
            if (sampleConfigs == null) {
                continue;
            }
            for (SampleConfig sampleConfig : sampleConfigs) {
                List<CallingForm> allMatchingCallingForms = configsAndMatchingForms.get(sampleConfig.id());
                for (CallingForm form : allMatchingCallingForms) {
                    InitCodeOutputType initCodeOutputType = InitCodeOutputType.SingleObject;
                    // In Java and C#, we need to handle flattening.
                    if (CallingForm.isFlattened(form)) {
                        if (!methodContext.getMethodConfig().isFlattening()) {
                            continue;
                        }
                        initCodeOutputType = InitCodeOutputType.FieldList;
                        methodContext = methodContext.getSurfaceInterfaceContext().asFlattenedMethodContext(methodContext, methodContext.getMethodConfig().getFlatteningConfigs().get(0));
                    }
                    SampleContext sampleContext = SampleContext.newBuilder().uniqueSampleId(registry.getUniqueSampleId(sampleConfig, form)).sampleType(SampleSpec.SampleType.STANDALONE).callingForm(form).clientMethodType(fromCallingForm(form)).sampleConfig(sampleConfig).initCodeOutputType(initCodeOutputType).methodContext(methodContext).build();
                    sampleContexts.add(sampleContext);
                }
            }
        }
    }
    return sampleContexts.build();
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) SampleConfig(com.google.api.codegen.config.SampleConfig) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 2 with CallingForm

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

the class SampleTransformer method generateSample.

private MethodSampleView generateSample(SampleConfig config, MethodContext methodContext, InitCodeContext initCodeContext) {
    methodContext = methodContext.cloneWithEmptyTypeTable();
    InitCodeView initCodeView = initCodeTransformer().generateInitCode(methodContext, initCodeContext);
    SampleValueSet valueSet = config.valueSet();
    CallingForm form = config.callingForm();
    String regionTag = config.regionTag();
    List<OutputSpec> outputs = valueSet.getOnSuccessList();
    if (outputs.isEmpty()) {
        outputs = OutputTransformer.defaultOutputSpecs(methodContext);
    }
    OutputContext outputContext = OutputContext.create();
    ImmutableList<OutputView> outputViews = outputTransformer().toViews(outputs, methodContext, valueSet, form, outputContext);
    ImportSectionView sampleImportSectionView = sampleImportTransformer().generateImportSection(methodContext.cloneWithEmptyTypeTable(), form, outputContext, methodContext.getTypeTable(), initCodeTransformer().getInitCodeNodes(methodContext, initCodeContext.cloneWithEmptySymbolTable()));
    SampleFunctionDocView sampleFunctionDocView = SampleFunctionDocView.newBuilder().paramDocLines(paramDocLines(methodContext, initCodeView)).mainDocLines(ImmutableList.<String>builder().addAll(methodContext.getNamer().getWrappedDocLines(valueSet.getDescription(), true)).build()).build();
    ImmutableList<String> metadataDescription = ImmutableList.<String>builder().addAll(methodContext.getNamer().getWrappedDocLines(valueSet.getDescription(), false)).build();
    String descriptionLine = metadataDescription.isEmpty() ? "" : metadataDescription.get(0);
    ImmutableList<String> additionalDescriptionLines = metadataDescription.isEmpty() ? ImmutableList.of() : metadataDescription.subList(1, metadataDescription.size());
    return MethodSampleView.newBuilder().callingForm(form).id(valueSet.getId()).sampleInitCode(initCodeView).outputs(outputViews).hasMultipleFileOutputs(outputContext.hasMultipleFileOutputs()).usesAsyncAwaitPattern(// Used by C# and Node.js
    methodContext.getNamer().usesAsyncAwaitPattern(form)).sampleImports(sampleImportSectionView).regionTag(regionTagFromSpec(regionTag, methodContext.getMethodModel().getSimpleName(), form, valueSet.getId())).sampleFunctionName(methodContext.getNamer().getSampleFunctionName(methodContext.getMethodModel())).sampleFunctionDoc(sampleFunctionDocView).title(config.valueSet().getTitle()).descriptionLine(descriptionLine).additionalDescriptionLines(additionalDescriptionLines).build();
}
Also used : OutputView(com.google.api.codegen.viewmodel.OutputView) CallingForm(com.google.api.codegen.viewmodel.CallingForm) SampleValueSet(com.google.api.codegen.SampleValueSet) OutputContext(com.google.api.codegen.config.OutputContext) SampleFunctionDocView(com.google.api.codegen.viewmodel.SampleFunctionDocView) InitCodeView(com.google.api.codegen.viewmodel.InitCodeView) OutputSpec(com.google.api.codegen.OutputSpec) ImportSectionView(com.google.api.codegen.viewmodel.ImportSectionView)

Example 3 with CallingForm

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

the class SampleTransformer method generateSamples.

/**
 * Returns a list of MethodSampleViews for the given MethodContext.
 *
 * @param initContext
 * @param methodContext
 * @param fieldConfigs
 * @param initCodeOutputType
 * @param callingForms The list of calling forms applicable to this method, for which we will
 *     generate samples if so configured via context.getMethodConfig()
 * @return A list of of the MethodSampleView, each of which corresponds to a specific sample
 *     forthe method
 */
public List<MethodSampleView> generateSamples(MethodContext methodContext, InitCodeContext initContext, Collection<FieldConfig> fieldConfigs, InitCodeOutputType initCodeOutputType, List<CallingForm> callingForms) {
    CallingForm defaultCallingForm = methodContext.getNamer().getDefaultCallingForm(methodContext);
    List<MethodSampleView> methodSampleViews = new ArrayList<>();
    MethodConfig methodConfig = methodContext.getMethodConfig();
    SampleValueSet defaultValueSet = defaultValueSet(methodConfig);
    for (SampleConfig sampleConfig : methodConfig.getSampleSpec().getSampleConfigs(callingForms, defaultCallingForm, defaultValueSet, sampleType())) {
        // Do not override outer initContext
        InitCodeContext thisContext = initContext;
        if (thisContext == null) {
            thisContext = createInitCodeContext(methodContext, fieldConfigs, initCodeOutputType, sampleConfig.valueSet());
        }
        methodSampleViews.add(generateSample(sampleConfig, methodContext, thisContext));
    }
    return methodSampleViews;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) SampleConfig(com.google.api.codegen.config.SampleConfig) CallingForm(com.google.api.codegen.viewmodel.CallingForm) ArrayList(java.util.ArrayList) SampleValueSet(com.google.api.codegen.SampleValueSet) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext)

Example 4 with CallingForm

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

the class JavaApiMethodTransformer method generateGrpcStreamingMethods.

private List<StaticLangApiMethodView> generateGrpcStreamingMethods(MethodContext methodContext, @Nullable SampleContext sampleContext) {
    List<StaticLangApiMethodView> apiMethods = new ArrayList<>();
    InterfaceContext interfaceContext = methodContext.getSurfaceInterfaceContext();
    List<CallingForm> callingForms;
    ImportTypeTable typeTable = interfaceContext.getImportTypeTable();
    MethodConfig methodConfig = methodContext.getMethodConfig();
    switch(methodConfig.getGrpcStreamingType()) {
        case BidiStreaming:
            typeTable.saveNicknameFor("com.google.api.gax.rpc.BidiStreamingCallable");
            callingForms = Collections.singletonList(CallingForm.CallableStreamingBidi);
            break;
        case ClientStreaming:
            typeTable.saveNicknameFor("com.google.api.gax.rpc.ClientStreamingCallable");
            callingForms = Collections.singletonList(CallingForm.CallableStreamingClient);
            break;
        case ServerStreaming:
            typeTable.saveNicknameFor("com.google.api.gax.rpc.ServerStreamingCallable");
            callingForms = Collections.singletonList(CallingForm.CallableStreamingServer);
            break;
        default:
            throw new IllegalArgumentException("Invalid streaming type: " + methodConfig.getGrpcStreamingType());
    }
    apiMethods.add(generateCallableMethod(methodContext.withCallingForms(callingForms), sampleContext));
    return apiMethods;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) CallingForm(com.google.api.codegen.viewmodel.CallingForm) ImportTypeTable(com.google.api.codegen.transformer.ImportTypeTable) ArrayList(java.util.ArrayList) InterfaceContext(com.google.api.codegen.config.InterfaceContext)

Example 5 with CallingForm

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

the class DynamicLangGapicSamplesTransformer method getSampleContexts.

public List<SampleContext> getSampleContexts(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig) {
    SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
    ImmutableTable<String, String, ImmutableList<SampleConfig>> sampleConfigTable = productConfig.getSampleConfigTable();
    ImmutableList.Builder<SampleContext> sampleContexts = ImmutableList.builder();
    // Loop through sample configs and and map each sample ID to its matching calling forms.
    // We need this information when we need to create, in a language-specific way, unique
    // sample ids when one sample id has multiple matching calling forms
    Map<String, List<CallingForm>> configsAndMatchingForms = new HashMap<>();
    for (InterfaceContext interfaceContext : interfaceContexts) {
        for (MethodModel method : interfaceContext.getSupportedMethods()) {
            MethodContext methodContext = interfaceContext.asRequestMethodContext(method);
            String interfaceName = interfaceContext.getInterfaceConfig().getInterfaceModel().getFullName();
            String methodName = method.getSimpleName();
            List<SampleConfig> sampleConfigs = sampleConfigTable.get(interfaceName, methodName);
            sampleConfigs = firstNonNull(sampleConfigs, ImmutableList.<SampleConfig>of());
            for (SampleConfig config : sampleConfigs) {
                List<CallingForm> allMatchingCallingForms = namer.getMatchingCallingForms(methodContext, config.callingPattern());
                List<CallingForm> existingForms = configsAndMatchingForms.get(config.id());
                if (existingForms == null) {
                    existingForms = new ArrayList<>();
                    configsAndMatchingForms.put(config.id(), existingForms);
                }
                existingForms.addAll(allMatchingCallingForms);
            }
        }
    }
    SampleFileRegistry registry = new SampleFileRegistry(namer, configsAndMatchingForms);
    for (InterfaceContext interfaceContext : interfaceContexts) {
        for (MethodModel method : interfaceContext.getSupportedMethods()) {
            MethodContext methodContext = interfaceContext.asRequestMethodContext(method);
            String interfaceName = interfaceContext.getInterfaceConfig().getInterfaceModel().getFullName();
            String methodName = method.getSimpleName();
            ImmutableList<SampleConfig> sampleConfigs = sampleConfigTable.get(interfaceName, methodName);
            if (sampleConfigs == null) {
                continue;
            }
            for (SampleConfig sampleConfig : sampleConfigs) {
                List<CallingForm> allMatchingCallingForms = configsAndMatchingForms.get(sampleConfig.id());
                for (CallingForm form : allMatchingCallingForms) {
                    InitCodeOutputType initCodeOutputType = methodContext.getMethodModel().getRequestStreaming() ? InitCodeOutputType.SingleObject : InitCodeOutputType.FieldList;
                    SampleContext sampleContext = SampleContext.newBuilder().uniqueSampleId(registry.getUniqueSampleId(sampleConfig, form)).sampleType(SampleSpec.SampleType.STANDALONE).callingForm(form).sampleConfig(sampleConfig).initCodeOutputType(initCodeOutputType).methodContext(methodContext).build();
                    sampleContexts.add(sampleContext);
                }
            }
        }
    }
    return sampleContexts.build();
}
Also used : MethodModel(com.google.api.codegen.config.MethodModel) SampleConfig(com.google.api.codegen.config.SampleConfig) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Aggregations

CallingForm (com.google.api.codegen.viewmodel.CallingForm)5 ArrayList (java.util.ArrayList)4 InterfaceContext (com.google.api.codegen.config.InterfaceContext)3 SampleConfig (com.google.api.codegen.config.SampleConfig)3 SampleValueSet (com.google.api.codegen.SampleValueSet)2 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)2 MethodConfig (com.google.api.codegen.config.MethodConfig)2 MethodContext (com.google.api.codegen.config.MethodContext)2 MethodModel (com.google.api.codegen.config.MethodModel)2 SampleContext (com.google.api.codegen.config.SampleContext)2 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)2 ImmutableList (com.google.common.collect.ImmutableList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 OutputSpec (com.google.api.codegen.OutputSpec)1 OutputContext (com.google.api.codegen.config.OutputContext)1 InitCodeContext (com.google.api.codegen.metacode.InitCodeContext)1 ImportTypeTable (com.google.api.codegen.transformer.ImportTypeTable)1 ImportSectionView (com.google.api.codegen.viewmodel.ImportSectionView)1 InitCodeView (com.google.api.codegen.viewmodel.InitCodeView)1