Search in sources :

Example 1 with SampleConfig

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

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

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

the class OutputTransformer method toViews.

// TODO(hzyi): Entry point for generating output views using gapic config. To be deprecated.
@Deprecated
ImmutableList<OutputView> toViews(List<OutputSpec> configs, MethodContext context, SampleValueSet valueSet, CallingForm form, OutputContext outputContext) {
    SampleConfig sampleConfig = SampleConfig.newBuilder().id(valueSet.getId()).type(SampleSpec.SampleType.STANDALONE).build();
    SampleContext sampleContext = SampleContext.newBuilder().uniqueSampleId(valueSet.getId()).sampleType(SampleSpec.SampleType.STANDALONE).sampleConfig(sampleConfig).initCodeOutputType(InitCodeOutputType.FieldList).callingForm(form).build();
    return toViews(fromOutputSpecs(configs), context, sampleContext, outputContext);
}
Also used : SampleConfig(com.google.api.codegen.config.SampleConfig) SampleContext(com.google.api.codegen.config.SampleContext)

Example 4 with SampleConfig

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

the class StaticLangGapicSamplesTransformer method transform.

@Override
public List<ViewModel> transform(ProtoApiModel apiModel, GapicProductConfig productConfig) {
    String packageName = productConfig.getPackageName();
    SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
    ModelTypeTable typeTable = newTypeTable.apply(packageName);
    FeatureConfig featureConfig = newFeatureConfig.apply(productConfig);
    List<InterfaceContext> interfaceContexts = Streams.stream(apiModel.getInterfaces(productConfig)).filter(iface -> productConfig.hasInterfaceConfig(iface)).map(iface -> GapicInterfaceContext.create(iface, productConfig, typeTable, namer, featureConfig)).collect(ImmutableList.toImmutableList());
    ImmutableTable<String, String, ImmutableList<SampleConfig>> sampleConfigTable = productConfig.getSampleConfigTable();
    List<ViewModel> sampleFiles;
    if (sampleConfigTable.isEmpty()) {
        // We don't have sample configs. Continue to use gapic config.
        sampleFiles = generateSamplesFromGapicConfigs(interfaceContexts, productConfig, namer);
    } else {
        // Generate samples using sample configs.
        sampleFiles = generateSamplesFromSampleConfigs(interfaceContexts, productConfig);
    }
    if (!interfaceContexts.isEmpty() && !sampleFiles.isEmpty()) {
        ViewModel entryPoint = generateSampleEntryPoint(interfaceContexts.get(0));
        if (entryPoint != null) {
            sampleFiles.add(entryPoint);
        }
    }
    return ImmutableList.copyOf(sampleFiles);
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) StaticLangApiMethodView(com.google.api.codegen.viewmodel.StaticLangApiMethodView) Function(java.util.function.Function) ArrayList(java.util.ArrayList) InterfaceContext(com.google.api.codegen.config.InterfaceContext) SampleEntryPointView(com.google.api.codegen.viewmodel.SampleEntryPointView) MethodSampleView(com.google.api.codegen.viewmodel.MethodSampleView) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) MethodModel(com.google.api.codegen.config.MethodModel) ViewModel(com.google.api.codegen.viewmodel.ViewModel) Map(java.util.Map) StaticLangSampleClassView(com.google.api.codegen.viewmodel.StaticLangSampleClassView) StaticLangFileView(com.google.api.codegen.viewmodel.StaticLangFileView) SampleConfig(com.google.api.codegen.config.SampleConfig) Nullable(javax.annotation.Nullable) GapicCodePathMapper(com.google.api.codegen.gapic.GapicCodePathMapper) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) Streams(com.google.common.collect.Streams) GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) ProtoApiModel(com.google.api.codegen.config.ProtoApiModel) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) List(java.util.List) Paths(java.nio.file.Paths) SampleSpec(com.google.api.codegen.config.SampleSpec) ImmutableTable(com.google.common.collect.ImmutableTable) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) ClientMethodType(com.google.api.codegen.viewmodel.ClientMethodType) ImmutableList(com.google.common.collect.ImmutableList) InterfaceContext(com.google.api.codegen.config.InterfaceContext) GapicInterfaceContext(com.google.api.codegen.config.GapicInterfaceContext) ViewModel(com.google.api.codegen.viewmodel.ViewModel)

Example 5 with SampleConfig

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

the class OutputTransformerTest method setUp.

@Before
public void setUp() {
    SampleConfig sampleConfig = SampleConfig.newBuilder().id("test-sample-value-set-id").type(SampleSpec.SampleType.STANDALONE).build();
    sampleContext = SampleContext.newBuilder().uniqueSampleId("test-sample-value-set-id").sampleConfig(sampleConfig).sampleType(SampleSpec.SampleType.STANDALONE).callingForm(form).initCodeOutputType(InitCodeOutputType.FieldList).build();
    parent = new OutputTransformer.ScopeTable();
    child = new OutputTransformer.ScopeTable(parent);
    MockitoAnnotations.initMocks(this);
    when(context.getFeatureConfig()).thenReturn(featureConfig);
    when(context.getMethodConfig()).thenReturn(config);
    when(context.getMethodModel()).thenReturn(model);
    when(context.getNamer()).thenReturn(namer);
    when(context.getTypeTable()).thenReturn(typeTable);
    when(model.getSimpleName()).thenReturn("methodSimpleName");
    when(namer.getSampleResponseVarName(context, form)).thenReturn("sampleResponseVarName");
}
Also used : SampleConfig(com.google.api.codegen.config.SampleConfig) Before(org.junit.Before)

Aggregations

SampleConfig (com.google.api.codegen.config.SampleConfig)6 SampleContext (com.google.api.codegen.config.SampleContext)4 CallingForm (com.google.api.codegen.viewmodel.CallingForm)4 ArrayList (java.util.ArrayList)4 GapicInterfaceContext (com.google.api.codegen.config.GapicInterfaceContext)3 InterfaceContext (com.google.api.codegen.config.InterfaceContext)3 MethodContext (com.google.api.codegen.config.MethodContext)3 MethodModel (com.google.api.codegen.config.MethodModel)3 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)3 ImmutableList (com.google.common.collect.ImmutableList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 MethodSampleView (com.google.api.codegen.viewmodel.MethodSampleView)2 SampleValueSet (com.google.api.codegen.SampleValueSet)1 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 ProtoApiModel (com.google.api.codegen.config.ProtoApiModel)1 SampleSpec (com.google.api.codegen.config.SampleSpec)1 GapicCodePathMapper (com.google.api.codegen.gapic.GapicCodePathMapper)1 InitCodeContext (com.google.api.codegen.metacode.InitCodeContext)1