1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package replacer
import (
"fmt"
"github.com/authzed/cel-go/cel"
)
func ReplaceVariable(e *cel.Env, existingAst *cel.Ast, oldVarName string, newVarName string) (*cel.Ast, error) {
newExpr, iss := e.Compile(newVarName)
if iss.Err() != nil {
return nil, fmt.Errorf("failed to compile new variable name: %w", iss.Err())
}
inlinedVars := []*InlineVariable{}
inlinedVars = append(inlinedVars, newInlineVariable(oldVarName, newExpr))
opt := cel.NewStaticOptimizer(newModifiedInliningOptimizer(inlinedVars...))
optimized, iss := opt.Optimize(e, existingAst)
if iss.Err() != nil {
return nil, fmt.Errorf("failed to optimize: %w", iss.Err())
}
return optimized, nil
}
|