use of com.google.api.codegen.config.InterfaceContext in project toolkit by googleapis.
the class StaticLangGapicSamplesTransformer method transform.
@Override
public List<ViewModel> transform(ProtoApiModel apiModel, GapicProductConfig productConfig) {
String packageName = productConfig.getPackageName();
SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
ModelTypeTable typeTable = newTypeTable.apply(packageName);
FeatureConfig featureConfig = newFeatureConfig.apply(productConfig);
List<InterfaceContext> interfaceContexts = Streams.stream(apiModel.getInterfaces(productConfig)).filter(iface -> productConfig.hasInterfaceConfig(iface)).map(iface -> GapicInterfaceContext.create(iface, productConfig, typeTable, namer, featureConfig)).collect(ImmutableList.toImmutableList());
ImmutableTable<String, String, ImmutableList<SampleConfig>> sampleConfigTable = productConfig.getSampleConfigTable();
List<ViewModel> sampleFiles;
if (sampleConfigTable.isEmpty()) {
// We don't have sample configs. Continue to use gapic config.
sampleFiles = generateSamplesFromGapicConfigs(interfaceContexts, productConfig, namer);
} else {
// Generate samples using sample configs.
sampleFiles = generateSamplesFromSampleConfigs(interfaceContexts, productConfig);
}
if (!interfaceContexts.isEmpty() && !sampleFiles.isEmpty()) {
ViewModel entryPoint = generateSampleEntryPoint(interfaceContexts.get(0));
if (entryPoint != null) {
sampleFiles.add(entryPoint);
}
}
return ImmutableList.copyOf(sampleFiles);
}
use of com.google.api.codegen.config.InterfaceContext in project toolkit by googleapis.
the class StaticLangGapicSamplesTransformer method generateSamplesFromSampleConfigs.
private List<ViewModel> generateSamplesFromSampleConfigs(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig) {
SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
List<SampleContext> sampleContexts = getSampleContexts(interfaceContexts, productConfig);
List<ViewModel> sampleFileViews = new ArrayList<>();
for (SampleContext sampleContext : sampleContexts) {
StaticLangApiMethodView methodView = apiMethodTransformer.generateApiMethod(sampleContext.methodContext(), sampleContext);
MethodSampleView methodSampleView = methodView.samples().get(0);
String fileName = namer.getApiSampleFileName(sampleContext.uniqueSampleId());
String className = namer.getApiSampleClassName(sampleContext.uniqueSampleId());
InterfaceContext interfaceContext = sampleContext.methodContext().getSurfaceInterfaceContext();
sampleFileViews.add(newSampleFileView(productConfig, interfaceContext, className, fileName, methodView, methodSampleView));
}
return sampleFileViews;
}
use of com.google.api.codegen.config.InterfaceContext in project toolkit by googleapis.
the class DynamicLangGapicSamplesTransformer method generateSamplesFromSampleConfigs.
private List<ViewModel> generateSamplesFromSampleConfigs(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig) {
SurfaceNamer namer = newSurfaceNamer.apply(productConfig);
List<SampleContext> sampleContexts = getSampleContexts(interfaceContexts, productConfig);
ImmutableList.Builder<ViewModel> sampleFileViews = ImmutableList.builder();
for (SampleContext sampleContext : sampleContexts) {
OptionalArrayMethodView methodView = apiMethodTransformer.generateApiMethod(sampleContext.methodContext(), sampleContext);
MethodSampleView methodSampleView = methodView.samples().get(0);
String fileName = namer.getApiSampleFileName(sampleContext.uniqueSampleId());
InterfaceContext interfaceContext = sampleContext.methodContext().getSurfaceInterfaceContext();
sampleFileViews.add(newSampleFileView(productConfig, interfaceContext, fileName, methodView, methodSampleView));
}
return sampleFileViews.build();
}
use of com.google.api.codegen.config.InterfaceContext in project toolkit by googleapis.
the class DynamicLangGapicSamplesTransformer 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 = methodContext.getMethodModel().getRequestStreaming() ? InitCodeOutputType.SingleObject : InitCodeOutputType.FieldList;
SampleContext sampleContext = SampleContext.newBuilder().uniqueSampleId(registry.getUniqueSampleId(sampleConfig, form)).sampleType(SampleSpec.SampleType.STANDALONE).callingForm(form).sampleConfig(sampleConfig).initCodeOutputType(initCodeOutputType).methodContext(methodContext).build();
sampleContexts.add(sampleContext);
}
}
}
}
return sampleContexts.build();
}
use of com.google.api.codegen.config.InterfaceContext in project toolkit by googleapis.
the class DynamicLangGapicSamplesTransformer method generateSamplesFromGapicConfigs.
private List<ViewModel> generateSamplesFromGapicConfigs(List<InterfaceContext> interfaceContexts, GapicProductConfig productConfig, SurfaceNamer namer) {
List<MethodSampleView> allSamples = interfaceContexts.stream().flatMap(c -> apiMethodTransformer.generateApiMethods(c).stream()).flatMap(m -> m.samples().stream()).collect(ImmutableList.toImmutableList());
SampleFileRegistry registry = new SampleFileRegistry(namer, allSamples);
ImmutableList.Builder<ViewModel> sampleFileViews = ImmutableList.builder();
for (InterfaceContext context : interfaceContexts) {
List<OptionalArrayMethodView> methods = apiMethodTransformer.generateApiMethods(context);
for (OptionalArrayMethodView method : methods) {
for (MethodSampleView sample : method.samples()) {
sampleFileViews.add(newSampleFileView(productConfig, context, registry.getSampleFileName(sample, method.name()), method, sample));
}
}
}
return sampleFileViews.build();
}
Aggregations