Skip to content

Commit 7b50380

Browse files
committed
Refactor StringMatcher parsing logic
1 parent 82d9a8b commit 7b50380

2 files changed

Lines changed: 33 additions & 37 deletions

File tree

xds/src/main/java/io/grpc/xds/internal/MatcherParser.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,36 @@ public static Matchers.StringMatcher parseStringMatcher(
9797
"Unknown StringMatcher match pattern: " + proto.getMatchPatternCase());
9898
}
9999
}
100+
101+
/** Translate StringMatcher xDS proto to internal StringMatcher. */
102+
public static Matchers.StringMatcher parseStringMatcher(
103+
com.github.xds.type.matcher.v3.StringMatcher proto) {
104+
switch (proto.getMatchPatternCase()) {
105+
case EXACT:
106+
return Matchers.StringMatcher.forExact(proto.getExact(), proto.getIgnoreCase());
107+
case PREFIX:
108+
return Matchers.StringMatcher.forPrefix(
109+
checkNonEmpty(proto.getPrefix(), "prefix"), proto.getIgnoreCase());
110+
case SUFFIX:
111+
return Matchers.StringMatcher.forSuffix(
112+
checkNonEmpty(proto.getSuffix(), "suffix"), proto.getIgnoreCase());
113+
case CONTAINS:
114+
return Matchers.StringMatcher.forContains(
115+
checkNonEmpty(proto.getContains(), "contains"), proto.getIgnoreCase());
116+
case SAFE_REGEX:
117+
String regex = checkNonEmpty(proto.getSafeRegex().getRegex(), "regex");
118+
return Matchers.StringMatcher.forSafeRegEx(Pattern.compile(regex));
119+
default:
120+
throw new IllegalArgumentException(
121+
"Unknown StringMatcher match pattern: " + proto.getMatchPatternCase());
122+
}
123+
}
124+
125+
private static String checkNonEmpty(String value, String name) {
126+
if (value.isEmpty()) {
127+
throw new IllegalArgumentException("StringMatcher " + name
128+
+ " (match_pattern) must be non-empty");
129+
}
130+
return value;
131+
}
100132
}

xds/src/main/java/io/grpc/xds/internal/matcher/PredicateEvaluator.java

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -125,43 +125,7 @@ private static final class SinglePredicateEvaluator extends PredicateEvaluator {
125125

126126
private static Matchers.StringMatcher fromStringMatcherProto(
127127
com.github.xds.type.matcher.v3.StringMatcher proto) {
128-
if (proto.hasExact()) {
129-
return Matchers.StringMatcher.forExact(proto.getExact(), proto.getIgnoreCase());
130-
}
131-
if (proto.hasPrefix()) {
132-
String prefix = proto.getPrefix();
133-
if (prefix.isEmpty()) {
134-
throw new IllegalArgumentException(
135-
"StringMatcher prefix (match_pattern) must be non-empty");
136-
}
137-
return Matchers.StringMatcher.forPrefix(prefix, proto.getIgnoreCase());
138-
}
139-
if (proto.hasSuffix()) {
140-
String suffix = proto.getSuffix();
141-
if (suffix.isEmpty()) {
142-
throw new IllegalArgumentException(
143-
"StringMatcher suffix (match_pattern) must be non-empty");
144-
}
145-
return Matchers.StringMatcher.forSuffix(suffix, proto.getIgnoreCase());
146-
}
147-
if (proto.hasContains()) {
148-
String contains = proto.getContains();
149-
if (contains.isEmpty()) {
150-
throw new IllegalArgumentException(
151-
"StringMatcher contains (match_pattern) must be non-empty");
152-
}
153-
return Matchers.StringMatcher.forContains(contains, proto.getIgnoreCase());
154-
}
155-
if (proto.hasSafeRegex()) {
156-
String regex = proto.getSafeRegex().getRegex();
157-
if (regex.isEmpty()) {
158-
throw new IllegalArgumentException(
159-
"StringMatcher regex (match_pattern) must be non-empty");
160-
}
161-
return Matchers.StringMatcher.forSafeRegEx(
162-
com.google.re2j.Pattern.compile(regex));
163-
}
164-
throw new IllegalArgumentException("Unknown StringMatcher match pattern");
128+
return io.grpc.xds.internal.MatcherParser.parseStringMatcher(proto);
165129
}
166130
}
167131

0 commit comments

Comments
 (0)