Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ lombok {
}

group 'com.gotocompany'
version '0.10.5'
version '0.10.6'

def projName = "firehose"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import dev.cel.common.types.CelKind;
import dev.cel.compiler.CelCompiler;
import dev.cel.runtime.CelRuntime;
import dev.cel.runtime.CelRuntimeFactory;
import lombok.extern.slf4j.Slf4j;

/**
Expand Down Expand Up @@ -52,8 +51,7 @@ public boolean evaluate(Message payload) {
*/
private void buildCelEnvironment(String celExpression) {
CelCompiler celCompiler = CelUtils.initializeCelCompiler(this.descriptor);
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder()
.build();
CelRuntime celRuntime = CelUtils.initializeCelRuntime();
this.celProgram = CelUtils.initializeCelProgram(celExpression, celRuntime, celCompiler,
celType -> celType.kind().equals(CelKind.BOOL));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.gotocompany.firehose.utils.CelUtils;
import dev.cel.compiler.CelCompiler;
import dev.cel.runtime.CelRuntime;
import dev.cel.runtime.CelRuntimeFactory;
import io.grpc.Metadata;
import org.apache.commons.collections.MapUtils;

Expand Down Expand Up @@ -102,7 +101,7 @@ private Object evaluateExpression(String input, Message message) {
* @return a map of CEL expressions to their corresponding programs
*/
private Map<String, CelRuntime.Program> initializeCelPrograms() {
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
CelRuntime celRuntime = CelUtils.initializeCelRuntime();
CelCompiler celCompiler = CelUtils.initializeCelCompiler(this.descriptor);
return this.metadataTemplate.entrySet()
.stream()
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/com/gotocompany/firehose/utils/CelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelOptions;
import dev.cel.common.CelValidationException;
import dev.cel.common.types.CelType;
import dev.cel.common.types.StructTypeReference;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerFactory;
import dev.cel.extensions.CelExtensions;
import dev.cel.parser.CelStandardMacro;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelRuntime;
import org.aeonbits.owner.util.Collections;

import dev.cel.runtime.CelRuntimeFactory;
import java.util.Collections;
import java.util.function.Predicate;

/**
Expand All @@ -28,7 +30,7 @@ public class CelUtils {
*/
public static Object evaluate(CelRuntime.Program program, Message payload) {
try {
return program.eval(Collections.map(payload.getDescriptorForType().getFullName(), payload));
return program.eval(Collections.singletonMap(payload.getDescriptorForType().getFullName(), payload));
} catch (CelEvaluationException e) {
throw new IllegalArgumentException("Could not evaluate Cel expression", e);
}
Expand All @@ -43,10 +45,22 @@ public static CelCompiler initializeCelCompiler(Descriptors.Descriptor descripto
return CelCompilerFactory.standardCelCompilerBuilder()
.setStandardMacros(CelStandardMacro.values())
.addVar(descriptor.getFullName(), StructTypeReference.create(descriptor.getFullName()))
.addLibraries(CelExtensions.strings(), CelExtensions.bindings(), CelExtensions.math(CelOptions.DEFAULT))
.addMessageTypes(descriptor)
.build();
}

/**
* Initializes the CEL runtime with extended libraries.
*
* @return the initialized CEL runtime
*/
public static CelRuntime initializeCelRuntime() {
return CelRuntimeFactory.standardCelRuntimeBuilder()
.addLibraries(CelExtensions.strings(), CelExtensions.math(CelOptions.DEFAULT))
.build();
}

/**
* Initializes a CEL program for a given expression.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ public class ProtoToMetadataMapperTest {
public void setup() {
Map<String, String> template = new HashMap<>();
template.put("$GenericResponse.detail", "$GenericResponse.success");
template.put("detail", "$GenericResponse.detail.lowerAscii()");
template.put("someField", "someValue");
template.put("$GenericResponse.success", "staticValue");
template.put("staticKey", "$(GenericResponse.errors[0].cause + '-' + GenericResponse.errors[0].code + '-' + string(GenericResponse.code))");
template.put("entity", "$GenericResponse.errors[0].entity");
template.put("binding", "$cel.bind(code, GenericResponse.code, code + 100)");
template.put("math", "$math.greatest(GenericResponse.code, 200)");
this.protoToMetadataMapper = new ProtoToMetadataMapper(
GenericResponse.getDescriptor(),
template
Expand All @@ -32,7 +35,7 @@ public void setup() {
public void shouldBuildDynamicMetadataWithCorrectPlaceholders() {
GenericResponse payload = GenericResponse.newBuilder()
.setSuccess(false)
.setDetail("detail_of_error")
.setDetail("Detail_Of_Error")
.setCode(100)
.addErrors(GenericError.newBuilder()
.setCode("404")
Expand All @@ -44,6 +47,8 @@ public void shouldBuildDynamicMetadataWithCorrectPlaceholders() {

Assertions.assertTrue(metadata.containsKey(Metadata.Key.of("detail_of_error", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertEquals("false", metadata.get(Metadata.Key.of("detail_of_error", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertTrue(metadata.containsKey(Metadata.Key.of("detail", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertEquals("detail_of_error", metadata.get(Metadata.Key.of("detail", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertTrue(metadata.containsKey(Metadata.Key.of("statickey", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertEquals("not_found-404-100", metadata.get(Metadata.Key.of("statickey", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertTrue(metadata.containsKey(Metadata.Key.of("somefield", Metadata.ASCII_STRING_MARSHALLER)));
Expand All @@ -52,6 +57,10 @@ public void shouldBuildDynamicMetadataWithCorrectPlaceholders() {
Assertions.assertEquals("", metadata.get(Metadata.Key.of("entity", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertTrue(metadata.containsKey(Metadata.Key.of("false", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertEquals("staticValue", metadata.get(Metadata.Key.of("false", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertTrue(metadata.containsKey(Metadata.Key.of("binding", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertEquals("200", metadata.get(Metadata.Key.of("binding", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertTrue(metadata.containsKey(Metadata.Key.of("math", Metadata.ASCII_STRING_MARSHALLER)));
Assertions.assertEquals("200", metadata.get(Metadata.Key.of("math", Metadata.ASCII_STRING_MARSHALLER)));
}

@Test
Expand Down