Search in sources :

Example 1 with SampleValueSet

use of com.google.api.codegen.SampleValueSet 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 2 with SampleValueSet

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

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

the class SampleSpecTest method valueSetsMatching.

@Test
public void valueSetsMatching() {
    SampleValueSet valueSetAlice = SampleValueSet.newBuilder().setId("alice").setParameters(SampleParameters.newBuilder().addDefaults("apple").build()).build();
    SampleValueSet valueSetBob = SampleValueSet.newBuilder().setId("bob").setParameters(SampleParameters.newBuilder().addDefaults("banana").build()).build();
    SampleValueSet valueSetAlison = SampleValueSet.newBuilder().setId("alison").setParameters(SampleParameters.newBuilder().addDefaults("apricot").build()).build();
    final MethodConfigProto methodConfigProto = MethodConfigProto.newBuilder().addSampleValueSets(valueSetAlice).addSampleValueSets(valueSetBob).addSampleValueSets(valueSetAlison).setSamples(SampleConfiguration.newBuilder().addStandalone(SampleTypeConfiguration.newBuilder().addValueSets("ali.*").addCallingForms(".*")).addStandalone(SampleTypeConfiguration.newBuilder().addValueSets("be.*").addCallingForms(".*"))).build();
    SampleSpec sampleSpec = new SampleSpec(methodConfigProto);
    final List<SampleValueSet> matchingValues = sampleSpec.getSampleConfigs(Collections.singletonList(CallingForm.Request), CallingForm.Request, null, SampleType.STANDALONE).stream().map(config -> config.valueSet()).collect(Collectors.toList());
    assertThat(matchingValues).containsExactly(valueSetAlice, valueSetAlison).inOrder();
}
Also used : SampleTypeConfiguration(com.google.api.codegen.SampleConfiguration.SampleTypeConfiguration) SampleConfiguration(com.google.api.codegen.SampleConfiguration) List(java.util.List) MethodConfigProto(com.google.api.codegen.MethodConfigProto) SampleParameters(com.google.api.codegen.SampleParameters) SampleValueSet(com.google.api.codegen.SampleValueSet) CallingForm(com.google.api.codegen.viewmodel.CallingForm) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) SampleType(com.google.api.codegen.config.SampleSpec.SampleType) MethodConfigProto(com.google.api.codegen.MethodConfigProto) SampleValueSet(com.google.api.codegen.SampleValueSet) Test(org.junit.Test)

Aggregations

SampleValueSet (com.google.api.codegen.SampleValueSet)3 CallingForm (com.google.api.codegen.viewmodel.CallingForm)3 MethodConfigProto (com.google.api.codegen.MethodConfigProto)1 OutputSpec (com.google.api.codegen.OutputSpec)1 SampleConfiguration (com.google.api.codegen.SampleConfiguration)1 SampleTypeConfiguration (com.google.api.codegen.SampleConfiguration.SampleTypeConfiguration)1 SampleParameters (com.google.api.codegen.SampleParameters)1 MethodConfig (com.google.api.codegen.config.MethodConfig)1 OutputContext (com.google.api.codegen.config.OutputContext)1 SampleConfig (com.google.api.codegen.config.SampleConfig)1 SampleType (com.google.api.codegen.config.SampleSpec.SampleType)1 InitCodeContext (com.google.api.codegen.metacode.InitCodeContext)1 ImportSectionView (com.google.api.codegen.viewmodel.ImportSectionView)1 InitCodeView (com.google.api.codegen.viewmodel.InitCodeView)1 MethodSampleView (com.google.api.codegen.viewmodel.MethodSampleView)1 OutputView (com.google.api.codegen.viewmodel.OutputView)1 SampleFunctionDocView (com.google.api.codegen.viewmodel.SampleFunctionDocView)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1