Skip to content

Commit 20c1933

Browse files
l46kokcopybara-github
authored andcommitted
Add capability to visit custom tags
PiperOrigin-RevId: 649184086
1 parent 29f344b commit 20c1933

File tree

16 files changed

+443
-88
lines changed

16 files changed

+443
-88
lines changed

policy/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ java_library(
88
exports = ["//policy/src/main/java/dev/cel/policy"],
99
)
1010

11+
java_library(
12+
name = "value_string",
13+
exports = ["//policy/src/main/java/dev/cel/policy:value_string"],
14+
)
15+
1116
java_library(
1217
name = "source",
1318
exports = ["//policy/src/main/java/dev/cel/policy:source"],
@@ -33,6 +38,11 @@ java_library(
3338
exports = ["//policy/src/main/java/dev/cel/policy:parser"],
3439
)
3540

41+
java_library(
42+
name = "parser_context",
43+
exports = ["//policy/src/main/java/dev/cel/policy:parser_context"],
44+
)
45+
3646
java_library(
3747
name = "parser_factory",
3848
exports = ["//policy/src/main/java/dev/cel/policy:parser_factory"],

policy/src/main/java/dev/cel/policy/BUILD.bazel

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ java_library(
109109
":source",
110110
":validation_exception",
111111
":value_string",
112+
"//common:source",
112113
"//common/internal",
113114
"@maven//:com_google_guava_guava",
114115
"@maven//:org_yaml_snakeyaml",
@@ -227,7 +228,6 @@ java_library(
227228
srcs = [
228229
"ValueString.java",
229230
],
230-
visibility = ["//visibility:private"],
231231
deps = [
232232
"//:auto_value",
233233
],
@@ -262,8 +262,9 @@ java_library(
262262
srcs = [
263263
"ParserContext.java",
264264
],
265-
visibility = ["//visibility:private"],
266265
deps = [
266+
":policy",
267+
":value_string",
267268
"//common:source",
268269
],
269270
)

policy/src/main/java/dev/cel/policy/CelPolicy.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import com.google.auto.value.AutoOneOf;
2020
import com.google.auto.value.AutoValue;
2121
import com.google.common.collect.ImmutableList;
22+
import com.google.common.collect.ImmutableMap;
2223
import com.google.common.collect.ImmutableSet;
2324
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2425
import java.util.Arrays;
26+
import java.util.Map;
2527
import java.util.Optional;
2628

2729
/**
@@ -37,11 +39,14 @@ public abstract class CelPolicy {
3739

3840
public abstract CelPolicySource policySource();
3941

42+
public abstract ImmutableMap<String, Object> metadata();
43+
4044
/** Creates a new builder to construct a {@link CelPolicy} instance. */
4145
public static Builder newBuilder() {
4246
return new AutoValue_CelPolicy.Builder()
4347
.setName(ValueString.of(0, ""))
44-
.setRule(Rule.newBuilder().build());
48+
.setRule(Rule.newBuilder().build())
49+
.setMetadata(ImmutableMap.of());
4550
}
4651

4752
/** Builder for {@link CelPolicy}. */
@@ -55,6 +60,23 @@ public abstract static class Builder {
5560

5661
public abstract Builder setPolicySource(CelPolicySource policySource);
5762

63+
// This should stay package-private to encourage add/set methods to be used instead.
64+
abstract ImmutableMap.Builder<String, Object> metadataBuilder();
65+
66+
public abstract Builder setMetadata(ImmutableMap<String, Object> value);
67+
68+
@CanIgnoreReturnValue
69+
public Builder putMetadata(String key, Object value) {
70+
metadataBuilder().put(key, value);
71+
return this;
72+
}
73+
74+
@CanIgnoreReturnValue
75+
public Builder putMetadata(Map<String, Object> map) {
76+
metadataBuilder().putAll(map);
77+
return this;
78+
}
79+
5880
public abstract CelPolicy build();
5981
}
6082

policy/src/main/java/dev/cel/policy/CelPolicyParser.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package dev.cel.policy;
1616

17+
import dev.cel.policy.ParserContext.PolicyParserContext;
18+
1719
/** CelPolicyParser is the interface for parsing policies into a canonical Policy representation. */
1820
public interface CelPolicyParser {
1921

@@ -41,47 +43,54 @@ interface TagVisitor<T> {
4143
* allow for continued parsing within a custom tag.
4244
*/
4345
default void visitPolicyTag(
44-
ParserContext<T> ctx, long id, String fieldName, T node, CelPolicy.Builder policyBuilder) {
45-
ctx.reportError(id, String.format("Unsupported policy tag: %s", fieldName));
46+
PolicyParserContext<T> ctx,
47+
long id,
48+
String tagName,
49+
T node,
50+
CelPolicy.Builder policyBuilder) {
51+
ctx.reportError(id, String.format("Unsupported policy tag: %s", tagName));
4652
}
4753

4854
/**
4955
* visitRuleTag accepts a parser context, field id, tag name, yaml node, as well as the parent
5056
* policy and current rule to allow for continued parsing within custom tags.
5157
*/
5258
default void visitRuleTag(
53-
ParserContext<T> ctx,
59+
PolicyParserContext<T> ctx,
5460
long id,
55-
String fieldName,
61+
String tagName,
5662
T node,
63+
CelPolicy.Builder policyBuilder,
5764
CelPolicy.Rule.Builder ruleBuilder) {
58-
ctx.reportError(id, String.format("Unsupported rule tag: %s", fieldName));
65+
ctx.reportError(id, String.format("Unsupported rule tag: %s", tagName));
5966
}
6067

6168
/**
6269
* visitMatchTag accepts a parser context, field id, tag name, yaml node, as well as the parent
6370
* policy and current match to allow for continued parsing within custom tags.
6471
*/
6572
default void visitMatchTag(
66-
ParserContext<T> ctx,
73+
PolicyParserContext<T> ctx,
6774
long id,
68-
String fieldName,
75+
String tagName,
6976
T node,
77+
CelPolicy.Builder policyBuilder,
7078
CelPolicy.Match.Builder matchBuilder) {
71-
ctx.reportError(id, String.format("Unsupported match tag: %s", fieldName));
79+
ctx.reportError(id, String.format("Unsupported match tag: %s", tagName));
7280
}
7381

7482
/**
7583
* visitVariableTag accepts a parser context, field id, tag name, yaml node, as well as the
7684
* parent policy and current variable to allow for continued parsing within custom tags.
7785
*/
7886
default void visitVariableTag(
79-
ParserContext<T> ctx,
87+
PolicyParserContext<T> ctx,
8088
long id,
81-
String fieldName,
89+
String tagName,
8290
T node,
91+
CelPolicy.Builder policyBuilder,
8392
CelPolicy.Variable.Builder variableBuilder) {
84-
ctx.reportError(id, String.format("Unsupported variable tag: %s", fieldName));
93+
ctx.reportError(id, String.format("Unsupported variable tag: %s", tagName));
8594
}
8695
}
8796
}

0 commit comments

Comments
 (0)