update dependencies (#6267)

Signed-off-by: hongming <coder.scala@gmail.com>
This commit is contained in:
hongming
2024-11-06 10:27:06 +08:00
committed by GitHub
parent faf255a084
commit cfebd96a1f
4263 changed files with 341374 additions and 132036 deletions

View File

@@ -100,7 +100,9 @@ var Wildcard = &Term{Value: Var("_")}
var WildcardPrefix = "$"
// Keywords contains strings that map to language keywords.
var Keywords = [...]string{
var Keywords = KeywordsForRegoVersion(DefaultRegoVersion)
var KeywordsV0 = [...]string{
"not",
"package",
"import",
@@ -114,9 +116,41 @@ var Keywords = [...]string{
"some",
}
var KeywordsV1 = [...]string{
"not",
"package",
"import",
"as",
"default",
"else",
"with",
"null",
"true",
"false",
"some",
"if",
"contains",
"in",
"every",
}
func KeywordsForRegoVersion(v RegoVersion) []string {
switch v {
case RegoV0:
return KeywordsV0[:]
case RegoV1, RegoV0CompatV1:
return KeywordsV1[:]
}
return nil
}
// IsKeyword returns true if s is a language keyword.
func IsKeyword(s string) bool {
for _, x := range Keywords {
return IsInKeywords(s, Keywords)
}
func IsInKeywords(s string, keywords []string) bool {
for _, x := range keywords {
if x == s {
return true
}
@@ -124,6 +158,26 @@ func IsKeyword(s string) bool {
return false
}
// IsKeywordInRegoVersion returns true if s is a language keyword.
func IsKeywordInRegoVersion(s string, regoVersion RegoVersion) bool {
switch regoVersion {
case RegoV0:
for _, x := range KeywordsV0 {
if x == s {
return true
}
}
case RegoV1, RegoV0CompatV1:
for _, x := range KeywordsV1 {
if x == s {
return true
}
}
}
return false
}
type (
// Node represents a node in an AST. Nodes may be statements in a policy module
// or elements of an ad-hoc query, expression, etc.
@@ -185,11 +239,12 @@ type (
// Rule represents a rule as defined in the language. Rules define the
// content of documents that represent policy decisions.
Rule struct {
Default bool `json:"default,omitempty"`
Head *Head `json:"head"`
Body Body `json:"body"`
Else *Rule `json:"else,omitempty"`
Location *Location `json:"location,omitempty"`
Default bool `json:"default,omitempty"`
Head *Head `json:"head"`
Body Body `json:"body"`
Else *Rule `json:"else,omitempty"`
Location *Location `json:"location,omitempty"`
Annotations []*Annotations `json:"annotations,omitempty"`
// Module is a pointer to the module containing this rule. If the rule
// was NOT created while parsing/constructing a module, this should be
@@ -232,7 +287,9 @@ type (
Negated bool `json:"negated,omitempty"`
Location *Location `json:"location,omitempty"`
jsonOptions astJSON.Options
jsonOptions astJSON.Options
generatedFrom *Expr
generates []*Expr
}
// SomeDecl represents a variable declaration statement. The symbols are variables.
@@ -309,8 +366,8 @@ func (mod *Module) Copy() *Module {
nodes[mod.Package] = cpy.Package
cpy.Annotations = make([]*Annotations, len(mod.Annotations))
for i := range mod.Annotations {
cpy.Annotations[i] = mod.Annotations[i].Copy(nodes[mod.Annotations[i].node])
for i, a := range mod.Annotations {
cpy.Annotations[i] = a.Copy(nodes[a.node])
}
cpy.Comments = make([]*Comment, len(mod.Comments))
@@ -362,7 +419,7 @@ func (mod *Module) String() string {
buf = append(buf, "")
for _, rule := range mod.Rules {
buf = appendAnnotationStrings(buf, rule)
buf = append(buf, rule.String())
buf = append(buf, rule.stringWithOpts(toStringOpts{regoVersion: mod.regoVersion}))
}
}
return strings.Join(buf, "\n")
@@ -407,6 +464,12 @@ func (mod *Module) RegoVersion() RegoVersion {
return mod.regoVersion
}
// SetRegoVersion sets the RegoVersion for the module.
// Note: Setting a rego-version that does not match the module's rego-version might have unintended consequences.
func (mod *Module) SetRegoVersion(v RegoVersion) {
mod.regoVersion = v
}
// NewComment returns a new Comment object.
func NewComment(text []byte) *Comment {
return &Comment{
@@ -521,7 +584,7 @@ func (pkg *Package) MarshalJSON() ([]byte, error) {
}
// IsValidImportPath returns an error indicating if the import path is invalid.
// If the import path is invalid, err is nil.
// If the import path is valid, err is nil.
func IsValidImportPath(v Value) (err error) {
switch v := v.(type) {
case Var:
@@ -657,6 +720,11 @@ func (rule *Rule) Compare(other *Rule) int {
if cmp := rule.Body.Compare(other.Body); cmp != 0 {
return cmp
}
if cmp := annotationsCompare(rule.Annotations, other.Annotations); cmp != 0 {
return cmp
}
return rule.Else.Compare(other.Else)
}
@@ -665,6 +733,12 @@ func (rule *Rule) Copy() *Rule {
cpy := *rule
cpy.Head = rule.Head.Copy()
cpy.Body = rule.Body.Copy()
cpy.Annotations = make([]*Annotations, len(rule.Annotations))
for i, a := range rule.Annotations {
cpy.Annotations[i] = a.Copy(&cpy)
}
if cpy.Else != nil {
cpy.Else = rule.Else.Copy()
}
@@ -710,18 +784,30 @@ func (rule *Rule) Ref() Ref {
}
func (rule *Rule) String() string {
return rule.stringWithOpts(toStringOpts{})
}
type toStringOpts struct {
regoVersion RegoVersion
}
func (rule *Rule) stringWithOpts(opts toStringOpts) string {
buf := []string{}
if rule.Default {
buf = append(buf, "default")
}
buf = append(buf, rule.Head.String())
buf = append(buf, rule.Head.stringWithOpts(opts))
if !rule.Default {
switch opts.regoVersion {
case RegoV1, RegoV0CompatV1:
buf = append(buf, "if")
}
buf = append(buf, "{")
buf = append(buf, rule.Body.String())
buf = append(buf, "}")
}
if rule.Else != nil {
buf = append(buf, rule.Else.elseString())
buf = append(buf, rule.Else.elseString(opts))
}
return strings.Join(buf, " ")
}
@@ -757,10 +843,14 @@ func (rule *Rule) MarshalJSON() ([]byte, error) {
}
}
if len(rule.Annotations) != 0 {
data["annotations"] = rule.Annotations
}
return json.Marshal(data)
}
func (rule *Rule) elseString() string {
func (rule *Rule) elseString(opts toStringOpts) string {
var buf []string
buf = append(buf, "else")
@@ -771,12 +861,17 @@ func (rule *Rule) elseString() string {
buf = append(buf, value.String())
}
switch opts.regoVersion {
case RegoV1, RegoV0CompatV1:
buf = append(buf, "if")
}
buf = append(buf, "{")
buf = append(buf, rule.Body.String())
buf = append(buf, "}")
if rule.Else != nil {
buf = append(buf, rule.Else.elseString())
buf = append(buf, rule.Else.elseString(opts))
}
return strings.Join(buf, " ")
@@ -936,16 +1031,28 @@ func (head *Head) Equal(other *Head) bool {
}
func (head *Head) String() string {
return head.stringWithOpts(toStringOpts{})
}
func (head *Head) stringWithOpts(opts toStringOpts) string {
buf := strings.Builder{}
buf.WriteString(head.Ref().String())
containsAdded := false
switch {
case len(head.Args) != 0:
buf.WriteString(head.Args.String())
case len(head.Reference) == 1 && head.Key != nil:
buf.WriteRune('[')
buf.WriteString(head.Key.String())
buf.WriteRune(']')
switch opts.regoVersion {
case RegoV0:
buf.WriteRune('[')
buf.WriteString(head.Key.String())
buf.WriteRune(']')
default:
containsAdded = true
buf.WriteString(" contains ")
buf.WriteString(head.Key.String())
}
}
if head.Value != nil {
if head.Assign {
@@ -954,7 +1061,7 @@ func (head *Head) String() string {
buf.WriteString(" = ")
}
buf.WriteString(head.Value.String())
} else if head.Name == "" && head.Key != nil {
} else if !containsAdded && head.Name == "" && head.Key != nil {
buf.WriteString(" contains ")
buf.WriteString(head.Key.String())
}
@@ -970,16 +1077,22 @@ func (head *Head) setJSONOptions(opts astJSON.Options) {
func (head *Head) MarshalJSON() ([]byte, error) {
var loc *Location
if head.jsonOptions.MarshalOptions.IncludeLocation.Head {
includeLoc := head.jsonOptions.MarshalOptions.IncludeLocation
if includeLoc.Head {
if head.Location != nil {
loc = head.Location
}
for _, term := range head.Reference {
if term.Location != nil {
term.jsonOptions.MarshalOptions.IncludeLocation.Term = includeLoc.Term
}
}
}
// NOTE(sr): we do this to override the rendering of `head.Reference`.
// It's still what'll be used via the default means of encoding/json
// for unmarshaling a json object into a Head struct!
// NOTE(charlieegan3): we also need to optionally include the location
type h Head
return json.Marshal(struct {
h
@@ -1234,7 +1347,7 @@ func (expr *Expr) Equal(other *Expr) bool {
//
// 1. Declarations are always less than other expressions.
// 2. Preceding expression (by Index) is always less than the other expression.
// 3. Non-negated expressions are always less than than negated expressions.
// 3. Non-negated expressions are always less than negated expressions.
// 4. Single term expressions are always less than built-in expressions.
//
// Otherwise, the expression terms are compared normally. If both expressions
@@ -1571,6 +1684,46 @@ func NewBuiltinExpr(terms ...*Term) *Expr {
return &Expr{Terms: terms}
}
func (expr *Expr) CogeneratedExprs() []*Expr {
visited := map[*Expr]struct{}{}
visitCogeneratedExprs(expr, func(e *Expr) bool {
if expr.Equal(e) {
return true
}
if _, ok := visited[e]; ok {
return true
}
visited[e] = struct{}{}
return false
})
result := make([]*Expr, 0, len(visited))
for e := range visited {
result = append(result, e)
}
return result
}
func (expr *Expr) BaseCogeneratedExpr() *Expr {
if expr.generatedFrom == nil {
return expr
}
return expr.generatedFrom.BaseCogeneratedExpr()
}
func visitCogeneratedExprs(expr *Expr, f func(*Expr) bool) {
if parent := expr.generatedFrom; parent != nil {
if stop := f(parent); !stop {
visitCogeneratedExprs(parent, f)
}
}
for _, child := range expr.generates {
if stop := f(child); !stop {
visitCogeneratedExprs(child, f)
}
}
}
func (d *SomeDecl) String() string {
if call, ok := d.Symbols[0].Value.(Call); ok {
if len(call) == 4 {