From 2ce01f93ac071204a04d5aae63f93f5528564f20 Mon Sep 17 00:00:00 2001 From: Tony Li <122298175+tonyli233@users.noreply.github.com> Date: Thu, 13 Apr 2023 16:11:46 -0400 Subject: [PATCH] Change registry.compileExprs to compileExprs (#53) change registry.compileExprs to compileExprs --- .github/workflows/ci.yaml | 2 ++ go/registry.go | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0aaa55a7..b79131c1 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -23,6 +23,8 @@ jobs: check-latest: true cache: true - uses: bufbuild/buf-setup-action@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} - uses: bufbuild/buf-lint-action@v1 - name: Lint # Often, lint & gofmt guidelines depend on the Go version. To prevent diff --git a/go/registry.go b/go/registry.go index 36545be7..c7c303f1 100644 --- a/go/registry.go +++ b/go/registry.go @@ -88,7 +88,8 @@ func (r *registry) buildMessageExpressions( if len(exprs) == 0 { return } - compiledExprs, err := r.compileExprs( + compiledExprs, err := compileExprs[*validate.Constraint]( + r.baseEnv, exprs, cel.TypeDescs(desc.ParentFile()), cel.Variable("this", cel.ObjectType(string(desc.FullName()))), @@ -185,43 +186,43 @@ func (r *registry) buildFieldExpressions(exprs []*validate.Constraint, fdesc pro } } - compiled, err := r.compileExprs(exprs, opts...) + compiled, err := compileExprs[*validate.Constraint]( + r.baseEnv, + exprs, + opts..., + ) return compiled, err } -func (r *registry) compileExprs(exprs []*validate.Constraint, envOpts ...cel.EnvOption) ([]compiledExpression, error) { +func compileExprs[T expression](env *cel.Env, exprs []T, envOpts ...cel.EnvOption) ([]compiledExpression, error) { if len(exprs) == 0 { return nil, nil } - - env, err := r.baseEnv.Extend(envOpts...) + newEnv, err := env.Extend(envOpts...) if err != nil { return nil, CompilationError{ cause: fmt.Errorf("failed to extend base environment: %w", err), } } - compiledExprs := make([]compiledExpression, len(exprs)) for i, expr := range exprs { compiledExprs[i].source = expr - ast, issues := env.Compile(expr.Expression) + ast, issues := newEnv.Compile(expr.GetExpression()) if err = issues.Err(); err != nil { return nil, CompilationError{ - cause: fmt.Errorf("failed to compile expression %s: %w", expr.Id, err), + cause: fmt.Errorf("failed to compile expression %s: %w", expr.GetId(), err), } } - - compiledExprs[i].program, err = env.Program(ast, cel.EvalOptions( + compiledExprs[i].program, err = newEnv.Program(ast, cel.EvalOptions( cel.OptOptimize, cel.OptCheckStringFormat, )) if err != nil { return nil, CompilationError{ - cause: fmt.Errorf("failed to compile program %s: %w", expr.Id, err), + cause: fmt.Errorf("failed to compile program %s: %w", expr.GetId(), err), } } } - return compiledExprs, nil }