mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-13 03:42:35 -06:00
5a0dc476be
A mixed bag of improvements I have had around for a while, and would like to see included in v1.12.0 if possible. I have about twice the amount of changes **not** included here as they could use some more testing. These changes should be low risk, I believe.. but obviously do let me know if you see any potential risks that I don't! - Add template string benchmarks - Faster template strings / print eval by not passing bctx in recursion - Allow passing nil value to `Query.WithQueryTracer` (no-op) - Reduce allocations in rego v1 compiler stages - Intern a few more common var name `Value`s - Remove redundant switch on `scope` in annotations code - Add a few more benchmarks in the `ast` package - Performance improvements in type checker, most notably removing a function literal for checking expression, which only ever had one implementation. We can extend this later if needed. - Prefer `NewGenericTransformer` over `&GenericTransformer` for easier tracking in pprof Signed-off-by: Anders Eknert <anders.eknert@apple.com>
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
// Copyright 2025 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package topdown
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/open-policy-agent/opa/v1/ast"
|
|
"github.com/open-policy-agent/opa/v1/topdown/builtins"
|
|
)
|
|
|
|
func builtinTemplateString(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
|
arr, err := builtins.ArrayOperand(operands[0].Value, 1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
buf := make([]string, arr.Len())
|
|
|
|
var count int
|
|
err = builtinPrintCrossProductOperands(bctx.Location, buf, arr, 0, func(buf []string) error {
|
|
count += 1
|
|
// Precautionary run-time assertion that template-strings can't produce multiple outputs; e.g. for custom relation type built-ins not known at compile-time.
|
|
if count > 1 {
|
|
return Halt{Err: &Error{
|
|
Code: ConflictErr,
|
|
Location: bctx.Location,
|
|
Message: "template-strings must not produce multiple outputs",
|
|
}}
|
|
}
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return iter(ast.StringTerm(strings.Join(buf, "")))
|
|
}
|
|
|
|
func init() {
|
|
RegisterBuiltinFunc(ast.InternalTemplateString.Name, builtinTemplateString)
|
|
}
|