Skip to content

Commit

Permalink
Minimal CelMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiitk committed Aug 14, 2024
1 parent 178a378 commit 9d69f97
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
42 changes: 42 additions & 0 deletions xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024 The gRPC Authors
*
* 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
*
* http://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 io.grpc.xds.internal.matchers;

import java.util.function.Predicate;

/** Unified Matcher API: xds.type.matcher.v3.CelMatcher. */
public class CelMatcher implements Predicate<HttpMatchInput> {
private final String description;

public CelMatcher(String description) {
// TODO(sergiitk): cache parsed CEL expressions
this.description = description != null ? description : "";
}

public String description() {
return description;
}

@Override
public boolean test(HttpMatchInput httpMatchInput) {
if (httpMatchInput.headers().keys().isEmpty()) {
return false;
}
// TODO(sergiitk): [IMPL] convert headers to cel args
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 The gRPC Authors
*
* 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
*
* http://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 io.grpc.xds.internal.matchers;

import com.google.auto.value.AutoValue;
import io.grpc.Metadata;

@AutoValue
public abstract class HttpMatchInput {
public abstract Metadata headers();
// TODO(sergiitk): [IMPL] consider
// public abstract ServerCall<?, ?> serverCall();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.Duration;
import io.grpc.Metadata;
import io.grpc.xds.internal.matchers.HttpMatchInput;

@AutoValue
public abstract class RlqsBucketSettings {

public abstract ImmutableMap<String, Function<Metadata, String>> bucketIdBuilder();
public abstract ImmutableMap<String, Function<HttpMatchInput, String>> bucketIdBuilder();

public abstract Duration reportingInterval();

public static RlqsBucketSettings create(
ImmutableMap<String, Function<Metadata, String>> bucketIdBuilder,
ImmutableMap<String, Function<HttpMatchInput, String>> bucketIdBuilder,
Duration reportingInterval) {
return new AutoValue_RlqsBucketSettings(bucketIdBuilder, reportingInterval);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 The gRPC Authors
*
* 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
*
* http://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 io.grpc.xds.internal.matchers;

import static com.google.common.truth.Truth.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class CelMatcherTest {

@Test
public void construct() {
String description = "Optional description";
CelMatcher matcher = new CelMatcher(description);
assertThat(matcher.description()).isEqualTo(description);

matcher = new CelMatcher(null);
assertThat(matcher.description()).isEqualTo("");
}
}

0 comments on commit 9d69f97

Please sign in to comment.