use of com.google.api.codegen.samplegen.v1p2.SampleSpecProto in project toolkit by googleapis.
the class SampleConfig method createSampleConfigTable.
public static ImmutableTable<String, String, ImmutableList<SampleConfig>> createSampleConfigTable(@Nullable SampleConfigProto sampleConfigProto, final Map<String, InterfaceConfig> interfaceConfigMap) {
if (sampleConfigProto == null) {
sampleConfigProto = SampleConfigProto.getDefaultInstance();
}
// First, apply region tag as IDs if IDs are not given
List<SampleSpecProto> sampleSpecs = new ArrayList<>();
for (SampleSpecProto spec : sampleConfigProto.getSamplesList()) {
if (spec.getId().isEmpty()) {
spec = spec.toBuilder().setId(spec.getRegionTag()).build();
}
sampleSpecs.add(spec);
}
// Then, check user specified sample IDs do not clash
Set<String> distinctIds = new HashSet<>();
Set<String> duplicateIds = sampleSpecs.stream().map(s -> s.getId()).filter(id -> !id.isEmpty()).filter(s -> !distinctIds.add(s)).collect(Collectors.toSet());
Preconditions.checkArgument(duplicateIds.isEmpty(), "Found duplicate IDs: %s", duplicateIds.stream().collect(Collectors.joining(", ")));
// Next, flatten the calling pattern list so we have one per sample
// Note these are not the final calling pattern values, because the
// regexes specified in the config need to be matched against
// language-specific calling pattern definitions.
List<SampleSpecProto> flattenedSampleSpecs = new ArrayList<>();
for (SampleSpecProto spec : sampleSpecs) {
if (spec.getCallingPatternsList().isEmpty()) {
flattenedSampleSpecs.add(spec.toBuilder().addCallingPatterns(DEFAULT_CALLING_PATTERN).build());
}
for (String pattern : spec.getCallingPatternsList()) {
flattenedSampleSpecs.add(spec.toBuilder().clearCallingPatterns().addCallingPatterns(pattern).build());
}
}
// Construct the table.
HashBasedTable<String, String, ArrayList<SampleConfig>> table = HashBasedTable.create();
for (SampleSpecProto sampleSpec : flattenedSampleSpecs) {
SampleConfig config = createOneSampleConfig(sampleSpec, interfaceConfigMap);
if (!table.contains(sampleSpec.getService(), sampleSpec.getRpc())) {
table.put(sampleSpec.getService(), sampleSpec.getRpc(), new ArrayList<>());
}
table.get(sampleSpec.getService(), sampleSpec.getRpc()).add(config);
}
// Make an immutable copy.
return table.cellSet().stream().collect(ImmutableTable.toImmutableTable(Table.Cell::getRowKey, Table.Cell::getColumnKey, v -> ImmutableList.copyOf(v.getValue())));
}
Aggregations