Skip to content

Commit

Permalink
Using --v1-compatible flag instead of --rego-v1
Browse files Browse the repository at this point in the history
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
  • Loading branch information
johanfylling committed Dec 15, 2023
1 parent 2d12c06 commit a411f82
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 41 deletions.
6 changes: 3 additions & 3 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type buildParams struct {
excludeVerifyFiles []string
plugin string
ns string
regoV1 bool
v1Compatible bool
}

func newBuildParams() buildParams {
Expand Down Expand Up @@ -254,7 +254,7 @@ against OPA v0.22.0:
addSigningPluginFlag(buildCommand.Flags(), &buildParams.plugin)
addClaimsFileFlag(buildCommand.Flags(), &buildParams.claimsFile)

addRegoV1Flag(buildCommand.Flags(), &buildParams.regoV1, false)
addV1CompatibleFlag(buildCommand.Flags(), &buildParams.v1Compatible, false)

RootCommand.AddCommand(buildCommand)
}
Expand Down Expand Up @@ -303,7 +303,7 @@ func dobuild(params buildParams, args []string) error {
WithBundleSigningConfig(bsc).
WithPartialNamespace(params.ns)

if params.regoV1 {
if params.v1Compatible {
compiler = compiler.WithRegoVersion(ast.RegoV1)
}

Expand Down
30 changes: 15 additions & 15 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,16 +977,16 @@ func TestBuildBundleModeIgnoreFlag(t *testing.T) {
})
}

func TestBuildWithRegoV1Flag(t *testing.T) {
func TestBuildWithV1CompatibleFlag(t *testing.T) {
tests := []struct {
note string
regoV1 bool
v1Compatible bool
files map[string]string
expectedFiles map[string]string
expectedErr string
}{
{
note: "rego-v0 mode: policy with no rego.v1 or future.keywords imports",
note: "default compatibility: policy with no rego.v1 or future.keywords imports",
files: map[string]string{
"test.rego": `package test
allow if {
Expand All @@ -996,7 +996,7 @@ func TestBuildWithRegoV1Flag(t *testing.T) {
expectedErr: "rego_parse_error",
},
{
note: "rego-v0 mode: policy with rego.v1 imports",
note: "default compatibility: policy with rego.v1 imports",
files: map[string]string{
"test.rego": `package test
import rego.v1
Expand All @@ -1017,7 +1017,7 @@ allow if {
},
},
{
note: "rego-v0 mode: policy with future.keywords imports",
note: "default compatibility: policy with future.keywords imports",
files: map[string]string{
"test.rego": `package test
import future.keywords.if
Expand All @@ -1038,8 +1038,8 @@ allow if {
},
},
{
note: "rego-v1 mode: policy with no rego.v1 or future.keywords imports",
regoV1: true,
note: "1.0 compatibility: policy with no rego.v1 or future.keywords imports",
v1Compatible: true,
files: map[string]string{
"test.rego": `package test
allow if {
Expand All @@ -1057,8 +1057,8 @@ allow if {
},
},
{
note: "rego-v1 mode: policy with rego.v1 import",
regoV1: true,
note: "1.0 compatibility: policy with rego.v1 import",
v1Compatible: true,
files: map[string]string{
"test.rego": `package test
import rego.v1
Expand All @@ -1077,8 +1077,8 @@ allow if {
},
},
{
note: "rego-v1 mode: policy with future.keywords import",
regoV1: true,
note: "1.0 compatibility: policy with future.keywords import",
v1Compatible: true,
files: map[string]string{
"test.rego": `package test
import future.keywords.if
Expand All @@ -1103,7 +1103,7 @@ allow if {
test.WithTempFS(tc.files, func(root string) {
params := newBuildParams()
params.outputFile = path.Join(root, "bundle.tar.gz")
params.regoV1 = tc.regoV1
params.v1Compatible = tc.v1Compatible

err := dobuild(params, []string{root})

Expand All @@ -1120,7 +1120,7 @@ allow if {
}

fl := loader.NewFileLoader()
if tc.regoV1 {
if tc.v1Compatible {
fl = fl.WithRegoVersion(ast.RegoV1)
}
_, err = fl.AsBundle(params.outputFile)
Expand Down Expand Up @@ -1167,7 +1167,7 @@ allow if {
}
}

func TestBuildWithRegoV1FlagOptimized(t *testing.T) {
func TestBuildWithV1CompatibleFlagOptimized(t *testing.T) {
tests := []struct {
note string
files map[string]string
Expand Down Expand Up @@ -1250,7 +1250,7 @@ foo contains __local1__1 if {
test.WithTempFS(tc.files, func(root string) {
params := newBuildParams()
params.outputFile = path.Join(root, "bundle.tar.gz")
params.regoV1 = true
params.v1Compatible = true
params.optimizationLevel = 1

err := dobuild(params, []string{root})
Expand Down
6 changes: 3 additions & 3 deletions cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type evalCommandParams struct {
optimizationLevel int
entrypoints repeatedStringFlag
strict bool
regoV1 bool
v1Compatible bool
}

func newEvalCommandParams() evalCommandParams {
Expand Down Expand Up @@ -327,7 +327,7 @@ access.
addTargetFlag(evalCommand.Flags(), params.target)
addCountFlag(evalCommand.Flags(), &params.count, "benchmark")
addStrictFlag(evalCommand.Flags(), &params.strict, false)
addRegoV1Flag(evalCommand.Flags(), &params.regoV1, false)
addV1CompatibleFlag(evalCommand.Flags(), &params.v1Compatible, false)

RootCommand.AddCommand(evalCommand)
}
Expand Down Expand Up @@ -665,7 +665,7 @@ func setupEval(args []string, params evalCommandParams) (*evalContext, error) {
regoArgs = append(regoArgs, rego.Strict(params.strict))
}

if params.regoV1 {
if params.v1Compatible {
regoArgs = append(regoArgs, rego.SetRegoVersion(ast.RegoV1))
}

Expand Down
29 changes: 14 additions & 15 deletions cmd/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1881,16 +1881,16 @@ func TestUnexpectedElseIfErr(t *testing.T) {
})
}

func TestEvalPolicyWithRegoV1Flag(t *testing.T) {
func TestEvalPolicyWithV1CompatibleFlag(t *testing.T) {
tests := []struct {
note string
regoV1 bool
modules map[string]string
query string
expectedErr string
note string
v1Compatible bool
modules map[string]string
query string
expectedErr string
}{
{
note: "rego-v0 mode: policy with no rego.v1 or future.keywords imports",
note: "default compatibility: policy with no rego.v1 or future.keywords imports",
modules: map[string]string{
"test.rego": `package test
allow if {
Expand All @@ -1901,8 +1901,8 @@ func TestEvalPolicyWithRegoV1Flag(t *testing.T) {
expectedErr: "rego_parse_error",
},
{
note: "rego-v1 mode: policy with no rego.v1 or future.keywords imports",
regoV1: true,
note: "1.0 compatibility: policy with no rego.v1 or future.keywords imports",
v1Compatible: true,
modules: map[string]string{
"test.rego": `package test
allow if {
Expand All @@ -1912,8 +1912,8 @@ func TestEvalPolicyWithRegoV1Flag(t *testing.T) {
query: "data.test.allow",
},
{
note: "rego-v1 mode: policy with rego.v1 import",
regoV1: true,
note: "1.0 compatibility: policy with rego.v1 import",
v1Compatible: true,
modules: map[string]string{
"test.rego": `package test
import rego.v1
Expand All @@ -1924,8 +1924,8 @@ func TestEvalPolicyWithRegoV1Flag(t *testing.T) {
query: "data.test.allow",
},
{
note: "rego-v1 mode: policy with future.keywords import",
regoV1: true,
note: "1.0 compatibility: policy with future.keywords import",
v1Compatible: true,
modules: map[string]string{
"test.rego": `package test
import future.keywords.if
Expand Down Expand Up @@ -1963,8 +1963,7 @@ func TestEvalPolicyWithRegoV1Flag(t *testing.T) {
test.WithTempFS(tc.modules, func(path string) {
params := newEvalCommandParams()
s.commandParams(&params, path)
//params.dataPaths = newrepeatedStringFlag([]string{path})
params.regoV1 = tc.regoV1
params.v1Compatible = tc.v1Compatible

var buf bytes.Buffer

Expand Down
8 changes: 4 additions & 4 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ func addStrictFlag(fs *pflag.FlagSet, strict *bool, value bool) {
fs.BoolVarP(strict, "strict", "S", value, "enable compiler strict mode")
}

func addRegoV1Flag(fs *pflag.FlagSet, regoV1 *bool, value bool) {
addRegoV1FlagWithDescription(fs, regoV1, value, "enforce Rego v1 compatibility")
}

func addRegoV1FlagWithDescription(fs *pflag.FlagSet, regoV1 *bool, value bool, description string) {
fs.BoolVar(regoV1, "rego-v1", value, description)
}

func addV1CompatibleFlag(fs *pflag.FlagSet, v1Compatible *bool, value bool) {
fs.BoolVar(v1Compatible, "v1-compatible", value, "opt-in to OPA features and behaviors that will be enabled by default in a future OPA v1.0 release")
}

func addE2EFlag(fs *pflag.FlagSet, e2e *bool, value bool) {
fs.BoolVar(e2e, "e2e", value, "run benchmarks against a running OPA server")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Current behaviors enabled by this flag include:
runCommand.Flags().BoolVar(&cmdParams.rt.H2CEnabled, "h2c", false, "enable H2C for HTTP listeners")
runCommand.Flags().StringVarP(&cmdParams.rt.OutputFormat, "format", "f", "pretty", "set shell output format, i.e, pretty, json")
runCommand.Flags().BoolVarP(&cmdParams.rt.Watch, "watch", "w", false, "watch command line files for changes")
runCommand.Flags().BoolVar(&cmdParams.rt.V1Compatible, "v1-compatible", false, "opt-in to OPA features and behaviors that will be enabled by default in a future OPA v1.0 release")
addV1CompatibleFlag(runCommand.Flags(), &cmdParams.rt.V1Compatible, false)
addMaxErrorsFlag(runCommand.Flags(), &cmdParams.rt.ErrorLimit)
runCommand.Flags().BoolVar(&cmdParams.rt.PprofEnabled, "pprof", false, "enables pprof endpoints")
runCommand.Flags().StringVar(&cmdParams.tlsCertFile, "tls-cert-file", "", "set path of TLS certificate file")
Expand Down

0 comments on commit a411f82

Please sign in to comment.