Search in sources :

Example 1 with OutputContext

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

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

the class OutputTransformer method mapLoopView.

private OutputView.MapLoopView mapLoopView(ResponseStatementProto.LoopStatement loop, MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext) {
    outputContext.mapSpecs().add(loop);
    ScopeTable scope = outputContext.scopeTable();
    String key = loop.getKey();
    String value = loop.getValue();
    OutputView.VariableView mapVar = accessor(new Scanner(loop.getMap()), methodContext, sampleContext, scope);
    TypeModel keyType = mapVar.type().getMapKeyType();
    TypeModel valueType = mapVar.type().getMapValueType();
    String keyTypeName = methodContext.getTypeTable().getNicknameFor(keyType);
    String valueTypeName = methodContext.getTypeTable().getNicknameFor(valueType);
    if (!key.isEmpty()) {
        assertIdentifierNotUsed(key, methodContext, sampleContext);
        scope.put(key, keyType, keyTypeName);
    }
    if (!value.isEmpty()) {
        assertIdentifierNotUsed(value, methodContext, sampleContext);
        scope.put(value, valueType, valueTypeName);
    }
    return OutputView.MapLoopView.newBuilder().keyVariableName(methodContext.getNamer().localVarName(Name.anyLower(key))).keyType(keyTypeName).valueVariableName(methodContext.getNamer().localVarName(Name.anyLower(value))).valueType(valueTypeName).map(mapVar).body(loop.getBodyList().stream().map(body -> toView(body, methodContext, sampleContext, outputContext)).collect(ImmutableList.toImmutableList())).build();
}
Also used : IntStream(java.util.stream.IntStream) FieldModel(com.google.api.codegen.config.FieldModel) ResponseStatementProto(com.google.api.codegen.samplegen.v1p2.ResponseStatementProto) HashMap(java.util.HashMap) HashSet(java.util.HashSet) LongRunningConfig(com.google.api.codegen.config.LongRunningConfig) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) Map(java.util.Map) OutputContext(com.google.api.codegen.config.OutputContext) SampleConfig(com.google.api.codegen.config.SampleConfig) Nullable(javax.annotation.Nullable) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleValueSet(com.google.api.codegen.SampleValueSet) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) Set(java.util.Set) TypeModel(com.google.api.codegen.config.TypeModel) Scanner(com.google.api.codegen.util.Scanner) Name(com.google.api.codegen.util.Name) List(java.util.List) OutputSpec(com.google.api.codegen.OutputSpec) SampleSpec(com.google.api.codegen.config.SampleSpec) Preconditions(com.google.common.base.Preconditions) OutputView(com.google.api.codegen.viewmodel.OutputView) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) Scanner(com.google.api.codegen.util.Scanner) OutputView(com.google.api.codegen.viewmodel.OutputView) TypeModel(com.google.api.codegen.config.TypeModel)

Example 3 with OutputContext

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

the class OutputTransformer method arrayLoopView.

private OutputView.ArrayLoopView arrayLoopView(ResponseStatementProto.LoopStatement loop, MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext) {
    ScopeTable scope = outputContext.scopeTable();
    String loopVariable = loop.getVariable();
    assertIdentifierNotUsed(loopVariable, methodContext, sampleContext);
    OutputView.VariableView accessor = accessorNewVariable(new Scanner(loop.getCollection()), methodContext, sampleContext, scope, loopVariable, true);
    return OutputView.ArrayLoopView.newBuilder().variableType(scope.getTypeName(loopVariable)).variableName(methodContext.getNamer().localVarName(Name.from(loopVariable))).collection(accessor).body(loop.getBodyList().stream().map(body -> toView(body, methodContext, sampleContext, outputContext)).collect(ImmutableList.toImmutableList())).build();
}
Also used : IntStream(java.util.stream.IntStream) FieldModel(com.google.api.codegen.config.FieldModel) ResponseStatementProto(com.google.api.codegen.samplegen.v1p2.ResponseStatementProto) HashMap(java.util.HashMap) HashSet(java.util.HashSet) LongRunningConfig(com.google.api.codegen.config.LongRunningConfig) ImmutableList(com.google.common.collect.ImmutableList) MethodContext(com.google.api.codegen.config.MethodContext) Map(java.util.Map) OutputContext(com.google.api.codegen.config.OutputContext) SampleConfig(com.google.api.codegen.config.SampleConfig) Nullable(javax.annotation.Nullable) InitCodeOutputType(com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType) SampleValueSet(com.google.api.codegen.SampleValueSet) SampleContext(com.google.api.codegen.config.SampleContext) CallingForm(com.google.api.codegen.viewmodel.CallingForm) Set(java.util.Set) TypeModel(com.google.api.codegen.config.TypeModel) Scanner(com.google.api.codegen.util.Scanner) Name(com.google.api.codegen.util.Name) List(java.util.List) OutputSpec(com.google.api.codegen.OutputSpec) SampleSpec(com.google.api.codegen.config.SampleSpec) Preconditions(com.google.common.base.Preconditions) OutputView(com.google.api.codegen.viewmodel.OutputView) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) Scanner(com.google.api.codegen.util.Scanner) OutputView(com.google.api.codegen.viewmodel.OutputView)

Example 4 with OutputContext

use of com.google.api.codegen.config.OutputContext 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)4 OutputView (com.google.api.codegen.viewmodel.OutputView)4 OutputSpec (com.google.api.codegen.OutputSpec)3 SampleValueSet (com.google.api.codegen.SampleValueSet)3 ResponseStatementProto (com.google.api.codegen.samplegen.v1p2.ResponseStatementProto)3 CallingForm (com.google.api.codegen.viewmodel.CallingForm)3 FieldModel (com.google.api.codegen.config.FieldModel)2 LongRunningConfig (com.google.api.codegen.config.LongRunningConfig)2 MethodContext (com.google.api.codegen.config.MethodContext)2 SampleConfig (com.google.api.codegen.config.SampleConfig)2 SampleContext (com.google.api.codegen.config.SampleContext)2 SampleSpec (com.google.api.codegen.config.SampleSpec)2 TypeModel (com.google.api.codegen.config.TypeModel)2 InitCodeOutputType (com.google.api.codegen.metacode.InitCodeContext.InitCodeOutputType)2 Name (com.google.api.codegen.util.Name)2 Scanner (com.google.api.codegen.util.Scanner)2 ImportSectionView (com.google.api.codegen.viewmodel.ImportSectionView)2 InitCodeView (com.google.api.codegen.viewmodel.InitCodeView)2 SampleFunctionDocView (com.google.api.codegen.viewmodel.SampleFunctionDocView)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2