Skip to content

Commit b1a9be4

Browse files
authored
Add static methods for more endpoint rules functions
* Add static method and improve isValidHostLabel * Add public version of substring
1 parent a20eb48 commit b1a9be4

File tree

2 files changed

+53
-32
lines changed

2 files changed

+53
-32
lines changed

smithy-rules-engine/src/main/java/software/amazon/smithy/rulesengine/language/syntax/expressions/functions/IsValidHostLabel.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.Arrays;
88
import java.util.List;
9+
import java.util.regex.Pattern;
910
import software.amazon.smithy.rulesengine.language.evaluation.type.Type;
1011
import software.amazon.smithy.rulesengine.language.evaluation.value.Value;
1112
import software.amazon.smithy.rulesengine.language.syntax.ToExpression;
@@ -19,6 +20,7 @@
1920
@SmithyUnstableApi
2021
public final class IsValidHostLabel extends LibraryFunction {
2122
public static final String ID = "isValidHostLabel";
23+
private static final Pattern HOST_LABEL_PATTERN = Pattern.compile("^[a-zA-Z\\d][a-zA-Z\\d\\-]{0,62}$");
2224
private static final Definition DEFINITION = new Definition();
2325

2426
IsValidHostLabel(FunctionNode functionNode) {
@@ -89,23 +91,30 @@ public Value evaluate(List<Value> arguments) {
8991
return Value.booleanValue(isValidHostLabel(hostLabel, allowDots));
9092
}
9193

92-
private boolean isValidHostLabel(String hostLabel, boolean allowDots) {
93-
if (allowDots) {
94-
// ensure that empty matches at the end are included
95-
for (String subLabel : hostLabel.split("[.]", -1)) {
96-
if (!isValidHostLabel(subLabel, false)) {
97-
return false;
98-
}
99-
}
100-
return true;
101-
} else {
102-
return hostLabel.matches("[a-zA-Z\\d][a-zA-Z\\d\\-]{1,62}");
103-
}
104-
}
105-
10694
@Override
10795
public IsValidHostLabel createFunction(FunctionNode functionNode) {
10896
return new IsValidHostLabel(functionNode);
10997
}
11098
}
99+
100+
/**
101+
* Check if a hostLabel is valid host.
102+
*
103+
* @param hostLabel Host label to check.
104+
* @param allowDots Set to true to allow dots.
105+
* @return true if the label is valid.
106+
*/
107+
public static boolean isValidHostLabel(String hostLabel, boolean allowDots) {
108+
if (!allowDots) {
109+
return HOST_LABEL_PATTERN.matcher(hostLabel).matches();
110+
} else {
111+
// ensure that empty matches at the end are included
112+
for (String subLabel : hostLabel.split("[.]", -1)) {
113+
if (!HOST_LABEL_PATTERN.matcher(subLabel).matches()) {
114+
return false;
115+
}
116+
}
117+
return true;
118+
}
119+
}
111120
}

smithy-rules-engine/src/main/java/software/amazon/smithy/rulesengine/language/syntax/expressions/functions/Substring.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,42 @@ public Value evaluate(List<Value> arguments) {
9797
int startIndex = arguments.get(1).expectIntegerValue().getValue();
9898
int stopIndex = arguments.get(2).expectIntegerValue().getValue();
9999
boolean reverse = arguments.get(3).expectBooleanValue().getValue();
100+
String result = getSubstring(str, startIndex, stopIndex, reverse);
101+
return result == null ? Value.emptyValue() : Value.stringValue(result);
102+
}
100103

101-
for (int i = 0; i < str.length(); i++) {
102-
char ch = str.charAt(i);
103-
if (!(ch <= 127)) {
104-
return Value.emptyValue();
105-
}
106-
}
104+
@Override
105+
public Substring createFunction(FunctionNode functionNode) {
106+
return new Substring(functionNode);
107+
}
108+
}
107109

108-
if (startIndex >= stopIndex || str.length() < stopIndex) {
109-
return Value.emptyValue();
110+
/**
111+
* Get the substring of {@code value} using the same logic as the substring function.
112+
*
113+
* @param value Value to get the substring of.
114+
* @param startIndex Start index.
115+
* @param stopIndex Stop index.
116+
* @param reverse True if the slice is from the end.
117+
* @return the substring value or null.
118+
*/
119+
public static String getSubstring(String value, int startIndex, int stopIndex, boolean reverse) {
120+
for (int i = 0; i < value.length(); i++) {
121+
if (!(value.charAt(i) <= 127)) {
122+
return null;
110123
}
124+
}
111125

112-
if (!reverse) {
113-
return Value.stringValue(str.substring(startIndex, stopIndex));
114-
} else {
115-
int revStart = str.length() - stopIndex;
116-
int revStop = str.length() - startIndex;
117-
return Value.stringValue(str.substring(revStart, revStop));
118-
}
126+
if (startIndex >= stopIndex || value.length() < stopIndex) {
127+
return null;
119128
}
120129

121-
@Override
122-
public Substring createFunction(FunctionNode functionNode) {
123-
return new Substring(functionNode);
130+
if (!reverse) {
131+
return value.substring(startIndex, stopIndex);
132+
} else {
133+
int revStart = value.length() - stopIndex;
134+
int revStop = value.length() - startIndex;
135+
return value.substring(revStart, revStop);
124136
}
125137
}
126138
}

0 commit comments

Comments
 (0)