use of com.google.api.codegen.config.SampleConfig 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.SampleConfig in project toolkit by googleapis.
the class SampleTransformer method generateSamples.
/**
* Returns a list of MethodSampleViews for the given MethodContext.
*
* @param initContext
* @param methodContext
* @param fieldConfigs
* @param initCodeOutputType
* @param callingForms The list of calling forms applicable to this method, for which we will
* generate samples if so configured via context.getMethodConfig()
* @return A list of of the MethodSampleView, each of which corresponds to a specific sample
* forthe method
*/
public List<MethodSampleView> generateSamples(MethodContext methodContext, InitCodeContext initContext, Collection<FieldConfig> fieldConfigs, InitCodeOutputType initCodeOutputType, List<CallingForm> callingForms) {
CallingForm defaultCallingForm = methodContext.getNamer().getDefaultCallingForm(methodContext);
List<MethodSampleView> methodSampleViews = new ArrayList<>();
MethodConfig methodConfig = methodContext.getMethodConfig();
SampleValueSet defaultValueSet = defaultValueSet(methodConfig);
for (SampleConfig sampleConfig : methodConfig.getSampleSpec().getSampleConfigs(callingForms, defaultCallingForm, defaultValueSet, sampleType())) {
// Do not override outer initContext
InitCodeContext thisContext = initContext;
if (thisContext == null) {
thisContext = createInitCodeContext(methodContext, fieldConfigs, initCodeOutputType, sampleConfig.valueSet());
}
methodSampleViews.add(generateSample(sampleConfig, methodContext, thisContext));
}
return methodSampleViews;
}
use of com.google.api.codegen.config.SampleConfig 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.SampleConfig 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.SampleConfig in project toolkit by googleapis.
the class OutputTransformerTest method setUp.
@Before
public void setUp() {
SampleConfig sampleConfig = SampleConfig.newBuilder().id("test-sample-value-set-id").type(SampleSpec.SampleType.STANDALONE).build();
sampleContext = SampleContext.newBuilder().uniqueSampleId("test-sample-value-set-id").sampleConfig(sampleConfig).sampleType(SampleSpec.SampleType.STANDALONE).callingForm(form).initCodeOutputType(InitCodeOutputType.FieldList).build();
parent = new OutputTransformer.ScopeTable();
child = new OutputTransformer.ScopeTable(parent);
MockitoAnnotations.initMocks(this);
when(context.getFeatureConfig()).thenReturn(featureConfig);
when(context.getMethodConfig()).thenReturn(config);
when(context.getMethodModel()).thenReturn(model);
when(context.getNamer()).thenReturn(namer);
when(context.getTypeTable()).thenReturn(typeTable);
when(model.getSimpleName()).thenReturn("methodSimpleName");
when(namer.getSampleResponseVarName(context, form)).thenReturn("sampleResponseVarName");
}
Aggregations