Search in sources :

Example 1 with SampleSpecProto

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())));
}
Also used : SampleValueSet(com.google.api.codegen.SampleValueSet) CallingForm(com.google.api.codegen.viewmodel.CallingForm) RequestFieldProto(com.google.api.codegen.samplegen.v1p2.RequestFieldProto) ResponseStatementProto(com.google.api.codegen.samplegen.v1p2.ResponseStatementProto) Set(java.util.Set) HashBasedTable(com.google.common.collect.HashBasedTable) SampleConfigProto(com.google.api.codegen.samplegen.v1p2.SampleConfigProto) Collectors(java.util.stream.Collectors) SampleSpecProto(com.google.api.codegen.samplegen.v1p2.SampleSpecProto) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ImmutableTable(com.google.common.collect.ImmutableTable) Map(java.util.Map) AutoValue(com.google.auto.value.AutoValue) Preconditions(com.google.common.base.Preconditions) Table(com.google.common.collect.Table) Nullable(javax.annotation.Nullable) HashBasedTable(com.google.common.collect.HashBasedTable) ImmutableTable(com.google.common.collect.ImmutableTable) Table(com.google.common.collect.Table) ArrayList(java.util.ArrayList) SampleSpecProto(com.google.api.codegen.samplegen.v1p2.SampleSpecProto) HashSet(java.util.HashSet)

Aggregations

SampleValueSet (com.google.api.codegen.SampleValueSet)1 RequestFieldProto (com.google.api.codegen.samplegen.v1p2.RequestFieldProto)1 ResponseStatementProto (com.google.api.codegen.samplegen.v1p2.ResponseStatementProto)1 SampleConfigProto (com.google.api.codegen.samplegen.v1p2.SampleConfigProto)1 SampleSpecProto (com.google.api.codegen.samplegen.v1p2.SampleSpecProto)1 CallingForm (com.google.api.codegen.viewmodel.CallingForm)1 AutoValue (com.google.auto.value.AutoValue)1 Preconditions (com.google.common.base.Preconditions)1 HashBasedTable (com.google.common.collect.HashBasedTable)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableTable (com.google.common.collect.ImmutableTable)1 Table (com.google.common.collect.Table)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1