use of com.google.api.codegen.config.SampleContext 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();
}
use of com.google.api.codegen.config.SampleContext in project toolkit by googleapis.
the class SampleManifestTransformer method transform.
@Override
public List<ViewModel> transform(ProtoApiModel model, GapicProductConfig productConfig) {
SurfaceNamer surfaceNamer = newSurfaceNamer.apply(productConfig);
ModelTypeTable typeTable = newTypeTable.apply(productConfig.getPackageName());
FeatureConfig featureConfig = newFeatureConfig.apply(productConfig);
ImmutableList.Builder<SampleManifestView.SampleEntry> builder = ImmutableList.builder();
List<InterfaceContext> interfaceContexts = Streams.stream(model.getInterfaces(productConfig)).filter(iface -> productConfig.hasInterfaceConfig(iface)).map(iface -> GapicInterfaceContext.create(iface, productConfig, typeTable, surfaceNamer, featureConfig)).collect(ImmutableList.toImmutableList());
List<SampleContext> sampleContexts = metadataNamer.getSampleContexts(interfaceContexts, productConfig);
for (SampleContext context : sampleContexts) {
String sample = context.uniqueSampleId();
String path = metadataNamer.getSamplePath(sample);
String regionTag = context.sampleConfig().regionTag();
String className = metadataNamer.getSampleClassName(sample);
builder.add(SampleManifestView.SampleEntry.create(sample, path, className, regionTag));
}
ImmutableList<SampleManifestView.SampleEntry> entries = builder.build();
// Only generate manifest files when samples are configured through sample config.
if (entries.isEmpty()) {
return Collections.emptyList();
}
SampleManifestView.Builder sampleManifestView = SampleManifestView.newBuilder();
sampleManifestView.environment(metadataNamer.getEnvironment());
sampleManifestView.bin(metadataNamer.getBin());
sampleManifestView.basePath(metadataNamer.getBasePath(productConfig));
sampleManifestView.packageName(metadataNamer.getPackageName(productConfig));
sampleManifestView.invocation(metadataNamer.getInvocation());
sampleManifestView.schemaVersion(MANIFEST_SCHEMA_VERSION);
sampleManifestView.sampleEntries(entries);
sampleManifestView.outputPath(getManifestOutputPath(model, productConfig));
sampleManifestView.templateFileName(TEMPLATE_NAME);
return Collections.singletonList(sampleManifestView.build());
}
use of com.google.api.codegen.config.SampleContext 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();
}
use of com.google.api.codegen.config.SampleContext 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);
}
use of com.google.api.codegen.config.SampleContext 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();
}
Aggregations