Update dependencies (#5518)
This commit is contained in:
129
vendor/github.com/open-policy-agent/opa/ast/annotations.go
generated
vendored
129
vendor/github.com/open-policy-agent/opa/ast/annotations.go
generated
vendored
@@ -29,6 +29,7 @@ type (
|
||||
Location *Location `json:"-"`
|
||||
Scope string `json:"scope"`
|
||||
Title string `json:"title,omitempty"`
|
||||
Entrypoint bool `json:"entrypoint,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Organizations []string `json:"organizations,omitempty"`
|
||||
RelatedResources []*RelatedResourceAnnotation `json:"related_resources,omitempty"`
|
||||
@@ -36,6 +37,7 @@ type (
|
||||
Schemas []*SchemaAnnotation `json:"schemas,omitempty"`
|
||||
Custom map[string]interface{} `json:"custom,omitempty"`
|
||||
node Node
|
||||
comments []*Comment
|
||||
}
|
||||
|
||||
// SchemaAnnotation contains a schema declaration for the document identified by the path.
|
||||
@@ -73,6 +75,10 @@ type (
|
||||
Annotations *Annotations `json:"annotations,omitempty"`
|
||||
node Node // The node the annotations are applied to
|
||||
}
|
||||
|
||||
AnnotationsRefSet []*AnnotationsRef
|
||||
|
||||
FlatAnnotationsRefSet AnnotationsRefSet
|
||||
)
|
||||
|
||||
func (a *Annotations) String() string {
|
||||
@@ -90,6 +96,15 @@ func (a *Annotations) SetLoc(l *Location) {
|
||||
a.Location = l
|
||||
}
|
||||
|
||||
// EndLoc returns the location of this annotation's last comment line.
|
||||
func (a *Annotations) EndLoc() *Location {
|
||||
count := len(a.comments)
|
||||
if count == 0 {
|
||||
return a.Location
|
||||
}
|
||||
return a.comments[count-1].Location
|
||||
}
|
||||
|
||||
// Compare returns an integer indicating if a is less than, equal to, or greater
|
||||
// than other.
|
||||
func (a *Annotations) Compare(other *Annotations) int {
|
||||
@@ -134,6 +149,13 @@ func (a *Annotations) Compare(other *Annotations) int {
|
||||
return cmp
|
||||
}
|
||||
|
||||
if a.Entrypoint != other.Entrypoint {
|
||||
if a.Entrypoint {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
if cmp := util.Compare(a.Custom, other.Custom); cmp != 0 {
|
||||
return cmp
|
||||
}
|
||||
@@ -154,8 +176,13 @@ func (a *Annotations) GetTargetPath() Ref {
|
||||
}
|
||||
|
||||
func NewAnnotationsRef(a *Annotations) *AnnotationsRef {
|
||||
var loc *Location
|
||||
if a.node != nil {
|
||||
loc = a.node.Loc()
|
||||
}
|
||||
|
||||
return &AnnotationsRef{
|
||||
Location: a.node.Loc(),
|
||||
Location: loc,
|
||||
Path: a.GetTargetPath(),
|
||||
Annotations: a,
|
||||
node: a.node,
|
||||
@@ -324,6 +351,10 @@ func (a *Annotations) toObject() (*Object, *Error) {
|
||||
obj.Insert(StringTerm("title"), StringTerm(a.Title))
|
||||
}
|
||||
|
||||
if a.Entrypoint {
|
||||
obj.Insert(StringTerm("entrypoint"), BooleanTerm(true))
|
||||
}
|
||||
|
||||
if len(a.Description) > 0 {
|
||||
obj.Insert(StringTerm("description"), StringTerm(a.Description))
|
||||
}
|
||||
@@ -426,6 +457,10 @@ func attachAnnotationsNodes(mod *Module) Errors {
|
||||
if err := validateAnnotationScopeAttachment(a); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
if err := validateAnnotationEntrypointAttachment(a); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
return errs
|
||||
@@ -449,6 +484,13 @@ func validateAnnotationScopeAttachment(a *Annotations) *Error {
|
||||
return NewError(ParseErr, a.Loc(), "invalid annotation scope '%v'", a.Scope)
|
||||
}
|
||||
|
||||
func validateAnnotationEntrypointAttachment(a *Annotations) *Error {
|
||||
if a.Entrypoint && !(a.Scope == annotationScopeRule || a.Scope == annotationScopePackage) {
|
||||
return NewError(ParseErr, a.Loc(), "annotation entrypoint applied to non-rule or package scope '%v'", a.Scope)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Copy returns a deep copy of a.
|
||||
func (a *AuthorAnnotation) Copy() *AuthorAnnotation {
|
||||
cpy := *a
|
||||
@@ -575,32 +617,39 @@ func BuildAnnotationSet(modules []*Module) (*AnnotationSet, Errors) {
|
||||
return as, nil
|
||||
}
|
||||
|
||||
// NOTE(philipc): During copy propagation, the underlying Nodes can be
|
||||
// stripped away from the annotations, leading to nil deref panics. We
|
||||
// silently ignore these cases for now, as a workaround.
|
||||
func (as *AnnotationSet) add(a *Annotations) *Error {
|
||||
switch a.Scope {
|
||||
case annotationScopeRule:
|
||||
rule := a.node.(*Rule)
|
||||
as.byRule[rule] = append(as.byRule[rule], a)
|
||||
if rule, ok := a.node.(*Rule); ok {
|
||||
as.byRule[rule] = append(as.byRule[rule], a)
|
||||
}
|
||||
case annotationScopePackage:
|
||||
pkg := a.node.(*Package)
|
||||
if exist, ok := as.byPackage[pkg]; ok {
|
||||
return errAnnotationRedeclared(a, exist.Location)
|
||||
if pkg, ok := a.node.(*Package); ok {
|
||||
if exist, ok := as.byPackage[pkg]; ok {
|
||||
return errAnnotationRedeclared(a, exist.Location)
|
||||
}
|
||||
as.byPackage[pkg] = a
|
||||
}
|
||||
as.byPackage[pkg] = a
|
||||
case annotationScopeDocument:
|
||||
rule := a.node.(*Rule)
|
||||
path := rule.Path()
|
||||
x := as.byPath.get(path)
|
||||
if x != nil {
|
||||
return errAnnotationRedeclared(a, x.Value.Location)
|
||||
if rule, ok := a.node.(*Rule); ok {
|
||||
path := rule.Path()
|
||||
x := as.byPath.get(path)
|
||||
if x != nil {
|
||||
return errAnnotationRedeclared(a, x.Value.Location)
|
||||
}
|
||||
as.byPath.insert(path, a)
|
||||
}
|
||||
as.byPath.insert(path, a)
|
||||
case annotationScopeSubpackages:
|
||||
pkg := a.node.(*Package)
|
||||
x := as.byPath.get(pkg.Path)
|
||||
if x != nil && x.Value != nil {
|
||||
return errAnnotationRedeclared(a, x.Value.Location)
|
||||
if pkg, ok := a.node.(*Package); ok {
|
||||
x := as.byPath.get(pkg.Path)
|
||||
if x != nil && x.Value != nil {
|
||||
return errAnnotationRedeclared(a, x.Value.Location)
|
||||
}
|
||||
as.byPath.insert(pkg.Path, a)
|
||||
}
|
||||
as.byPath.insert(pkg.Path, a)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -638,7 +687,7 @@ func (as *AnnotationSet) GetPackageScope(pkg *Package) *Annotations {
|
||||
|
||||
// Flatten returns a flattened list view of this AnnotationSet.
|
||||
// The returned slice is sorted, first by the annotations' target path, then by their target location
|
||||
func (as *AnnotationSet) Flatten() []*AnnotationsRef {
|
||||
func (as *AnnotationSet) Flatten() FlatAnnotationsRefSet {
|
||||
// This preallocation often won't be optimal, but it's superior to starting with a nil slice.
|
||||
refs := make([]*AnnotationsRef, 0, len(as.byPath.Children)+len(as.byRule)+len(as.byPackage))
|
||||
|
||||
@@ -656,13 +705,7 @@ func (as *AnnotationSet) Flatten() []*AnnotationsRef {
|
||||
|
||||
// Sort by path, then annotation location, for stable output
|
||||
sort.SliceStable(refs, func(i, j int) bool {
|
||||
if refs[i].Path.Compare(refs[j].Path) < 0 {
|
||||
return true
|
||||
}
|
||||
if refs[i].Annotations.Location.Compare(refs[j].Annotations.Location) < 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return refs[i].Compare(refs[j]) < 0
|
||||
})
|
||||
|
||||
return refs
|
||||
@@ -675,7 +718,7 @@ func (as *AnnotationSet) Flatten() []*AnnotationsRef {
|
||||
// 2. The 'package' scope entry, if any
|
||||
// 3. Entries for the 'subpackages' scope, if any; ordered from the closest package path to the fartest. E.g.: 'do.re.mi', 'do.re', 'do'
|
||||
// The returned slice is guaranteed to always contain at least one entry, corresponding to the given rule.
|
||||
func (as *AnnotationSet) Chain(rule *Rule) []*AnnotationsRef {
|
||||
func (as *AnnotationSet) Chain(rule *Rule) AnnotationsRefSet {
|
||||
var refs []*AnnotationsRef
|
||||
|
||||
ruleAnnots := as.GetRuleScope(rule)
|
||||
@@ -721,6 +764,26 @@ func (as *AnnotationSet) Chain(rule *Rule) []*AnnotationsRef {
|
||||
return refs
|
||||
}
|
||||
|
||||
func (ars FlatAnnotationsRefSet) Insert(ar *AnnotationsRef) FlatAnnotationsRefSet {
|
||||
result := make(FlatAnnotationsRefSet, 0, len(ars)+1)
|
||||
|
||||
// insertion sort, first by path, then location
|
||||
for i, current := range ars {
|
||||
if ar.Compare(current) < 0 {
|
||||
result = append(result, ar)
|
||||
result = append(result, ars[i:]...)
|
||||
break
|
||||
}
|
||||
result = append(result, current)
|
||||
}
|
||||
|
||||
if len(result) < len(ars)+1 {
|
||||
result = append(result, ar)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func newAnnotationTree() *annotationTreeNode {
|
||||
return &annotationTreeNode{
|
||||
Value: nil,
|
||||
@@ -784,3 +847,15 @@ func (t *annotationTreeNode) flatten(refs []*AnnotationsRef) []*AnnotationsRef {
|
||||
}
|
||||
return refs
|
||||
}
|
||||
|
||||
func (ar *AnnotationsRef) Compare(other *AnnotationsRef) int {
|
||||
if c := ar.Path.Compare(other.Path); c != 0 {
|
||||
return c
|
||||
}
|
||||
|
||||
if c := ar.Annotations.Location.Compare(other.Annotations.Location); c != 0 {
|
||||
return c
|
||||
}
|
||||
|
||||
return ar.Annotations.Compare(other.Annotations)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user