mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
e43ef0a979
Earlier this evening I tried to run the Go [modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize) analyzer on OPA. That didn't go as planned: - https://github.com/golang/go/issues/73661 - https://github.com/golang/go/issues/73663 While we wait for that to be fixed, I figured an old-fashioned search-and-replace across the repo may work for at least the `interface{}` to `any` conversion. That should help make it easier to see the other fixes as applied by the modernize tool once it has had those issues resolved. Signed-off-by: Anders Eknert <anders@styra.com>
87 lines
2.6 KiB
Go
87 lines
2.6 KiB
Go
// Copyright 2017 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 format implements formatting of Rego source files.
|
|
package format
|
|
|
|
import (
|
|
"github.com/open-policy-agent/opa/ast"
|
|
"github.com/open-policy-agent/opa/types"
|
|
v1 "github.com/open-policy-agent/opa/v1/format"
|
|
)
|
|
|
|
// Opts lets you control the code formatting via `AstWithOpts()`.
|
|
type Opts = v1.Opts
|
|
|
|
// Source formats a Rego source file. The bytes provided must describe a complete
|
|
// Rego module. If they don't, Source will return an error resulting from the attempt
|
|
// to parse the bytes.
|
|
func Source(filename string, src []byte) ([]byte, error) {
|
|
return SourceWithOpts(filename, src, Opts{
|
|
RegoVersion: ast.DefaultRegoVersion,
|
|
ParserOptions: &ast.ParserOptions{
|
|
RegoVersion: ast.DefaultRegoVersion,
|
|
},
|
|
})
|
|
}
|
|
|
|
func SourceWithOpts(filename string, src []byte, opts Opts) ([]byte, error) {
|
|
if opts.RegoVersion == ast.RegoUndefined {
|
|
opts.RegoVersion = ast.DefaultRegoVersion
|
|
}
|
|
if opts.ParserOptions == nil {
|
|
opts.ParserOptions = &ast.ParserOptions{}
|
|
}
|
|
if opts.ParserOptions.RegoVersion == ast.RegoUndefined {
|
|
opts.ParserOptions.RegoVersion = ast.DefaultRegoVersion
|
|
}
|
|
|
|
return v1.SourceWithOpts(filename, src, opts)
|
|
}
|
|
|
|
// MustAst is a helper function to format a Rego AST element. If any errors
|
|
// occurs this function will panic. This is mostly used for test
|
|
func MustAst(x any) []byte {
|
|
bs, err := Ast(x)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return bs
|
|
}
|
|
|
|
// MustAstWithOpts is a helper function to format a Rego AST element. If any errors
|
|
// occurs this function will panic. This is mostly used for test
|
|
func MustAstWithOpts(x any, opts Opts) []byte {
|
|
bs, err := AstWithOpts(x, opts)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return bs
|
|
}
|
|
|
|
// Ast formats a Rego AST element. If the passed value is not a valid AST
|
|
// element, Ast returns nil and an error. If AST nodes are missing locations
|
|
// an arbitrary location will be used.
|
|
func Ast(x any) ([]byte, error) {
|
|
return AstWithOpts(x, Opts{
|
|
RegoVersion: ast.DefaultRegoVersion,
|
|
})
|
|
}
|
|
|
|
func AstWithOpts(x any, opts Opts) ([]byte, error) {
|
|
if opts.RegoVersion == ast.RegoUndefined {
|
|
opts.RegoVersion = ast.DefaultRegoVersion
|
|
}
|
|
|
|
return v1.AstWithOpts(x, opts)
|
|
}
|
|
|
|
// ArgErrDetail but for `fmt` checks since compiler has not run yet.
|
|
type ArityFormatErrDetail = v1.ArityFormatErrDetail
|
|
|
|
// arityMismatchError but for `fmt` checks since the compiler has not run yet.
|
|
func ArityFormatMismatchError(operands []*ast.Term, operator string, loc *ast.Location, f *types.Function) *ast.Error {
|
|
return v1.ArityFormatMismatchError(operands, operator, loc, f)
|
|
}
|