mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
2378494a23
Mostly automated fixes from running: ``` go run golang.org/x/tools/go/analysis/passes/modernize/cmd/modernize@latest --fix ./... ``` But carefully reviewed, and several fixes reverted as they looked like they potentially could be less performant, and in a few cases due to bugs in the analyzer that changed semantics of the code. Will report these upstream. Mostly good fixes though! Signed-off-by: Anders Eknert <anders.eknert@apple.com>
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package ast
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/open-policy-agent/opa/v1/util"
|
|
)
|
|
|
|
// Dump returns a string representation of the tree structure rooted at this node.
|
|
func (n *TreeNode) Dump() string {
|
|
var sb strings.Builder
|
|
n.dumpRecursive(&sb, "", "")
|
|
return sb.String()
|
|
}
|
|
|
|
func (n *TreeNode) dumpRecursive(sb *strings.Builder, prefix, childPrefix string) {
|
|
sb.WriteString(prefix)
|
|
fmt.Fprintf(sb, "%v", n.Key)
|
|
|
|
if n.Hide {
|
|
sb.WriteString(" [hidden]")
|
|
}
|
|
if n.External != nil {
|
|
fmt.Fprintf(sb, " ext:%v", n.External.Ref)
|
|
}
|
|
if len(n.Values) > 0 {
|
|
sb.WriteString(" rules:")
|
|
util.WriteInt(sb, len(n.Values))
|
|
}
|
|
sb.WriteByte('\n')
|
|
|
|
if len(n.Children) == 0 {
|
|
return
|
|
}
|
|
|
|
keys := make([]Value, 0, len(n.Children))
|
|
for k := range n.Children {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
return Compare(keys[i], keys[j]) < 0
|
|
})
|
|
|
|
for i, key := range keys {
|
|
child := n.Children[key]
|
|
isLast := i == len(keys)-1
|
|
var newPrefix, newChildPrefix string
|
|
if isLast {
|
|
newPrefix = childPrefix + "└── "
|
|
newChildPrefix = childPrefix + " "
|
|
} else {
|
|
newPrefix = childPrefix + "├── "
|
|
newChildPrefix = childPrefix + "│ "
|
|
}
|
|
child.dumpRecursive(sb, newPrefix, newChildPrefix)
|
|
}
|
|
}
|