1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.google.api.generator.gapic.protoparser; 16 17 import com.google.api.pathtemplate.PathTemplate; 18 import com.google.common.collect.ImmutableSortedSet; 19 import java.util.Set; 20 21 public class PatternParser { 22 23 // This method tries to parse all named segments from pattern and sort in natual order 24 // e.g. /v1beta1/{table_name=tests/*}/{routing_id=instances/*}/** -> (routing_id, table_name) getPatternBindings(String pattern)25 public static Set<String> getPatternBindings(String pattern) { 26 ImmutableSortedSet.Builder<String> bindings = ImmutableSortedSet.naturalOrder(); 27 if (pattern.isEmpty()) { 28 return bindings.build(); 29 } 30 31 PathTemplate template = PathTemplate.create(pattern); 32 // Filter out any unbound variable like "$0, $1, etc. 33 template.vars().stream().filter(s -> !s.contains("$")).forEach(bindings::add); 34 return bindings.build(); 35 } 36 } 37