Skip to content

Commit c3d0398

Browse files
committed
Minimal CelMatcher
1 parent debd599 commit c3d0398

File tree

4 files changed

+110
-4
lines changed

4 files changed

+110
-4
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2024 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.xds.internal.matchers;
18+
19+
import java.util.function.Predicate;
20+
21+
/** Unified Matcher API: xds.type.matcher.v3.CelMatcher. */
22+
public class CelMatcher implements Predicate<HttpMatchInput> {
23+
private final String description;
24+
25+
public CelMatcher(String description) {
26+
// TODO(sergiitk): cache parsed CEL expressions
27+
this.description = description != null ? description : "";
28+
}
29+
30+
public String description() {
31+
return description;
32+
}
33+
34+
@Override
35+
public boolean test(HttpMatchInput httpMatchInput) {
36+
if (httpMatchInput.headers().keys().isEmpty()) {
37+
return false;
38+
}
39+
// TODO(sergiitk): [IMPL] convert headers to cel args
40+
return true;
41+
}
42+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2024 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package io.grpc.xds.internal.matchers;
19+
20+
import com.google.auto.value.AutoValue;
21+
import io.grpc.Metadata;
22+
23+
@AutoValue
24+
public abstract class HttpMatchInput {
25+
public abstract Metadata headers();
26+
// TODO(sergiitk): [IMPL] consider
27+
// public abstract ServerCall<?, ?> serverCall();
28+
}

xds/src/main/java/io/grpc/xds/internal/rlqs/RlqsBucketSettings.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@
2020
import com.google.common.base.Function;
2121
import com.google.common.collect.ImmutableMap;
2222
import com.google.protobuf.Duration;
23-
import io.grpc.Metadata;
23+
import io.grpc.xds.internal.matchers.HttpMatchInput;
2424

2525
@AutoValue
2626
public abstract class RlqsBucketSettings {
2727

28-
public abstract ImmutableMap<String, Function<Metadata, String>> bucketIdBuilder();
28+
public abstract ImmutableMap<String, Function<HttpMatchInput, String>> bucketIdBuilder();
2929

3030
public abstract Duration reportingInterval();
3131

3232
public static RlqsBucketSettings create(
33-
ImmutableMap<String, Function<Metadata, String>> bucketIdBuilder,
33+
ImmutableMap<String, Function<HttpMatchInput, String>> bucketIdBuilder,
3434
Duration reportingInterval) {
3535
return new AutoValue_RlqsBucketSettings(bucketIdBuilder, reportingInterval);
3636
}
37-
3837
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.xds.internal.matchers;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.JUnit4;
24+
25+
@RunWith(JUnit4.class)
26+
public class CelMatcherTest {
27+
28+
@Test
29+
public void construct() {
30+
String description = "Optional description";
31+
CelMatcher matcher = new CelMatcher(description);
32+
assertThat(matcher.description()).isEqualTo(description);
33+
34+
matcher = new CelMatcher(null);
35+
assertThat(matcher.description()).isEqualTo("");
36+
}
37+
}

0 commit comments

Comments
 (0)