Update dependencies (#5518)

This commit is contained in:
hongming
2023-02-12 23:09:20 +08:00
committed by GitHub
parent d3b35fb2da
commit a979342f56
1486 changed files with 126660 additions and 71128 deletions

View File

@@ -8,6 +8,7 @@ package ast
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"math"
@@ -20,7 +21,6 @@ import (
"sync"
"github.com/OneOfOne/xxhash"
"github.com/pkg/errors"
"github.com/open-policy-agent/opa/ast/location"
"github.com/open-policy-agent/opa/util"
@@ -134,12 +134,12 @@ func As(v Value, x interface{}) error {
// Resolver defines the interface for resolving references to native Go values.
type Resolver interface {
Resolve(ref Ref) (interface{}, error)
Resolve(Ref) (interface{}, error)
}
// ValueResolver defines the interface for resolving references to AST values.
type ValueResolver interface {
Resolve(ref Ref) (Value, error)
Resolve(Ref) (Value, error)
}
// UnknownValueErr indicates a ValueResolver was unable to resolve a reference
@@ -216,6 +216,11 @@ func valueToInterface(v Value, resolver Resolver, opt JSONOpt) (interface{}, err
return nil, err
}
return buf, nil
case *lazyObj:
if opt.CopyMaps {
return valueToInterface(v.force(), resolver, opt)
}
return v.native, nil
case Set:
buf := []interface{}{}
iter := func(x *Term) error {
@@ -252,6 +257,7 @@ func JSON(v Value) (interface{}, error) {
// JSONOpt defines parameters for AST to JSON conversion.
type JSONOpt struct {
SortSets bool // sort sets before serializing (this makes conversion more expensive)
CopyMaps bool // enforces copying of map[string]interface{} read from the store
}
// JSONWithOpt returns the JSON representation of v. The value must not contain any
@@ -384,9 +390,13 @@ func (term *Term) Equal(other *Term) bool {
// Get returns a value referred to by name from the term.
func (term *Term) Get(name *Term) *Term {
switch v := term.Value.(type) {
case *object:
return v.Get(name)
case *Array:
return v.Get(name)
case *object:
case interface {
Get(*Term) *Term
}:
return v.Get(name)
case Set:
if v.Contains(name) {
@@ -1810,6 +1820,164 @@ func ObjectTerm(o ...[2]*Term) *Term {
return &Term{Value: NewObject(o...)}
}
func LazyObject(blob map[string]interface{}) Object {
return &lazyObj{native: blob}
}
type lazyObj struct {
strict Object
native map[string]interface{}
}
func (l *lazyObj) force() Object {
if l.strict == nil {
l.strict = MustInterfaceToValue(l.native).(Object)
}
return l.strict
}
func (l *lazyObj) Compare(other Value) int {
return l.force().Compare(other)
}
func (l *lazyObj) Copy() Object {
return l
}
func (l *lazyObj) Diff(other Object) Object {
return l.force().Diff(other)
}
func (l *lazyObj) Intersect(other Object) [][3]*Term {
return l.force().Intersect(other)
}
func (l *lazyObj) Iter(f func(*Term, *Term) error) error {
return l.force().Iter(f)
}
func (l *lazyObj) Until(f func(*Term, *Term) bool) bool {
// NOTE(sr): there could be benefits in not forcing here -- if we abort because
// `f` returns true, we could save us from converting the rest of the object.
return l.force().Until(f)
}
func (l *lazyObj) Foreach(f func(*Term, *Term)) {
l.force().Foreach(f)
}
func (l *lazyObj) Filter(filter Object) (Object, error) {
return l.force().Filter(filter)
}
func (l *lazyObj) Map(f func(*Term, *Term) (*Term, *Term, error)) (Object, error) {
return l.force().Map(f)
}
func (l *lazyObj) MarshalJSON() ([]byte, error) {
return l.force().(*object).MarshalJSON()
}
func (l *lazyObj) Merge(other Object) (Object, bool) {
return l.force().Merge(other)
}
func (l *lazyObj) MergeWith(other Object, conflictResolver func(v1, v2 *Term) (*Term, bool)) (Object, bool) {
return l.force().MergeWith(other, conflictResolver)
}
func (l *lazyObj) Len() int {
return len(l.native)
}
func (l *lazyObj) String() string {
return l.force().String()
}
// get is merely there to implement the Object interface -- `get` there serves the
// purpose of prohibiting external implementations. It's never called for lazyObj.
func (*lazyObj) get(*Term) *objectElem {
return nil
}
func (l *lazyObj) Get(k *Term) *Term {
if l.strict != nil {
return l.strict.Get(k)
}
if s, ok := k.Value.(String); ok {
if val, ok := l.native[string(s)]; ok {
switch val := val.(type) {
case map[string]interface{}:
return NewTerm(&lazyObj{native: val})
default:
return NewTerm(MustInterfaceToValue(val))
}
}
}
return nil
}
func (l *lazyObj) Insert(k, v *Term) {
l.force().Insert(k, v)
}
func (*lazyObj) IsGround() bool {
return true
}
func (l *lazyObj) Hash() int {
return l.force().Hash()
}
func (l *lazyObj) Keys() []*Term {
if l.strict != nil {
return l.strict.Keys()
}
ret := make([]*Term, 0, len(l.native))
for k := range l.native {
ret = append(ret, StringTerm(k))
}
sort.Sort(termSlice(ret))
return ret
}
func (l *lazyObj) KeysIterator() ObjectKeysIterator {
return &lazyObjKeysIterator{keys: l.Keys()}
}
type lazyObjKeysIterator struct {
current int
keys []*Term
}
func (ki *lazyObjKeysIterator) Next() (*Term, bool) {
if ki.current == len(ki.keys) {
return nil, false
}
ki.current++
return ki.keys[ki.current-1], true
}
func (l *lazyObj) Find(path Ref) (Value, error) {
if l.strict != nil {
return l.strict.Find(path)
}
if len(path) == 0 {
return l, nil
}
if p0, ok := path[0].Value.(String); ok {
if v, ok := l.native[string(p0)]; ok {
switch v := v.(type) {
case map[string]interface{}:
return (&lazyObj{native: v}).Find(path[1:])
default:
return MustInterfaceToValue(v).Find(path[1:])
}
}
}
return nil, errFindNotFound
}
type object struct {
elems map[int]*objectElem
keys objectElemSlice
@@ -1861,6 +2029,9 @@ func (obj *object) sortedKeys() objectElemSlice {
// Compare compares obj to other, return <0, 0, or >0 if it is less than, equal to,
// or greater than other.
func (obj *object) Compare(other Value) int {
if x, ok := other.(*lazyObj); ok {
other = x.force()
}
o1 := sortOrder(obj)
o2 := sortOrder(other)
if o1 < o2 {