Class CelUtils
java.lang.Object
io.confluent.kafka.schemaregistry.rules.cel.CelUtils
Shared CEL helpers used by both the transform-path executor (
CelExecutor) and
the validation-path executor (CelValidator). All methods are stateless.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classCompiled rule output: the runnableCelRuntime.Programplus the compile-time result-typeCelKind.static enumRegex engine used by the CELmatches/matches_stringoverloads.static enum -
Method Summary
Modifier and TypeMethodDescriptionstatic CelUtils.CompiledRulebuildCompiledRule(CelUtils.ScriptType type, String expr, Object schemaHint, List<dev.cel.common.CelVarDecl> varDecls, CelUtils.RegexEngine regexEngine) LikebuildProgram(io.confluent.kafka.schemaregistry.rules.cel.CelUtils.ScriptType, java.lang.String, java.lang.Object, java.util.List<dev.cel.common.CelVarDecl>)but exposes the compiled AST's result-typeCelKindalongside the runnable program.static dev.cel.runtime.CelRuntime.ProgrambuildProgram(CelUtils.ScriptType type, String expr, Object schemaHint, List<dev.cel.common.CelVarDecl> varDecls) Build a CELCelRuntime.Programfor(type, expr, schemaHint, varDecls).static dev.cel.runtime.CelRuntime.ProgrambuildProgram(CelUtils.ScriptType type, String expr, Object schemaHint, List<dev.cel.common.CelVarDecl> varDecls, CelUtils.RegexEngine regexEngine) static dev.cel.common.types.CelTypefindCelType(Object arg) Infer theCelTypefor a runtime value.static dev.cel.common.types.CelTypefindCelTypeForAvroSchema(org.apache.avro.Schema schema) Map an AvroSchemato aCelType.static dev.cel.common.types.CelTypefindCelTypeForClass(Class<?> type) Map a JavaClassto aCelType.static ObjecttoCelValue(Object value) Convert a native value to a CEL-compatible shape:IndexedRecord(covers bothGenericRecordandSpecificRecord) → JDKMap(Google cel-java's runtime selects fields on Maps natively)Utf8/GenericEnumSymbol→StringNarrow numeric types (Integer,Short,Byte,Float) widened to CEL's natural widths (long/double)byte[]/ByteString→CelByteString(CEL bytes literals only compare equal to other CelByteStrings) Lists/maps recursively converted Other values (proto Messages, BigDecimal, primitives at correct width, etc.) pass through unchanged.static ObjecttoCelValueForJson(Object value, com.fasterxml.jackson.databind.ObjectMapper mapper) JSON-flavored variant oftoCelValue(Object): additionally converts JacksonJsonNodeand arbitrary POJOs to maps via the suppliedObjectMapper.toDeclTypes(Map<String, Object> args) Build a name→CelTypemap by inferring each value's type.static List<dev.cel.common.CelVarDecl>toVarDecls(Map<String, dev.cel.common.types.CelType> args) Convenience wrapper to constructCelVarDecllist from a name→type map, preserving insertion order.
-
Method Details
-
buildProgram
public static dev.cel.runtime.CelRuntime.Program buildProgram(CelUtils.ScriptType type, String expr, Object schemaHint, List<dev.cel.common.CelVarDecl> varDecls) throws dev.cel.common.CelValidationException, dev.cel.runtime.CelEvaluationException Build a CELCelRuntime.Programfor(type, expr, schemaHint, varDecls). The compiler is configured with the standard library, ourBuiltinLibrary, and any format-specific type registration; the runtime mirrors the compile-side configuration so resolved overload IDs find their bindings.For AVRO and JSON values, callers should pre-convert native values (
GenericRecord, JacksonJsonNode, etc.) to CEL-native shapes (Map / List / scalar) viatoCelValue(Object)before binding. Field access (this.field,this["field"]) then works through CEL's built-in map semantics — no custom type provider is required.- Throws:
dev.cel.common.CelValidationExceptiondev.cel.runtime.CelEvaluationException
-
buildProgram
public static dev.cel.runtime.CelRuntime.Program buildProgram(CelUtils.ScriptType type, String expr, Object schemaHint, List<dev.cel.common.CelVarDecl> varDecls, CelUtils.RegexEngine regexEngine) throws dev.cel.common.CelValidationException, dev.cel.runtime.CelEvaluationException - Throws:
dev.cel.common.CelValidationExceptiondev.cel.runtime.CelEvaluationException
-
buildCompiledRule
public static CelUtils.CompiledRule buildCompiledRule(CelUtils.ScriptType type, String expr, Object schemaHint, List<dev.cel.common.CelVarDecl> varDecls, CelUtils.RegexEngine regexEngine) throws dev.cel.common.CelValidationException, dev.cel.runtime.CelEvaluationException LikebuildProgram(io.confluent.kafka.schemaregistry.rules.cel.CelUtils.ScriptType, java.lang.String, java.lang.Object, java.util.List<dev.cel.common.CelVarDecl>)but exposes the compiled AST's result-typeCelKindalongside the runnable program. Lets callers (notablyCelValidator) reject rules whose result type doesn't fit their contract, at compile time rather than first eval.- Throws:
dev.cel.common.CelValidationExceptiondev.cel.runtime.CelEvaluationException
-
toVarDecls
public static List<dev.cel.common.CelVarDecl> toVarDecls(Map<String, dev.cel.common.types.CelType> args) Convenience wrapper to constructCelVarDecllist from a name→type map, preserving insertion order. -
toDeclTypes
Build a name→CelTypemap by inferring each value's type. Insertion order is preserved (usesLinkedHashMap) so generated declarations match argument binding order. -
findCelType
Infer theCelTypefor a runtime value. Used to declarethis(and other variables) in the compiler so the type-checker can validate field access. -
findCelTypeForAvroSchema
public static dev.cel.common.types.CelType findCelTypeForAvroSchema(org.apache.avro.Schema schema) Map an AvroSchemato aCelType.- Primitives map to their natural CEL counterpart (BOOLEAN→BOOL, INT/LONG→INT, FLOAT/DOUBLE→DOUBLE, STRING/ENUM→STRING, BYTES/FIXED→BYTES, NULL→NULL_TYPE).
RECORD→StructTypeReferenceso theAvroCelTypeProviderregistered bybuildProgram(io.confluent.kafka.schemaregistry.rules.cel.CelUtils.ScriptType, java.lang.String, java.lang.Object, java.util.List<dev.cel.common.CelVarDecl>)resolves field types at type-check time. Runtime values still flow through as JDK maps (viatoCelValue(java.lang.Object)); the struct declaration is purely for compile-side field validation. If the provider can't represent the record cleanly,buildProgram(io.confluent.kafka.schemaregistry.rules.cel.CelUtils.ScriptType, java.lang.String, java.lang.Object, java.util.List<dev.cel.common.CelVarDecl>)retries withthisdowngraded tomap<string, dyn>.ARRAY→list<dyn>;MAP→map<string, dyn>.UNION: nullable[null, X]unwraps to X; multi-branch unions (and any unrecognized type) fall back todyn.
-
findCelTypeForClass
Map a JavaClassto aCelType. Used when the value isn't a GenericContainer or proto Message (i.e., a JSON-derived primitive value or a primitive Avro/Proto field bound tothis). -
toCelValue
Convert a native value to a CEL-compatible shape:IndexedRecord(covers bothGenericRecordandSpecificRecord) → JDKMap(Google cel-java's runtime selects fields on Maps natively)Utf8/GenericEnumSymbol→String- Narrow numeric types (
Integer,Short,Byte,Float) widened to CEL's natural widths (long/double) byte[]/ByteString→CelByteString(CEL bytes literals only compare equal to other CelByteStrings)- Lists/maps recursively converted
-
toCelValueForJson
public static Object toCelValueForJson(Object value, com.fasterxml.jackson.databind.ObjectMapper mapper) JSON-flavored variant oftoCelValue(Object): additionally converts JacksonJsonNodeand arbitrary POJOs to maps via the suppliedObjectMapper. Used on the JSON validation/transform path where the value passed in might be a bean — we materialize beans as maps so the CEL runtime's native map field-access path applies.Primitives, strings, byte buffers, proto Messages, Avro
GenericRecordandUtf8delegate totoCelValue(Object).
-