Skip to content

Commit 76cc6bf

Browse files
committed
Refresh API documentation and guides
Refresh Javadocs for existing APIs and packages. Add user guides and architecture, integration, configuration, and compatibility documentation for the complete library.
1 parent bf2e7de commit 76cc6bf

151 files changed

Lines changed: 6180 additions & 1122 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 108 additions & 432 deletions
Large diffs are not rendered by default.

‎core/src/main/java/org/projectnessie/cel/Ast.java‎

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,21 @@
2525
import org.projectnessie.cel.common.Source;
2626

2727
/**
28-
* Ast representing the checked or unchecked expression, its source, and related metadata such as
29-
* source position information.
28+
* A parsed or type-checked CEL abstract syntax tree and its source metadata.
29+
*
30+
* <p>An AST returned by {@link Env#parse(String)} is unchecked. Passing it to {@link
31+
* Env#check(Ast)} produces a checked AST with reference and type metadata. Prefer a checked AST
32+
* when creating a {@link Program}; unchecked ASTs are supported but provide less planning
33+
* information.
34+
*
35+
* <p>The expression and source-info values are immutable Protobuf messages. The constructors retain
36+
* the supplied {@link Source} and, for the full constructor, the supplied reference and type maps.
37+
* Callers that construct an AST directly must keep retained mutable state stable while the AST is
38+
* in use.
39+
*
40+
* <p>{@link CEL#astToParsedExpr(Ast)} and {@link CEL#astToCheckedExpr(Ast)} expose the
41+
* corresponding Protobuf representations. Exporting those messages does not by itself guarantee
42+
* that another CEL runtime recognizes implementation-specific macro expansions.
3043
*/
3144
public final class Ast {
3245
private final Expr expr;
@@ -35,10 +48,28 @@ public final class Ast {
3548
final Map<Long, Reference> refMap;
3649
final Map<Long, Type> typeMap;
3750

51+
/**
52+
* Creates an unchecked AST.
53+
*
54+
* @param expr parsed CEL expression
55+
* @param info source and position metadata
56+
* @param source source used to parse the expression
57+
*/
3858
public Ast(Expr expr, SourceInfo info, Source source) {
3959
this(expr, info, source, new HashMap<>(), new HashMap<>());
4060
}
4161

62+
/**
63+
* Creates an AST with explicit reference and type metadata.
64+
*
65+
* <p>The maps are retained rather than copied. A non-empty type map marks the AST as checked.
66+
*
67+
* @param expr parsed or checked CEL expression
68+
* @param info source and position metadata
69+
* @param source source used to parse or check the expression
70+
* @param refMap checked reference metadata keyed by expression ID
71+
* @param typeMap checked type metadata keyed by expression ID
72+
*/
4273
public Ast(
4374
Expr expr,
4475
SourceInfo info,
@@ -52,30 +83,46 @@ public Ast(
5283
this.typeMap = typeMap;
5384
}
5485

55-
/** Expr returns the proto serializable instance of the parsed/checked expression. */
86+
/**
87+
* Returns the parsed or checked expression Protobuf message.
88+
*
89+
* @return expression tree
90+
*/
5691
public Expr getExpr() {
5792
return expr;
5893
}
5994

60-
/** IsChecked returns whether the Ast value has been successfully type-checked. */
95+
/**
96+
* Reports whether this AST contains checked type metadata.
97+
*
98+
* @return {@code true} when the type map is non-null and non-empty
99+
*/
61100
public boolean isChecked() {
62101
return typeMap != null && !typeMap.isEmpty();
63102
}
64103

104+
/**
105+
* Returns the source associated with this AST.
106+
*
107+
* @return retained source
108+
*/
65109
public Source getSource() {
66110
return source;
67111
}
68112

69113
/**
70-
* SourceInfo returns character offset and newling position information about expression elements.
114+
* Returns source-location and macro-expansion metadata for expression elements.
115+
*
116+
* @return source information
71117
*/
72118
public SourceInfo getSourceInfo() {
73119
return info;
74120
}
75121

76122
/**
77-
* ResultType returns the output type of the expression if the Ast has been type-checked, else
78-
* returns decls.Dyn as the parse step cannot infer the type.
123+
* Returns the checked result type of this expression.
124+
*
125+
* @return the root expression's checked type, or {@link Decls#Dyn} for an unchecked AST
79126
*/
80127
public Type getResultType() {
81128
if (!isChecked()) {
@@ -85,8 +132,12 @@ public Type getResultType() {
85132
}
86133

87134
/**
88-
* Source returns a view of the input used to create the Ast. This source may be complete or
89-
* constructed from the SourceInfo.
135+
* Returns the source text associated with this AST.
136+
*
137+
* <p>This is the retained source's content, not a newly unparsed representation of the
138+
* expression. Use {@link CEL#astToString(Ast)} to obtain a stable unparsed expression.
139+
*
140+
* @return source content
90141
*/
91142
@Override
92143
public String toString() {

‎core/src/main/java/org/projectnessie/cel/CEL.java‎

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848
import org.projectnessie.cel.interpreter.InterpretableDecorator;
4949
import org.projectnessie.cel.interpreter.Interpreter;
5050

51+
/**
52+
* Low-level factories and conversion utilities for CEL environments, programs, activations, and
53+
* ASTs.
54+
*
55+
* <p>Most applications begin with {@link Env#newEnv(EnvOption...)} and follow the
56+
* parse/check/program/evaluate lifecycle. This class supports integrations that need explicit
57+
* Protobuf AST conversion, partial activations, attribute patterns, or cost estimates.
58+
*/
5159
public final class CEL {
5260

5361
/**
@@ -187,7 +195,16 @@ static boolean nativePlanningPermitted(
187195
|| evalOptions.size() == 1 && evalOptions.contains(EvalOption.OptOptimize));
188196
}
189197

190-
/** CheckedExprToAst converts a checked expression proto message to an Ast. */
198+
/**
199+
* Converts a checked-expression Protobuf message to an AST.
200+
*
201+
* <p>The returned source is reconstructed from the message's {@link SourceInfo}; it need not
202+
* contain the original source text.
203+
*
204+
* @param checkedExpr checked expression message
205+
* @return an AST that is considered checked when the message contains non-empty type metadata
206+
* @throws NullPointerException if {@code checkedExpr} is {@code null}
207+
*/
191208
public static Ast checkedExprToAst(CheckedExpr checkedExpr) {
192209
Map<Long, Reference> refMap = checkedExpr.getReferenceMapMap();
193210
Map<Long, Type> typeMap = checkedExpr.getTypeMapMap();
@@ -200,9 +217,16 @@ public static Ast checkedExprToAst(CheckedExpr checkedExpr) {
200217
}
201218

202219
/**
203-
* AstToCheckedExpr converts an Ast to an protobuf CheckedExpr value.
220+
* Converts a checked AST to a checked-expression Protobuf message.
204221
*
205-
* <p>If the Ast.IsChecked() returns false, this conversion method will return an error.
222+
* <p>The conversion preserves CEL-Java's current expression, reference, type, and source-info
223+
* shape. It does not establish cross-runtime support for implementation-specific macro
224+
* expansions.
225+
*
226+
* @param a checked AST
227+
* @return checked-expression message
228+
* @throws NullPointerException if {@code a} is {@code null}
229+
* @throws IllegalArgumentException if {@code a} is unchecked
206230
*/
207231
public static CheckedExpr astToCheckedExpr(Ast a) {
208232
if (!a.isChecked()) {
@@ -216,68 +240,107 @@ public static CheckedExpr astToCheckedExpr(Ast a) {
216240
.build();
217241
}
218242

219-
/** ParsedExprToAst converts a parsed expression proto message to an Ast. */
243+
/**
244+
* Converts a parsed-expression Protobuf message to an unchecked AST.
245+
*
246+
* <p>The returned source is reconstructed from the message's {@link SourceInfo}; it need not
247+
* contain the original source text.
248+
*
249+
* @param parsedExpr parsed expression message
250+
* @return unchecked AST
251+
* @throws NullPointerException if {@code parsedExpr} is {@code null}
252+
*/
220253
public static Ast parsedExprToAst(ParsedExpr parsedExpr) {
221254
SourceInfo si = parsedExpr.getSourceInfo();
222255
return new Ast(parsedExpr.getExpr(), si, newInfoSource(si));
223256
}
224257

225-
/** AstToParsedExpr converts an Ast to an protobuf ParsedExpr value. */
258+
/**
259+
* Converts an AST to a parsed-expression Protobuf message.
260+
*
261+
* <p>Checked reference and type maps are not part of {@link ParsedExpr} and are therefore not
262+
* included.
263+
*
264+
* @param a parsed or checked AST
265+
* @return parsed-expression message
266+
* @throws NullPointerException if {@code a} is {@code null}
267+
*/
226268
public static ParsedExpr astToParsedExpr(Ast a) {
227269
return ParsedExpr.newBuilder().setExpr(a.getExpr()).setSourceInfo(a.getSourceInfo()).build();
228270
}
229271

230272
/**
231-
* AstToString converts an Ast back to a string if possible.
273+
* Unparses an AST to stable CEL source text.
232274
*
233275
* <p>Note, the conversion may not be an exact replica of the original expression, but will
234276
* produce a string that is semantically equivalent and whose textual representation is stable.
277+
*
278+
* @param a AST to unparse
279+
* @return stable CEL expression text
280+
* @throws NullPointerException if {@code a} is {@code null}
235281
*/
236282
public static String astToString(Ast a) {
237283
Expr expr = a.getExpr();
238284
SourceInfo info = a.getSourceInfo();
239285
return unparse(expr, info);
240286
}
241287

242-
/** NoVars returns an empty Activation. */
288+
/**
289+
* Returns an activation with no variable bindings.
290+
*
291+
* @return empty activation
292+
*/
243293
public static Activation noVars() {
244294
return emptyActivation();
245295
}
246296

247297
/**
248-
* PartialVars returns a PartialActivation which contains variables and a set of AttributePattern
249-
* values that indicate variables or parts of variables whose value are not yet known.
298+
* Creates a partial activation containing bindings and attributes whose values are unknown.
299+
*
300+
* <p>{@code vars} may be an {@link Activation}, a Java map, a {@link
301+
* java.util.function.Function}, or an {@link
302+
* org.projectnessie.cel.interpreter.ActivationFunction}. The returned activation copies the
303+
* unknown-pattern array but retains its mutable pattern elements.
250304
*
251-
* <p>The `vars` value may either be an interpreter.Activation or any valid input to the
252-
* interpreter.NewActivation call.
305+
* @param vars bindings accepted by {@link Activation#newActivation(Object)}
306+
* @param unknowns attribute patterns that evaluate as unknown
307+
* @return partial activation
308+
* @throws NullPointerException if {@code vars} or {@code unknowns} is {@code null}
309+
* @throws IllegalArgumentException if {@code vars} is not a supported activation input
253310
*/
254311
public static PartialActivation partialVars(Object vars, AttributePattern... unknowns) {
255312
return newPartialActivation(vars, unknowns);
256313
}
257314

258315
/**
259-
* AttributePattern returns an AttributePattern that matches a top-level variable. The pattern is
260-
* mutable, and its methods support the specification of one or more qualifier patterns.
316+
* Creates a mutable attribute pattern matching a top-level variable.
261317
*
262-
* <p>For example, the AttributePattern(`a`).QualString(`b`) represents a variable access `a` with
263-
* a string field or index qualification `b`. This pattern will match Attributes `a`, and `a.b`,
264-
* but not `a.c`.
318+
* <p>For example, {@code attributePattern("a").qualString("b")} represents the access {@code
319+
* a.b}. It matches attributes {@code a} and {@code a.b}, but not {@code a.c}.
265320
*
266321
* <p>When using a CEL expression within a container, e.g. a package or namespace, the variable
267322
* name in the pattern must match the qualified name produced during the variable namespace
268-
* resolution. For example, when variable `a` is declared within an expression whose container is
269-
* `ns.app`, the fully qualified variable name may be `ns.app.a`, `ns.a`, or `a` per the CEL
270-
* namespace resolution rules. Pick the fully qualified variable name that makes sense within the
271-
* container as the AttributePattern `varName` argument.
323+
* resolution. For example, when variable {@code a} is declared within an expression whose
324+
* container is {@code ns.app}, the fully qualified variable name may be {@code ns.app.a}, {@code
325+
* ns.a}, or {@code a} per the CEL namespace resolution rules. Use the qualified name selected by
326+
* resolution as {@code varName}.
272327
*
273-
* <p>See the interpreter.AttributePattern and interpreter.AttributeQualifierPattern for more info
274-
* about how to create and manipulate AttributePattern values.
328+
* @param varName top-level or resolved qualified variable name
329+
* @return mutable attribute pattern
275330
*/
276331
public static AttributePattern attributePattern(String varName) {
277332
return newAttributePattern(varName);
278333
}
279334

280-
/** EstimateCost returns the heuristic cost interval for the program. */
335+
/**
336+
* Returns the heuristic cost interval reported by an object that implements {@link Coster}.
337+
*
338+
* <p>A cost estimate is descriptive only; it does not impose an evaluation budget or runtime
339+
* limit.
340+
*
341+
* @param p program or other cost-reporting object
342+
* @return reported cost interval, or {@link Cost#Unknown} when {@code p} is not a {@code Coster}
343+
*/
281344
public static Cost estimateCost(Object p) {
282345
if (p instanceof Coster) {
283346
return ((Coster) p).cost();

0 commit comments

Comments
 (0)