use of com.google.api.codegen.viewmodel.OutputView 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();
}
use of com.google.api.codegen.viewmodel.OutputView in project toolkit by googleapis.
the class OutputTransformer method toView.
private OutputView toView(ResponseStatementProto config, MethodContext methodContext, SampleContext sampleContext, OutputContext outputContext) {
Runnable once = new Runnable() {
boolean ran;
@Override
public void run() {
Preconditions.checkArgument(!ran, "%s:%s: only one field of ResponseStatementProto may be set", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id());
ran = true;
}
};
OutputView view = null;
if (config.hasLoop()) {
once.run();
view = loopView(config.getLoop(), methodContext, sampleContext, outputContext);
}
if (config.getPrintCount() > 0) {
once.run();
view = printView(config.getPrintList(), methodContext, sampleContext, outputContext);
}
if (!config.getDefine().isEmpty()) {
once.run();
view = defineView(new Scanner(config.getDefine()), methodContext, sampleContext, outputContext);
}
if (config.getCommentCount() > 0) {
once.run();
view = commentView(config.getCommentList(), methodContext);
}
if (config.hasWriteFile()) {
once.run();
view = writeFileView(config.getWriteFile(), methodContext, sampleContext, outputContext);
}
return Preconditions.checkNotNull(view, "%s:%s: one field of ResponseStatementProto must be set", methodContext.getMethodModel().getSimpleName(), sampleContext.sampleConfig().id());
}
use of com.google.api.codegen.viewmodel.OutputView 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();
}
Aggregations