Skip to content

Commit

Permalink
Removing deprecated fields and functions related to rego-v1 compatibi…
Browse files Browse the repository at this point in the history
…lity (#6542)

Also deprecating `ParserOptions.EffectiveRegoVersion()`, which will be removed in a future release.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
  • Loading branch information
johanfylling committed Jan 24, 2024
1 parent 48490ee commit 0738022
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 47 deletions.
20 changes: 7 additions & 13 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,13 @@ type ParserOptions struct {
SkipRules bool
JSONOptions *astJSON.Options
// RegoVersion is the version of Rego to parse for.
// RegoV1Compatible additionally affects the Rego version. Use EffectiveRegoVersion to get the effective Rego version.
RegoVersion RegoVersion
// RegoV1Compatible is equivalent to setting RegoVersion to RegoV0CompatV1.
// RegoV1Compatible takes precedence, and if set to true, RegoVersion is ignored.
// Deprecated: use RegoVersion instead. Will be removed in a future version of OPA.
RegoV1Compatible bool
RegoVersion RegoVersion
unreleasedKeywords bool // TODO(sr): cleanup
}

// EffectiveRegoVersion returns the effective RegoVersion to use for parsing.
// Deprecated: Use RegoVersion instead.
func (po *ParserOptions) EffectiveRegoVersion() RegoVersion {
if po.RegoV1Compatible {
return RegoV0CompatV1
}
return po.RegoVersion
}

Expand Down Expand Up @@ -291,7 +285,7 @@ func (p *Parser) Parse() ([]Statement, []*Comment, Errors) {

allowedFutureKeywords := map[string]tokens.Token{}

if p.po.EffectiveRegoVersion() == RegoV1 {
if p.po.RegoVersion == RegoV1 {
// RegoV1 includes all future keywords in the default language definition
for k, v := range futureKeywords {
allowedFutureKeywords[k] = v
Expand Down Expand Up @@ -325,7 +319,7 @@ func (p *Parser) Parse() ([]Statement, []*Comment, Errors) {
}

selected := map[string]tokens.Token{}
if p.po.AllFutureKeywords || p.po.EffectiveRegoVersion() == RegoV1 {
if p.po.AllFutureKeywords || p.po.RegoVersion == RegoV1 {
for kw, tok := range allowedFutureKeywords {
selected[kw] = tok
}
Expand All @@ -346,7 +340,7 @@ func (p *Parser) Parse() ([]Statement, []*Comment, Errors) {
}
p.s.s = p.s.s.WithKeywords(selected)

if p.po.EffectiveRegoVersion() == RegoV1 {
if p.po.RegoVersion == RegoV1 {
for kw, tok := range allowedFutureKeywords {
p.s.s.AddKeyword(kw, tok)
}
Expand Down Expand Up @@ -2614,7 +2608,7 @@ func (p *Parser) regoV1Import(imp *Import) {
return
}

if p.po.EffectiveRegoVersion() == RegoV1 {
if p.po.RegoVersion == RegoV1 {
// We're parsing for Rego v1, where the 'rego.v1' import is a no-op.
return
}
Expand Down
4 changes: 2 additions & 2 deletions ast/parser_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ func ParseModuleWithOpts(filename, input string, popts ParserOptions) (*Module,
if err != nil {
return nil, err
}
return parseModule(filename, stmts, comments, popts.EffectiveRegoVersion())
return parseModule(filename, stmts, comments, popts.RegoVersion)
}

// ParseBody returns exactly one body.
Expand Down Expand Up @@ -626,7 +626,7 @@ func ParseStatementsWithOpts(filename, input string, popts ParserOptions) ([]Sta
WithCapabilities(popts.Capabilities).
WithSkipRules(popts.SkipRules).
WithJSONOptions(popts.JSONOptions).
WithRegoVersion(popts.EffectiveRegoVersion()).
WithRegoVersion(popts.RegoVersion).
withUnreleasedKeywords(popts.unreleasedKeywords)

stmts, comments, errs := parser.Parse()
Expand Down
24 changes: 7 additions & 17 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,10 @@ type Opts struct {
// carry along their original source locations.
IgnoreLocations bool

// RegoV1 is equivalent to setting RegoVersion to ast.RegoV0Compat1.
// RegoV1 takes precedence over RegoVersion.
// Deprecated: use RegoVersion instead.
RegoV1 bool
// RegoVersion is the version of Rego to format code for.
RegoVersion ast.RegoVersion
}

func (o *Opts) effectiveRegoVersion() ast.RegoVersion {
if o.RegoV1 {
return ast.RegoV0CompatV1
}
return o.RegoVersion
}

// defaultLocationFile is the file name used in `Ast()` for terms
// without a location, as could happen when pretty-printing the
// results of partial eval.
Expand All @@ -54,7 +44,7 @@ func Source(filename string, src []byte) ([]byte, error) {

func SourceWithOpts(filename string, src []byte, opts Opts) ([]byte, error) {
parserOpts := ast.ParserOptions{}
if opts.effectiveRegoVersion() == ast.RegoV1 {
if opts.RegoVersion == ast.RegoV1 {
// If the rego version is V1, wee need to parse it as such, to allow for future keywords not being imported.
// Otherwise, we'll default to RegoV0
parserOpts.RegoVersion = ast.RegoV1
Expand All @@ -65,7 +55,7 @@ func SourceWithOpts(filename string, src []byte, opts Opts) ([]byte, error) {
return nil, err
}

if opts.effectiveRegoVersion() == ast.RegoV0CompatV1 || opts.effectiveRegoVersion() == ast.RegoV1 {
if opts.RegoVersion == ast.RegoV0CompatV1 || opts.RegoVersion == ast.RegoV1 {
errors := ast.CheckRegoV1(module)
if len(errors) > 0 {
return nil, errors
Expand Down Expand Up @@ -133,7 +123,7 @@ func AstWithOpts(x interface{}, opts Opts) ([]byte, error) {

o := fmtOpts{}

if opts.effectiveRegoVersion() == ast.RegoV0CompatV1 || opts.effectiveRegoVersion() == ast.RegoV1 {
if opts.RegoVersion == ast.RegoV0CompatV1 || opts.RegoVersion == ast.RegoV1 {
o.regoV1 = true
o.ifs = true
o.contains = true
Expand Down Expand Up @@ -194,13 +184,13 @@ func AstWithOpts(x interface{}, opts Opts) ([]byte, error) {

switch x := x.(type) {
case *ast.Module:
if opts.effectiveRegoVersion() == ast.RegoV1 {
if opts.RegoVersion == ast.RegoV1 {
x.Imports = filterRegoV1Import(x.Imports)
} else if opts.effectiveRegoVersion() == ast.RegoV0CompatV1 {
} else if opts.RegoVersion == ast.RegoV0CompatV1 {
x.Imports = ensureRegoV1Import(x.Imports)
}

if opts.effectiveRegoVersion() == ast.RegoV0CompatV1 || opts.effectiveRegoVersion() == ast.RegoV1 || moduleIsRegoV1Compatible(x) {
if opts.RegoVersion == ast.RegoV0CompatV1 || opts.RegoVersion == ast.RegoV1 || moduleIsRegoV1Compatible(x) {
x.Imports = future.FilterFutureImports(x.Imports)
} else {
for kw := range extraFutureKeywordImports {
Expand Down
16 changes: 1 addition & 15 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ type FileLoader interface {
WithProcessAnnotation(bool) FileLoader
WithCapabilities(*ast.Capabilities) FileLoader
WithJSONOptions(*astJSON.Options) FileLoader

// WithRegoV1Compatible
// Deprecated: use WithRegoVersion instead
WithRegoV1Compatible(bool) FileLoader

WithRegoVersion(ast.RegoVersion) FileLoader
}

Expand Down Expand Up @@ -187,15 +182,6 @@ func (fl *fileLoader) WithJSONOptions(opts *astJSON.Options) FileLoader {
return fl
}

// WithRegoV1Compatible enforces Rego v0 with Rego v1 compatibility.
// See ParserOptions.RegoV1Compatible for more details.
// Deprecated: use WithRegoVersion instead
// TODO: Remove, this is an internal type, so won't be a breaking change.
func (fl *fileLoader) WithRegoV1Compatible(compatible bool) FileLoader {
fl.opts.RegoV1Compatible = compatible
return fl
}

// WithRegoVersion sets the ast.RegoVersion to use when parsing and compiling modules.
func (fl *fileLoader) WithRegoVersion(version ast.RegoVersion) FileLoader {
fl.opts.RegoVersion = version
Expand Down Expand Up @@ -271,7 +257,7 @@ func (fl fileLoader) AsBundle(path string) (*bundle.Bundle, error) {
WithProcessAnnotations(fl.opts.ProcessAnnotation).
WithCapabilities(fl.opts.Capabilities).
WithJSONOptions(fl.opts.JSONOptions).
WithRegoVersion(fl.opts.EffectiveRegoVersion())
WithRegoVersion(fl.opts.RegoVersion)

// For bundle directories add the full path in front of module file names
// to simplify debugging.
Expand Down

0 comments on commit 0738022

Please sign in to comment.