mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-14 20:32:27 -06:00
51a50ca042
Parsing is generally fast, so this mainly improves performance of creating big bundles with many Rego files in them. For Regal's embedded bundle, loading it from memory would previously take 16 ms on my laptop, and now it takes 9 ms. There are other things in this process that could be concurrent too, like JSON unmarshalling of multiple data files. But starting with parsing modules. This PR adds `errgroup` as a direct dependency (previously indirect) as it is a nicer way to work with wait groups, and one that can be useful elsewhere in the codebase (like in the compiler). Also, and as usual, went off on a bit of a tangent refactoring code related to the bundle build process, and made sure to use some common helpers in code where available. Signed-off-by: Anders Eknert <anders.eknert@apple.com>
37 lines
976 B
Go
37 lines
976 B
Go
// Copyright 2020 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 ref implements internal helpers for references
|
|
package ref
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/open-policy-agent/opa/v1/ast"
|
|
"github.com/open-policy-agent/opa/v1/storage"
|
|
"github.com/open-policy-agent/opa/v1/util"
|
|
)
|
|
|
|
// ParseDataPath returns a ref from the slash separated path s rooted at data.
|
|
// All path segments are treated as identifier strings.
|
|
func ParseDataPath(s string) (ast.Ref, error) {
|
|
path, ok := storage.ParsePath(util.WithPrefix(s, "/"))
|
|
if !ok {
|
|
return nil, errors.New("invalid path")
|
|
}
|
|
|
|
return path.Ref(ast.DefaultRootDocument), nil
|
|
}
|
|
|
|
// ArrayPath will take an ast.Array and build an ast.Ref using the ast.Terms in the Array
|
|
func ArrayPath(a *ast.Array) ast.Ref {
|
|
ref := make(ast.Ref, 0, a.Len())
|
|
|
|
a.Foreach(func(term *ast.Term) {
|
|
ref = append(ref, term)
|
|
})
|
|
|
|
return ref
|
|
}
|