Search in sources :

Example 6 with SampleContext

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

the class StaticLangGapicSamplesTransformer method generateSamplesFromSampleConfigs.

private List<ViewModel> generateSamplesFromSampleConfigs(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig) {
    SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
    List<SampleContext> sampleContexts = getSampleContexts(interfaceContexts, productConfig);
    List<ViewModel> sampleFileViews = new ArrayList<>();
    for (SampleContext sampleContext : sampleContexts) {
        StaticLangApiMethodView methodView = apiMethodTransformer.generateApiMethod(sampleContext.methodContext(), sampleContext);
        MethodSampleView methodSampleView = methodView.samples().get(0);
        String fileName = namer.getApiSampleFileName(sampleContext.uniqueSampleId());
        String className = namer.getApiSampleClassName(sampleContext.uniqueSampleId());
        InterfaceContext interfaceContext = sampleContext.methodContext().getSurfaceInterfaceContext();
        sampleFileViews.add(newSampleFileView(productConfig, interfaceContext, className, fileName, methodView, methodSampleView));
    }
    return sampleFileViews;
}
Also used : MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) SampleContext(com.google.api.codegen.config.SampleContext) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) ArrayList(java.util.ArrayList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext)

Example 7 with SampleContext

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

the class DynamicLangGapicSamplesTransformer method generateSamplesFromSampleConfigs.

private List<ViewModel> generateSamplesFromSampleConfigs(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig) {
    SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
    List<SampleContext> sampleContexts = getSampleContexts(interfaceContexts, productConfig);
    ImmutableList.Builder<ViewModel> sampleFileViews = ImmutableList.builder();
    for (SampleContext sampleContext : sampleContexts) {
        OptionalArrayMethodView methodView = apiMethodTransformer.generateApiMethod(sampleContext.methodContext(), sampleContext);
        MethodSampleView methodSampleView = methodView.samples().get(0);
        String fileName = namer.getApiSampleFileName(sampleContext.uniqueSampleId());
        InterfaceContext interfaceContext = sampleContext.methodContext().getSurfaceInterfaceContext();
        sampleFileViews.add(newSampleFileView(productConfig, interfaceContext, fileName, methodView, methodSampleView));
    }
    return sampleFileViews.build();
}
Also used : MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) OptionalArrayMethodView(com.google.api.codegen.viewmodel.OptionalArrayMethodView) SampleContext(com.google.api.codegen.config.SampleContext) ImmutableList(com.google.common.collect.ImmutableList) ViewModel(com.google.api.codegen.viewmodel.ViewModel) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext)

Example 8 with SampleContext

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

SampleContext (com.google.api.codegen.config.SampleContext)8 ImmutableList (com.google.common.collect.ImmutableList)6 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)5 InterfaceContext (com.google.api.codegen.config.InterfaceContext)5 SampleConfig (com.google.api.codegen.config.SampleConfig)5 List (java.util.List)5 MethodContext (com.google.api.codegen.config.MethodContext)4 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)4 CallingForm (com.google.api.codegen.viewmodel.CallingForm)4 HashMap (java.util.HashMap)4 ViewModel (com.google.api.codegen.viewmodel.ViewModel)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 OutputSpec (com.google.api.codegen.OutputSpec)2 SampleValueSet (com.google.api.codegen.SampleValueSet)2 FieldModel (com.google.api.codegen.config.FieldModel)2 LongRunningConfig (com.google.api.codegen.config.LongRunningConfig)2 MethodModel (com.google.api.codegen.config.MethodModel)2 OutputContext (com.google.api.codegen.config.OutputContext)2 SampleSpec (com.google.api.codegen.config.SampleSpec)2