Skip to content

Commit

Permalink
feat:支持范围表达式匹配&参数匹配逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Nov 8, 2023
1 parent 4da8a7b commit 2f3a9f4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static boolean matchStringValue(MatchString matchString, String actualVal
return matchStringValue(matchType, actualValue, matchValue, regexToPattern);
}

private static boolean matchStringValue(MatchStringType matchType, String actualValue, String matchValue) {
public static boolean matchStringValue(MatchStringType matchType, String actualValue, String matchValue) {
return matchStringValue(matchType, actualValue, matchValue, DEFAULT_REGEX_PATTERN);
}

Expand Down Expand Up @@ -105,6 +105,20 @@ private static boolean matchStringValue(MatchStringType matchType, String actual
}
return true;
}
case RANGE: {
String[] tokens = matchValue.split("~");
if (tokens.length != 2) {
return false;
}
try {
long left = Long.parseLong(tokens[0]);
long right = Long.parseLong(tokens[1]);
long matchV = Long.parseLong(actualValue);
return matchV >= left && matchV <= right;
} catch (NumberFormatException ignore) {
return false;
}
}
}
return false;
}
Expand Down Expand Up @@ -148,9 +162,8 @@ public static boolean matchMetadata(Map<String, MatchString> ruleMeta, Map<Strin
&& ruleMetaValue.getValueType() != MatchString.ValueType.PARAMETER) {
continue;
}

// 这里获取到的是真正流量标签的 value 或者实例的标签 value
String destMetaValue = destMeta.get(ruleMetaKey);

allMetaMatched = isAllMetaMatched(isMatchSource, ruleMetaKey, ruleMetaValue, destMetaValue,
multiEnvRouterParamMap, variables);
}
Expand Down Expand Up @@ -196,11 +209,11 @@ private static boolean matchValueByValueType(boolean isMatchSource, String ruleM
// 当匹配的是source,记录请求的 K V
multiEnvRouterParamMap.put(ruleMetaKey, destMetaValue);
} else {
// 当匹配的是dest, 判断value
if (!multiEnvRouterParamMap.containsKey(ruleMetaKey)) {
// 当匹配的是dest, 并且如果是参数类型, 则需要根据 dest 的 value 获取到流量标签里面对应 key 的真实 value
if (!multiEnvRouterParamMap.containsKey(ruleMetaValue.getValue().getValue())) {
allMetaMatched = false;
} else {
String ruleValue = multiEnvRouterParamMap.get(ruleMetaKey);
String ruleValue = multiEnvRouterParamMap.get(ruleMetaValue.getValue().getValue());
// contains key
allMetaMatched = matchStringValue(ruleMetaValue.getType(), ruleValue, destMetaValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;

import static com.tencent.polaris.api.utils.RuleUtils.matchMetadata;
import static com.tencent.polaris.api.utils.RuleUtils.matchStringValue;
import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -105,4 +106,17 @@ public void testMatchMetadata() {

assertThat(matchMetadata(ruleMeta3, destMeta3)).isTrue();
}

@Test
public void testRangeMatch() {
assertThat(matchStringValue(ModelProto.MatchString.MatchStringType.RANGE, "123", "123~456")).isTrue();
assertThat(matchStringValue(ModelProto.MatchString.MatchStringType.RANGE, "231", "123~456")).isTrue();
assertThat(matchStringValue(ModelProto.MatchString.MatchStringType.RANGE, "456", "123~456")).isTrue();
assertThat(matchStringValue(ModelProto.MatchString.MatchStringType.RANGE, "789", "123~456")).isFalse();
assertThat(matchStringValue(ModelProto.MatchString.MatchStringType.RANGE, "0", "123~456")).isFalse();
assertThat(matchStringValue(ModelProto.MatchString.MatchStringType.RANGE, "a", "123~456")).isFalse();
assertThat(matchStringValue(ModelProto.MatchString.MatchStringType.RANGE, "123", "a~456")).isFalse();
assertThat(matchStringValue(ModelProto.MatchString.MatchStringType.RANGE, "123", "123~123as")).isFalse();
}

}

0 comments on commit 2f3a9f4

Please sign in to comment.