Search in sources :

Example 1 with ResponseStatementProto

use of com.google.api.codegen.samplegen.v1p2.ResponseStatementProto in project toolkit by googleapis.

the class SampleTransformer method generateSample.

// entry point for generating standalone samples using sample config.
public MethodSampleView generateSample(MethodContext methodContext, SampleContext sampleContext) {
    methodContext = methodContext.cloneWithEmptyTypeTable();
    // request
    InitCodeContext initCodeContext = createInitCodeContext(methodContext, sampleContext);
    InitCodeView initCodeView = initCodeTransformer().generateInitCode(methodContext, initCodeContext);
    // response
    OutputContext outputContext = OutputContext.create();
    List<ResponseStatementProto> outputs = sampleContext.sampleConfig().responseConfigs();
    if (outputs.isEmpty()) {
        outputs = OutputTransformer.defaultResponseStatements(methodContext);
    }
    ImmutableList<OutputView> outputViews = outputTransformer().toViews(outputs, methodContext, sampleContext, outputContext);
    // imports
    ImportSectionView sampleImportSectionView = sampleImportTransformer().generateImportSection(methodContext.cloneWithEmptyTypeTable(), sampleContext.callingForm(), outputContext, methodContext.getTypeTable(), initCodeTransformer().getInitCodeNodes(methodContext, // to avoid symbol collision
    initCodeContext.cloneWithEmptySymbolTable()));
    // Documentation
    SampleFunctionDocView sampleFunctionDocView = SampleFunctionDocView.newBuilder().paramDocLines(paramDocLines(methodContext, initCodeView)).mainDocLines(ImmutableList.<String>builder().addAll(methodContext.getNamer().getWrappedDocLines(sampleContext.sampleConfig().description(), true)).build()).build();
    // metadata
    ImmutableList<String> metadataDescription = ImmutableList.<String>builder().addAll(methodContext.getNamer().getWrappedDocLines(sampleContext.sampleConfig().description(), false)).build();
    String descriptionLine = metadataDescription.isEmpty() ? "" : metadataDescription.get(0);
    ImmutableList<String> additionalDescriptionLines = metadataDescription.isEmpty() ? ImmutableList.of() : metadataDescription.subList(1, metadataDescription.size());
    // assemble
    return MethodSampleView.newBuilder().id(sampleContext.uniqueSampleId()).callingForm(sampleContext.callingForm()).sampleInitCode(initCodeView).outputs(outputViews).hasMultipleFileOutputs(outputContext.hasMultipleFileOutputs()).usesAsyncAwaitPattern(methodContext.getNamer().usesAsyncAwaitPattern(// Used by C# and Node.js
    sampleContext.callingForm())).sampleImports(sampleImportSectionView).regionTag(sampleContext.sampleConfig().regionTag()).sampleFunctionName(methodContext.getNamer().getSampleFunctionName(methodContext.getMethodModel())).sampleFunctionDoc(sampleFunctionDocView).title(sampleContext.sampleConfig().title()).descriptionLine(descriptionLine).additionalDescriptionLines(additionalDescriptionLines).build();
}
Also used : ResponseStatementProto(com.google.api.codegen.samplegen.v1p2.ResponseStatementProto) OutputView(com.google.api.codegen.viewmodel.OutputView) InitCodeContext(com.google.api.codegen.metacode.InitCodeContext) OutputContext(com.google.api.codegen.config.OutputContext) SampleFunctionDocView(com.google.api.codegen.viewmodel.SampleFunctionDocView) InitCodeView(com.google.api.codegen.viewmodel.InitCodeView) ImportSectionView(com.google.api.codegen.viewmodel.ImportSectionView)

Aggregations

OutputContext (com.google.api.codegen.config.OutputContext)1 InitCodeContext (com.google.api.codegen.metacode.InitCodeContext)1 ResponseStatementProto (com.google.api.codegen.samplegen.v1p2.ResponseStatementProto)1 ImportSectionView (com.google.api.codegen.viewmodel.ImportSectionView)1 InitCodeView (com.google.api.codegen.viewmodel.InitCodeView)1 OutputView (com.google.api.codegen.viewmodel.OutputView)1 SampleFunctionDocView (com.google.api.codegen.viewmodel.SampleFunctionDocView)1