Skip to content

Commit ae3d320

Browse files
authored
Merge pull request #12 from stacklok/doc
Add doc.go for cel and env packages
2 parents be33c9c + 821e2e7 commit ae3d320

4 files changed

Lines changed: 103 additions & 4 deletions

File tree

cel/doc.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
/*
5+
Package cel provides a generic CEL expression engine for compiling and evaluating
6+
expressions against arbitrary data contexts.
7+
8+
The engine provides lazy-initialized, thread-safe environment caching, expression
9+
compilation with structured parse and type-check error reporting, boolean and
10+
generic value evaluation helpers, and built-in safeguards against denial-of-service
11+
via configurable expression length and runtime cost limits.
12+
13+
# Basic Usage
14+
15+
Create an engine with variable declarations, compile an expression, and evaluate it:
16+
17+
engine := cel.NewEngine(
18+
celgo.Variable("claims", celgo.MapType(celgo.StringType, celgo.DynType)),
19+
)
20+
21+
expr, err := engine.Compile(`claims["sub"] == "user123"`)
22+
if err != nil {
23+
// handle compilation error
24+
}
25+
26+
ctx := map[string]any{"claims": map[string]any{"sub": "user123"}}
27+
result, err := expr.EvaluateBool(ctx)
28+
// result == true
29+
30+
# Expression Validation
31+
32+
Use Check to validate an expression without creating a compiled program. This is
33+
useful for validating configuration at startup:
34+
35+
err := engine.Check(`claims["sub"] == "user123"`)
36+
if err != nil {
37+
// expression is invalid
38+
}
39+
40+
# Error Handling
41+
42+
Compilation errors are returned as structured types with location information:
43+
44+
expr, err := engine.Compile(`claims["sub"`)
45+
var parseErr *cel.ParseError
46+
if errors.As(err, &parseErr) {
47+
fmt.Println(parseErr.Source) // the original expression
48+
fmt.Println(parseErr.Errors) // line/column/message details
49+
}
50+
51+
expr, err = engine.Compile(`undefined_var == "test"`)
52+
var checkErr *cel.CheckError
53+
if errors.As(err, &checkErr) {
54+
fmt.Println(checkErr.AsJSON()) // structured JSON error details
55+
}
56+
57+
# DoS Protection
58+
59+
The engine includes configurable safeguards against denial-of-service:
60+
61+
engine := cel.NewEngine(opts...).
62+
WithMaxExpressionLength(5000). // reject overly long expressions
63+
WithCostLimit(500000) // limit runtime evaluation cost
64+
65+
# Concurrency
66+
67+
The Engine and CompiledExpression types are safe for concurrent use. A compiled
68+
expression can be evaluated from multiple goroutines simultaneously.
69+
*/
70+
package cel

cel/engine.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
// Package cel provides a generic CEL expression engine for evaluating
5-
// expressions against arbitrary data contexts.
64
package cel
75

86
import (

env/doc.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
/*
5+
Package env provides an interface-based abstraction for environment variable
6+
access, enabling dependency injection and testing isolation.
7+
8+
# Basic Usage
9+
10+
Use OSReader to read environment variables via the standard os package:
11+
12+
reader := &env.OSReader{}
13+
value := reader.Getenv("MY_VAR")
14+
15+
# Testing
16+
17+
The Reader interface allows injecting a mock in tests to avoid relying on
18+
real environment variables. A generated mock is available in the mocks
19+
sub-package:
20+
21+
ctrl := gomock.NewController(t)
22+
mock := mocks.NewMockReader(ctrl)
23+
mock.EXPECT().Getenv("MY_VAR").Return("test-value")
24+
25+
result := myFunc(mock)
26+
27+
# Design
28+
29+
This package follows the interface-based dependency injection pattern used
30+
throughout toolhive-core. Production code accepts an env.Reader, while tests
31+
substitute the generated mock.
32+
*/
33+
package env

env/env.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
// Package env provides abstractions for environment variable access
5-
// to enable dependency injection and testing isolation.
64
package env
75

86
//go:generate mockgen -source=env.go -destination=mocks/mock_reader.go -package=mocks Reader

0 commit comments

Comments
 (0)