mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
Simplify interning (#7714)
Use a single generic entrypoint for obtaining interned terms regardless of type. Signed-off-by: Anders Eknert <anders@styra.com>
This commit is contained in:
+2
-2
@@ -9,14 +9,14 @@ import (
|
||||
)
|
||||
|
||||
func InternedBooleanTerm(b bool) *Term {
|
||||
return v1.InternedBooleanTerm(b)
|
||||
return v1.InternedTerm(b)
|
||||
}
|
||||
|
||||
// InternedIntNumberTerm returns a term with the given integer value. The term is
|
||||
// cached between -1 to 512, and for values outside of that range, this function
|
||||
// is equivalent to ast.IntNumberTerm.
|
||||
func InternedIntNumberTerm(i int) *Term {
|
||||
return v1.InternedIntNumberTerm(i)
|
||||
return v1.InternedTerm(i)
|
||||
}
|
||||
|
||||
func HasInternedIntNumberTerm(i int) bool {
|
||||
|
||||
+1
-4
@@ -69,10 +69,7 @@ func parse(args []string, params *parseParams, stdout io.Writer, stderr io.Write
|
||||
exposeLocation := false
|
||||
exposeComments := true
|
||||
for _, opt := range strings.Split(params.jsonInclude, ",") {
|
||||
value := true
|
||||
if strings.HasPrefix(opt, "-") {
|
||||
value = false
|
||||
}
|
||||
value := !strings.HasPrefix(opt, "-")
|
||||
|
||||
if strings.HasSuffix(opt, "locations") {
|
||||
exposeLocation = value
|
||||
|
||||
@@ -723,7 +723,7 @@ func (e *EditTree) Unfold(path ast.Ref) (*EditTree, error) {
|
||||
return child.Unfold(path[1:])
|
||||
}
|
||||
|
||||
idxt := ast.InternedIntNumberTerm(idx)
|
||||
idxt := ast.InternedTerm(idx)
|
||||
|
||||
// Fall back to looking up the key in e.value.
|
||||
// Extend the tree if key is present. Error otherwise.
|
||||
@@ -1028,7 +1028,7 @@ func (e *EditTree) Exists(path ast.Ref) bool {
|
||||
}
|
||||
// Fallback if child lookup failed.
|
||||
// We have to ensure that the lookup term is a number here, or Find will fail.
|
||||
_, err = x.Find(ast.Ref{ast.InternedIntNumberTerm(idx)}.Concat(path[1:]))
|
||||
_, err = x.Find(ast.Ref{ast.InternedTerm(idx)}.Concat(path[1:]))
|
||||
return err == nil
|
||||
default:
|
||||
// Catch all primitive types.
|
||||
|
||||
@@ -24,7 +24,7 @@ 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.InternedStringTerm("keywords"))
|
||||
path[1].Equal(ast.InternedTerm("keywords"))
|
||||
}
|
||||
|
||||
// IsFutureKeyword returns true if the passed *ast.Import is `future.keywords.{kw}`
|
||||
@@ -32,7 +32,7 @@ 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.InternedStringTerm("keywords")) &&
|
||||
path[1].Equal(ast.InternedTerm("keywords")) &&
|
||||
path[2].Equal(ast.StringTerm(kw))
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ 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.InternedStringTerm("keywords")) {
|
||||
path[1].Equal(ast.InternedTerm("keywords")) {
|
||||
if str, ok := path[2].Value.(ast.String); ok {
|
||||
return string(str), true
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ func Term(params Params) (*ast.Term, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
obj.Insert(ast.InternedStringTerm("config"), ast.NewTerm(v))
|
||||
obj.Insert(ast.InternedTerm("config"), ast.NewTerm(v))
|
||||
}
|
||||
|
||||
env := ast.NewObject()
|
||||
@@ -52,11 +52,11 @@ func Term(params Params) (*ast.Term, error) {
|
||||
}
|
||||
}
|
||||
|
||||
obj.Insert(ast.InternedStringTerm("env"), ast.NewTerm(env))
|
||||
obj.Insert(ast.InternedStringTerm("version"), ast.StringTerm(version.Version))
|
||||
obj.Insert(ast.InternedStringTerm("commit"), ast.StringTerm(version.Vcs))
|
||||
obj.Insert(ast.InternedStringTerm("authorization_enabled"), ast.InternedBooleanTerm(params.IsAuthorizationEnabled))
|
||||
obj.Insert(ast.InternedStringTerm("skip_known_schema_check"), ast.InternedBooleanTerm(params.SkipKnownSchemaCheck))
|
||||
obj.Insert(ast.InternedTerm("env"), ast.NewTerm(env))
|
||||
obj.Insert(ast.InternedTerm("version"), ast.StringTerm(version.Version))
|
||||
obj.Insert(ast.InternedTerm("commit"), ast.StringTerm(version.Vcs))
|
||||
obj.Insert(ast.InternedTerm("authorization_enabled"), ast.InternedTerm(params.IsAuthorizationEnabled))
|
||||
obj.Insert(ast.InternedTerm("skip_known_schema_check"), ast.InternedTerm(params.SkipKnownSchemaCheck))
|
||||
|
||||
return ast.NewTerm(obj), nil
|
||||
}
|
||||
|
||||
+12
-12
@@ -295,7 +295,7 @@ func builtinGraphQLParseAndVerify(_ BuiltinContext, operands []*ast.Term, iter f
|
||||
var err error
|
||||
|
||||
unverified := ast.ArrayTerm(
|
||||
ast.InternedBooleanTerm(false),
|
||||
ast.InternedTerm(false),
|
||||
ast.NewTerm(ast.NewObject()),
|
||||
ast.NewTerm(ast.NewObject()),
|
||||
)
|
||||
@@ -353,7 +353,7 @@ func builtinGraphQLParseAndVerify(_ BuiltinContext, operands []*ast.Term, iter f
|
||||
|
||||
// Construct return value.
|
||||
verified := ast.ArrayTerm(
|
||||
ast.InternedBooleanTerm(true),
|
||||
ast.InternedTerm(true),
|
||||
ast.NewTerm(queryResult),
|
||||
ast.NewTerm(querySchema),
|
||||
)
|
||||
@@ -421,10 +421,10 @@ func builtinGraphQLIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*as
|
||||
queryDoc, err = objectToQueryDocument(x)
|
||||
default:
|
||||
// Error if wrong type.
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
switch x := operands[1].Value.(type) {
|
||||
@@ -434,23 +434,23 @@ func builtinGraphQLIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*as
|
||||
schemaDoc, err = objectToSchemaDocument(x)
|
||||
default:
|
||||
// Error if wrong type.
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
// Validate the query against the schema, erroring if there's an issue.
|
||||
schema, err := convertSchema(schemaDoc)
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
if err := validateQuery(schema, queryDoc); err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
// If we got this far, the GraphQL query passed validation.
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
}
|
||||
|
||||
func builtinGraphQLSchemaIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -464,15 +464,15 @@ func builtinGraphQLSchemaIsValid(_ BuiltinContext, operands []*ast.Term, iter fu
|
||||
schemaDoc, err = objectToSchemaDocument(x)
|
||||
default:
|
||||
// Error if wrong type.
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
// Validate the schema, this determines the result
|
||||
_, err = convertSchema(schemaDoc)
|
||||
return iter(ast.InternedBooleanTerm(err == nil))
|
||||
return iter(ast.InternedTerm(err == nil))
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
+20
-20
@@ -423,28 +423,28 @@ func (a *Annotations) toObject() (*Object, *Error) {
|
||||
if len(a.Scope) > 0 {
|
||||
switch a.Scope {
|
||||
case annotationScopeDocument:
|
||||
obj.Insert(InternedStringTerm("scope"), InternedStringTerm("document"))
|
||||
obj.Insert(InternedTerm("scope"), InternedTerm("document"))
|
||||
case annotationScopePackage:
|
||||
obj.Insert(InternedStringTerm("scope"), InternedStringTerm("package"))
|
||||
obj.Insert(InternedTerm("scope"), InternedTerm("package"))
|
||||
case annotationScopeRule:
|
||||
obj.Insert(InternedStringTerm("scope"), InternedStringTerm("rule"))
|
||||
obj.Insert(InternedTerm("scope"), InternedTerm("rule"))
|
||||
case annotationScopeSubpackages:
|
||||
obj.Insert(InternedStringTerm("scope"), InternedStringTerm("subpackages"))
|
||||
obj.Insert(InternedTerm("scope"), InternedTerm("subpackages"))
|
||||
default:
|
||||
obj.Insert(InternedStringTerm("scope"), StringTerm(a.Scope))
|
||||
obj.Insert(InternedTerm("scope"), StringTerm(a.Scope))
|
||||
}
|
||||
}
|
||||
|
||||
if len(a.Title) > 0 {
|
||||
obj.Insert(InternedStringTerm("title"), StringTerm(a.Title))
|
||||
obj.Insert(InternedTerm("title"), StringTerm(a.Title))
|
||||
}
|
||||
|
||||
if a.Entrypoint {
|
||||
obj.Insert(InternedStringTerm("entrypoint"), InternedBooleanTerm(true))
|
||||
obj.Insert(InternedTerm("entrypoint"), InternedTerm(true))
|
||||
}
|
||||
|
||||
if len(a.Description) > 0 {
|
||||
obj.Insert(InternedStringTerm("description"), StringTerm(a.Description))
|
||||
obj.Insert(InternedTerm("description"), StringTerm(a.Description))
|
||||
}
|
||||
|
||||
if len(a.Organizations) > 0 {
|
||||
@@ -452,19 +452,19 @@ func (a *Annotations) toObject() (*Object, *Error) {
|
||||
for _, org := range a.Organizations {
|
||||
orgs = append(orgs, StringTerm(org))
|
||||
}
|
||||
obj.Insert(InternedStringTerm("organizations"), ArrayTerm(orgs...))
|
||||
obj.Insert(InternedTerm("organizations"), ArrayTerm(orgs...))
|
||||
}
|
||||
|
||||
if len(a.RelatedResources) > 0 {
|
||||
rrs := make([]*Term, 0, len(a.RelatedResources))
|
||||
for _, rr := range a.RelatedResources {
|
||||
rrObj := NewObject(Item(InternedStringTerm("ref"), StringTerm(rr.Ref.String())))
|
||||
rrObj := NewObject(Item(InternedTerm("ref"), StringTerm(rr.Ref.String())))
|
||||
if len(rr.Description) > 0 {
|
||||
rrObj.Insert(InternedStringTerm("description"), StringTerm(rr.Description))
|
||||
rrObj.Insert(InternedTerm("description"), StringTerm(rr.Description))
|
||||
}
|
||||
rrs = append(rrs, NewTerm(rrObj))
|
||||
}
|
||||
obj.Insert(InternedStringTerm("related_resources"), ArrayTerm(rrs...))
|
||||
obj.Insert(InternedTerm("related_resources"), ArrayTerm(rrs...))
|
||||
}
|
||||
|
||||
if len(a.Authors) > 0 {
|
||||
@@ -472,14 +472,14 @@ func (a *Annotations) toObject() (*Object, *Error) {
|
||||
for _, author := range a.Authors {
|
||||
aObj := NewObject()
|
||||
if len(author.Name) > 0 {
|
||||
aObj.Insert(InternedStringTerm("name"), StringTerm(author.Name))
|
||||
aObj.Insert(InternedTerm("name"), StringTerm(author.Name))
|
||||
}
|
||||
if len(author.Email) > 0 {
|
||||
aObj.Insert(InternedStringTerm("email"), StringTerm(author.Email))
|
||||
aObj.Insert(InternedTerm("email"), StringTerm(author.Email))
|
||||
}
|
||||
as = append(as, NewTerm(aObj))
|
||||
}
|
||||
obj.Insert(InternedStringTerm("authors"), ArrayTerm(as...))
|
||||
obj.Insert(InternedTerm("authors"), ArrayTerm(as...))
|
||||
}
|
||||
|
||||
if len(a.Schemas) > 0 {
|
||||
@@ -487,21 +487,21 @@ func (a *Annotations) toObject() (*Object, *Error) {
|
||||
for _, s := range a.Schemas {
|
||||
sObj := NewObject()
|
||||
if len(s.Path) > 0 {
|
||||
sObj.Insert(InternedStringTerm("path"), NewTerm(s.Path.toArray()))
|
||||
sObj.Insert(InternedTerm("path"), NewTerm(s.Path.toArray()))
|
||||
}
|
||||
if len(s.Schema) > 0 {
|
||||
sObj.Insert(InternedStringTerm("schema"), NewTerm(s.Schema.toArray()))
|
||||
sObj.Insert(InternedTerm("schema"), NewTerm(s.Schema.toArray()))
|
||||
}
|
||||
if s.Definition != nil {
|
||||
def, err := InterfaceToValue(s.Definition)
|
||||
if err != nil {
|
||||
return nil, NewError(CompileErr, a.Location, "invalid definition in schema annotation: %s", err.Error())
|
||||
}
|
||||
sObj.Insert(InternedStringTerm("definition"), NewTerm(def))
|
||||
sObj.Insert(InternedTerm("definition"), NewTerm(def))
|
||||
}
|
||||
ss = append(ss, NewTerm(sObj))
|
||||
}
|
||||
obj.Insert(InternedStringTerm("schemas"), ArrayTerm(ss...))
|
||||
obj.Insert(InternedTerm("schemas"), ArrayTerm(ss...))
|
||||
}
|
||||
|
||||
if len(a.Custom) > 0 {
|
||||
@@ -509,7 +509,7 @@ func (a *Annotations) toObject() (*Object, *Error) {
|
||||
if err != nil {
|
||||
return nil, NewError(CompileErr, a.Location, "invalid custom annotation %s", err.Error())
|
||||
}
|
||||
obj.Insert(InternedStringTerm("custom"), NewTerm(c))
|
||||
obj.Insert(InternedTerm("custom"), NewTerm(c))
|
||||
}
|
||||
|
||||
return &obj, nil
|
||||
|
||||
+1
-1
@@ -3399,7 +3399,7 @@ func (b *Builtin) Ref() Ref {
|
||||
ref := make(Ref, len(parts))
|
||||
ref[0] = VarTerm(parts[0])
|
||||
for i := 1; i < len(parts); i++ {
|
||||
ref[i] = InternedStringTerm(parts[i])
|
||||
ref[i] = InternedTerm(parts[i])
|
||||
}
|
||||
return ref
|
||||
}
|
||||
|
||||
+3
-3
@@ -964,7 +964,7 @@ func (c *Compiler) buildComprehensionIndices() {
|
||||
}
|
||||
}
|
||||
|
||||
var futureKeywordsPrefix = Ref{FutureRootDocument, InternedStringTerm("keywords")}
|
||||
var futureKeywordsPrefix = Ref{FutureRootDocument, InternedTerm("keywords")}
|
||||
|
||||
// buildRequiredCapabilities updates the required capabilities on the compiler
|
||||
// to include any keyword and feature dependencies present in the modules. The
|
||||
@@ -2531,13 +2531,13 @@ func createMetadataChain(chain []*AnnotationsRef) (*Term, *Error) {
|
||||
for _, link := range chain {
|
||||
// Dropping leading 'data' element of path
|
||||
p := link.Path[1:].toArray()
|
||||
obj := NewObject(Item(InternedStringTerm("path"), NewTerm(p)))
|
||||
obj := NewObject(Item(InternedTerm("path"), NewTerm(p)))
|
||||
if link.Annotations != nil {
|
||||
annotObj, err := link.Annotations.toObject()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
obj.Insert(InternedStringTerm("annotations"), NewTerm(*annotObj))
|
||||
obj.Insert(InternedTerm("annotations"), NewTerm(*annotObj))
|
||||
}
|
||||
metaArray = metaArray.Append(NewTerm(obj))
|
||||
}
|
||||
|
||||
+2
-2
@@ -402,7 +402,7 @@ func (i *refindices) updateGlobMatch(rule *Rule, expr *Expr) {
|
||||
if ref == nil {
|
||||
for j, arg := range args {
|
||||
if arg.Equal(match) {
|
||||
ref = Ref{FunctionArgRootDocument, InternedIntNumberTerm(j)}
|
||||
ref = Ref{FunctionArgRootDocument, InternedTerm(j)}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -822,7 +822,7 @@ func eqOperandsToRefAndValue(isVirtual func(Ref) bool, args []*Term, a, b *Term)
|
||||
for i, arg := range args {
|
||||
if arg.Value.Compare(a.Value) == 0 {
|
||||
if bval, ok := indexValue(b); ok {
|
||||
return &refindex{Ref: Ref{FunctionArgRootDocument, InternedIntNumberTerm(i)}, Value: bval}, true
|
||||
return &refindex{Ref: Ref{FunctionArgRootDocument, InternedTerm(i)}, Value: bval}, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+100
-220
@@ -8,18 +8,16 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type internable interface {
|
||||
bool | string | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
|
||||
}
|
||||
|
||||
// NOTE! Great care must be taken **not** to modify the terms returned
|
||||
// from these functions, as they are shared across all callers.
|
||||
// This package is currently considered experimental, and may change
|
||||
// at any time without notice.
|
||||
|
||||
var (
|
||||
booleanTrueTerm = &Term{Value: Boolean(true)}
|
||||
booleanFalseTerm = &Term{Value: Boolean(false)}
|
||||
|
||||
// since this is by far the most common negative number
|
||||
minusOneTerm = &Term{Value: Number("-1")}
|
||||
|
||||
InternedNullTerm = &Term{Value: Null{}}
|
||||
|
||||
InternedEmptyString = StringTerm("")
|
||||
@@ -28,6 +26,16 @@ var (
|
||||
InternedEmptySet = SetTerm()
|
||||
|
||||
InternedEmptyArrayValue = NewArray()
|
||||
|
||||
booleanTrueTerm = &Term{Value: Boolean(true)}
|
||||
booleanFalseTerm = &Term{Value: Boolean(false)}
|
||||
|
||||
// since this is by far the most common negative number
|
||||
minusOneTerm = &Term{Value: Number("-1")}
|
||||
|
||||
internedStringTerms = map[string]*Term{
|
||||
"": InternedEmptyString,
|
||||
}
|
||||
)
|
||||
|
||||
// InternStringTerm interns the given strings as terms. Note that Interning is
|
||||
@@ -44,28 +52,37 @@ func InternStringTerm(str ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
// InternedBooleanTerm returns an interned term with the given boolean value.
|
||||
func InternedBooleanTerm(b bool) *Term {
|
||||
if b {
|
||||
return booleanTrueTerm
|
||||
// Interned returns a possibly interned term for the given scalar value.
|
||||
// If the value is not interned, a new term is created for that value.
|
||||
func InternedTerm[T internable](v T) *Term {
|
||||
switch value := any(v).(type) {
|
||||
case bool:
|
||||
return internedBooleanTerm(value)
|
||||
case string:
|
||||
return internedStringTerm(value)
|
||||
case int:
|
||||
return internedIntNumberTerm(value)
|
||||
case int8:
|
||||
return internedIntNumberTerm(int(value))
|
||||
case int16:
|
||||
return internedIntNumberTerm(int(value))
|
||||
case int32:
|
||||
return internedIntNumberTerm(int(value))
|
||||
case int64:
|
||||
return internedIntNumberTerm(int(value))
|
||||
case uint:
|
||||
return internedIntNumberTerm(int(value))
|
||||
case uint8:
|
||||
return internedIntNumberTerm(int(value))
|
||||
case uint16:
|
||||
return internedIntNumberTerm(int(value))
|
||||
case uint32:
|
||||
return internedIntNumberTerm(int(value))
|
||||
case uint64:
|
||||
return internedIntNumberTerm(int(value))
|
||||
default:
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
return booleanFalseTerm
|
||||
}
|
||||
|
||||
// InternedIntNumberTerm returns a term with the given integer value. The term is
|
||||
// cached between -1 to 512, and for values outside of that range, this function
|
||||
// is equivalent to ast.IntNumberTerm.
|
||||
func InternedIntNumberTerm(i int) *Term {
|
||||
if i >= 0 && i < len(intNumberTerms) {
|
||||
return intNumberTerms[i]
|
||||
}
|
||||
|
||||
if i == -1 {
|
||||
return minusOneTerm
|
||||
}
|
||||
|
||||
return &Term{Value: Number(strconv.Itoa(i))}
|
||||
}
|
||||
|
||||
// InternedIntFromString returns a term with the given integer value if the string
|
||||
@@ -85,17 +102,6 @@ func HasInternedIntNumberTerm(i int) bool {
|
||||
return i >= -1 && i < len(intNumberTerms)
|
||||
}
|
||||
|
||||
// InternedStringTerm returns an interned term with the given string value. If the
|
||||
// provided string is not interned, a new term is created for that value. It does *not*
|
||||
// modify the global interned terms map.
|
||||
func InternedStringTerm(s string) *Term {
|
||||
if term, ok := internedStringTerms[s]; ok {
|
||||
return term
|
||||
}
|
||||
|
||||
return StringTerm(s)
|
||||
}
|
||||
|
||||
// Returns an interned string term representing the integer value i, if
|
||||
// interned. If not, creates a new StringTerm for the integer value.
|
||||
func InternedIntegerString(i int) *Term {
|
||||
@@ -117,195 +123,69 @@ func InternedIntegerString(i int) *Term {
|
||||
return StringTerm(s)
|
||||
}
|
||||
|
||||
var internedStringTerms = map[string]*Term{
|
||||
"": InternedEmptyString,
|
||||
"0": StringTerm("0"),
|
||||
"1": StringTerm("1"),
|
||||
"2": StringTerm("2"),
|
||||
"3": StringTerm("3"),
|
||||
"4": StringTerm("4"),
|
||||
"5": StringTerm("5"),
|
||||
"6": StringTerm("6"),
|
||||
"7": StringTerm("7"),
|
||||
"8": StringTerm("8"),
|
||||
"9": StringTerm("9"),
|
||||
"10": StringTerm("10"),
|
||||
"11": StringTerm("11"),
|
||||
"12": StringTerm("12"),
|
||||
"13": StringTerm("13"),
|
||||
"14": StringTerm("14"),
|
||||
"15": StringTerm("15"),
|
||||
"16": StringTerm("16"),
|
||||
"17": StringTerm("17"),
|
||||
"18": StringTerm("18"),
|
||||
"19": StringTerm("19"),
|
||||
"20": StringTerm("20"),
|
||||
"21": StringTerm("21"),
|
||||
"22": StringTerm("22"),
|
||||
"23": StringTerm("23"),
|
||||
"24": StringTerm("24"),
|
||||
"25": StringTerm("25"),
|
||||
"26": StringTerm("26"),
|
||||
"27": StringTerm("27"),
|
||||
"28": StringTerm("28"),
|
||||
"29": StringTerm("29"),
|
||||
"30": StringTerm("30"),
|
||||
"31": StringTerm("31"),
|
||||
"32": StringTerm("32"),
|
||||
"33": StringTerm("33"),
|
||||
"34": StringTerm("34"),
|
||||
"35": StringTerm("35"),
|
||||
"36": StringTerm("36"),
|
||||
"37": StringTerm("37"),
|
||||
"38": StringTerm("38"),
|
||||
"39": StringTerm("39"),
|
||||
"40": StringTerm("40"),
|
||||
"41": StringTerm("41"),
|
||||
"42": StringTerm("42"),
|
||||
"43": StringTerm("43"),
|
||||
"44": StringTerm("44"),
|
||||
"45": StringTerm("45"),
|
||||
"46": StringTerm("46"),
|
||||
"47": StringTerm("47"),
|
||||
"48": StringTerm("48"),
|
||||
"49": StringTerm("49"),
|
||||
"50": StringTerm("50"),
|
||||
"51": StringTerm("51"),
|
||||
"52": StringTerm("52"),
|
||||
"53": StringTerm("53"),
|
||||
"54": StringTerm("54"),
|
||||
"55": StringTerm("55"),
|
||||
"56": StringTerm("56"),
|
||||
"57": StringTerm("57"),
|
||||
"58": StringTerm("58"),
|
||||
"59": StringTerm("59"),
|
||||
"60": StringTerm("60"),
|
||||
"61": StringTerm("61"),
|
||||
"62": StringTerm("62"),
|
||||
"63": StringTerm("63"),
|
||||
"64": StringTerm("64"),
|
||||
"65": StringTerm("65"),
|
||||
"66": StringTerm("66"),
|
||||
"67": StringTerm("67"),
|
||||
"68": StringTerm("68"),
|
||||
"69": StringTerm("69"),
|
||||
"70": StringTerm("70"),
|
||||
"71": StringTerm("71"),
|
||||
"72": StringTerm("72"),
|
||||
"73": StringTerm("73"),
|
||||
"74": StringTerm("74"),
|
||||
"75": StringTerm("75"),
|
||||
"76": StringTerm("76"),
|
||||
"77": StringTerm("77"),
|
||||
"78": StringTerm("78"),
|
||||
"79": StringTerm("79"),
|
||||
"80": StringTerm("80"),
|
||||
"81": StringTerm("81"),
|
||||
"82": StringTerm("82"),
|
||||
"83": StringTerm("83"),
|
||||
"84": StringTerm("84"),
|
||||
"85": StringTerm("85"),
|
||||
"86": StringTerm("86"),
|
||||
"87": StringTerm("87"),
|
||||
"88": StringTerm("88"),
|
||||
"89": StringTerm("89"),
|
||||
"90": StringTerm("90"),
|
||||
"91": StringTerm("91"),
|
||||
"92": StringTerm("92"),
|
||||
"93": StringTerm("93"),
|
||||
"94": StringTerm("94"),
|
||||
"95": StringTerm("95"),
|
||||
"96": StringTerm("96"),
|
||||
"97": StringTerm("97"),
|
||||
"98": StringTerm("98"),
|
||||
"99": StringTerm("99"),
|
||||
"100": StringTerm("100"),
|
||||
// InternedBooleanTerm returns an interned term with the given boolean value.
|
||||
func internedBooleanTerm(b bool) *Term {
|
||||
if b {
|
||||
return booleanTrueTerm
|
||||
}
|
||||
|
||||
// Types
|
||||
"null": StringTerm("null"),
|
||||
"boolean": StringTerm("boolean"),
|
||||
"number": StringTerm("number"),
|
||||
"string": StringTerm("string"),
|
||||
"array": StringTerm("array"),
|
||||
"object": StringTerm("object"),
|
||||
"set": StringTerm("set"),
|
||||
return booleanFalseTerm
|
||||
}
|
||||
|
||||
// Runtime
|
||||
"config": StringTerm("config"),
|
||||
"env": StringTerm("env"),
|
||||
"version": StringTerm("version"),
|
||||
"commit": StringTerm("commit"),
|
||||
"authorization_enabled": StringTerm("authorization_enabled"),
|
||||
"skip_known_schema_check": StringTerm("skip_known_schema_check"),
|
||||
// InternedIntNumberTerm returns a term with the given integer value. The term is
|
||||
// cached between -1 to 512, and for values outside of that range, this function
|
||||
// is equivalent to IntNumberTerm.
|
||||
func internedIntNumberTerm(i int) *Term {
|
||||
if i >= 0 && i < len(intNumberTerms) {
|
||||
return intNumberTerms[i]
|
||||
}
|
||||
|
||||
// Annotations
|
||||
"annotations": StringTerm("annotations"),
|
||||
"scope": StringTerm("scope"),
|
||||
"title": StringTerm("title"),
|
||||
"entrypoint": StringTerm("entrypoint"),
|
||||
"description": StringTerm("description"),
|
||||
"organizations": StringTerm("organizations"),
|
||||
"authors": StringTerm("authors"),
|
||||
"related_resources": StringTerm("related_resources"),
|
||||
"schemas": StringTerm("schemas"),
|
||||
"custom": StringTerm("custom"),
|
||||
"ref": StringTerm("ref"),
|
||||
"name": StringTerm("name"),
|
||||
"email": StringTerm("email"),
|
||||
"schema": StringTerm("schema"),
|
||||
"definition": StringTerm("definition"),
|
||||
"document": StringTerm("document"),
|
||||
"package": StringTerm("package"),
|
||||
"rule": StringTerm("rule"),
|
||||
"subpackages": StringTerm("subpackages"),
|
||||
if i == -1 {
|
||||
return minusOneTerm
|
||||
}
|
||||
|
||||
// Debug
|
||||
"text": StringTerm("text"),
|
||||
"value": StringTerm("value"),
|
||||
"bindings": StringTerm("bindings"),
|
||||
"expressions": StringTerm("expressions"),
|
||||
return &Term{Value: Number(strconv.Itoa(i))}
|
||||
}
|
||||
|
||||
// Various
|
||||
"data": StringTerm("data"),
|
||||
"input": StringTerm("input"),
|
||||
"result": StringTerm("result"),
|
||||
"keywords": StringTerm("keywords"),
|
||||
"path": StringTerm("path"),
|
||||
"v1": StringTerm("v1"),
|
||||
"error": StringTerm("error"),
|
||||
"partial": StringTerm("partial"),
|
||||
// InternedStringTerm returns an interned term with the given string value. If the
|
||||
// provided string is not interned, a new term is created for that value. It does *not*
|
||||
// modify the global interned terms map.
|
||||
func internedStringTerm(s string) *Term {
|
||||
if term, ok := internedStringTerms[s]; ok {
|
||||
return term
|
||||
}
|
||||
|
||||
// HTTP
|
||||
"code": StringTerm("code"),
|
||||
"message": StringTerm("message"),
|
||||
"status_code": StringTerm("status_code"),
|
||||
"method": StringTerm("method"),
|
||||
"url": StringTerm("url"),
|
||||
return StringTerm(s)
|
||||
}
|
||||
|
||||
// JWT
|
||||
"enc": StringTerm("enc"),
|
||||
"cty": StringTerm("cty"),
|
||||
"iss": StringTerm("iss"),
|
||||
"exp": StringTerm("exp"),
|
||||
"nbf": StringTerm("nbf"),
|
||||
"aud": StringTerm("aud"),
|
||||
"secret": StringTerm("secret"),
|
||||
"cert": StringTerm("cert"),
|
||||
|
||||
// Decisions
|
||||
"revision": StringTerm("revision"),
|
||||
"labels": StringTerm("labels"),
|
||||
"decision_id": StringTerm("decision_id"),
|
||||
"bundles": StringTerm("bundles"),
|
||||
"query": StringTerm("query"),
|
||||
"mapped_result": StringTerm("mapped_result"),
|
||||
"nd_builtin_cache": StringTerm("nd_builtin_cache"),
|
||||
"erased": StringTerm("erased"),
|
||||
"masked": StringTerm("masked"),
|
||||
"requested_by": StringTerm("requested_by"),
|
||||
"timestamp": StringTerm("timestamp"),
|
||||
"metrics": StringTerm("metrics"),
|
||||
"req_id": StringTerm("req_id"),
|
||||
func init() {
|
||||
InternStringTerm(
|
||||
// Numbers
|
||||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
|
||||
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38",
|
||||
"39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56",
|
||||
"57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74",
|
||||
"75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92",
|
||||
"93", "94", "95", "96", "97", "98", "99", "100",
|
||||
// Types
|
||||
"null", "boolean", "number", "string", "array", "object", "set", "var", "ref", "true", "false",
|
||||
// Runtime
|
||||
"config", "env", "version", "commit", "authorization_enabled", "skip_known_schema_check",
|
||||
// Annotations
|
||||
"annotations", "scope", "title", "entrypoint", "description", "organizations", "authors", "related_resources",
|
||||
"schemas", "custom", "name", "email", "schema", "definition", "document", "package", "rule", "subpackages",
|
||||
// Debug
|
||||
"text", "value", "bindings", "expressions",
|
||||
// Various
|
||||
"data", "input", "result", "keywords", "path", "v1", "error", "partial",
|
||||
// HTTP
|
||||
"code", "message", "status_code", "method", "url", "uri",
|
||||
// JWT
|
||||
"enc", "cty", "iss", "exp", "nbf", "aud", "secret", "cert",
|
||||
// Decisions
|
||||
"revision", "labels", "decision_id", "bundles", "query", "mapped_result", "nd_builtin_cache",
|
||||
"erased", "masked", "requested_by", "timestamp", "metrics", "req_id",
|
||||
)
|
||||
}
|
||||
|
||||
var stringToIntNumberTermMap = map[string]*Term{
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package ast_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/open-policy-agent/opa/v1/ast"
|
||||
)
|
||||
|
||||
var (
|
||||
val = ast.String("open-policy-agent")
|
||||
obj = map[string]ast.Value{"open-policy-agent": val}
|
||||
)
|
||||
|
||||
//go:noinline
|
||||
func getPackageVarValue() ast.Value {
|
||||
return val
|
||||
}
|
||||
|
||||
//go:noinline
|
||||
func getObjectValue() ast.Value {
|
||||
return obj["open-policy-agent"]
|
||||
}
|
||||
|
||||
//go:noinline
|
||||
func getInternedValue() ast.Value {
|
||||
return ast.InternedTerm("open-policy-agent").Value
|
||||
}
|
||||
|
||||
//go:noinline
|
||||
func getNewValue() ast.Value {
|
||||
return ast.String("open-policy-agent")
|
||||
}
|
||||
|
||||
// Benchmark experiment to compare the performance of accessing values in different ways.
|
||||
//
|
||||
// BenchmarkInterningAccessValue/package_var_value-12 100000000 10.95 ns/op 16 B/op 1 allocs/op
|
||||
// BenchmarkInterningAccessValue/interned_value-12 175498335 6.81 ns/op 0 B/op 0 allocs/op
|
||||
// BenchmarkInterningAccessValue/object_value-12 247139934 4.78 ns/op 0 B/op 0 allocs/op
|
||||
// BenchmarkInterningAccessValue/new_value-12 1000000000 0.70 ns/op 0 B/op 0 allocs/op
|
||||
func BenchmarkInterningAccessValue(b *testing.B) {
|
||||
ast.InternStringTerm("open-policy-agent")
|
||||
|
||||
b.Run("package var value", func(b *testing.B) {
|
||||
for range b.N {
|
||||
_ = getPackageVarValue()
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("interned value", func(b *testing.B) {
|
||||
for range b.N {
|
||||
_ = getInternedValue()
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("object value", func(b *testing.B) {
|
||||
for range b.N {
|
||||
_ = getObjectValue()
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("new value", func(b *testing.B) {
|
||||
for range b.N {
|
||||
_ = getNewValue()
|
||||
}
|
||||
})
|
||||
}
|
||||
+2
-2
@@ -36,7 +36,7 @@ const DefaultMaxParsingRecursionDepth = 100000
|
||||
// recursion exceeds the maximum allowed depth
|
||||
var ErrMaxParsingRecursionDepthExceeded = errors.New("max parsing recursion depth exceeded")
|
||||
|
||||
var RegoV1CompatibleRef = Ref{VarTerm("rego"), InternedStringTerm("v1")}
|
||||
var RegoV1CompatibleRef = Ref{VarTerm("rego"), InternedTerm("v1")}
|
||||
|
||||
// RegoVersion defines the Rego syntax requirements for a module.
|
||||
type RegoVersion int
|
||||
@@ -2778,7 +2778,7 @@ func IsFutureKeywordForRegoVersion(s string, v RegoVersion) bool {
|
||||
func (p *Parser) futureImport(imp *Import, allowedFutureKeywords map[string]tokens.Token) {
|
||||
path := imp.Path.Value.(Ref)
|
||||
|
||||
if len(path) == 1 || !path[1].Equal(InternedStringTerm("keywords")) {
|
||||
if len(path) == 1 || !path[1].Equal(InternedTerm("keywords")) {
|
||||
p.errorf(imp.Path.Location, "invalid import, must be `future.keywords`")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1245,10 +1245,10 @@ func TestFutureImports(t *testing.T) {
|
||||
assertParseErrorContains(t, "keyword import + alias", "import future.keywords.in as xyz", "`future` imports cannot be aliased")
|
||||
|
||||
assertParseImport(t, "import kw with kw in options",
|
||||
"import future.keywords.in", &Import{Path: RefTerm(VarTerm("future"), InternedStringTerm("keywords"), StringTerm("in"))},
|
||||
"import future.keywords.in", &Import{Path: RefTerm(VarTerm("future"), InternedTerm("keywords"), StringTerm("in"))},
|
||||
ParserOptions{FutureKeywords: []string{"in"}})
|
||||
assertParseImport(t, "import kw with all kw in options",
|
||||
"import future.keywords.in", &Import{Path: RefTerm(VarTerm("future"), InternedStringTerm("keywords"), StringTerm("in"))},
|
||||
"import future.keywords.in", &Import{Path: RefTerm(VarTerm("future"), InternedTerm("keywords"), StringTerm("in"))},
|
||||
ParserOptions{AllFutureKeywords: true})
|
||||
|
||||
mod := `
|
||||
|
||||
+5
-5
@@ -61,7 +61,7 @@ func InterfaceToValue(x any) (Value, error) {
|
||||
case nil:
|
||||
return NullValue, nil
|
||||
case bool:
|
||||
return InternedBooleanTerm(x).Value, nil
|
||||
return InternedTerm(x).Value, nil
|
||||
case json.Number:
|
||||
if interned := InternedIntNumberTermFromString(string(x)); interned != nil {
|
||||
return interned.Value, nil
|
||||
@@ -587,9 +587,9 @@ type Boolean bool
|
||||
// BooleanTerm creates a new Term with a Boolean value.
|
||||
func BooleanTerm(b bool) *Term {
|
||||
if b {
|
||||
return &Term{Value: InternedBooleanTerm(true).Value}
|
||||
return &Term{Value: InternedTerm(true).Value}
|
||||
}
|
||||
return &Term{Value: InternedBooleanTerm(false).Value}
|
||||
return &Term{Value: InternedTerm(false).Value}
|
||||
}
|
||||
|
||||
// Equal returns true if the other Value is a Boolean and is equal.
|
||||
@@ -624,7 +624,7 @@ func (bol Boolean) Compare(other Value) int {
|
||||
// Find returns the current value or a not found error.
|
||||
func (bol Boolean) Find(path Ref) (Value, error) {
|
||||
if len(path) == 0 {
|
||||
return InternedBooleanTerm(bool(bol)).Value, nil
|
||||
return InternedTerm(bool(bol)).Value, nil
|
||||
}
|
||||
return nil, errFindNotFound
|
||||
}
|
||||
@@ -1237,7 +1237,7 @@ func (ref Ref) toArray() *Array {
|
||||
if _, ok := term.Value.(String); ok {
|
||||
terms = append(terms, term)
|
||||
} else {
|
||||
terms = append(terms, InternedStringTerm(term.Value.String()))
|
||||
terms = append(terms, InternedTerm(term.Value.String()))
|
||||
}
|
||||
}
|
||||
return NewArray(terms...)
|
||||
|
||||
@@ -19,7 +19,7 @@ func BenchmarkObjectLookup(b *testing.B) {
|
||||
b.Run(strconv.Itoa(n), func(b *testing.B) {
|
||||
obj := NewObject()
|
||||
for i := range n {
|
||||
obj.Insert(StringTerm(strconv.Itoa(i)), InternedIntNumberTerm(i))
|
||||
obj.Insert(StringTerm(strconv.Itoa(i)), InternedTerm(i))
|
||||
}
|
||||
key := StringTerm(strconv.Itoa(n - 1))
|
||||
b.ResetTimer()
|
||||
|
||||
+5
-4
@@ -923,13 +923,14 @@ func TestSetOperations(t *testing.T) {
|
||||
s2 := MustParseTerm(tc.b).Value.(Set)
|
||||
s3 := MustParseTerm(tc.c).Value.(Set)
|
||||
var result Set
|
||||
if tc.op == "-" {
|
||||
switch tc.op {
|
||||
case "-":
|
||||
result = s1.Diff(s2)
|
||||
} else if tc.op == "&" {
|
||||
case "&":
|
||||
result = s1.Intersect(s2)
|
||||
} else if tc.op == "|" {
|
||||
case "|":
|
||||
result = s1.Union(s2)
|
||||
} else {
|
||||
default:
|
||||
panic("bad operation")
|
||||
}
|
||||
if result.Compare(s3) != 0 {
|
||||
|
||||
+4
-4
@@ -499,14 +499,14 @@ func (t *thread) resultVars(rs rego.ResultSet) VarRef {
|
||||
continue
|
||||
}
|
||||
expressions = expressions.Append(ast.ObjectTerm(
|
||||
ast.Item(ast.InternedStringTerm("text"), t),
|
||||
ast.Item(ast.InternedStringTerm("value"), ast.NewTerm(v)),
|
||||
ast.Item(ast.InternedTerm("text"), t),
|
||||
ast.Item(ast.InternedTerm("value"), ast.NewTerm(v)),
|
||||
))
|
||||
}
|
||||
|
||||
res := ast.NewObject(
|
||||
ast.Item(ast.InternedStringTerm("bindings"), ast.NewTerm(bindings)),
|
||||
ast.Item(ast.InternedStringTerm("expressions"), ast.NewTerm(expressions)),
|
||||
ast.Item(ast.InternedTerm("bindings"), ast.NewTerm(bindings)),
|
||||
ast.Item(ast.InternedTerm("expressions"), ast.NewTerm(expressions)),
|
||||
)
|
||||
|
||||
vars = append(vars, namedVar{
|
||||
|
||||
+4
-4
@@ -533,7 +533,7 @@ func (w *writer) writeRules(rules []*ast.Rule, comments []*ast.Comment) ([]*ast.
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
var expandedConst = ast.NewBody(ast.NewExpr(ast.InternedBooleanTerm(true)))
|
||||
var expandedConst = ast.NewBody(ast.NewExpr(ast.InternedTerm(true)))
|
||||
|
||||
func (w *writer) groupableOneLiner(rule *ast.Rule) bool {
|
||||
// Location required to determine if two rules are adjacent in the policy.
|
||||
@@ -790,7 +790,7 @@ func (w *writer) writeHead(head *ast.Head, isDefault bool, isExpandedConst bool,
|
||||
}
|
||||
|
||||
if head.Value != nil &&
|
||||
(head.Key != nil || !ast.InternedBooleanTerm(true).Equal(head.Value) || isExpandedConst || isDefault) {
|
||||
(head.Key != nil || !ast.InternedTerm(true).Equal(head.Value) || isExpandedConst || isDefault) {
|
||||
|
||||
// in rego v1, explicitly print value for ref-head constants that aren't partial set assignments, e.g.:
|
||||
// * a -> parser error, won't reach here
|
||||
@@ -801,7 +801,7 @@ func (w *writer) writeHead(head *ast.Head, isDefault bool, isExpandedConst bool,
|
||||
|
||||
if head.Location == head.Value.Location &&
|
||||
head.Name != "else" &&
|
||||
ast.InternedBooleanTerm(true).Equal(head.Value) &&
|
||||
ast.InternedTerm(true).Equal(head.Value) &&
|
||||
!isRegoV1RefConst {
|
||||
// If the value location is the same as the location of the head,
|
||||
// we know that the value is generated, i.e. f(1)
|
||||
@@ -2211,5 +2211,5 @@ func isRegoV1Compatible(imp *ast.Import) bool {
|
||||
path := imp.Path.Value.(ast.Ref)
|
||||
return len(path) == 2 &&
|
||||
ast.RegoRootDocument.Equal(path[0]) &&
|
||||
path[1].Equal(ast.InternedStringTerm("v1"))
|
||||
path[1].Equal(ast.InternedTerm("v1"))
|
||||
}
|
||||
|
||||
+1
-1
@@ -462,7 +462,7 @@ func getSchemaSetByPathKey(path string) ast.Ref {
|
||||
key := make(ast.Ref, 1+len(parts))
|
||||
key[0] = ast.SchemaRootDocument
|
||||
for i := range parts {
|
||||
key[i+1] = ast.StringTerm(parts[i])
|
||||
key[i+1] = ast.InternedTerm(parts[i])
|
||||
}
|
||||
|
||||
return key
|
||||
|
||||
+20
-20
@@ -89,7 +89,7 @@ type HTTPRequestContext struct {
|
||||
func (b *BundleInfoV1) AST() ast.Value {
|
||||
result := ast.NewObject()
|
||||
if len(b.Revision) > 0 {
|
||||
result.Insert(ast.InternedStringTerm("revision"), ast.StringTerm(b.Revision))
|
||||
result.Insert(ast.InternedTerm("revision"), ast.StringTerm(b.Revision))
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -100,7 +100,7 @@ func (b *BundleInfoV1) AST() ast.Value {
|
||||
func (e *EventV1) AST() (ast.Value, error) {
|
||||
var err error
|
||||
event := ast.NewObject(
|
||||
ast.Item(ast.InternedStringTerm("decision_id"), ast.StringTerm(e.DecisionID)),
|
||||
ast.Item(ast.InternedTerm("decision_id"), ast.StringTerm(e.DecisionID)),
|
||||
)
|
||||
|
||||
if e.Labels != nil {
|
||||
@@ -108,13 +108,13 @@ func (e *EventV1) AST() (ast.Value, error) {
|
||||
for k, v := range e.Labels {
|
||||
labelsObj.Insert(ast.StringTerm(k), ast.StringTerm(v))
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("labels"), ast.NewTerm(labelsObj))
|
||||
event.Insert(ast.InternedTerm("labels"), ast.NewTerm(labelsObj))
|
||||
} else {
|
||||
event.Insert(ast.InternedStringTerm("labels"), ast.NullTerm())
|
||||
event.Insert(ast.InternedTerm("labels"), ast.NullTerm())
|
||||
}
|
||||
|
||||
if len(e.Revision) > 0 {
|
||||
event.Insert(ast.InternedStringTerm("revision"), ast.StringTerm(e.Revision))
|
||||
event.Insert(ast.InternedTerm("revision"), ast.StringTerm(e.Revision))
|
||||
}
|
||||
|
||||
if len(e.Bundles) > 0 {
|
||||
@@ -122,25 +122,25 @@ func (e *EventV1) AST() (ast.Value, error) {
|
||||
for k, v := range e.Bundles {
|
||||
bundlesObj.Insert(ast.StringTerm(k), ast.NewTerm(v.AST()))
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("bundles"), ast.NewTerm(bundlesObj))
|
||||
event.Insert(ast.InternedTerm("bundles"), ast.NewTerm(bundlesObj))
|
||||
}
|
||||
|
||||
if len(e.Path) > 0 {
|
||||
event.Insert(ast.InternedStringTerm("path"), ast.StringTerm(e.Path))
|
||||
event.Insert(ast.InternedTerm("path"), ast.StringTerm(e.Path))
|
||||
}
|
||||
|
||||
if len(e.Query) > 0 {
|
||||
event.Insert(ast.InternedStringTerm("query"), ast.StringTerm(e.Query))
|
||||
event.Insert(ast.InternedTerm("query"), ast.StringTerm(e.Query))
|
||||
}
|
||||
|
||||
if e.inputAST != nil {
|
||||
event.Insert(ast.InternedStringTerm("input"), ast.NewTerm(e.inputAST))
|
||||
event.Insert(ast.InternedTerm("input"), ast.NewTerm(e.inputAST))
|
||||
} else if e.Input != nil {
|
||||
e.inputAST, err = roundtripJSONToAST(e.Input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("input"), ast.NewTerm(e.inputAST))
|
||||
event.Insert(ast.InternedTerm("input"), ast.NewTerm(e.inputAST))
|
||||
}
|
||||
|
||||
if e.Result != nil {
|
||||
@@ -148,7 +148,7 @@ func (e *EventV1) AST() (ast.Value, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("result"), ast.NewTerm(results))
|
||||
event.Insert(ast.InternedTerm("result"), ast.NewTerm(results))
|
||||
}
|
||||
|
||||
if e.MappedResult != nil {
|
||||
@@ -156,7 +156,7 @@ func (e *EventV1) AST() (ast.Value, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("mapped_result"), ast.NewTerm(mResults))
|
||||
event.Insert(ast.InternedTerm("mapped_result"), ast.NewTerm(mResults))
|
||||
}
|
||||
|
||||
if e.NDBuiltinCache != nil {
|
||||
@@ -164,7 +164,7 @@ func (e *EventV1) AST() (ast.Value, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("nd_builtin_cache"), ast.NewTerm(ndbCache))
|
||||
event.Insert(ast.InternedTerm("nd_builtin_cache"), ast.NewTerm(ndbCache))
|
||||
}
|
||||
|
||||
if len(e.Erased) > 0 {
|
||||
@@ -172,7 +172,7 @@ func (e *EventV1) AST() (ast.Value, error) {
|
||||
for i, v := range e.Erased {
|
||||
erased[i] = ast.StringTerm(v)
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("erased"), ast.ArrayTerm(erased...))
|
||||
event.Insert(ast.InternedTerm("erased"), ast.ArrayTerm(erased...))
|
||||
}
|
||||
|
||||
if len(e.Masked) > 0 {
|
||||
@@ -180,7 +180,7 @@ func (e *EventV1) AST() (ast.Value, error) {
|
||||
for i, v := range e.Masked {
|
||||
masked[i] = ast.StringTerm(v)
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("masked"), ast.ArrayTerm(masked...))
|
||||
event.Insert(ast.InternedTerm("masked"), ast.ArrayTerm(masked...))
|
||||
}
|
||||
|
||||
if e.Error != nil {
|
||||
@@ -188,11 +188,11 @@ func (e *EventV1) AST() (ast.Value, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("error"), ast.NewTerm(evalErr))
|
||||
event.Insert(ast.InternedTerm("error"), ast.NewTerm(evalErr))
|
||||
}
|
||||
|
||||
if len(e.RequestedBy) > 0 {
|
||||
event.Insert(ast.InternedStringTerm("requested_by"), ast.StringTerm(e.RequestedBy))
|
||||
event.Insert(ast.InternedTerm("requested_by"), ast.StringTerm(e.RequestedBy))
|
||||
}
|
||||
|
||||
// Use the timestamp JSON marshaller to ensure the format is the same as
|
||||
@@ -201,18 +201,18 @@ func (e *EventV1) AST() (ast.Value, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("timestamp"), ast.StringTerm(strings.Trim(string(timeBytes), "\"")))
|
||||
event.Insert(ast.InternedTerm("timestamp"), ast.StringTerm(strings.Trim(string(timeBytes), "\"")))
|
||||
|
||||
if e.Metrics != nil {
|
||||
m, err := ast.InterfaceToValue(e.Metrics)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event.Insert(ast.InternedStringTerm("metrics"), ast.NewTerm(m))
|
||||
event.Insert(ast.InternedTerm("metrics"), ast.NewTerm(m))
|
||||
}
|
||||
|
||||
if e.RequestID > 0 {
|
||||
event.Insert(ast.InternedStringTerm("req_id"), ast.UIntNumberTerm(e.RequestID))
|
||||
event.Insert(ast.InternedTerm("req_id"), ast.UIntNumberTerm(e.RequestID))
|
||||
}
|
||||
|
||||
return event, nil
|
||||
|
||||
@@ -168,7 +168,7 @@ func getResult(evalResult *opa.Result) (ast.Value, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := obj.Get(ast.InternedStringTerm("result"))
|
||||
result := obj.Get(ast.InternedTerm("result"))
|
||||
|
||||
return result.Value, nil
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ func newUpdateArrayAST(data *ast.Array, op storage.PatchOp, path storage.Path, i
|
||||
}
|
||||
|
||||
func newUpdateObjectAST(data ast.Object, op storage.PatchOp, path storage.Path, idx int, value ast.Value) (*updateAST, error) {
|
||||
key := ast.InternedStringTerm(path[idx])
|
||||
key := ast.InternedTerm(path[idx])
|
||||
val := data.Get(key)
|
||||
|
||||
if idx == len(path)-1 {
|
||||
@@ -200,7 +200,7 @@ func setInAst(data ast.Value, path storage.Path, value ast.Value) (ast.Value, er
|
||||
}
|
||||
|
||||
func setInAstObject(obj ast.Object, path storage.Path, value ast.Value) (ast.Value, error) {
|
||||
key := ast.InternedStringTerm(path[0])
|
||||
key := ast.InternedTerm(path[0])
|
||||
|
||||
if len(path) == 1 {
|
||||
obj.Insert(key, ast.NewTerm(value))
|
||||
@@ -256,7 +256,7 @@ func removeInAst(value ast.Value, path storage.Path) (ast.Value, error) {
|
||||
}
|
||||
|
||||
func removeInAstObject(obj ast.Object, path storage.Path) (ast.Value, error) {
|
||||
key := ast.InternedStringTerm(path[0])
|
||||
key := ast.InternedTerm(path[0])
|
||||
|
||||
if len(path) == 1 {
|
||||
var items [][2]*ast.Term
|
||||
|
||||
+21
-21
@@ -14,13 +14,13 @@ import (
|
||||
func builtinCount(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch a := operands[0].Value.(type) {
|
||||
case *ast.Array:
|
||||
return iter(ast.InternedIntNumberTerm(a.Len()))
|
||||
return iter(ast.InternedTerm(a.Len()))
|
||||
case ast.Object:
|
||||
return iter(ast.InternedIntNumberTerm(a.Len()))
|
||||
return iter(ast.InternedTerm(a.Len()))
|
||||
case ast.Set:
|
||||
return iter(ast.InternedIntNumberTerm(a.Len()))
|
||||
return iter(ast.InternedTerm(a.Len()))
|
||||
case ast.String:
|
||||
return iter(ast.InternedIntNumberTerm(len([]rune(a))))
|
||||
return iter(ast.InternedTerm(len([]rune(a))))
|
||||
}
|
||||
return builtins.NewOperandTypeErr(1, operands[0].Value, "array", "object", "set", "string")
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func builtinSum(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) err
|
||||
return true
|
||||
})
|
||||
if !nonInts {
|
||||
return iter(ast.InternedIntNumberTerm(is))
|
||||
return iter(ast.InternedTerm(is))
|
||||
}
|
||||
|
||||
// Non-integer values found, so we need to sum as floats.
|
||||
@@ -70,7 +70,7 @@ func builtinSum(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) err
|
||||
return true
|
||||
})
|
||||
if !nonInts {
|
||||
return iter(ast.InternedIntNumberTerm(is))
|
||||
return iter(ast.InternedTerm(is))
|
||||
}
|
||||
|
||||
sum := big.NewFloat(0)
|
||||
@@ -209,7 +209,7 @@ func builtinAll(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) err
|
||||
switch val := operands[0].Value.(type) {
|
||||
case ast.Set:
|
||||
res := true
|
||||
match := ast.InternedBooleanTerm(true)
|
||||
match := ast.InternedTerm(true)
|
||||
val.Until(func(term *ast.Term) bool {
|
||||
if !match.Equal(term) {
|
||||
res = false
|
||||
@@ -217,10 +217,10 @@ func builtinAll(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) err
|
||||
}
|
||||
return false
|
||||
})
|
||||
return iter(ast.InternedBooleanTerm(res))
|
||||
return iter(ast.InternedTerm(res))
|
||||
case *ast.Array:
|
||||
res := true
|
||||
match := ast.InternedBooleanTerm(true)
|
||||
match := ast.InternedTerm(true)
|
||||
val.Until(func(term *ast.Term) bool {
|
||||
if !match.Equal(term) {
|
||||
res = false
|
||||
@@ -228,7 +228,7 @@ func builtinAll(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) err
|
||||
}
|
||||
return false
|
||||
})
|
||||
return iter(ast.InternedBooleanTerm(res))
|
||||
return iter(ast.InternedTerm(res))
|
||||
default:
|
||||
return builtins.NewOperandTypeErr(1, operands[0].Value, "array", "set")
|
||||
}
|
||||
@@ -237,11 +237,11 @@ func builtinAll(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) err
|
||||
func builtinAny(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch val := operands[0].Value.(type) {
|
||||
case ast.Set:
|
||||
res := val.Len() > 0 && val.Contains(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedBooleanTerm(res))
|
||||
res := val.Len() > 0 && val.Contains(ast.InternedTerm(true))
|
||||
return iter(ast.InternedTerm(res))
|
||||
case *ast.Array:
|
||||
res := false
|
||||
match := ast.InternedBooleanTerm(true)
|
||||
match := ast.InternedTerm(true)
|
||||
val.Until(func(term *ast.Term) bool {
|
||||
if match.Equal(term) {
|
||||
res = true
|
||||
@@ -249,7 +249,7 @@ func builtinAny(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) err
|
||||
}
|
||||
return false
|
||||
})
|
||||
return iter(ast.InternedBooleanTerm(res))
|
||||
return iter(ast.InternedTerm(res))
|
||||
default:
|
||||
return builtins.NewOperandTypeErr(1, operands[0].Value, "array", "set")
|
||||
}
|
||||
@@ -259,20 +259,20 @@ func builtinMember(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term)
|
||||
containee := operands[0]
|
||||
switch c := operands[1].Value.(type) {
|
||||
case ast.Set:
|
||||
return iter(ast.InternedBooleanTerm(c.Contains(containee)))
|
||||
return iter(ast.InternedTerm(c.Contains(containee)))
|
||||
case *ast.Array:
|
||||
for i := range c.Len() {
|
||||
if c.Elem(i).Value.Compare(containee.Value) == 0 {
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
}
|
||||
}
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
case ast.Object:
|
||||
return iter(ast.InternedBooleanTerm(c.Until(func(_, v *ast.Term) bool {
|
||||
return iter(ast.InternedTerm(c.Until(func(_, v *ast.Term) bool {
|
||||
return v.Value.Compare(containee.Value) == 0
|
||||
})))
|
||||
}
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
func builtinMemberWithKey(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -283,9 +283,9 @@ func builtinMemberWithKey(_ BuiltinContext, operands []*ast.Term, iter func(*ast
|
||||
if act := c.Get(key); act != nil {
|
||||
ret = act.Value.Compare(val.Value) == 0
|
||||
}
|
||||
return iter(ast.InternedBooleanTerm(ret))
|
||||
return iter(ast.InternedTerm(ret))
|
||||
}
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -11,14 +11,14 @@ import (
|
||||
func BenchmarkSumIntArray(b *testing.B) {
|
||||
bcx := BuiltinContext{}
|
||||
arr := ast.ArrayTerm(
|
||||
ast.InternedIntNumberTerm(1),
|
||||
ast.InternedIntNumberTerm(2),
|
||||
ast.InternedIntNumberTerm(3),
|
||||
ast.InternedIntNumberTerm(4),
|
||||
ast.InternedIntNumberTerm(5),
|
||||
ast.InternedIntNumberTerm(6),
|
||||
ast.InternedTerm(1),
|
||||
ast.InternedTerm(2),
|
||||
ast.InternedTerm(3),
|
||||
ast.InternedTerm(4),
|
||||
ast.InternedTerm(5),
|
||||
ast.InternedTerm(6),
|
||||
)
|
||||
exp := ast.InternedIntNumberTerm(21)
|
||||
exp := ast.InternedTerm(21)
|
||||
|
||||
verify := func(x *ast.Term) error {
|
||||
// Can do simple equality check since we are using interned terms
|
||||
@@ -67,14 +67,14 @@ func BenchmarkSumFloatArray(b *testing.B) {
|
||||
func BenchmarkSumIntSet(b *testing.B) {
|
||||
bcx := BuiltinContext{}
|
||||
set := ast.SetTerm(
|
||||
ast.InternedIntNumberTerm(1),
|
||||
ast.InternedIntNumberTerm(2),
|
||||
ast.InternedIntNumberTerm(3),
|
||||
ast.InternedIntNumberTerm(4),
|
||||
ast.InternedIntNumberTerm(5),
|
||||
ast.InternedIntNumberTerm(6),
|
||||
ast.InternedTerm(1),
|
||||
ast.InternedTerm(2),
|
||||
ast.InternedTerm(3),
|
||||
ast.InternedTerm(4),
|
||||
ast.InternedTerm(5),
|
||||
ast.InternedTerm(6),
|
||||
)
|
||||
exp := ast.InternedIntNumberTerm(21)
|
||||
exp := ast.InternedTerm(21)
|
||||
|
||||
verify := func(x *ast.Term) error {
|
||||
if x != exp {
|
||||
|
||||
@@ -67,7 +67,7 @@ func builtinPlus(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) er
|
||||
y, ok2 := n2.Int()
|
||||
|
||||
if ok1 && ok2 && inSmallIntRange(x) && inSmallIntRange(y) {
|
||||
return iter(ast.InternedIntNumberTerm(x + y))
|
||||
return iter(ast.InternedTerm(x + y))
|
||||
}
|
||||
|
||||
f := new(big.Float).Add(builtins.NumberToFloat(n1), builtins.NumberToFloat(n2))
|
||||
@@ -89,7 +89,7 @@ func builtinMultiply(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term
|
||||
y, ok2 := n2.Int()
|
||||
|
||||
if ok1 && ok2 && inSmallIntRange(x) && inSmallIntRange(y) {
|
||||
return iter(ast.InternedIntNumberTerm(x * y))
|
||||
return iter(ast.InternedTerm(x * y))
|
||||
}
|
||||
|
||||
f := new(big.Float).Mul(builtins.NumberToFloat(n1), builtins.NumberToFloat(n2))
|
||||
@@ -155,7 +155,7 @@ func builtinMinus(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) e
|
||||
y, oky := n2.Int()
|
||||
|
||||
if okx && oky && inSmallIntRange(x) && inSmallIntRange(y) {
|
||||
return iter(ast.InternedIntNumberTerm(x - y))
|
||||
return iter(ast.InternedTerm(x - y))
|
||||
}
|
||||
|
||||
f := new(big.Float).Sub(builtins.NumberToFloat(n1), builtins.NumberToFloat(n2))
|
||||
@@ -199,7 +199,7 @@ func builtinRem(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) err
|
||||
return errors.New("modulo by zero")
|
||||
}
|
||||
|
||||
return iter(ast.InternedIntNumberTerm(x % y))
|
||||
return iter(ast.InternedTerm(x % y))
|
||||
}
|
||||
|
||||
op1, err1 := builtins.NumberToInt(n1)
|
||||
|
||||
@@ -39,7 +39,7 @@ type NDBCache map[string]ast.Object
|
||||
func (c NDBCache) AsValue() ast.Value {
|
||||
out := ast.NewObject()
|
||||
for bname, obj := range c {
|
||||
out.Insert(ast.InternedStringTerm(bname), ast.NewTerm(obj))
|
||||
out.Insert(ast.InternedTerm(bname), ast.NewTerm(obj))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
+3
-3
@@ -15,12 +15,12 @@ import (
|
||||
func builtinToNumber(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch a := operands[0].Value.(type) {
|
||||
case ast.Null:
|
||||
return iter(ast.InternedIntNumberTerm(0))
|
||||
return iter(ast.InternedTerm(0))
|
||||
case ast.Boolean:
|
||||
if a {
|
||||
return iter(ast.InternedIntNumberTerm(1))
|
||||
return iter(ast.InternedTerm(1))
|
||||
}
|
||||
return iter(ast.InternedIntNumberTerm(0))
|
||||
return iter(ast.InternedTerm(0))
|
||||
case ast.Number:
|
||||
return iter(operands[0])
|
||||
case ast.String:
|
||||
|
||||
+7
-7
@@ -76,7 +76,7 @@ func builtinNetCIDRIntersects(_ BuiltinContext, operands []*ast.Term, iter func(
|
||||
// If either net contains the others starting IP they are overlapping
|
||||
cidrsOverlap := cidrnetA.Contains(cidrnetB.IP) || cidrnetB.Contains(cidrnetA.IP)
|
||||
|
||||
return iter(ast.InternedBooleanTerm(cidrsOverlap))
|
||||
return iter(ast.InternedTerm(cidrsOverlap))
|
||||
}
|
||||
|
||||
func builtinNetCIDRContains(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -93,7 +93,7 @@ func builtinNetCIDRContains(_ BuiltinContext, operands []*ast.Term, iter func(*a
|
||||
|
||||
ip := net.ParseIP(string(bStr))
|
||||
if ip != nil {
|
||||
return iter(ast.InternedBooleanTerm(cidrnetA.Contains(ip)))
|
||||
return iter(ast.InternedTerm(cidrnetA.Contains(ip)))
|
||||
}
|
||||
|
||||
// It wasn't an IP, try and parse it as a CIDR
|
||||
@@ -114,7 +114,7 @@ func builtinNetCIDRContains(_ BuiltinContext, operands []*ast.Term, iter func(*a
|
||||
cidrContained = cidrnetA.Contains(lastIP)
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(cidrContained))
|
||||
return iter(ast.InternedTerm(cidrContained))
|
||||
}
|
||||
|
||||
var errNetCIDRContainsMatchElementType = errors.New("element must be string or non-empty array")
|
||||
@@ -143,7 +143,7 @@ func evalNetCIDRContainsMatchesOperand(operand int, a *ast.Term, iter func(cidr,
|
||||
if err != nil {
|
||||
return fmt.Errorf("operand %v: %v", operand, err)
|
||||
}
|
||||
if err := iter(cidr, ast.InternedIntNumberTerm(i)); err != nil {
|
||||
if err := iter(cidr, ast.InternedTerm(i)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -220,13 +220,13 @@ func builtinNetCIDRExpand(bctx BuiltinContext, operands []*ast.Term, iter func(*
|
||||
func builtinNetCIDRIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
cidr, err := builtins.StringOperand(operands[0].Value, 1)
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
if _, _, err := net.ParseCIDR(string(cidr)); err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
}
|
||||
|
||||
type cidrBlockRange struct {
|
||||
|
||||
@@ -34,7 +34,7 @@ func compareEq(a, b ast.Value) bool {
|
||||
|
||||
func builtinCompare(cmp compareFunc) BuiltinFunc {
|
||||
return func(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
return iter(ast.InternedBooleanTerm(cmp(operands[0].Value, operands[1].Value)))
|
||||
return iter(ast.InternedTerm(cmp(operands[0].Value, operands[1].Value)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -99,12 +99,12 @@ func builtinCryptoX509ParseAndVerifyCertificates(_ BuiltinContext, operands []*a
|
||||
|
||||
certs, err := getX509CertsFromString(string(input))
|
||||
if err != nil {
|
||||
return iter(ast.ArrayTerm(ast.InternedBooleanTerm(false), ast.InternedEmptyArray))
|
||||
return iter(ast.ArrayTerm(ast.InternedTerm(false), ast.InternedEmptyArray))
|
||||
}
|
||||
|
||||
verified, err := verifyX509CertificateChain(certs, x509.VerifyOptions{})
|
||||
if err != nil {
|
||||
return iter(ast.ArrayTerm(ast.InternedBooleanTerm(false), ast.InternedEmptyArray))
|
||||
return iter(ast.ArrayTerm(ast.InternedTerm(false), ast.InternedEmptyArray))
|
||||
}
|
||||
|
||||
value, err := ast.InterfaceToValue(extendCertificates(verified))
|
||||
@@ -112,7 +112,7 @@ func builtinCryptoX509ParseAndVerifyCertificates(_ BuiltinContext, operands []*a
|
||||
return err
|
||||
}
|
||||
|
||||
valid := ast.ArrayTerm(ast.InternedBooleanTerm(true), ast.NewTerm(value))
|
||||
valid := ast.ArrayTerm(ast.InternedTerm(true), ast.NewTerm(value))
|
||||
|
||||
return iter(valid)
|
||||
}
|
||||
@@ -148,7 +148,7 @@ func builtinCryptoX509ParseAndVerifyCertificatesWithOptions(_ BuiltinContext, op
|
||||
|
||||
certs, err := getX509CertsFromString(string(input))
|
||||
if err != nil {
|
||||
return iter(ast.ArrayTerm(ast.InternedBooleanTerm(false), ast.InternedEmptyArray))
|
||||
return iter(ast.ArrayTerm(ast.InternedTerm(false), ast.InternedEmptyArray))
|
||||
}
|
||||
|
||||
// Collect the cert verification options
|
||||
@@ -159,7 +159,7 @@ func builtinCryptoX509ParseAndVerifyCertificatesWithOptions(_ BuiltinContext, op
|
||||
|
||||
verified, err := verifyX509CertificateChain(certs, verifyOpt)
|
||||
if err != nil {
|
||||
return iter(ast.ArrayTerm(ast.InternedBooleanTerm(false), ast.InternedEmptyArray))
|
||||
return iter(ast.ArrayTerm(ast.InternedTerm(false), ast.InternedEmptyArray))
|
||||
}
|
||||
|
||||
value, err := ast.InterfaceToValue(verified)
|
||||
@@ -167,7 +167,7 @@ func builtinCryptoX509ParseAndVerifyCertificatesWithOptions(_ BuiltinContext, op
|
||||
return err
|
||||
}
|
||||
|
||||
return iter(ast.ArrayTerm(ast.InternedBooleanTerm(true), ast.NewTerm(value)))
|
||||
return iter(ast.ArrayTerm(ast.InternedTerm(true), ast.NewTerm(value)))
|
||||
}
|
||||
|
||||
func extractVerifyOpts(options ast.Object) (verifyOpt x509.VerifyOptions, err error) {
|
||||
@@ -513,7 +513,7 @@ func builtinCryptoHmacEqual(_ BuiltinContext, operands []*ast.Term, iter func(*a
|
||||
|
||||
res := hmac.Equal([]byte(mac1), []byte(mac2))
|
||||
|
||||
return iter(ast.InternedBooleanTerm(res))
|
||||
return iter(ast.InternedTerm(res))
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -144,10 +144,10 @@ func builtinJSONIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*ast.T
|
||||
|
||||
str, err := builtins.StringOperand(operands[0].Value, 1)
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(json.Valid([]byte(str))))
|
||||
return iter(ast.InternedTerm(json.Valid([]byte(str))))
|
||||
}
|
||||
|
||||
func builtinBase64Encode(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -169,17 +169,17 @@ func builtinBase64Decode(_ BuiltinContext, operands []*ast.Term, iter func(*ast.
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return iter(ast.StringTerm(string(result)))
|
||||
return iter(ast.InternedTerm(string(result)))
|
||||
}
|
||||
|
||||
func builtinBase64IsValid(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
str, err := builtins.StringOperand(operands[0].Value, 1)
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
_, err = base64.StdEncoding.DecodeString(string(str))
|
||||
return iter(ast.InternedBooleanTerm(err == nil))
|
||||
return iter(ast.InternedTerm(err == nil))
|
||||
}
|
||||
|
||||
func builtinBase64UrlEncode(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -224,7 +224,7 @@ func builtinBase64UrlDecode(_ BuiltinContext, operands []*ast.Term, iter func(*a
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return iter(ast.StringTerm(string(result)))
|
||||
return iter(ast.InternedTerm(string(result)))
|
||||
}
|
||||
|
||||
func builtinURLQueryEncode(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -355,12 +355,12 @@ func builtinYAMLUnmarshal(_ BuiltinContext, operands []*ast.Term, iter func(*ast
|
||||
func builtinYAMLIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
str, err := builtins.StringOperand(operands[0].Value, 1)
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
var x any
|
||||
err = yaml.Unmarshal([]byte(str), &x)
|
||||
return iter(ast.InternedBooleanTerm(err == nil))
|
||||
return iter(ast.InternedTerm(err == nil))
|
||||
}
|
||||
|
||||
func builtinHexEncode(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
|
||||
+6
-6
@@ -463,7 +463,7 @@ func (e *eval) evalStep(iter evalIterator) error {
|
||||
return iter(e)
|
||||
})
|
||||
}
|
||||
if !e.bindings.Plug(rterm).Equal(ast.InternedBooleanTerm(false)) {
|
||||
if !e.bindings.Plug(rterm).Equal(ast.InternedTerm(false)) {
|
||||
defined = true
|
||||
err := iter(e)
|
||||
e.traceRedo(expr)
|
||||
@@ -524,7 +524,7 @@ func (e *eval) evalStep(iter evalIterator) error {
|
||||
return iter(e)
|
||||
})
|
||||
}
|
||||
if !e.bindings.Plug(rterm).Equal(ast.InternedBooleanTerm(false)) {
|
||||
if !e.bindings.Plug(rterm).Equal(ast.InternedTerm(false)) {
|
||||
return iter(e)
|
||||
}
|
||||
return nil
|
||||
@@ -2060,7 +2060,7 @@ func (e evalFunc) eval(iter unifyIterator) error {
|
||||
if len(defRule.Head.Args) == len(e.terms)-1 {
|
||||
// The function is called without collecting the result in an output term,
|
||||
// therefore any successful evaluation of the function is of interest, including the default value ...
|
||||
if ret := defRule.Head.Value; ret == nil || !ret.Equal(ast.InternedBooleanTerm(false)) {
|
||||
if ret := defRule.Head.Value; ret == nil || !ret.Equal(ast.InternedTerm(false)) {
|
||||
// ... unless the default value is false,
|
||||
mustGenerateSupport = true
|
||||
}
|
||||
@@ -2475,7 +2475,7 @@ func (e evalTree) enumerate(iter unifyIterator) error {
|
||||
switch doc := doc.(type) {
|
||||
case *ast.Array:
|
||||
for i := range doc.Len() {
|
||||
k := ast.InternedIntNumberTerm(i)
|
||||
k := ast.InternedTerm(i)
|
||||
err := e.e.biunify(k, e.ref[e.pos], e.bindings, e.bindings, func() error {
|
||||
return e.next(iter, k)
|
||||
})
|
||||
@@ -3442,7 +3442,7 @@ func (e evalVirtualComplete) eval(iter unifyIterator) error {
|
||||
if e.ir.Default != nil {
|
||||
// If inlining has been disabled for the rterm, and the default rule has a 'false' result value,
|
||||
// the default value is inconsequential, and support does not need to be generated.
|
||||
if !(e.ir.Default.Head.Value.Equal(ast.InternedBooleanTerm(false)) && e.e.inliningControl.Disabled(e.rterm.Value, false)) {
|
||||
if !(e.ir.Default.Head.Value.Equal(ast.InternedTerm(false)) && e.e.inliningControl.Disabled(e.rterm.Value, false)) {
|
||||
// If the other term is not constant OR it's equal to the default value, then
|
||||
// a support rule must be produced as the default value _may_ be required. On
|
||||
// the other hand, if the other term is constant (i.e., it does not require
|
||||
@@ -3760,7 +3760,7 @@ func (e evalTerm) enumerate(iter unifyIterator) error {
|
||||
// win across most policies. Those cases are however much more complex, as we need to deal with
|
||||
// any type on either side, not just int/var as is the case here.
|
||||
for i := range v.Len() {
|
||||
a := ast.InternedIntNumberTerm(i)
|
||||
a := ast.InternedTerm(i)
|
||||
b := e.ref[e.pos]
|
||||
|
||||
if _, ok := b.Value.(ast.Var); ok {
|
||||
|
||||
@@ -1574,19 +1574,19 @@ func TestPartialRule(t *testing.T) {
|
||||
|
||||
type deadlineCtx struct{}
|
||||
|
||||
func (_ *deadlineCtx) Err() error {
|
||||
func (*deadlineCtx) Err() error {
|
||||
return context.DeadlineExceeded
|
||||
}
|
||||
|
||||
func (_ *deadlineCtx) Deadline() (time.Time, bool) {
|
||||
func (*deadlineCtx) Deadline() (time.Time, bool) {
|
||||
return time.Now(), false
|
||||
}
|
||||
|
||||
func (_ *deadlineCtx) Value(_ any) any {
|
||||
func (*deadlineCtx) Value(_ any) any {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (_ *deadlineCtx) Done() <-chan struct{} {
|
||||
func (*deadlineCtx) Done() <-chan struct{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -57,7 +57,7 @@ func builtinGlobMatch(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return iter(ast.InternedBooleanTerm(m))
|
||||
return iter(ast.InternedTerm(m))
|
||||
}
|
||||
|
||||
func globCompileAndMatch(bctx BuiltinContext, id, pattern, match string, delimiters []rune) (bool, error) {
|
||||
|
||||
+12
-12
@@ -331,7 +331,7 @@ func builtinGraphQLParseAndVerify(bctx BuiltinContext, operands []*ast.Term, ite
|
||||
var err error
|
||||
|
||||
unverified := ast.ArrayTerm(
|
||||
ast.InternedBooleanTerm(false),
|
||||
ast.InternedTerm(false),
|
||||
ast.NewTerm(ast.NewObject()),
|
||||
ast.NewTerm(ast.NewObject()),
|
||||
)
|
||||
@@ -404,7 +404,7 @@ func builtinGraphQLParseAndVerify(bctx BuiltinContext, operands []*ast.Term, ite
|
||||
|
||||
// Construct return value.
|
||||
verified := ast.ArrayTerm(
|
||||
ast.InternedBooleanTerm(true),
|
||||
ast.InternedTerm(true),
|
||||
ast.NewTerm(queryResult),
|
||||
ast.NewTerm(querySchema),
|
||||
)
|
||||
@@ -482,10 +482,10 @@ func builtinGraphQLIsValid(bctx BuiltinContext, operands []*ast.Term, iter func(
|
||||
queryDoc, err = objectToQueryDocument(x)
|
||||
default:
|
||||
// Error if wrong type.
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
schemaCacheKey, schema := cacheGetSchema(bctx, operands[1])
|
||||
@@ -497,26 +497,26 @@ func builtinGraphQLIsValid(bctx BuiltinContext, operands []*ast.Term, iter func(
|
||||
schemaDoc, err = objectToSchemaDocument(x)
|
||||
default:
|
||||
// Error if wrong type.
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
// Validate the query against the schema, erroring if there's an issue.
|
||||
schema, err = convertSchema(schemaDoc)
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
cacheInsertSchema(bctx, schemaCacheKey, schema)
|
||||
}
|
||||
|
||||
if err := validateQuery(schema, queryDoc); err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
// If we got this far, the GraphQL query passed validation.
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
}
|
||||
|
||||
func builtinGraphQLSchemaIsValid(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -535,10 +535,10 @@ func builtinGraphQLSchemaIsValid(bctx BuiltinContext, operands []*ast.Term, iter
|
||||
schemaDoc, err = objectToSchemaDocument(x)
|
||||
default:
|
||||
// Error if wrong type.
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
// Validate the schema, this determines the result
|
||||
// and whether there is a schema to cache
|
||||
@@ -548,7 +548,7 @@ func builtinGraphQLSchemaIsValid(bctx BuiltinContext, operands []*ast.Term, iter
|
||||
}
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(err == nil))
|
||||
return iter(ast.InternedTerm(err == nil))
|
||||
}
|
||||
|
||||
// Insert Schema into cache
|
||||
|
||||
+10
-10
@@ -96,7 +96,7 @@ var (
|
||||
allowedKeys = ast.NewSet()
|
||||
keyCache = make(map[string]*ast.Term, len(allowedKeyNames))
|
||||
cacheableCodes = ast.NewSet()
|
||||
requiredKeys = ast.NewSet(ast.InternedStringTerm("method"), ast.InternedStringTerm("url"))
|
||||
requiredKeys = ast.NewSet(ast.InternedTerm("method"), ast.InternedTerm("url"))
|
||||
httpSendLatencyMetricKey = "rego_builtin_http_send"
|
||||
httpSendInterQueryCacheHits = httpSendLatencyMetricKey + "_interquery_cache_hits"
|
||||
)
|
||||
@@ -162,19 +162,19 @@ func generateRaiseErrorResult(err error) *ast.Term {
|
||||
switch err.(type) {
|
||||
case *url.Error:
|
||||
errObj = ast.NewObject(
|
||||
ast.Item(ast.InternedStringTerm("code"), httpSendNetworkErrTerm),
|
||||
ast.Item(ast.InternedStringTerm("message"), ast.StringTerm(err.Error())),
|
||||
ast.Item(ast.InternedTerm("code"), httpSendNetworkErrTerm),
|
||||
ast.Item(ast.InternedTerm("message"), ast.StringTerm(err.Error())),
|
||||
)
|
||||
default:
|
||||
errObj = ast.NewObject(
|
||||
ast.Item(ast.InternedStringTerm("code"), httpSendInternalErrTerm),
|
||||
ast.Item(ast.InternedStringTerm("message"), ast.StringTerm(err.Error())),
|
||||
ast.Item(ast.InternedTerm("code"), httpSendInternalErrTerm),
|
||||
ast.Item(ast.InternedTerm("message"), ast.StringTerm(err.Error())),
|
||||
)
|
||||
}
|
||||
|
||||
return ast.ObjectTerm(
|
||||
ast.Item(ast.InternedStringTerm("status_code"), ast.InternedIntNumberTerm(0)),
|
||||
ast.Item(ast.InternedStringTerm("error"), ast.NewTerm(errObj)),
|
||||
ast.Item(ast.InternedTerm("status_code"), ast.InternedTerm(0)),
|
||||
ast.Item(ast.InternedTerm("error"), ast.NewTerm(errObj)),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -983,7 +983,7 @@ func (c *interQueryCache) checkHTTPSendInterQueryCache() (ast.Value, error) {
|
||||
|
||||
// insertIntoHTTPSendInterQueryCache inserts given key and value in the inter-query cache
|
||||
func insertIntoHTTPSendInterQueryCache(bctx BuiltinContext, key ast.Value, resp *http.Response, respBody []byte, cacheParams *forceCacheParams) error {
|
||||
if resp == nil || (!forceCaching(cacheParams) && !canStore(resp.Header)) || !cacheableCodes.Contains(ast.InternedIntNumberTerm(resp.StatusCode)) {
|
||||
if resp == nil || (!forceCaching(cacheParams) && !canStore(resp.Header)) || !cacheableCodes.Contains(ast.InternedTerm(resp.StatusCode)) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1027,7 +1027,7 @@ func createKeys() {
|
||||
|
||||
func createCacheableHTTPStatusCodes() {
|
||||
for _, element := range cacheableHTTPStatusCodes {
|
||||
cacheableCodes.Add(ast.InternedIntNumberTerm(element))
|
||||
cacheableCodes.Add(ast.InternedTerm(element))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1569,7 +1569,7 @@ func (c *intraQueryCache) InsertIntoCache(value *http.Response) (ast.Value, erro
|
||||
return nil, handleHTTPSendErr(c.bctx, err)
|
||||
}
|
||||
|
||||
if cacheableCodes.Contains(ast.InternedIntNumberTerm(value.StatusCode)) {
|
||||
if cacheableCodes.Contains(ast.InternedTerm(value.StatusCode)) {
|
||||
insertIntoHTTPSendCache(c.bctx, c.key, result)
|
||||
}
|
||||
|
||||
|
||||
@@ -291,7 +291,7 @@ func gen3LayerObject(l1Keys, l2Keys, l3Keys int) ast.Value {
|
||||
for j := range l2Keys {
|
||||
l3Obj := ast.NewObject()
|
||||
for k := range l3Keys {
|
||||
l3Obj.Insert(ast.StringTerm(strconv.Itoa(k)), ast.InternedBooleanTerm(true))
|
||||
l3Obj.Insert(ast.StringTerm(strconv.Itoa(k)), ast.InternedTerm(true))
|
||||
}
|
||||
l2Obj.Insert(ast.StringTerm(strconv.Itoa(j)), ast.NewTerm(l3Obj))
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func astValueToJSONSchemaLoader(value ast.Value) (gojsonschema.JSONLoader, error
|
||||
}
|
||||
|
||||
func newResultTerm(valid bool, data *ast.Term) *ast.Term {
|
||||
return ast.ArrayTerm(ast.InternedBooleanTerm(valid), data)
|
||||
return ast.ArrayTerm(ast.InternedTerm(valid), data)
|
||||
}
|
||||
|
||||
// builtinJSONSchemaVerify accepts 1 argument which can be string or object and checks if it is valid JSON schema.
|
||||
|
||||
@@ -117,11 +117,11 @@ func generateCheapRange(operands []*ast.Term, step int, iter func(*ast.Term) err
|
||||
|
||||
if x <= y {
|
||||
for i := x; i <= y; i += step {
|
||||
terms = append(terms, ast.InternedIntNumberTerm(i))
|
||||
terms = append(terms, ast.InternedTerm(i))
|
||||
}
|
||||
} else {
|
||||
for i := x; i >= y; i -= step {
|
||||
terms = append(terms, ast.InternedIntNumberTerm(i))
|
||||
terms = append(terms, ast.InternedTerm(i))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ func builtinRandIntn(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.T
|
||||
}
|
||||
|
||||
if n == 0 {
|
||||
return iter(ast.InternedIntNumberTerm(0))
|
||||
return iter(ast.InternedTerm(0))
|
||||
}
|
||||
|
||||
if n < 0 {
|
||||
@@ -188,7 +188,7 @@ func builtinRandIntn(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.T
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result := ast.InternedIntNumberTerm(r.Intn(n))
|
||||
result := ast.InternedTerm(r.Intn(n))
|
||||
bctx.Cache.Put(key, result)
|
||||
|
||||
return iter(result)
|
||||
|
||||
@@ -18,7 +18,7 @@ func BenchmarkNumbersRange(b *testing.B) {
|
||||
}{
|
||||
{
|
||||
name: "interned",
|
||||
operands: []*ast.Term{ast.InternedIntNumberTerm(0), ast.InternedIntNumberTerm(99)},
|
||||
operands: []*ast.Term{ast.InternedTerm(0), ast.InternedTerm(99)},
|
||||
},
|
||||
{
|
||||
name: "not interned",
|
||||
@@ -41,14 +41,14 @@ func BenchmarkNumbersRange(b *testing.B) {
|
||||
func BenchmarkNumbersRangeStep(b *testing.B) {
|
||||
bctx := BuiltinContext{}
|
||||
expect100Items := expectCountIter(b, 100)
|
||||
step := ast.InternedIntNumberTerm(2)
|
||||
step := ast.InternedTerm(2)
|
||||
tests := []struct {
|
||||
name string
|
||||
operands []*ast.Term
|
||||
}{
|
||||
{
|
||||
name: "interned",
|
||||
operands: []*ast.Term{ast.InternedIntNumberTerm(0), ast.InternedIntNumberTerm(199), step},
|
||||
operands: []*ast.Term{ast.InternedTerm(0), ast.InternedTerm(199), step},
|
||||
},
|
||||
{
|
||||
name: "not interned",
|
||||
|
||||
+1
-1
@@ -400,7 +400,7 @@ func (q *Query) PartialRun(ctx context.Context) (partials []ast.Body, support []
|
||||
saveSet: newSaveSet(q.unknowns, b, q.instr),
|
||||
saveStack: newSaveStack(),
|
||||
saveSupport: newSaveSupport(),
|
||||
saveNamespace: ast.InternedStringTerm(q.partialNamespace),
|
||||
saveNamespace: ast.InternedTerm(q.partialNamespace),
|
||||
skipSaveNamespace: q.skipSaveNamespace,
|
||||
inliningControl: &inliningControl{
|
||||
shallow: q.shallowInlining,
|
||||
|
||||
+7
-7
@@ -25,15 +25,15 @@ func builtinRegexIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*ast.
|
||||
|
||||
s, err := builtins.StringOperand(operands[0].Value, 1)
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
_, err = regexp.Compile(string(s))
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
}
|
||||
|
||||
func builtinRegexMatch(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -49,7 +49,7 @@ func builtinRegexMatch(bctx BuiltinContext, operands []*ast.Term, iter func(*ast
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return iter(ast.InternedBooleanTerm(re.MatchString(string(s2))))
|
||||
return iter(ast.InternedTerm(re.MatchString(string(s2))))
|
||||
}
|
||||
|
||||
func builtinRegexMatchTemplate(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -79,7 +79,7 @@ func builtinRegexMatchTemplate(_ BuiltinContext, operands []*ast.Term, iter func
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return iter(ast.InternedBooleanTerm(re.MatchString(string(match))))
|
||||
return iter(ast.InternedTerm(re.MatchString(string(match))))
|
||||
}
|
||||
|
||||
func builtinRegexSplit(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -177,7 +177,7 @@ func builtinGlobsMatch(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Te
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return iter(ast.InternedBooleanTerm(ne))
|
||||
return iter(ast.InternedTerm(ne))
|
||||
}
|
||||
|
||||
func builtinRegexFind(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -264,7 +264,7 @@ func builtinRegexReplace(bctx BuiltinContext, operands []*ast.Term, iter func(*a
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(res))
|
||||
return iter(ast.InternedTerm(res))
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -19,7 +19,7 @@ func builtinOPARuntime(bctx BuiltinContext, _ []*ast.Term, iter func(*ast.Term)
|
||||
return iter(ast.InternedEmptyObject)
|
||||
}
|
||||
|
||||
if bctx.Runtime.Get(ast.InternedStringTerm("config")) != nil {
|
||||
if bctx.Runtime.Get(ast.InternedTerm("config")) != nil {
|
||||
iface, err := ast.ValueToInterface(bctx.Runtime.Value, nothingResolver)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -34,13 +34,13 @@ func builtinSemVerCompare(_ BuiltinContext, operands []*ast.Term, iter func(*ast
|
||||
|
||||
result := versionA.Compare(*versionB)
|
||||
|
||||
return iter(ast.InternedIntNumberTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
|
||||
func builtinSemVerIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
versionString, err := builtins.StringOperand(operands[0].Value, 1)
|
||||
if err != nil {
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
|
||||
result := true
|
||||
@@ -50,7 +50,7 @@ func builtinSemVerIsValid(_ BuiltinContext, operands []*ast.Term, iter func(*ast
|
||||
result = false
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
+29
-29
@@ -51,7 +51,7 @@ func builtinAnyPrefixMatch(_ BuiltinContext, operands []*ast.Term, iter func(*as
|
||||
return builtins.NewOperandTypeErr(2, b, "string", "set", "array")
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(anyStartsWithAny(strs, prefixes)))
|
||||
return iter(ast.InternedTerm(anyStartsWithAny(strs, prefixes)))
|
||||
}
|
||||
|
||||
func builtinAnySuffixMatch(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -91,7 +91,7 @@ func builtinAnySuffixMatch(_ BuiltinContext, operands []*ast.Term, iter func(*as
|
||||
return builtins.NewOperandTypeErr(2, b, "string", "set", "array")
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(anyStartsWithAny(strsReversed, suffixesReversed)))
|
||||
return iter(ast.InternedTerm(anyStartsWithAny(strsReversed, suffixesReversed)))
|
||||
}
|
||||
|
||||
func anyStartsWithAny(strs []string, prefixes []string) bool {
|
||||
@@ -148,7 +148,7 @@ func builtinFormatInt(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
|
||||
f := builtins.NumberToFloat(input)
|
||||
i, _ := f.Int(nil)
|
||||
|
||||
return iter(ast.InternedStringTerm(fmt.Sprintf(format, i)))
|
||||
return iter(ast.InternedTerm(fmt.Sprintf(format, i)))
|
||||
}
|
||||
|
||||
func builtinConcat(b BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -202,7 +202,7 @@ func builtinConcat(b BuiltinContext, operands []*ast.Term, iter func(*ast.Term)
|
||||
sb.WriteString(string(b.Elem(i).Value.(ast.String)))
|
||||
}
|
||||
}
|
||||
return iter(ast.InternedStringTerm(sb.String()))
|
||||
return iter(ast.InternedTerm(sb.String()))
|
||||
case ast.Set:
|
||||
for _, v := range b.Slice() {
|
||||
s, ok := v.Value.(ast.String)
|
||||
@@ -222,7 +222,7 @@ func builtinConcat(b BuiltinContext, operands []*ast.Term, iter func(*ast.Term)
|
||||
sb.WriteString(sep)
|
||||
}
|
||||
}
|
||||
return iter(ast.InternedStringTerm(sb.String()))
|
||||
return iter(ast.InternedTerm(sb.String()))
|
||||
}
|
||||
|
||||
return builtins.NewOperandTypeErr(2, operands[1].Value, "set", "array")
|
||||
@@ -284,7 +284,7 @@ func builtinIndexOf(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term)
|
||||
// this is a false positive in the indexAlloc rule that thinks
|
||||
// we're converting byte arrays to strings
|
||||
//nolint:gocritic
|
||||
return iter(ast.InternedIntNumberTerm(strings.Index(string(base), string(search))))
|
||||
return iter(ast.InternedTerm(strings.Index(string(base), string(search))))
|
||||
}
|
||||
|
||||
baseRunes := []rune(string(base))
|
||||
@@ -294,14 +294,14 @@ func builtinIndexOf(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term)
|
||||
for i, r := range baseRunes {
|
||||
if len(baseRunes) >= i+searchLen {
|
||||
if r == searchRunes[0] && runesEqual(baseRunes[i:i+searchLen], searchRunes) {
|
||||
return iter(ast.InternedIntNumberTerm(i))
|
||||
return iter(ast.InternedTerm(i))
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return iter(ast.InternedIntNumberTerm(-1))
|
||||
return iter(ast.InternedTerm(-1))
|
||||
}
|
||||
|
||||
func builtinIndexOfN(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -326,7 +326,7 @@ func builtinIndexOfN(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term
|
||||
for i, r := range baseRunes {
|
||||
if len(baseRunes) >= i+searchLen {
|
||||
if r == searchRunes[0] && runesEqual(baseRunes[i:i+searchLen], searchRunes) {
|
||||
arr = append(arr, ast.InternedIntNumberTerm(i))
|
||||
arr = append(arr, ast.InternedTerm(i))
|
||||
}
|
||||
} else {
|
||||
break
|
||||
@@ -370,7 +370,7 @@ func builtinSubstring(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
|
||||
}
|
||||
|
||||
if length < 0 {
|
||||
return iter(ast.InternedStringTerm(sbase[startIndex:]))
|
||||
return iter(ast.InternedTerm(sbase[startIndex:]))
|
||||
}
|
||||
|
||||
if startIndex == 0 && length >= len(sbase) {
|
||||
@@ -378,7 +378,7 @@ func builtinSubstring(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
|
||||
}
|
||||
|
||||
upto := min(len(sbase), startIndex+length)
|
||||
return iter(ast.InternedStringTerm(sbase[startIndex:upto]))
|
||||
return iter(ast.InternedTerm(sbase[startIndex:upto]))
|
||||
}
|
||||
|
||||
if startIndex == 0 && length >= utf8.RuneCountInString(sbase) {
|
||||
@@ -399,7 +399,7 @@ func builtinSubstring(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
|
||||
s = string(runes[startIndex:upto])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(s))
|
||||
return iter(ast.InternedTerm(s))
|
||||
}
|
||||
|
||||
func isASCII(s string) bool {
|
||||
@@ -422,7 +422,7 @@ func builtinContains(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term
|
||||
return err
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(strings.Contains(string(s), string(substr))))
|
||||
return iter(ast.InternedTerm(strings.Contains(string(s), string(substr))))
|
||||
}
|
||||
|
||||
func builtinStringCount(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -440,7 +440,7 @@ func builtinStringCount(_ BuiltinContext, operands []*ast.Term, iter func(*ast.T
|
||||
searchTerm := string(substr)
|
||||
count := strings.Count(baseTerm, searchTerm)
|
||||
|
||||
return iter(ast.InternedIntNumberTerm(count))
|
||||
return iter(ast.InternedTerm(count))
|
||||
}
|
||||
|
||||
func builtinStartsWith(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -454,7 +454,7 @@ func builtinStartsWith(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Te
|
||||
return err
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(strings.HasPrefix(string(s), string(prefix))))
|
||||
return iter(ast.InternedTerm(strings.HasPrefix(string(s), string(prefix))))
|
||||
}
|
||||
|
||||
func builtinEndsWith(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -468,7 +468,7 @@ func builtinEndsWith(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term
|
||||
return err
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(strings.HasSuffix(string(s), string(suffix))))
|
||||
return iter(ast.InternedTerm(strings.HasSuffix(string(s), string(suffix))))
|
||||
}
|
||||
|
||||
func builtinLower(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -484,7 +484,7 @@ func builtinLower(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) e
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(low))
|
||||
return iter(ast.InternedTerm(low))
|
||||
}
|
||||
|
||||
func builtinUpper(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -500,7 +500,7 @@ func builtinUpper(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) e
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(upp))
|
||||
return iter(ast.InternedTerm(upp))
|
||||
}
|
||||
|
||||
func builtinSplit(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -522,7 +522,7 @@ func builtinSplit(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) e
|
||||
arr := make([]*ast.Term, len(elems))
|
||||
|
||||
for i := range elems {
|
||||
arr[i] = ast.InternedStringTerm(elems[i])
|
||||
arr[i] = ast.InternedTerm(elems[i])
|
||||
}
|
||||
|
||||
return iter(ast.ArrayTerm(arr...))
|
||||
@@ -549,7 +549,7 @@ func builtinReplace(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term)
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(replaced))
|
||||
return iter(ast.InternedTerm(replaced))
|
||||
}
|
||||
|
||||
func builtinReplaceN(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -579,7 +579,7 @@ func builtinReplaceN(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term
|
||||
oldnewArr = append(oldnewArr, string(keyVal), string(strVal))
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(strings.NewReplacer(oldnewArr...).Replace(string(s))))
|
||||
return iter(ast.InternedTerm(strings.NewReplacer(oldnewArr...).Replace(string(s))))
|
||||
}
|
||||
|
||||
func builtinTrim(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -598,7 +598,7 @@ func builtinTrim(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) er
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(strings.Trim(string(s), string(c))))
|
||||
return iter(ast.InternedTerm(strings.Trim(string(s), string(c))))
|
||||
}
|
||||
|
||||
func builtinTrimLeft(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -617,7 +617,7 @@ func builtinTrimLeft(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(trimmed))
|
||||
return iter(ast.InternedTerm(trimmed))
|
||||
}
|
||||
|
||||
func builtinTrimPrefix(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -636,7 +636,7 @@ func builtinTrimPrefix(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Te
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(trimmed))
|
||||
return iter(ast.InternedTerm(trimmed))
|
||||
}
|
||||
|
||||
func builtinTrimRight(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -655,7 +655,7 @@ func builtinTrimRight(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(trimmed))
|
||||
return iter(ast.InternedTerm(trimmed))
|
||||
}
|
||||
|
||||
func builtinTrimSuffix(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -674,7 +674,7 @@ func builtinTrimSuffix(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Te
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(trimmed))
|
||||
return iter(ast.InternedTerm(trimmed))
|
||||
}
|
||||
|
||||
func builtinTrimSpace(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -688,7 +688,7 @@ func builtinTrimSpace(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(trimmed))
|
||||
return iter(ast.InternedTerm(trimmed))
|
||||
}
|
||||
|
||||
func builtinSprintf(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -736,7 +736,7 @@ func builtinSprintf(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term)
|
||||
}
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(fmt.Sprintf(string(s), args...)))
|
||||
return iter(ast.InternedTerm(fmt.Sprintf(string(s), args...)))
|
||||
}
|
||||
|
||||
func builtinReverse(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -745,7 +745,7 @@ func builtinReverse(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term)
|
||||
return err
|
||||
}
|
||||
|
||||
return iter(ast.InternedStringTerm(reverseString(string(s))))
|
||||
return iter(ast.InternedTerm(reverseString(string(s))))
|
||||
}
|
||||
|
||||
func reverseString(str string) string {
|
||||
|
||||
@@ -146,8 +146,8 @@ func BenchmarkSubstring(b *testing.B) {
|
||||
operands := []*ast.Term{
|
||||
// insert any non-asci character to see the difference of that optimization
|
||||
ast.StringTerm("The quick brown fox jumps over the lazy dog"),
|
||||
ast.InternedIntNumberTerm(6),
|
||||
ast.InternedIntNumberTerm(10),
|
||||
ast.InternedTerm(6),
|
||||
ast.InternedTerm(10),
|
||||
}
|
||||
|
||||
iter := eqIter(ast.StringTerm("ick brown "))
|
||||
@@ -175,7 +175,7 @@ func BenchmarkIndexOf(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
|
||||
for range b.N {
|
||||
if err := builtinIndexOf(BuiltinContext{}, operands, eqIter(ast.InternedIntNumberTerm(40))); err != nil {
|
||||
if err := builtinIndexOf(BuiltinContext{}, operands, eqIter(ast.InternedTerm(40))); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -193,8 +193,8 @@ func eqIter(a *ast.Term) func(*ast.Term) error {
|
||||
// 0 allocs for numbers between 0 and 100 and base 10, 3 allocs for anything else.
|
||||
func BenchmarkFormatInt(b *testing.B) {
|
||||
operands := []*ast.Term{
|
||||
ast.InternedIntNumberTerm(99),
|
||||
ast.InternedIntNumberTerm(10),
|
||||
ast.InternedTerm(99),
|
||||
ast.InternedTerm(10),
|
||||
}
|
||||
bctx := BuiltinContext{}
|
||||
want := eqIter(ast.StringTerm("99"))
|
||||
@@ -213,7 +213,7 @@ func BenchmarkSprintfSingleInteger(b *testing.B) {
|
||||
operands := []*ast.Term{
|
||||
ast.StringTerm("%d"),
|
||||
ast.ArrayTerm(
|
||||
ast.InternedIntNumberTerm(99),
|
||||
ast.InternedTerm(99),
|
||||
),
|
||||
}
|
||||
bctx := BuiltinContext{}
|
||||
@@ -312,27 +312,27 @@ func BenchmarkConcat(b *testing.B) {
|
||||
}{
|
||||
{
|
||||
name: "0 elements '.' sep",
|
||||
operands: []*ast.Term{ast.InternedStringTerm("."), ast.InternedEmptyArray},
|
||||
operands: []*ast.Term{ast.InternedTerm("."), ast.InternedEmptyArray},
|
||||
expected: ast.InternedEmptyString,
|
||||
},
|
||||
{
|
||||
name: "1 element '.' sep",
|
||||
operands: []*ast.Term{ast.InternedStringTerm("."), ast.ArrayTerm(ast.InternedStringTerm("foobar"))},
|
||||
expected: ast.InternedStringTerm("foobar"),
|
||||
operands: []*ast.Term{ast.InternedTerm("."), ast.ArrayTerm(ast.InternedTerm("foobar"))},
|
||||
expected: ast.InternedTerm("foobar"),
|
||||
},
|
||||
{
|
||||
name: "100 elements ',' sep",
|
||||
operands: []*ast.Term{ast.InternedStringTerm(","), repeatTerm(ast.InternedStringTerm("foobar"), 100)},
|
||||
operands: []*ast.Term{ast.InternedTerm(","), repeatTerm(ast.InternedTerm("foobar"), 100)},
|
||||
expected: ast.StringTerm(strings.Repeat("foobar,", 99) + "foobar"),
|
||||
},
|
||||
{
|
||||
name: "100 elements ', ' sep",
|
||||
operands: []*ast.Term{ast.InternedStringTerm(", "), repeatTerm(ast.InternedStringTerm("foobar"), 100)},
|
||||
operands: []*ast.Term{ast.InternedTerm(", "), repeatTerm(ast.InternedTerm("foobar"), 100)},
|
||||
expected: ast.StringTerm(strings.Repeat("foobar, ", 99) + "foobar"),
|
||||
},
|
||||
{
|
||||
name: "100 elements blank sep",
|
||||
operands: []*ast.Term{ast.InternedEmptyString, repeatTerm(ast.InternedStringTerm("foobar"), 100)},
|
||||
operands: []*ast.Term{ast.InternedEmptyString, repeatTerm(ast.InternedTerm("foobar"), 100)},
|
||||
expected: ast.StringTerm(strings.Repeat("foobar", 100)),
|
||||
},
|
||||
}
|
||||
@@ -351,9 +351,9 @@ func BenchmarkConcat(b *testing.B) {
|
||||
func BenchmarkConcatVsSprintfSimple(b *testing.B) {
|
||||
bctx := BuiltinContext{}
|
||||
|
||||
foo := ast.InternedStringTerm("foo")
|
||||
bar := ast.InternedStringTerm("bar")
|
||||
expected := ast.InternedStringTerm("foobar")
|
||||
foo := ast.InternedTerm("foo")
|
||||
bar := ast.InternedTerm("bar")
|
||||
expected := ast.InternedTerm("foobar")
|
||||
|
||||
b.Run("concat foobar", func(b *testing.B) {
|
||||
operands := []*ast.Term{ast.InternedEmptyString, ast.ArrayTerm(foo, bar)}
|
||||
@@ -368,7 +368,7 @@ func BenchmarkConcatVsSprintfSimple(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
|
||||
b.Run("sprintf foobar", func(b *testing.B) {
|
||||
operands := []*ast.Term{ast.InternedStringTerm("%s%s"), ast.ArrayTerm(foo, bar)}
|
||||
operands := []*ast.Term{ast.InternedTerm("%s%s"), ast.ArrayTerm(foo, bar)}
|
||||
|
||||
for range b.N {
|
||||
if err := builtinSprintf(bctx, operands, eqIter(expected)); err != nil {
|
||||
|
||||
@@ -216,22 +216,22 @@ func builtinObjectSubset(_ BuiltinContext, operands []*ast.Term, iter func(*ast.
|
||||
|
||||
if ok, superObj, subObj := bothObjects(superTerm, subTerm); ok {
|
||||
// Both operands are objects.
|
||||
return iter(ast.InternedBooleanTerm(objectSubset(superObj, subObj)))
|
||||
return iter(ast.InternedTerm(objectSubset(superObj, subObj)))
|
||||
}
|
||||
|
||||
if ok, superSet, subSet := bothSets(superTerm, subTerm); ok {
|
||||
// Both operands are sets.
|
||||
return iter(ast.InternedBooleanTerm(setSubset(superSet, subSet)))
|
||||
return iter(ast.InternedTerm(setSubset(superSet, subSet)))
|
||||
}
|
||||
|
||||
if ok, superArray, subArray := bothArrays(superTerm, subTerm); ok {
|
||||
// Both operands are sets.
|
||||
return iter(ast.InternedBooleanTerm(arraySubset(superArray, subArray)))
|
||||
return iter(ast.InternedTerm(arraySubset(superArray, subArray)))
|
||||
}
|
||||
|
||||
if ok, superArray, subSet := arraySet(superTerm, subTerm); ok {
|
||||
// Super operand is array and sub operand is set
|
||||
return iter(ast.InternedBooleanTerm(arraySetSubset(superArray, subSet)))
|
||||
return iter(ast.InternedTerm(arraySetSubset(superArray, subSet)))
|
||||
}
|
||||
|
||||
return builtins.ErrOperand("both arguments object.subset must be of the same type or array and set")
|
||||
|
||||
+4
-4
@@ -128,7 +128,7 @@ func builtinDate(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) er
|
||||
}
|
||||
year, month, day := t.Date()
|
||||
|
||||
return iter(ast.ArrayTerm(ast.InternedIntNumberTerm(year), ast.InternedIntNumberTerm(int(month)), ast.InternedIntNumberTerm(day)))
|
||||
return iter(ast.ArrayTerm(ast.InternedTerm(year), ast.InternedTerm(int(month)), ast.InternedTerm(day)))
|
||||
}
|
||||
|
||||
func builtinClock(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
@@ -137,7 +137,7 @@ func builtinClock(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) e
|
||||
return err
|
||||
}
|
||||
hour, minute, second := t.Clock()
|
||||
result := ast.NewArray(ast.InternedIntNumberTerm(hour), ast.InternedIntNumberTerm(minute), ast.InternedIntNumberTerm(second))
|
||||
result := ast.NewArray(ast.InternedTerm(hour), ast.InternedTerm(minute), ast.InternedTerm(second))
|
||||
return iter(ast.NewTerm(result))
|
||||
}
|
||||
|
||||
@@ -238,8 +238,8 @@ func builtinDiff(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) er
|
||||
}
|
||||
// END REDISTRIBUTION FROM APACHE 2.0 LICENSED PROJECT
|
||||
|
||||
return iter(ast.ArrayTerm(ast.InternedIntNumberTerm(year), ast.InternedIntNumberTerm(month), ast.InternedIntNumberTerm(day),
|
||||
ast.InternedIntNumberTerm(hour), ast.InternedIntNumberTerm(min), ast.InternedIntNumberTerm(sec)))
|
||||
return iter(ast.ArrayTerm(ast.InternedTerm(year), ast.InternedTerm(month), ast.InternedTerm(day),
|
||||
ast.InternedTerm(hour), ast.InternedTerm(min), ast.InternedTerm(sec)))
|
||||
}
|
||||
|
||||
func tzTime(a ast.Value) (t time.Time, lay string, err error) {
|
||||
|
||||
+21
-21
@@ -75,7 +75,7 @@ func builtinJWTDecode(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
|
||||
return fmt.Errorf("JWT payload had invalid encoding: %v", err)
|
||||
}
|
||||
|
||||
if cty := token.decodedHeader.Get(ast.InternedStringTerm("cty")); cty != nil {
|
||||
if cty := token.decodedHeader.Get(ast.InternedTerm("cty")); cty != nil {
|
||||
ctyVal := string(cty.Value.(ast.String))
|
||||
// It is possible for the contents of a token to be another
|
||||
// token as a result of nested signing or encryption. To handle
|
||||
@@ -128,7 +128,7 @@ func builtinJWTVerifyRS256(bctx BuiltinContext, operands []*ast.Term, iter func(
|
||||
signature)
|
||||
})
|
||||
if err == nil {
|
||||
return iter(ast.InternedBooleanTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -143,7 +143,7 @@ func builtinJWTVerifyRS384(bctx BuiltinContext, operands []*ast.Term, iter func(
|
||||
signature)
|
||||
})
|
||||
if err == nil {
|
||||
return iter(ast.InternedBooleanTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -158,7 +158,7 @@ func builtinJWTVerifyRS512(bctx BuiltinContext, operands []*ast.Term, iter func(
|
||||
signature)
|
||||
})
|
||||
if err == nil {
|
||||
return iter(ast.InternedBooleanTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -174,7 +174,7 @@ func builtinJWTVerifyPS256(bctx BuiltinContext, operands []*ast.Term, iter func(
|
||||
nil)
|
||||
})
|
||||
if err == nil {
|
||||
return iter(ast.InternedBooleanTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -190,7 +190,7 @@ func builtinJWTVerifyPS384(bctx BuiltinContext, operands []*ast.Term, iter func(
|
||||
nil)
|
||||
})
|
||||
if err == nil {
|
||||
return iter(ast.InternedBooleanTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -206,7 +206,7 @@ func builtinJWTVerifyPS512(bctx BuiltinContext, operands []*ast.Term, iter func(
|
||||
nil)
|
||||
})
|
||||
if err == nil {
|
||||
return iter(ast.InternedBooleanTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -226,7 +226,7 @@ func builtinJWTVerifyRSA(bctx BuiltinContext, jwt ast.Value, keyStr ast.Value, h
|
||||
func builtinJWTVerifyES256(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
result, err := builtinJWTVerify(bctx, operands[0].Value, operands[1].Value, sha256.New, verifyES)
|
||||
if err == nil {
|
||||
return iter(ast.InternedBooleanTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -235,7 +235,7 @@ func builtinJWTVerifyES256(bctx BuiltinContext, operands []*ast.Term, iter func(
|
||||
func builtinJWTVerifyES384(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
result, err := builtinJWTVerify(bctx, operands[0].Value, operands[1].Value, sha512.New384, verifyES)
|
||||
if err == nil {
|
||||
return iter(ast.InternedBooleanTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -244,7 +244,7 @@ func builtinJWTVerifyES384(bctx BuiltinContext, operands []*ast.Term, iter func(
|
||||
func builtinJWTVerifyES512(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
result, err := builtinJWTVerify(bctx, operands[0].Value, operands[1].Value, sha512.New, verifyES)
|
||||
if err == nil {
|
||||
return iter(ast.InternedBooleanTerm(result))
|
||||
return iter(ast.InternedTerm(result))
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -439,7 +439,7 @@ func builtinJWTVerifyHS(bctx BuiltinContext, operands []*ast.Term, hashF func()
|
||||
}
|
||||
|
||||
if found, _, _, valid := getTokenFromCache(bctx, jwt, astSecret); found {
|
||||
return iter(ast.InternedBooleanTerm(valid))
|
||||
return iter(ast.InternedTerm(valid))
|
||||
}
|
||||
|
||||
// Decode the JSON Web Token
|
||||
@@ -465,7 +465,7 @@ func builtinJWTVerifyHS(bctx BuiltinContext, operands []*ast.Term, hashF func()
|
||||
|
||||
putTokenInCache(bctx, jwt, astSecret, nil, nil, valid)
|
||||
|
||||
return iter(ast.InternedBooleanTerm(valid))
|
||||
return iter(ast.InternedTerm(valid))
|
||||
}
|
||||
|
||||
// -- Full JWT verification and decoding --
|
||||
@@ -993,7 +993,7 @@ func builtinJWTDecodeVerify(bctx BuiltinContext, operands []*ast.Term, iter func
|
||||
}
|
||||
|
||||
unverified := ast.ArrayTerm(
|
||||
ast.InternedBooleanTerm(false),
|
||||
ast.InternedTerm(false),
|
||||
ast.InternedEmptyObject,
|
||||
ast.InternedEmptyObject,
|
||||
)
|
||||
@@ -1010,8 +1010,8 @@ func builtinJWTDecodeVerify(bctx BuiltinContext, operands []*ast.Term, iter func
|
||||
|
||||
// FIXME: optimize
|
||||
k, _ := b.Filter(ast.NewObject(
|
||||
ast.Item(ast.InternedStringTerm("secret"), ast.InternedEmptyObject),
|
||||
ast.Item(ast.InternedStringTerm("cert"), ast.InternedEmptyObject),
|
||||
ast.Item(ast.InternedTerm("secret"), ast.InternedEmptyObject),
|
||||
ast.Item(ast.InternedTerm("cert"), ast.InternedEmptyObject),
|
||||
))
|
||||
|
||||
if found, th, tp, validSignature := getTokenFromCache(bctx, a, k); found {
|
||||
@@ -1117,7 +1117,7 @@ func builtinJWTDecodeVerify(bctx BuiltinContext, operands []*ast.Term, iter func
|
||||
// Check registered claim names against constraints or environment
|
||||
// RFC7159 4.1.1 iss
|
||||
if constraints.iss != "" {
|
||||
if iss := payload.Get(ast.InternedStringTerm("iss")); iss != nil {
|
||||
if iss := payload.Get(ast.InternedTerm("iss")); iss != nil {
|
||||
issVal := string(iss.Value.(ast.String))
|
||||
if constraints.iss != issVal {
|
||||
return iter(unverified)
|
||||
@@ -1127,7 +1127,7 @@ func builtinJWTDecodeVerify(bctx BuiltinContext, operands []*ast.Term, iter func
|
||||
}
|
||||
}
|
||||
// RFC7159 4.1.3 aud
|
||||
if aud := payload.Get(ast.InternedStringTerm("aud")); aud != nil {
|
||||
if aud := payload.Get(ast.InternedTerm("aud")); aud != nil {
|
||||
if !constraints.validAudience(aud.Value) {
|
||||
return iter(unverified)
|
||||
}
|
||||
@@ -1137,7 +1137,7 @@ func builtinJWTDecodeVerify(bctx BuiltinContext, operands []*ast.Term, iter func
|
||||
}
|
||||
}
|
||||
// RFC7159 4.1.4 exp
|
||||
if exp := payload.Get(ast.InternedStringTerm("exp")); exp != nil {
|
||||
if exp := payload.Get(ast.InternedTerm("exp")); exp != nil {
|
||||
switch v := exp.Value.(type) {
|
||||
case ast.Number:
|
||||
// constraints.time is in nanoseconds but exp Value is in seconds
|
||||
@@ -1150,7 +1150,7 @@ func builtinJWTDecodeVerify(bctx BuiltinContext, operands []*ast.Term, iter func
|
||||
}
|
||||
}
|
||||
// RFC7159 4.1.5 nbf
|
||||
if nbf := payload.Get(ast.InternedStringTerm("nbf")); nbf != nil {
|
||||
if nbf := payload.Get(ast.InternedTerm("nbf")); nbf != nil {
|
||||
switch v := nbf.Value.(type) {
|
||||
case ast.Number:
|
||||
// constraints.time is in nanoseconds but nbf Value is in seconds
|
||||
@@ -1164,7 +1164,7 @@ func builtinJWTDecodeVerify(bctx BuiltinContext, operands []*ast.Term, iter func
|
||||
}
|
||||
|
||||
verified := ast.ArrayTerm(
|
||||
ast.InternedBooleanTerm(true),
|
||||
ast.InternedTerm(true),
|
||||
ast.NewTerm(header),
|
||||
ast.NewTerm(payload),
|
||||
)
|
||||
@@ -1218,7 +1218,7 @@ func validateJWTHeader(h string) (ast.Object, error) {
|
||||
// won't support it for now.
|
||||
// This code checks which kind of JWT we are dealing with according to
|
||||
// RFC 7516 Section 9: https://tools.ietf.org/html/rfc7516#section-9
|
||||
if header.Get(ast.InternedStringTerm("enc")) != nil {
|
||||
if header.Get(ast.InternedTerm("enc")) != nil {
|
||||
return nil, errors.New("JWT is a JWE object, which is not supported")
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -533,7 +533,7 @@ func builtinTrace(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term
|
||||
}
|
||||
|
||||
if !bctx.TraceEnabled {
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
}
|
||||
|
||||
evt := Event{
|
||||
@@ -548,7 +548,7 @@ func builtinTrace(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term
|
||||
bctx.QueryTracers[i].TraceEvent(evt)
|
||||
}
|
||||
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
}
|
||||
|
||||
func rewrite(event *Event) *Event {
|
||||
|
||||
+14
-14
@@ -11,63 +11,63 @@ import (
|
||||
func builtinIsNumber(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch operands[0].Value.(type) {
|
||||
case ast.Number:
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
default:
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
}
|
||||
|
||||
func builtinIsString(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch operands[0].Value.(type) {
|
||||
case ast.String:
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
default:
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
}
|
||||
|
||||
func builtinIsBoolean(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch operands[0].Value.(type) {
|
||||
case ast.Boolean:
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
default:
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
}
|
||||
|
||||
func builtinIsArray(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch operands[0].Value.(type) {
|
||||
case *ast.Array:
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
default:
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
}
|
||||
|
||||
func builtinIsSet(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch operands[0].Value.(type) {
|
||||
case ast.Set:
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
default:
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
}
|
||||
|
||||
func builtinIsObject(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch operands[0].Value.(type) {
|
||||
case ast.Object:
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
default:
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
}
|
||||
|
||||
func builtinIsNull(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch operands[0].Value.(type) {
|
||||
case ast.Null:
|
||||
return iter(ast.InternedBooleanTerm(true))
|
||||
return iter(ast.InternedTerm(true))
|
||||
default:
|
||||
return iter(ast.InternedBooleanTerm(false))
|
||||
return iter(ast.InternedTerm(false))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,19 +13,19 @@ import (
|
||||
func builtinTypeName(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
switch operands[0].Value.(type) {
|
||||
case ast.Null:
|
||||
return iter(ast.InternedStringTerm("null"))
|
||||
return iter(ast.InternedTerm("null"))
|
||||
case ast.Boolean:
|
||||
return iter(ast.InternedStringTerm("boolean"))
|
||||
return iter(ast.InternedTerm("boolean"))
|
||||
case ast.Number:
|
||||
return iter(ast.InternedStringTerm("number"))
|
||||
return iter(ast.InternedTerm("number"))
|
||||
case ast.String:
|
||||
return iter(ast.InternedStringTerm("string"))
|
||||
return iter(ast.InternedTerm("string"))
|
||||
case *ast.Array:
|
||||
return iter(ast.InternedStringTerm("array"))
|
||||
return iter(ast.InternedTerm("array"))
|
||||
case ast.Object:
|
||||
return iter(ast.InternedStringTerm("object"))
|
||||
return iter(ast.InternedTerm("object"))
|
||||
case ast.Set:
|
||||
return iter(ast.InternedStringTerm("set"))
|
||||
return iter(ast.InternedTerm("set"))
|
||||
}
|
||||
|
||||
return errors.New("illegal value")
|
||||
|
||||
+2
-2
@@ -57,7 +57,7 @@ func walk(filter, path *ast.Array, input *ast.Term, iter func(*ast.Term) error)
|
||||
switch v := input.Value.(type) {
|
||||
case *ast.Array:
|
||||
for i := range v.Len() {
|
||||
if err := walk(filter, pathAppend(path, ast.InternedIntNumberTerm(i)), v.Elem(i), iter); err != nil {
|
||||
if err := walk(filter, pathAppend(path, ast.InternedTerm(i)), v.Elem(i), iter); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func walkNoPath(input *ast.Term, iter func(*ast.Term) error) error {
|
||||
}
|
||||
|
||||
inputArray := input.Value.(*ast.Array)
|
||||
value := inputArray.Get(ast.InternedIntNumberTerm(1)).Value
|
||||
value := inputArray.Get(ast.InternedTerm(1)).Value
|
||||
|
||||
switch v := value.(type) {
|
||||
case ast.Object:
|
||||
|
||||
Reference in New Issue
Block a user