mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
Enable static check of consistent receiver names (#9008)
Style thing really but I think one that makes sense. I renamed only by what was already the most popular option. Tested building against Go 1.27 to make sure I didn't mess that up. Signed-off-by: Anders Eknert <anders.eknert@apple.com>
This commit is contained in:
@@ -84,7 +84,6 @@ linters:
|
|||||||
"all",
|
"all",
|
||||||
"-ST1000",
|
"-ST1000",
|
||||||
"-ST1003",
|
"-ST1003",
|
||||||
"-ST1016",
|
|
||||||
"-ST1020",
|
"-ST1020",
|
||||||
"-ST1021",
|
"-ST1021",
|
||||||
"-ST1022",
|
"-ST1022",
|
||||||
|
|||||||
@@ -221,9 +221,9 @@ func getABIVersion(mod api.Module) (int32, int32, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// close releases the wazero Runtime and all resources associated with this VM.
|
// close releases the wazero Runtime and all resources associated with this VM.
|
||||||
func (v *VM) close() {
|
func (i *VM) close() {
|
||||||
if v.runtime != nil {
|
if i.runtime != nil {
|
||||||
v.runtime.Close(context.Background())
|
i.runtime.Close(context.Background())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -280,9 +280,9 @@ func mermaidFormatWith(w *With, b *mermaidBuilder) string {
|
|||||||
|
|
||||||
// --- Not ---
|
// --- Not ---
|
||||||
|
|
||||||
func (not *Not) mermaidFormat(b *mermaidBuilder) string {
|
func (n *Not) mermaidFormat(b *mermaidBuilder) string {
|
||||||
id := b.node("stadium", "not")
|
id := b.node("stadium", "not")
|
||||||
for i, expr := range not.Body {
|
for i, expr := range n.Body {
|
||||||
exprID := mermaidFormatExpr(expr, b)
|
exprID := mermaidFormatExpr(expr, b)
|
||||||
b.edgeLabeled(id, exprID, strconv.Itoa(i))
|
b.edgeLabeled(id, exprID, strconv.Itoa(i))
|
||||||
}
|
}
|
||||||
|
|||||||
+49
-49
@@ -7,8 +7,8 @@ import (
|
|||||||
"github.com/open-policy-agent/opa/v1/util"
|
"github.com/open-policy-agent/opa/v1/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *Module) AppendText(buf []byte) ([]byte, error) {
|
func (mod *Module) AppendText(buf []byte) ([]byte, error) {
|
||||||
if m == nil {
|
if mod == nil {
|
||||||
return append(buf, "<nil module>"...), nil
|
return append(buf, "<nil module>"...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ func (m *Module) AppendText(buf []byte) ([]byte, error) {
|
|||||||
// NOTE(anderseknert): this DOES allocate still, and while that's unfortunate,
|
// NOTE(anderseknert): this DOES allocate still, and while that's unfortunate,
|
||||||
// we'll be better off dealing with that when we have v2 JSON in the stdlib than
|
// we'll be better off dealing with that when we have v2 JSON in the stdlib than
|
||||||
// doing manual JSON marshalling (and string length calculations) here.
|
// doing manual JSON marshalling (and string length calculations) here.
|
||||||
for _, annotations := range m.Annotations {
|
for _, annotations := range mod.Annotations {
|
||||||
// rule annotations are attached to rules, so only check for package scoped ones here
|
// rule annotations are attached to rules, so only check for package scoped ones here
|
||||||
if annotations.Scope == "package" || annotations.Scope == "subpackages" {
|
if annotations.Scope == "package" || annotations.Scope == "subpackages" {
|
||||||
buf = append(buf, "# METADATA\n# "...)
|
buf = append(buf, "# METADATA\n# "...)
|
||||||
@@ -26,13 +26,13 @@ func (m *Module) AppendText(buf []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if buf, err = m.Package.AppendText(buf); err != nil {
|
if buf, err = mod.Package.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
buf = append(buf, '\n')
|
buf = append(buf, '\n')
|
||||||
|
|
||||||
if len(m.Imports) > 0 {
|
if len(mod.Imports) > 0 {
|
||||||
for _, imp := range m.Imports {
|
for _, imp := range mod.Imports {
|
||||||
buf = append(buf, '\n')
|
buf = append(buf, '\n')
|
||||||
if buf, err = imp.AppendText(buf); err != nil {
|
if buf, err = imp.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -41,10 +41,10 @@ func (m *Module) AppendText(buf []byte) ([]byte, error) {
|
|||||||
buf = append(buf, '\n')
|
buf = append(buf, '\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(m.Rules) > 0 {
|
if len(mod.Rules) > 0 {
|
||||||
for _, rule := range m.Rules {
|
for _, rule := range mod.Rules {
|
||||||
buf = append(buf, '\n')
|
buf = append(buf, '\n')
|
||||||
if buf, err = rule.appendWithOpts(toStringOpts{regoVersion: m.regoVersion}, buf); err != nil {
|
if buf, err = rule.appendWithOpts(toStringOpts{regoVersion: mod.regoVersion}, buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,45 +86,45 @@ func (imp *Import) AppendText(buf []byte) ([]byte, error) {
|
|||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rule) AppendText(buf []byte) ([]byte, error) {
|
func (rule *Rule) AppendText(buf []byte) ([]byte, error) {
|
||||||
regoVersion := DefaultRegoVersion
|
regoVersion := DefaultRegoVersion
|
||||||
if r.Module != nil {
|
if rule.Module != nil {
|
||||||
regoVersion = r.Module.RegoVersion()
|
regoVersion = rule.Module.RegoVersion()
|
||||||
}
|
}
|
||||||
return r.appendWithOpts(toStringOpts{regoVersion: regoVersion}, buf)
|
return rule.appendWithOpts(toStringOpts{regoVersion: regoVersion}, buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rule) appendWithOpts(opts toStringOpts, buf []byte) ([]byte, error) {
|
func (rule *Rule) appendWithOpts(opts toStringOpts, buf []byte) ([]byte, error) {
|
||||||
// See note in [Module.AppendText] regarding annotations.
|
// See note in [Module.AppendText] regarding annotations.
|
||||||
for _, annotations := range r.Annotations {
|
for _, annotations := range rule.Annotations {
|
||||||
buf = append(buf, "# METADATA\n# "...)
|
buf = append(buf, "# METADATA\n# "...)
|
||||||
buf = append(buf, annotations.String()...)
|
buf = append(buf, annotations.String()...)
|
||||||
buf = append(buf, '\n')
|
buf = append(buf, '\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Default {
|
if rule.Default {
|
||||||
buf = append(buf, "default "...)
|
buf = append(buf, "default "...)
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if buf, err = r.Head.appendWithOpts(opts, buf); err != nil {
|
if buf, err = rule.Head.appendWithOpts(opts, buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !r.Default {
|
if !rule.Default {
|
||||||
switch opts.RegoVersion() {
|
switch opts.RegoVersion() {
|
||||||
case RegoV1, RegoV0CompatV1:
|
case RegoV1, RegoV0CompatV1:
|
||||||
buf = append(buf, " if { "...)
|
buf = append(buf, " if { "...)
|
||||||
default:
|
default:
|
||||||
buf = append(buf, " { "...)
|
buf = append(buf, " { "...)
|
||||||
}
|
}
|
||||||
if buf, err = r.Body.AppendText(buf); err != nil {
|
if buf, err = rule.Body.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
buf = append(buf, " }"...)
|
buf = append(buf, " }"...)
|
||||||
}
|
}
|
||||||
if r.Else != nil {
|
if rule.Else != nil {
|
||||||
if buf, err = r.Else.appendElse(opts, buf); err != nil {
|
if buf, err = rule.Else.appendElse(opts, buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,13 +132,13 @@ func (r *Rule) appendWithOpts(opts toStringOpts, buf []byte) ([]byte, error) {
|
|||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rule) appendElse(opts toStringOpts, buf []byte) ([]byte, error) {
|
func (rule *Rule) appendElse(opts toStringOpts, buf []byte) ([]byte, error) {
|
||||||
buf = append(buf, " else "...)
|
buf = append(buf, " else "...)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if r.Head.Value != nil {
|
if rule.Head.Value != nil {
|
||||||
buf = append(buf, "= "...)
|
buf = append(buf, "= "...)
|
||||||
if buf, err = r.Head.Value.AppendText(buf); err != nil {
|
if buf, err = rule.Head.Value.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -148,13 +148,13 @@ func (r *Rule) appendElse(opts toStringOpts, buf []byte) ([]byte, error) {
|
|||||||
} else {
|
} else {
|
||||||
buf = append(buf, " { "...)
|
buf = append(buf, " { "...)
|
||||||
}
|
}
|
||||||
if buf, err = r.Body.AppendText(buf); err != nil {
|
if buf, err = rule.Body.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
buf = append(buf, " }"...)
|
buf = append(buf, " }"...)
|
||||||
|
|
||||||
if r.Else != nil {
|
if rule.Else != nil {
|
||||||
if buf, err = r.Else.appendElse(opts, buf); err != nil {
|
if buf, err = rule.Else.appendElse(opts, buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -162,52 +162,52 @@ func (r *Rule) appendElse(opts toStringOpts, buf []byte) ([]byte, error) {
|
|||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Head) AppendText(buf []byte) ([]byte, error) {
|
func (head *Head) AppendText(buf []byte) ([]byte, error) {
|
||||||
return h.appendWithOpts(toStringOpts{}, buf)
|
return head.appendWithOpts(toStringOpts{}, buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Head) appendWithOpts(opts toStringOpts, buf []byte) ([]byte, error) {
|
func (head *Head) appendWithOpts(opts toStringOpts, buf []byte) ([]byte, error) {
|
||||||
var err error
|
var err error
|
||||||
if h.Reference == nil {
|
if head.Reference == nil {
|
||||||
buf = append(buf, h.Name...)
|
buf = append(buf, head.Name...)
|
||||||
} else {
|
} else {
|
||||||
if buf, err = h.Reference.AppendText(buf); err != nil {
|
if buf, err = head.Reference.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
containsAdded := false
|
containsAdded := false
|
||||||
switch {
|
switch {
|
||||||
case len(h.Args) != 0:
|
case len(head.Args) != 0:
|
||||||
if buf, err = h.Args.AppendText(buf); err != nil {
|
if buf, err = head.Args.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case len(h.Reference) == 1 && h.Key != nil:
|
case len(head.Reference) == 1 && head.Key != nil:
|
||||||
switch opts.RegoVersion() {
|
switch opts.RegoVersion() {
|
||||||
case RegoV0:
|
case RegoV0:
|
||||||
buf = append(buf, '[')
|
buf = append(buf, '[')
|
||||||
if buf, err = h.Key.AppendText(buf); err != nil {
|
if buf, err = head.Key.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
buf = append(buf, ']')
|
buf = append(buf, ']')
|
||||||
default:
|
default:
|
||||||
if buf, err = h.Key.AppendText(append(buf, " contains "...)); err != nil {
|
if buf, err = head.Key.AppendText(append(buf, " contains "...)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
containsAdded = true
|
containsAdded = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if h.Value != nil {
|
if head.Value != nil {
|
||||||
if h.Assign {
|
if head.Assign {
|
||||||
buf = append(buf, " := "...)
|
buf = append(buf, " := "...)
|
||||||
} else {
|
} else {
|
||||||
buf = append(buf, " = "...)
|
buf = append(buf, " = "...)
|
||||||
}
|
}
|
||||||
if buf, err = h.Value.AppendText(buf); err != nil {
|
if buf, err = head.Value.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else if !containsAdded && h.Name == "" && h.Key != nil {
|
} else if !containsAdded && head.Name == "" && head.Key != nil {
|
||||||
if buf, err = h.Key.AppendText(append(buf, " contains "...)); err != nil {
|
if buf, err = head.Key.AppendText(append(buf, " contains "...)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -275,20 +275,20 @@ func (w *With) AppendText(buf []byte) ([]byte, error) {
|
|||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Every) AppendText(buf []byte) ([]byte, error) {
|
func (q *Every) AppendText(buf []byte) ([]byte, error) {
|
||||||
buf = append(buf, "every "...)
|
buf = append(buf, "every "...)
|
||||||
var err error
|
var err error
|
||||||
if w.Key != nil {
|
if q.Key != nil {
|
||||||
if buf, err = w.Key.AppendText(buf); err != nil {
|
if buf, err = q.Key.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
buf = append(buf, ", "...)
|
buf = append(buf, ", "...)
|
||||||
}
|
}
|
||||||
if buf, err = w.Value.AppendText(buf); err == nil {
|
if buf, err = q.Value.AppendText(buf); err == nil {
|
||||||
buf = append(buf, " in "...)
|
buf = append(buf, " in "...)
|
||||||
if buf, err = w.Domain.AppendText(buf); err == nil {
|
if buf, err = q.Domain.AppendText(buf); err == nil {
|
||||||
buf = append(buf, " { "...)
|
buf = append(buf, " { "...)
|
||||||
if buf, err = w.Body.AppendText(buf); err == nil {
|
if buf, err = q.Body.AppendText(buf); err == nil {
|
||||||
buf = append(buf, " }"...)
|
buf = append(buf, " }"...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-43
@@ -66,13 +66,13 @@ func (a Args) MarshalJSONTo(e *jsontext.Encoder) error {
|
|||||||
// Rego source rather than as JSON. Module's own fields are fully described by
|
// Rego source rather than as JSON. Module's own fields are fully described by
|
||||||
// their struct tags, so the encoding is left to them, as it is pre-1.27. The
|
// their struct tags, so the encoding is left to them, as it is pre-1.27. The
|
||||||
// field types provide their own MarshalJSONTo where one is needed.
|
// field types provide their own MarshalJSONTo where one is needed.
|
||||||
func (m *Module) MarshalJSONTo(e *jsontext.Encoder) error {
|
func (mod *Module) MarshalJSONTo(e *jsontext.Encoder) error {
|
||||||
// Declare a new type and use a type conversion to avoid recursively calling
|
// Declare a new type and use a type conversion to avoid recursively calling
|
||||||
// Module#MarshalJSONTo. It's the highest precedence marshaller, so there is
|
// Module#MarshalJSONTo. It's the highest precedence marshaller, so there is
|
||||||
// nothing below it to fall to, and the new type has no methods of its own.
|
// nothing below it to fall to, and the new type has no methods of its own.
|
||||||
type module Module
|
type module Module
|
||||||
|
|
||||||
return json.MarshalEncode(e, (*module)(m))
|
return json.MarshalEncode(e, (*module)(mod))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pkg *Package) MarshalJSONTo(e *jsontext.Encoder) error {
|
func (pkg *Package) MarshalJSONTo(e *jsontext.Encoder) error {
|
||||||
@@ -91,57 +91,57 @@ func (pkg *Package) MarshalJSONTo(e *jsontext.Encoder) error {
|
|||||||
return e.WriteToken(jsontext.EndObject)
|
return e.WriteToken(jsontext.EndObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Import) MarshalJSONTo(e *jsontext.Encoder) error {
|
func (imp *Import) MarshalJSONTo(e *jsontext.Encoder) error {
|
||||||
e.WriteToken(jsontext.BeginObject)
|
e.WriteToken(jsontext.BeginObject)
|
||||||
|
|
||||||
if err := jsonv2.WriteField(e, "path", i.Path); err != nil {
|
if err := jsonv2.WriteField(e, "path", imp.Path); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if astJSON.GetOptions().MarshalOptions.IncludeLocation.Import && i.Location != nil {
|
if astJSON.GetOptions().MarshalOptions.IncludeLocation.Import && imp.Location != nil {
|
||||||
if err := jsonv2.WriteField(e, "location", i.Location); err != nil {
|
if err := jsonv2.WriteField(e, "location", imp.Location); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(i.Alias) > 0 {
|
if len(imp.Alias) > 0 {
|
||||||
e.WriteToken(jsontext.String("alias"))
|
e.WriteToken(jsontext.String("alias"))
|
||||||
e.WriteToken(jsontext.String(string(i.Alias)))
|
e.WriteToken(jsontext.String(string(imp.Alias)))
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.WriteToken(jsontext.EndObject)
|
return e.WriteToken(jsontext.EndObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rule) MarshalJSONTo(e *jsontext.Encoder) error {
|
func (rule *Rule) MarshalJSONTo(e *jsontext.Encoder) error {
|
||||||
e.WriteToken(jsontext.BeginObject)
|
e.WriteToken(jsontext.BeginObject)
|
||||||
|
|
||||||
if r.Default {
|
if rule.Default {
|
||||||
e.WriteToken(jsontext.String("default"))
|
e.WriteToken(jsontext.String("default"))
|
||||||
e.WriteToken(jsontext.True)
|
e.WriteToken(jsontext.True)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Else != nil {
|
if rule.Else != nil {
|
||||||
if err := jsonv2.WriteField(e, "else", r.Else); err != nil {
|
if err := jsonv2.WriteField(e, "else", rule.Else); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := jsonv2.WriteField(e, "head", r.Head); err != nil {
|
if err := jsonv2.WriteField(e, "head", rule.Head); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := jsonv2.WriteField(e, "body", r.Body); err != nil {
|
if err := jsonv2.WriteField(e, "body", rule.Body); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(r.Annotations) > 0 {
|
if len(rule.Annotations) > 0 {
|
||||||
if err := jsonv2.WriteFieldArray(e, "annotations", r.Annotations); err != nil {
|
if err := jsonv2.WriteFieldArray(e, "annotations", rule.Annotations); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if astJSON.GetOptions().MarshalOptions.IncludeLocation.Rule && r.Location != nil {
|
if astJSON.GetOptions().MarshalOptions.IncludeLocation.Rule && rule.Location != nil {
|
||||||
if err := jsonv2.WriteField(e, "location", r.Location); err != nil {
|
if err := jsonv2.WriteField(e, "location", rule.Location); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -149,43 +149,43 @@ func (r *Rule) MarshalJSONTo(e *jsontext.Encoder) error {
|
|||||||
return e.WriteToken(jsontext.EndObject)
|
return e.WriteToken(jsontext.EndObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Head) MarshalJSONTo(e *jsontext.Encoder) error {
|
func (head *Head) MarshalJSONTo(e *jsontext.Encoder) error {
|
||||||
e.WriteToken(jsontext.BeginObject)
|
e.WriteToken(jsontext.BeginObject)
|
||||||
|
|
||||||
if h.Name != "" {
|
if head.Name != "" {
|
||||||
e.WriteToken(jsontext.String("name"))
|
e.WriteToken(jsontext.String("name"))
|
||||||
e.WriteToken(jsontext.String(string(h.Name)))
|
e.WriteToken(jsontext.String(string(head.Name)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := jsonv2.WriteField(e, "ref", h.Ref()); err != nil {
|
if err := jsonv2.WriteField(e, "ref", head.Ref()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(h.Args) > 0 {
|
if len(head.Args) > 0 {
|
||||||
if err := jsonv2.WriteFieldArray(e, "args", h.Args); err != nil {
|
if err := jsonv2.WriteFieldArray(e, "args", head.Args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.Key != nil {
|
if head.Key != nil {
|
||||||
if err := jsonv2.WriteField(e, "key", h.Key); err != nil {
|
if err := jsonv2.WriteField(e, "key", head.Key); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.Value != nil {
|
if head.Value != nil {
|
||||||
if err := jsonv2.WriteField(e, "value", h.Value); err != nil {
|
if err := jsonv2.WriteField(e, "value", head.Value); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.Assign {
|
if head.Assign {
|
||||||
e.WriteToken(jsontext.String("assign"))
|
e.WriteToken(jsontext.String("assign"))
|
||||||
e.WriteToken(jsontext.True)
|
e.WriteToken(jsontext.True)
|
||||||
}
|
}
|
||||||
|
|
||||||
if astJSON.GetOptions().MarshalOptions.IncludeLocation.Head && h.Location != nil {
|
if astJSON.GetOptions().MarshalOptions.IncludeLocation.Head && head.Location != nil {
|
||||||
if err := jsonv2.WriteField(e, "location", h.Location); err != nil {
|
if err := jsonv2.WriteField(e, "location", head.Location); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,8 +258,8 @@ func (q *Every) MarshalJSONTo(e *jsontext.Encoder) error {
|
|||||||
return e.WriteToken(jsontext.EndObject)
|
return e.WriteToken(jsontext.EndObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b Body) MarshalJSONTo(e *jsontext.Encoder) error {
|
func (body Body) MarshalJSONTo(e *jsontext.Encoder) error {
|
||||||
return jsonv2.WriteMarshalerToArray(e, b)
|
return jsonv2.WriteMarshalerToArray(e, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON returns JSON encoded bytes representing body.
|
// MarshalJSON returns JSON encoded bytes representing body.
|
||||||
@@ -280,46 +280,46 @@ func (expr *Expr) UnmarshalJSON(bs []byte) error {
|
|||||||
return unmarshalExpr(expr, v)
|
return unmarshalExpr(expr, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expr) MarshalJSONTo(enc *jsontext.Encoder) error {
|
func (expr *Expr) MarshalJSONTo(enc *jsontext.Encoder) error {
|
||||||
enc.WriteToken(jsontext.BeginObject)
|
enc.WriteToken(jsontext.BeginObject)
|
||||||
|
|
||||||
enc.WriteToken(jsontext.String("index"))
|
enc.WriteToken(jsontext.String("index"))
|
||||||
enc.WriteToken(jsontext.Int(int64(e.Index)))
|
enc.WriteToken(jsontext.Int(int64(expr.Index)))
|
||||||
|
|
||||||
includeLocation := astJSON.GetOptions().MarshalOptions.IncludeLocation
|
includeLocation := astJSON.GetOptions().MarshalOptions.IncludeLocation
|
||||||
if e.Location != nil && includeLocation.Expr {
|
if expr.Location != nil && includeLocation.Expr {
|
||||||
if err := jsonv2.WriteField(enc, "location", e.Location); err != nil {
|
if err := jsonv2.WriteField(enc, "location", expr.Location); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Negated {
|
if expr.Negated {
|
||||||
enc.WriteToken(jsontext.String("negated"))
|
enc.WriteToken(jsontext.String("negated"))
|
||||||
enc.WriteToken(jsontext.True)
|
enc.WriteToken(jsontext.True)
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Generated {
|
if expr.Generated {
|
||||||
enc.WriteToken(jsontext.String("generated"))
|
enc.WriteToken(jsontext.String("generated"))
|
||||||
enc.WriteToken(jsontext.True)
|
enc.WriteToken(jsontext.True)
|
||||||
}
|
}
|
||||||
|
|
||||||
enc.WriteToken(jsontext.String("terms"))
|
enc.WriteToken(jsontext.String("terms"))
|
||||||
var err error
|
var err error
|
||||||
switch t := e.Terms.(type) {
|
switch t := expr.Terms.(type) {
|
||||||
case []*Term:
|
case []*Term:
|
||||||
err = jsonv2.WriteMarshalerToArrayOrNull(enc, t)
|
err = jsonv2.WriteMarshalerToArrayOrNull(enc, t)
|
||||||
case json.MarshalerTo:
|
case json.MarshalerTo:
|
||||||
err = t.MarshalJSONTo(enc)
|
err = t.MarshalJSONTo(enc)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unsupported expr terms type: %T", e.Terms)
|
return fmt.Errorf("unsupported expr terms type: %T", expr.Terms)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal expr terms: %w", err)
|
return fmt.Errorf("failed to marshal expr terms: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(e.With) > 0 {
|
if len(expr.With) > 0 {
|
||||||
if err := jsonv2.WriteFieldArray(enc, "with", e.With); err != nil {
|
if err := jsonv2.WriteFieldArray(enc, "with", expr.With); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+93
-94
@@ -27,17 +27,16 @@ func TermSliceStringLength(terms []*Term, delimLen int) (n int) {
|
|||||||
return max(n-delimLen, 0)
|
return max(n-delimLen, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Term) StringLength() int {
|
func (term *Term) StringLength() int {
|
||||||
if sl, ok := t.Value.(StringLengther); ok {
|
if sl, ok := term.Value.(StringLengther); ok {
|
||||||
return sl.StringLength()
|
return sl.StringLength()
|
||||||
}
|
}
|
||||||
|
panic("expected all ast.Value types to implement StringLenghter interface, got: " + ValueName(term.Value))
|
||||||
panic("expected all ast.Value types to implement StringLenghter interface, got: " + ValueName(t.Value))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s String) StringLength() int {
|
func (str String) StringLength() int {
|
||||||
n := 2 // surrounding quotes
|
n := 2 // surrounding quotes
|
||||||
bs := util.StringToByteSlice(s)
|
bs := util.StringToByteSlice(str)
|
||||||
for i := 0; i < len(bs); {
|
for i := 0; i < len(bs); {
|
||||||
r, size := utf8.DecodeRune(bs[i:])
|
r, size := utf8.DecodeRune(bs[i:])
|
||||||
switch r {
|
switch r {
|
||||||
@@ -57,12 +56,12 @@ func (s String) StringLength() int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n Number) StringLength() int {
|
func (num Number) StringLength() int {
|
||||||
return len(n)
|
return len(num)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b Boolean) StringLength() int {
|
func (bol Boolean) StringLength() int {
|
||||||
if b {
|
if bol {
|
||||||
return 4
|
return 4
|
||||||
}
|
}
|
||||||
return 5
|
return 5
|
||||||
@@ -80,27 +79,27 @@ func (s *set) StringLength() int {
|
|||||||
return TermSliceStringLength(s.Slice(), 2) + 2
|
return TermSliceStringLength(s.Slice(), 2) + 2
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Array) StringLength() int {
|
func (arr *Array) StringLength() int {
|
||||||
if a.Len() == 0 {
|
if arr.Len() == 0 {
|
||||||
return 2 // []
|
return 2 // []
|
||||||
}
|
}
|
||||||
// surrounding brackets + ", " for every element - 1
|
// surrounding brackets + ", " for every element - 1
|
||||||
return TermSliceStringLength(a.elems, 2) + 2
|
return TermSliceStringLength(arr.elems, 2) + 2
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *object) StringLength() (n int) {
|
func (obj *object) StringLength() (n int) {
|
||||||
if o.Len() == 0 {
|
if obj.Len() == 0 {
|
||||||
return 2 // {}
|
return 2 // {}
|
||||||
}
|
}
|
||||||
// ": " for every item + ", " for every item - 1
|
// ": " for every item + ", " for every item - 1
|
||||||
o.Foreach(func(key, value *Term) {
|
obj.Foreach(func(key, value *Term) {
|
||||||
n += key.StringLength() + 4 + value.StringLength() // ": " and ", "
|
n += key.StringLength() + 4 + value.StringLength() // ": " and ", "
|
||||||
})
|
})
|
||||||
return n // surrounding {} but also minus last ", "
|
return n // surrounding {} but also minus last ", "
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) StringLength() int {
|
func (lob *lazyObj) StringLength() int {
|
||||||
return l.force().(*object).StringLength()
|
return lob.force().(*object).StringLength()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TemplateString) StringLength() (n int) {
|
func (ts *TemplateString) StringLength() (n int) {
|
||||||
@@ -138,23 +137,23 @@ func comprehensionTermStringLength(t *Term) int {
|
|||||||
return t.StringLength()
|
return t.StringLength()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Ref) StringLength() (n int) {
|
func (ref Ref) StringLength() (n int) {
|
||||||
rlen := len(r)
|
rlen := len(ref)
|
||||||
if rlen == 0 {
|
if rlen == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if s, ok := r[0].Value.(String); ok {
|
if s, ok := ref[0].Value.(String); ok {
|
||||||
n = len(s) // first term should never be quoted
|
n = len(s) // first term should never be quoted
|
||||||
} else {
|
} else {
|
||||||
n = r[0].StringLength()
|
n = ref[0].StringLength()
|
||||||
}
|
}
|
||||||
|
|
||||||
if rlen == 1 {
|
if rlen == 1 {
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, p := range r[1:] {
|
for _, p := range ref[1:] {
|
||||||
switch v := p.Value.(type) {
|
switch v := p.Value.(type) {
|
||||||
case String:
|
case String:
|
||||||
str := string(v)
|
str := string(v)
|
||||||
@@ -177,113 +176,113 @@ func (v Var) StringLength() int {
|
|||||||
return len(v)
|
return len(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SetComprehension) StringLength() int {
|
func (sc *SetComprehension) StringLength() int {
|
||||||
return comprehensionTermStringLength(s.Term) + s.Body.StringLength() + 5 // {} and " | "
|
return comprehensionTermStringLength(sc.Term) + sc.Body.StringLength() + 5 // {} and " | "
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArrayComprehension) StringLength() int {
|
func (ac *ArrayComprehension) StringLength() int {
|
||||||
return comprehensionTermStringLength(a.Term) + a.Body.StringLength() + 5 // [] and " | "
|
return comprehensionTermStringLength(ac.Term) + ac.Body.StringLength() + 5 // [] and " | "
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *ObjectComprehension) StringLength() (n int) {
|
func (oc *ObjectComprehension) StringLength() (n int) {
|
||||||
n += comprehensionTermStringLength(o.Key)
|
n += comprehensionTermStringLength(oc.Key)
|
||||||
n += comprehensionTermStringLength(o.Value)
|
n += comprehensionTermStringLength(oc.Value)
|
||||||
n += o.Body.StringLength()
|
n += oc.Body.StringLength()
|
||||||
return n + 7 // "{}"", " | ", and ": "
|
return n + 7 // "{}"", " | ", and ": "
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Module) StringLength() (n int) {
|
func (mod *Module) StringLength() (n int) {
|
||||||
if m.Package != nil {
|
if mod.Package != nil {
|
||||||
n += m.Package.StringLength() + 2 // newlines
|
n += mod.Package.StringLength() + 2 // newlines
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(m.Imports) > 0 {
|
if len(mod.Imports) > 0 {
|
||||||
for _, imp := range m.Imports {
|
for _, imp := range mod.Imports {
|
||||||
n += imp.StringLength() + 1 // newline
|
n += imp.StringLength() + 1 // newline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(m.Rules) > 0 {
|
if len(mod.Rules) > 0 {
|
||||||
for _, rule := range m.Rules {
|
for _, rule := range mod.Rules {
|
||||||
n += rule.stringLengthWithOpts(toStringOpts{regoVersion: m.regoVersion}) + 1 // newline
|
n += rule.stringLengthWithOpts(toStringOpts{regoVersion: mod.regoVersion}) + 1 // newline
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Package) StringLength() int {
|
func (pkg *Package) StringLength() int {
|
||||||
if p == nil {
|
if pkg == nil {
|
||||||
return 21 // <illegal nil package>
|
return 21 // <illegal nil package>
|
||||||
}
|
}
|
||||||
if len(p.Path) <= 1 {
|
if len(pkg.Path) <= 1 {
|
||||||
return 25 + p.Path.StringLength() // // package <illegal path " ... ">
|
return 25 + pkg.Path.StringLength() // // package <illegal path " ... ">
|
||||||
}
|
}
|
||||||
|
|
||||||
return 8 + p.Path[1:].StringLength() // "package ..."
|
return 8 + pkg.Path[1:].StringLength() // "package ..."
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Import) StringLength() (n int) {
|
func (imp *Import) StringLength() (n int) {
|
||||||
n = 7 + i.Path.StringLength() // "import " and path
|
n = 7 + imp.Path.StringLength() // "import " and path
|
||||||
if i.Alias != "" {
|
if imp.Alias != "" {
|
||||||
n += 4 + i.Alias.StringLength() // " as " and alias
|
n += 4 + imp.Alias.StringLength() // " as " and alias
|
||||||
}
|
}
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rule) StringLength() int {
|
func (rule *Rule) StringLength() int {
|
||||||
return r.stringLengthWithOpts(toStringOpts{})
|
return rule.stringLengthWithOpts(toStringOpts{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rule) stringLengthWithOpts(opts toStringOpts) int {
|
func (rule *Rule) stringLengthWithOpts(opts toStringOpts) int {
|
||||||
n := 0
|
n := 0
|
||||||
if r.Default {
|
if rule.Default {
|
||||||
n += 8 // "default "
|
n += 8 // "default "
|
||||||
}
|
}
|
||||||
n += r.Head.stringLengthWithOpts(opts)
|
n += rule.Head.stringLengthWithOpts(opts)
|
||||||
if !r.Default {
|
if !rule.Default {
|
||||||
switch opts.RegoVersion() {
|
switch opts.RegoVersion() {
|
||||||
case RegoV1, RegoV0CompatV1:
|
case RegoV1, RegoV0CompatV1:
|
||||||
n += 6 // " if { "
|
n += 6 // " if { "
|
||||||
default:
|
default:
|
||||||
n += 3 // " { "
|
n += 3 // " { "
|
||||||
}
|
}
|
||||||
n += r.Body.StringLength() + 2 // body and closing " }"
|
n += rule.Body.StringLength() + 2 // body and closing " }"
|
||||||
}
|
}
|
||||||
if r.Else != nil {
|
if rule.Else != nil {
|
||||||
n += r.Else.stringLengthWithOpts(opts)
|
n += rule.Else.stringLengthWithOpts(opts)
|
||||||
}
|
}
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Head) StringLength() int {
|
func (head *Head) StringLength() int {
|
||||||
return h.stringLengthWithOpts(toStringOpts{})
|
return head.stringLengthWithOpts(toStringOpts{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Head) stringLengthWithOpts(opts toStringOpts) int {
|
func (head *Head) stringLengthWithOpts(opts toStringOpts) int {
|
||||||
n := h.Reference.StringLength()
|
n := head.Reference.StringLength()
|
||||||
containsAdded := false
|
containsAdded := false
|
||||||
switch {
|
switch {
|
||||||
case len(h.Args) != 0:
|
case len(head.Args) != 0:
|
||||||
n += h.Args.StringLength()
|
n += head.Args.StringLength()
|
||||||
case len(h.Reference) == 1 && h.Key != nil:
|
case len(head.Reference) == 1 && head.Key != nil:
|
||||||
switch opts.RegoVersion() {
|
switch opts.RegoVersion() {
|
||||||
case RegoV0:
|
case RegoV0:
|
||||||
n += 2 + h.Key.StringLength() // for []
|
n += 2 + head.Key.StringLength() // for []
|
||||||
default:
|
default:
|
||||||
n += 10 + h.Key.StringLength() // " contains "
|
n += 10 + head.Key.StringLength() // " contains "
|
||||||
containsAdded = true
|
containsAdded = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if h.Value != nil {
|
if head.Value != nil {
|
||||||
if h.Assign {
|
if head.Assign {
|
||||||
n += 4 // " := "
|
n += 4 // " := "
|
||||||
} else {
|
} else {
|
||||||
n += 3 // " = "
|
n += 3 // " = "
|
||||||
}
|
}
|
||||||
n += h.Value.StringLength()
|
n += head.Value.StringLength()
|
||||||
} else if !containsAdded && h.Name == "" && h.Key != nil {
|
} else if !containsAdded && head.Name == "" && head.Key != nil {
|
||||||
n += 10 + h.Key.StringLength() // " contains "
|
n += 10 + head.Key.StringLength() // " contains "
|
||||||
}
|
}
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
@@ -296,20 +295,20 @@ func (a Args) StringLength() (n int) {
|
|||||||
return n - 2 // minus last ", "
|
return n - 2 // minus last ", "
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b Body) StringLength() (n int) {
|
func (body Body) StringLength() (n int) {
|
||||||
for _, expr := range b {
|
for _, expr := range body {
|
||||||
n += expr.StringLength() + 2 // "; "
|
n += expr.StringLength() + 2 // "; "
|
||||||
}
|
}
|
||||||
return max(n-2, 0) // minus last "; " (if `n` isn't 0)
|
return max(n-2, 0) // minus last "; " (if `n` isn't 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expr) StringLength() (n int) {
|
func (expr *Expr) StringLength() (n int) {
|
||||||
if e.Negated {
|
if expr.Negated {
|
||||||
n += 4 // "not "
|
n += 4 // "not "
|
||||||
}
|
}
|
||||||
switch terms := e.Terms.(type) {
|
switch terms := expr.Terms.(type) {
|
||||||
case []*Term:
|
case []*Term:
|
||||||
if e.IsEquality() && validEqAssignArgCount(e) {
|
if expr.IsEquality() && validEqAssignArgCount(expr) {
|
||||||
n += terms[1].StringLength() + len(Equality.Infix) + terms[2].StringLength() + 2 // spaces around =
|
n += terms[1].StringLength() + len(Equality.Infix) + terms[2].StringLength() + 2 // spaces around =
|
||||||
} else {
|
} else {
|
||||||
n += Call(terms).StringLength()
|
n += Call(terms).StringLength()
|
||||||
@@ -317,10 +316,10 @@ func (e *Expr) StringLength() (n int) {
|
|||||||
case StringLengther:
|
case StringLengther:
|
||||||
n += terms.StringLength()
|
n += terms.StringLength()
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("string length estimation not implemented for type: %T", e.Terms))
|
panic(fmt.Sprintf("string length estimation not implemented for type: %T", expr.Terms))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, w := range e.With {
|
for _, w := range expr.With {
|
||||||
n += w.StringLength() + 1 // space before with
|
n += w.StringLength() + 1 // space before with
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,20 +330,20 @@ func (w *With) StringLength() int {
|
|||||||
return w.Target.StringLength() + w.Value.StringLength() + 9 // "with " and " as "
|
return w.Target.StringLength() + w.Value.StringLength() + 9 // "with " and " as "
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Every) StringLength() int {
|
func (q *Every) StringLength() int {
|
||||||
n := 6 // "every "
|
n := 6 // "every "
|
||||||
if e.Key != nil {
|
if q.Key != nil {
|
||||||
n += e.Key.StringLength() + 2 // ", "
|
n += q.Key.StringLength() + 2 // ", "
|
||||||
}
|
}
|
||||||
n += e.Value.StringLength() + 4 // " in "
|
n += q.Value.StringLength() + 4 // " in "
|
||||||
n += e.Domain.StringLength() + 3 // " { "
|
n += q.Domain.StringLength() + 3 // " { "
|
||||||
n += e.Body.StringLength() + 2 // " }"
|
n += q.Body.StringLength() + 2 // " }"
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SomeDecl) StringLength() int {
|
func (d *SomeDecl) StringLength() int {
|
||||||
n := 5 // "some "
|
n := 5 // "some "
|
||||||
if call, ok := s.Symbols[0].Value.(Call); ok {
|
if call, ok := d.Symbols[0].Value.(Call); ok {
|
||||||
n += 4 // " in "
|
n += 4 // " in "
|
||||||
n += call[1].StringLength()
|
n += call[1].StringLength()
|
||||||
if len(call) == 4 {
|
if len(call) == 4 {
|
||||||
@@ -356,24 +355,24 @@ func (s *SomeDecl) StringLength() int {
|
|||||||
}
|
}
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
return n + TermSliceStringLength(s.Symbols, 2)
|
return n + TermSliceStringLength(d.Symbols, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Comment) StringLength() int {
|
func (c *Comment) StringLength() int {
|
||||||
return 1 + len(c.Text) // '#' + text
|
return 1 + len(c.Text) // '#' + text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (not *Not) StringLength() int {
|
func (n *Not) StringLength() int {
|
||||||
if !not.ExplicitBody && len(not.Body) == 1 {
|
if !n.ExplicitBody && len(n.Body) == 1 {
|
||||||
if notBodyNeedsParens(not.Body) {
|
if notBodyNeedsParens(n.Body) {
|
||||||
// "not (...)"
|
// "not (...)"
|
||||||
return 6 + not.Body.StringLength()
|
return 6 + n.Body.StringLength()
|
||||||
}
|
}
|
||||||
// "not ..."
|
// "not ..."
|
||||||
return 4 + not.Body.StringLength()
|
return 4 + n.Body.StringLength()
|
||||||
}
|
}
|
||||||
// "not {...}"
|
// "not {...}"
|
||||||
return 6 + not.Body.StringLength()
|
return 6 + n.Body.StringLength()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *LogicalAnd) StringLength() int {
|
func (a *LogicalAnd) StringLength() int {
|
||||||
|
|||||||
+56
-56
@@ -2042,72 +2042,72 @@ type lazyObj struct {
|
|||||||
native map[string]any
|
native map[string]any
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) force() Object {
|
func (lob *lazyObj) force() Object {
|
||||||
if l.strict == nil {
|
if lob.strict == nil {
|
||||||
l.strict = MustInterfaceToValue(l.native).(Object)
|
lob.strict = MustInterfaceToValue(lob.native).(Object)
|
||||||
// NOTE(jf): a possible performance improvement here would be to check how many
|
// NOTE(jf): a possible performance improvement here would be to check how many
|
||||||
// entries have been realized to AST in the cache, and if some threshold compared to the
|
// entries have been realized to AST in the cache, and if some threshold compared to the
|
||||||
// total number of keys is exceeded, realize the remaining entries and set l.strict to l.cache.
|
// total number of keys is exceeded, realize the remaining entries and set l.strict to l.cache.
|
||||||
l.cache = map[string]Value{} // We don't need the cache anymore; drop it to free up memory.
|
lob.cache = map[string]Value{} // We don't need the cache anymore; drop it to free up memory.
|
||||||
}
|
}
|
||||||
return l.strict
|
return lob.strict
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Compare(other Value) int {
|
func (lob *lazyObj) Compare(other Value) int {
|
||||||
if c := valueTypeCompare(l, other); c != 0 {
|
if c := valueTypeCompare(lob, other); c != 0 {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
return l.force().Compare(other)
|
return lob.force().Compare(other)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Copy() Object {
|
func (lob *lazyObj) Copy() Object {
|
||||||
return l
|
return lob
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Diff(other Object) Object {
|
func (lob *lazyObj) Diff(other Object) Object {
|
||||||
return l.force().Diff(other)
|
return lob.force().Diff(other)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Intersect(other Object) [][3]*Term {
|
func (lob *lazyObj) Intersect(other Object) [][3]*Term {
|
||||||
return l.force().Intersect(other)
|
return lob.force().Intersect(other)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Iter(f func(*Term, *Term) error) error {
|
func (lob *lazyObj) Iter(f func(*Term, *Term) error) error {
|
||||||
return l.force().Iter(f)
|
return lob.force().Iter(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Until(f func(*Term, *Term) bool) bool {
|
func (lob *lazyObj) Until(f func(*Term, *Term) bool) bool {
|
||||||
// NOTE(sr): there could be benefits in not forcing here -- if we abort because
|
// NOTE(sr): there could be benefits in not forcing here -- if we abort because
|
||||||
// `f` returns true, we could save us from converting the rest of the object.
|
// `f` returns true, we could save us from converting the rest of the object.
|
||||||
return l.force().Until(f)
|
return lob.force().Until(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Foreach(f func(*Term, *Term)) {
|
func (lob *lazyObj) Foreach(f func(*Term, *Term)) {
|
||||||
l.force().Foreach(f)
|
lob.force().Foreach(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Filter(filter Object) (Object, error) {
|
func (lob *lazyObj) Filter(filter Object) (Object, error) {
|
||||||
return l.force().Filter(filter)
|
return lob.force().Filter(filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Map(f func(*Term, *Term) (*Term, *Term, error)) (Object, error) {
|
func (lob *lazyObj) Map(f func(*Term, *Term) (*Term, *Term, error)) (Object, error) {
|
||||||
return l.force().Map(f)
|
return lob.force().Map(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Merge(other Object) (Object, bool) {
|
func (lob *lazyObj) Merge(other Object) (Object, bool) {
|
||||||
return l.force().Merge(other)
|
return lob.force().Merge(other)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) MergeWith(other Object, conflictResolver func(v1, v2 *Term) (*Term, bool)) (Object, bool) {
|
func (lob *lazyObj) MergeWith(other Object, conflictResolver func(v1, v2 *Term) (*Term, bool)) (Object, bool) {
|
||||||
return l.force().MergeWith(other, conflictResolver)
|
return lob.force().MergeWith(other, conflictResolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Len() int {
|
func (lob *lazyObj) Len() int {
|
||||||
return len(l.native)
|
return len(lob.native)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) String() string {
|
func (lob *lazyObj) String() string {
|
||||||
return l.force().String()
|
return lob.force().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// get is merely there to implement the Object interface -- `get` there serves the
|
// get is merely there to implement the Object interface -- `get` there serves the
|
||||||
@@ -2116,16 +2116,16 @@ func (*lazyObj) get(*Term) *objectElem {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Get(k *Term) *Term {
|
func (lob *lazyObj) Get(k *Term) *Term {
|
||||||
if l.strict != nil {
|
if lob.strict != nil {
|
||||||
return l.strict.Get(k)
|
return lob.strict.Get(k)
|
||||||
}
|
}
|
||||||
if s, ok := k.Value.(String); ok {
|
if s, ok := k.Value.(String); ok {
|
||||||
if v, ok := l.cache[string(s)]; ok {
|
if v, ok := lob.cache[string(s)]; ok {
|
||||||
return NewTerm(v)
|
return NewTerm(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
if val, ok := l.native[string(s)]; ok {
|
if val, ok := lob.native[string(s)]; ok {
|
||||||
var converted Value
|
var converted Value
|
||||||
switch val := val.(type) {
|
switch val := val.(type) {
|
||||||
case map[string]any:
|
case map[string]any:
|
||||||
@@ -2133,31 +2133,31 @@ func (l *lazyObj) Get(k *Term) *Term {
|
|||||||
default:
|
default:
|
||||||
converted = MustInterfaceToValue(val)
|
converted = MustInterfaceToValue(val)
|
||||||
}
|
}
|
||||||
l.cache[string(s)] = converted
|
lob.cache[string(s)] = converted
|
||||||
return NewTerm(converted)
|
return NewTerm(converted)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Insert(k, v *Term) {
|
func (lob *lazyObj) Insert(k, v *Term) {
|
||||||
l.force().Insert(k, v)
|
lob.force().Insert(k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*lazyObj) IsGround() bool {
|
func (*lazyObj) IsGround() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Hash() int {
|
func (lob *lazyObj) Hash() int {
|
||||||
return l.force().Hash()
|
return lob.force().Hash()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Keys() []*Term {
|
func (lob *lazyObj) Keys() []*Term {
|
||||||
if l.strict != nil {
|
if lob.strict != nil {
|
||||||
return l.strict.Keys()
|
return lob.strict.Keys()
|
||||||
}
|
}
|
||||||
ret := make([]*Term, 0, len(l.native))
|
ret := make([]*Term, 0, len(lob.native))
|
||||||
for k := range l.native {
|
for k := range lob.native {
|
||||||
ret = append(ret, StringTerm(k))
|
ret = append(ret, StringTerm(k))
|
||||||
}
|
}
|
||||||
slices.SortFunc(ret, TermValueCompare)
|
slices.SortFunc(ret, TermValueCompare)
|
||||||
@@ -2165,8 +2165,8 @@ func (l *lazyObj) Keys() []*Term {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) KeysIterator() ObjectKeysIterator {
|
func (lob *lazyObj) KeysIterator() ObjectKeysIterator {
|
||||||
return &lazyObjKeysIterator{keys: l.Keys()}
|
return &lazyObjKeysIterator{keys: lob.Keys()}
|
||||||
}
|
}
|
||||||
|
|
||||||
type lazyObjKeysIterator struct {
|
type lazyObjKeysIterator struct {
|
||||||
@@ -2182,19 +2182,19 @@ func (ki *lazyObjKeysIterator) Next() (*Term, bool) {
|
|||||||
return ki.keys[ki.current-1], true
|
return ki.keys[ki.current-1], true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) Find(path Ref) (Value, error) {
|
func (lob *lazyObj) Find(path Ref) (Value, error) {
|
||||||
if l.strict != nil {
|
if lob.strict != nil {
|
||||||
return l.strict.Find(path)
|
return lob.strict.Find(path)
|
||||||
}
|
}
|
||||||
if len(path) == 0 {
|
if len(path) == 0 {
|
||||||
return l, nil
|
return lob, nil
|
||||||
}
|
}
|
||||||
if p0, ok := path[0].Value.(String); ok {
|
if p0, ok := path[0].Value.(String); ok {
|
||||||
if v, ok := l.cache[string(p0)]; ok {
|
if v, ok := lob.cache[string(p0)]; ok {
|
||||||
return v.Find(path[1:])
|
return v.Find(path[1:])
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := l.native[string(p0)]; ok {
|
if v, ok := lob.native[string(p0)]; ok {
|
||||||
var converted Value
|
var converted Value
|
||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
case map[string]any:
|
case map[string]any:
|
||||||
@@ -2202,7 +2202,7 @@ func (l *lazyObj) Find(path Ref) (Value, error) {
|
|||||||
default:
|
default:
|
||||||
converted = MustInterfaceToValue(v)
|
converted = MustInterfaceToValue(v)
|
||||||
}
|
}
|
||||||
l.cache[string(p0)] = converted
|
lob.cache[string(p0)] = converted
|
||||||
return converted.Find(path[1:])
|
return converted.Find(path[1:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-18
@@ -26,8 +26,8 @@ func (v Var) AppendText(buf []byte) ([]byte, error) {
|
|||||||
return append(buf, v...), nil
|
return append(buf, v...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b Boolean) AppendText(buf []byte) ([]byte, error) {
|
func (bol Boolean) AppendText(buf []byte) ([]byte, error) {
|
||||||
if b {
|
if bol {
|
||||||
return append(buf, "true"...), nil
|
return append(buf, "true"...), nil
|
||||||
}
|
}
|
||||||
return append(buf, "false"...), nil
|
return append(buf, "false"...), nil
|
||||||
@@ -92,8 +92,8 @@ func (obj *object) AppendText(buf []byte) ([]byte, error) {
|
|||||||
return append(buf, '}'), nil
|
return append(buf, '}'), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (obj *lazyObj) AppendText(buf []byte) ([]byte, error) {
|
func (lob *lazyObj) AppendText(buf []byte) ([]byte, error) {
|
||||||
return append(buf, obj.force().String()...), nil
|
return append(buf, lob.force().String()...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *set) AppendText(buf []byte) ([]byte, error) {
|
func (s *set) AppendText(buf []byte) ([]byte, error) {
|
||||||
@@ -173,30 +173,30 @@ func (ts *TemplateString) AppendText(buf []byte) ([]byte, error) {
|
|||||||
return append(buf, '"'), nil
|
return append(buf, '"'), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Ref) AppendText(buf []byte) ([]byte, error) {
|
func (ref Ref) AppendText(buf []byte) ([]byte, error) {
|
||||||
reflen := len(r)
|
reflen := len(ref)
|
||||||
if reflen == 0 {
|
if reflen == 0 {
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
if reflen == 1 {
|
if reflen == 1 {
|
||||||
if s, ok := r[0].Value.(String); ok {
|
if s, ok := ref[0].Value.(String); ok {
|
||||||
// While a ref head is typically a Var, a lone String term should not be quoted
|
// While a ref head is typically a Var, a lone String term should not be quoted
|
||||||
return append(buf, s...), nil
|
return append(buf, s...), nil
|
||||||
}
|
}
|
||||||
return r[0].AppendText(buf)
|
return ref[0].AppendText(buf)
|
||||||
}
|
}
|
||||||
if name, ok := BuiltinNameFromRef(r); ok {
|
if name, ok := BuiltinNameFromRef(ref); ok {
|
||||||
return append(buf, name...), nil
|
return append(buf, name...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if s, ok := r[0].Value.(String); ok {
|
if s, ok := ref[0].Value.(String); ok {
|
||||||
buf = append(buf, s...)
|
buf = append(buf, s...)
|
||||||
} else if buf, err = r[0].AppendText(buf); err != nil {
|
} else if buf, err = ref[0].AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, p := range r[1:] {
|
for _, p := range ref[1:] {
|
||||||
switch v := p.Value.(type) {
|
switch v := p.Value.(type) {
|
||||||
case String:
|
case String:
|
||||||
str := string(v)
|
str := string(v)
|
||||||
@@ -287,23 +287,23 @@ func appendComprehensionTerm(buf []byte, term *Term) ([]byte, error) {
|
|||||||
return term.AppendText(buf)
|
return term.AppendText(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (not *Not) AppendText(buf []byte) ([]byte, error) {
|
func (n *Not) AppendText(buf []byte) ([]byte, error) {
|
||||||
if !not.ExplicitBody && len(not.Body) == 1 {
|
if !n.ExplicitBody && len(n.Body) == 1 {
|
||||||
if notBodyNeedsParens(not.Body) {
|
if notBodyNeedsParens(n.Body) {
|
||||||
buf = append(buf, "not ("...)
|
buf = append(buf, "not ("...)
|
||||||
var err error
|
var err error
|
||||||
if buf, err = not.Body.AppendText(buf); err != nil {
|
if buf, err = n.Body.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return append(buf, ')'), nil
|
return append(buf, ')'), nil
|
||||||
}
|
}
|
||||||
buf = append(buf, "not "...)
|
buf = append(buf, "not "...)
|
||||||
return not.Body.AppendText(buf)
|
return n.Body.AppendText(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = append(buf, "not {"...)
|
buf = append(buf, "not {"...)
|
||||||
var err error
|
var err error
|
||||||
if buf, err = not.Body.AppendText(buf); err != nil {
|
if buf, err = n.Body.AppendText(buf); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return append(buf, '}'), nil
|
return append(buf, '}'), nil
|
||||||
|
|||||||
+2
-2
@@ -65,8 +65,8 @@ func (s *set) MarshalJSON() ([]byte, error) {
|
|||||||
return json.Marshal(s.sortedKeys())
|
return json.Marshal(s.sortedKeys())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lazyObj) MarshalJSON() ([]byte, error) {
|
func (lob *lazyObj) MarshalJSON() ([]byte, error) {
|
||||||
return l.force().(*object).MarshalJSON()
|
return lob.force().(*object).MarshalJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Not) MarshalJSON() ([]byte, error) {
|
func (n *Not) MarshalJSON() ([]byte, error) {
|
||||||
|
|||||||
+10
-10
@@ -80,24 +80,24 @@ func (str String) MarshalJSONTo(e *jsontext.Encoder) error {
|
|||||||
return e.WriteToken(jsontext.String(string(str)))
|
return e.WriteToken(jsontext.String(string(str)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Term) MarshalJSONTo(e *jsontext.Encoder) (err error) {
|
func (term *Term) MarshalJSONTo(e *jsontext.Encoder) (err error) {
|
||||||
// Token write errors are unchecked: an unbalanced value fails at the closing
|
// Token write errors are unchecked: an unbalanced value fails at the closing
|
||||||
// token. A marshaller can fail having written a balanced value, so is checked.
|
// token. A marshaller can fail having written a balanced value, so is checked.
|
||||||
e.WriteToken(jsontext.BeginObject)
|
e.WriteToken(jsontext.BeginObject)
|
||||||
|
|
||||||
includeLocation := astJSON.GetOptions().MarshalOptions.IncludeLocation
|
includeLocation := astJSON.GetOptions().MarshalOptions.IncludeLocation
|
||||||
if t.Location != nil && includeLocation.Term {
|
if term.Location != nil && includeLocation.Term {
|
||||||
if err := jsonv2.WriteField(e, "location", t.Location); err != nil {
|
if err := jsonv2.WriteField(e, "location", term.Location); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
e.WriteToken(jsontext.String("type"))
|
e.WriteToken(jsontext.String("type"))
|
||||||
e.WriteToken(jsontext.String(ValueName(t.Value)))
|
e.WriteToken(jsontext.String(ValueName(term.Value)))
|
||||||
|
|
||||||
e.WriteToken(jsontext.String("value"))
|
e.WriteToken(jsontext.String("value"))
|
||||||
if err = marshalValueTo(e, t.Value); err != nil {
|
if err = marshalValueTo(e, term.Value); err != nil {
|
||||||
return fmt.Errorf("failed to marshal term of %s: %w", ValueName(t.Value), err)
|
return fmt.Errorf("failed to marshal term of %s: %w", ValueName(term.Value), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.WriteToken(jsontext.EndObject)
|
return e.WriteToken(jsontext.EndObject)
|
||||||
@@ -108,8 +108,8 @@ func (term *Term) MarshalJSON() ([]byte, error) {
|
|||||||
return jsonv2.MarshalMarshalerTo(term)
|
return jsonv2.MarshalMarshalerTo(term)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Ref) MarshalJSONTo(e *jsontext.Encoder) (err error) {
|
func (ref Ref) MarshalJSONTo(e *jsontext.Encoder) (err error) {
|
||||||
return jsonv2.WriteMarshalerToArrayOrNull(e, r)
|
return jsonv2.WriteMarshalerToArrayOrNull(e, ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TemplateString) MarshalJSONTo(e *jsontext.Encoder) (err error) {
|
func (t *TemplateString) MarshalJSONTo(e *jsontext.Encoder) (err error) {
|
||||||
@@ -210,8 +210,8 @@ func (obj *object) MarshalJSON() ([]byte, error) {
|
|||||||
return jsonv2.MarshalMarshalerTo(obj)
|
return jsonv2.MarshalMarshalerTo(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Array) MarshalJSONTo(e *jsontext.Encoder) error {
|
func (arr *Array) MarshalJSONTo(e *jsontext.Encoder) error {
|
||||||
return jsonv2.WriteMarshalerToArray(e, a.elems)
|
return jsonv2.WriteMarshalerToArray(e, arr.elems)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON returns JSON encoded bytes representing arr.
|
// MarshalJSON returns JSON encoded bytes representing arr.
|
||||||
|
|||||||
@@ -211,9 +211,9 @@ func newKeyVaultSignPlugin(ap *azureManagedIdentitiesAuthPlugin, cfg *azureKeyVa
|
|||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (akv *azureKeyVaultSignPlugin) setDefaults() {
|
func (ap *azureKeyVaultSignPlugin) setDefaults() {
|
||||||
if akv.config.APIVersion == "" {
|
if ap.config.APIVersion == "" {
|
||||||
akv.config.APIVersion = defaultKeyVaultAPIVersion
|
ap.config.APIVersion = defaultKeyVaultAPIVersion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user