Initial QSfera import
This commit is contained in:
+49
@@ -0,0 +1,49 @@
|
||||
// Copyright 2021 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 future
|
||||
|
||||
import "github.com/open-policy-agent/opa/v1/ast"
|
||||
|
||||
// FilterFutureImports filters OUT any future imports from the passed slice of
|
||||
// `*ast.Import`s.
|
||||
func FilterFutureImports(imps []*ast.Import) []*ast.Import {
|
||||
ret := []*ast.Import{}
|
||||
for _, imp := range imps {
|
||||
path := imp.Path.Value.(ast.Ref)
|
||||
if !ast.FutureRootDocument.Equal(path[0]) {
|
||||
ret = append(ret, imp)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// IsAllFutureKeywords returns true if the passed *ast.Import is `future.keywords`
|
||||
func IsAllFutureKeywords(imp *ast.Import) bool {
|
||||
path := imp.Path.Value.(ast.Ref)
|
||||
return len(path) == 2 &&
|
||||
ast.FutureRootDocument.Equal(path[0]) &&
|
||||
path[1].Equal(ast.InternedTerm("keywords"))
|
||||
}
|
||||
|
||||
// IsFutureKeyword returns true if the passed *ast.Import is `future.keywords.{kw}`
|
||||
func IsFutureKeyword(imp *ast.Import, kw string) bool {
|
||||
path := imp.Path.Value.(ast.Ref)
|
||||
return len(path) == 3 &&
|
||||
ast.FutureRootDocument.Equal(path[0]) &&
|
||||
path[1].Equal(ast.InternedTerm("keywords")) &&
|
||||
path[2].Equal(ast.StringTerm(kw))
|
||||
}
|
||||
|
||||
func WhichFutureKeyword(imp *ast.Import) (string, bool) {
|
||||
path := imp.Path.Value.(ast.Ref)
|
||||
if len(path) == 3 &&
|
||||
ast.FutureRootDocument.Equal(path[0]) &&
|
||||
path[1].Equal(ast.InternedTerm("keywords")) {
|
||||
if str, ok := path[2].Value.(ast.String); ok {
|
||||
return string(str), true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
// Copyright 2021 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 future
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/open-policy-agent/opa/v1/ast"
|
||||
)
|
||||
|
||||
// ParserOptionsFromFutureImports transforms a slice of `ast.Import`s into the
|
||||
// `ast.ParserOptions` that can be used to parse a statement according to the
|
||||
// included "future.keywords" and "future.keywords.xyz" imports.
|
||||
func ParserOptionsFromFutureImports(imports []*ast.Import) (ast.ParserOptions, error) {
|
||||
popts := ast.ParserOptions{
|
||||
FutureKeywords: []string{},
|
||||
}
|
||||
for _, imp := range imports {
|
||||
path := imp.Path.Value.(ast.Ref)
|
||||
if !ast.FutureRootDocument.Equal(path[0]) {
|
||||
continue
|
||||
}
|
||||
if len(path) >= 2 {
|
||||
if string(path[1].Value.(ast.String)) != "keywords" {
|
||||
return popts, fmt.Errorf("unknown future import: %v", imp)
|
||||
}
|
||||
if len(path) == 2 {
|
||||
// retun, one "future.keywords" import means we can disregard any others
|
||||
return ast.ParserOptions{AllFutureKeywords: true}, nil
|
||||
}
|
||||
}
|
||||
if len(path) == 3 {
|
||||
if imp.Alias != "" {
|
||||
return popts, errors.New("alias not supported")
|
||||
}
|
||||
popts.FutureKeywords = append(popts.FutureKeywords, string(path[2].Value.(ast.String)))
|
||||
}
|
||||
}
|
||||
return popts, nil
|
||||
}
|
||||
Reference in New Issue
Block a user