-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add duration and regex literal validators
PiperOrigin-RevId: 561101227
- Loading branch information
1 parent
5593808
commit 149f12d
Showing
10 changed files
with
686 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
validator/src/main/java/dev/cel/validator/validators/DurationLiteralValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dev.cel.validator.validators; | ||
|
||
import com.google.protobuf.Duration; | ||
|
||
/** DurationLiteralValidator ensures that duration literal arguments are valid. */ | ||
public class DurationLiteralValidator extends LiteralValidator { | ||
public static final DurationLiteralValidator INSTANCE = | ||
new DurationLiteralValidator("duration", Duration.class); | ||
|
||
private DurationLiteralValidator(String functionName, Class<?> expectedResultType) { | ||
super(functionName, expectedResultType); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
validator/src/main/java/dev/cel/validator/validators/LiteralValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dev.cel.validator.validators; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import dev.cel.bundle.Cel; | ||
import dev.cel.common.CelAbstractSyntaxTree; | ||
import dev.cel.common.CelSource; | ||
import dev.cel.common.ast.CelExpr; | ||
import dev.cel.common.ast.CelExpr.ExprKind.Kind; | ||
import dev.cel.common.navigation.CelNavigableAst; | ||
import dev.cel.common.navigation.CelNavigableExpr; | ||
import dev.cel.validator.CelAstValidator; | ||
import java.util.Optional; | ||
|
||
/** | ||
* LiteralValidator defines the common logic to handle simple validation of a literal in a function | ||
* call by evaluating it and ensuring that no errors are thrown (example: duration / timestamp | ||
* literals). | ||
*/ | ||
abstract class LiteralValidator implements CelAstValidator { | ||
private final String functionName; | ||
private final Class<?> expectedResultType; | ||
|
||
protected LiteralValidator(String functionName, Class<?> expectedResultType) { | ||
this.functionName = functionName; | ||
this.expectedResultType = expectedResultType; | ||
} | ||
|
||
@Override | ||
public void validate(CelNavigableAst navigableAst, Cel cel, IssuesFactory issuesFactory) { | ||
navigableAst | ||
.getRoot() | ||
.descendants() | ||
.filter( | ||
node -> | ||
node.getKind().equals(Kind.CONSTANT) | ||
&& node.parent() | ||
.map( | ||
parent -> parent.expr().callOrDefault().function().equals(functionName)) | ||
.orElse(false)) | ||
.map(CelNavigableExpr::expr) | ||
.forEach( | ||
expr -> { | ||
CelExpr callExpr = | ||
CelExpr.ofCallExpr( | ||
1, | ||
Optional.empty(), | ||
functionName, | ||
ImmutableList.of(CelExpr.ofConstantExpr(2, expr.constant()))); | ||
|
||
CelAbstractSyntaxTree ast = | ||
CelAbstractSyntaxTree.newParsedAst(callExpr, CelSource.newBuilder().build()); | ||
try { | ||
ast = cel.check(ast).getAst(); | ||
Object result = cel.createProgram(ast).eval(); | ||
|
||
if (!expectedResultType.isInstance(result)) { | ||
throw new IllegalStateException( | ||
String.format( | ||
"Expected %s type but got %s instead", | ||
expectedResultType.getName(), result.getClass().getName())); | ||
} | ||
|
||
} catch (Exception e) { | ||
issuesFactory.addError( | ||
expr.id(), | ||
String.format( | ||
"%s validation failed. Reason: %s", functionName, e.getMessage())); | ||
} | ||
}); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
validator/src/main/java/dev/cel/validator/validators/RegexLiteralValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dev.cel.validator.validators; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import dev.cel.bundle.Cel; | ||
import dev.cel.common.ast.CelExpr; | ||
import dev.cel.common.ast.CelExpr.ExprKind.Kind; | ||
import dev.cel.common.navigation.CelNavigableAst; | ||
import dev.cel.validator.CelAstValidator; | ||
import java.util.regex.Pattern; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
/** RegexLiteralValidator ensures that regex patterns are valid. */ | ||
public class RegexLiteralValidator implements CelAstValidator { | ||
public static final RegexLiteralValidator INSTANCE = new RegexLiteralValidator(); | ||
|
||
@Override | ||
public void validate(CelNavigableAst navigableAst, Cel cel, IssuesFactory issuesFactory) { | ||
navigableAst | ||
.getRoot() | ||
.descendants() | ||
.filter(node -> node.expr().callOrDefault().function().equals("matches")) | ||
.filter(node -> ImmutableList.of(1, 2).contains(node.expr().call().args().size())) | ||
.map(node -> node.expr().call()) | ||
.forEach( | ||
matchesCallExpr -> { | ||
CelExpr regexArg = | ||
matchesCallExpr.target().isPresent() | ||
? matchesCallExpr.args().get(0) | ||
: matchesCallExpr.args().get(1); | ||
if (!regexArg.exprKind().getKind().equals(Kind.CONSTANT)) { | ||
return; | ||
} | ||
|
||
String regexPattern = regexArg.constant().stringValue(); | ||
try { | ||
Pattern.compile(regexPattern); | ||
} catch (PatternSyntaxException e) { | ||
issuesFactory.addError( | ||
regexArg.id(), "Regex validation failed. Reason: " + e.getMessage()); | ||
} | ||
}); | ||
} | ||
|
||
private RegexLiteralValidator() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.