|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 The Authors of CEL-Java |
| 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 | +package org.projectnessie.cel.quarkus.smoke.corepb4jackson3; |
| 17 | + |
| 18 | +import static java.util.Arrays.asList; |
| 19 | +import static java.util.Map.of; |
| 20 | +import static org.projectnessie.cel.Env.newCustomEnv; |
| 21 | +import static org.projectnessie.cel.EnvOption.declarations; |
| 22 | +import static org.projectnessie.cel.EnvOption.types; |
| 23 | +import static org.projectnessie.cel.Library.StdLib; |
| 24 | + |
| 25 | +import com.google.protobuf.Int32Value; |
| 26 | +import dev.cel.expr.conformance.proto3.NestedTestAllTypes; |
| 27 | +import dev.cel.expr.conformance.proto3.TestAllTypes; |
| 28 | +import io.quarkus.runtime.annotations.RegisterForReflection; |
| 29 | +import jakarta.ws.rs.GET; |
| 30 | +import jakarta.ws.rs.Path; |
| 31 | +import jakarta.ws.rs.Produces; |
| 32 | +import jakarta.ws.rs.core.MediaType; |
| 33 | +import java.time.Instant; |
| 34 | +import org.projectnessie.cel.Ast; |
| 35 | +import org.projectnessie.cel.Env; |
| 36 | +import org.projectnessie.cel.Env.AstIssuesTuple; |
| 37 | +import org.projectnessie.cel.Program; |
| 38 | +import org.projectnessie.cel.Program.EvalResult; |
| 39 | +import org.projectnessie.cel.checker.Decls; |
| 40 | +import org.projectnessie.cel.common.types.Err; |
| 41 | +import org.projectnessie.cel.common.types.pb.ProtoTypeRegistry; |
| 42 | +import org.projectnessie.cel.common.types.ref.Val; |
| 43 | +import org.projectnessie.cel.types.jackson3.Jackson3Registry; |
| 44 | + |
| 45 | +@Path("/cel/native-smoke") |
| 46 | +public class SmokeResource { |
| 47 | + private static final String JACKSON_EXPRESSION = |
| 48 | + "input.name == \"reports\" && request.time < timestamp(\"2026-08-01T00:00:00Z\")"; |
| 49 | + |
| 50 | + private static final String PROTO_EXPRESSION = "proto.single_int32_wrapper == 123"; |
| 51 | + |
| 52 | + @GET |
| 53 | + @Produces(MediaType.APPLICATION_JSON) |
| 54 | + public SmokeResponse smoke() { |
| 55 | + return new SmokeResponse( |
| 56 | + "cel-core+cel-generated-pb+cel-jackson3", evaluateJackson(), evaluateProto()); |
| 57 | + } |
| 58 | + |
| 59 | + private boolean evaluateJackson() { |
| 60 | + Env env = |
| 61 | + newCustomEnv( |
| 62 | + Jackson3Registry.newRegistry(), |
| 63 | + asList( |
| 64 | + StdLib(), |
| 65 | + types(Input.class), |
| 66 | + declarations( |
| 67 | + Decls.newVar("input", Decls.newObjectType(Input.class.getName())), |
| 68 | + Decls.newVar("request.time", Decls.Timestamp)))); |
| 69 | + |
| 70 | + Program program = compile(env, JACKSON_EXPRESSION); |
| 71 | + EvalResult result = |
| 72 | + program.eval( |
| 73 | + of( |
| 74 | + "input", |
| 75 | + new Input("reports"), |
| 76 | + "request.time", |
| 77 | + Instant.parse("2026-07-31T23:59:59Z"))); |
| 78 | + return booleanResult(result); |
| 79 | + } |
| 80 | + |
| 81 | + private boolean evaluateProto() { |
| 82 | + Env env = |
| 83 | + newCustomEnv( |
| 84 | + ProtoTypeRegistry.newRegistry(), |
| 85 | + asList( |
| 86 | + StdLib(), |
| 87 | + types(TestAllTypes.getDefaultInstance()), |
| 88 | + declarations( |
| 89 | + Decls.newVar( |
| 90 | + "proto", |
| 91 | + Decls.newObjectType(TestAllTypes.getDescriptor().getFullName()))))); |
| 92 | + |
| 93 | + Program program = compile(env, PROTO_EXPRESSION); |
| 94 | + EvalResult result = |
| 95 | + program.eval( |
| 96 | + of( |
| 97 | + "proto", |
| 98 | + TestAllTypes.newBuilder().setSingleInt32Wrapper(Int32Value.of(123)).build())); |
| 99 | + return booleanResult(result); |
| 100 | + } |
| 101 | + |
| 102 | + private static Program compile(Env env, String expression) { |
| 103 | + AstIssuesTuple parsed = env.parse(expression); |
| 104 | + if (parsed.hasIssues()) { |
| 105 | + throw new IllegalStateException("parse failed: " + parsed.getIssues()); |
| 106 | + } |
| 107 | + |
| 108 | + Ast ast = parsed.getAst(); |
| 109 | + AstIssuesTuple checked = env.check(ast); |
| 110 | + if (checked.hasIssues()) { |
| 111 | + throw new IllegalStateException("check failed: " + checked.getIssues()); |
| 112 | + } |
| 113 | + |
| 114 | + return env.program(checked.getAst()); |
| 115 | + } |
| 116 | + |
| 117 | + private static boolean booleanResult(EvalResult result) { |
| 118 | + Val value = result.getVal(); |
| 119 | + if (Err.isError(value)) { |
| 120 | + throw new IllegalStateException("evaluation failed: " + value); |
| 121 | + } |
| 122 | + return value.booleanValue(); |
| 123 | + } |
| 124 | + |
| 125 | + public record SmokeResponse(String engine, boolean jackson, boolean protobuf) {} |
| 126 | + |
| 127 | + @RegisterForReflection(targets = {TestAllTypes.class, NestedTestAllTypes.class}) |
| 128 | + static final class ProtobufReflection {} |
| 129 | + |
| 130 | + @RegisterForReflection |
| 131 | + public static final class Input { |
| 132 | + private final String name; |
| 133 | + |
| 134 | + public Input(String name) { |
| 135 | + this.name = name; |
| 136 | + } |
| 137 | + |
| 138 | + public String getName() { |
| 139 | + return name; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments