File size: 4,799 Bytes
fea99b3 | 1 2 3 4 5 6 7 8 9 10 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | package models
import (
"encoding/json"
"strings"
"github.com/samber/lo"
"github.com/openmeterio/openmeter/pkg/treex"
)
type FieldDescriptor struct {
field string
exp FieldExpression
attrs Attributes
node *treex.Node[*FieldDescriptor]
}
func (s FieldDescriptor) Clone() *FieldDescriptor {
desc := &FieldDescriptor{
field: s.field,
exp: s.exp,
attrs: s.attrs.Clone(),
node: s.node.ShallowClone(),
}
desc.node.SetValue(desc)
return desc
}
func (s FieldDescriptor) WithExpression(exp FieldExpression) *FieldDescriptor {
s.exp = exp
s.node = s.node.ShallowClone()
s.node.SetValue(&s)
return &s
}
func (s FieldDescriptor) WithAttributes(attrs Attributes) *FieldDescriptor {
curr := s.attrs
if curr == nil {
curr = make(Attributes)
}
s.attrs = curr.Merge(attrs)
s.node = s.node.ShallowClone()
s.node.SetValue(&s)
return &s
}
func (p FieldDescriptor) GetAttributes() Attributes {
return p.attrs
}
func (p FieldDescriptor) WithPrefix(_prefix *FieldDescriptor) *FieldDescriptor {
var prefix *FieldDescriptor
if _prefix != nil {
prefix = _prefix.Clone()
}
p.node = p.node.ShallowClone()
p.node.SetValue(&p)
if prefix == nil {
return NewFieldSelectorGroup(&p)
}
return NewFieldSelectorGroup(prefix, &p)
}
func (p *FieldDescriptor) MarshalJSON() ([]byte, error) {
if p == nil {
return json.Marshal("")
}
return json.Marshal(p.JSONPath())
}
func (p *FieldDescriptor) String() string {
if p == nil {
return ""
}
b := strings.Builder{}
// We'll use a DFS traversal to build the string
if err := p.Tree(func(t *FieldDescriptorTree) error {
leafCount := 0
return t.DFS(func(n *treex.Node[*FieldDescriptor]) (bool, error) {
// Only leaf nodes (childrenless segments with field names) make up the path, the rest of the graph is just hierarchical information
if n.IsLeaf() {
desc := n.Value()
if leafCount > 0 {
b.WriteString(".")
}
b.WriteString(n.Value().field)
if desc.exp != nil {
if exp := desc.exp.String(); exp != "" {
b.WriteString("[")
b.WriteString(exp)
b.WriteString("]")
}
}
leafCount++
}
return false, nil
})
}); err != nil {
return ""
}
return b.String()
}
func (p *FieldDescriptor) JSONPath() string {
if p == nil {
return ""
}
b := strings.Builder{}
// Tree.Root().IsRoot() is always true so we have to make this assertion here
if p.node.IsRoot() {
b.WriteString("$")
b.WriteString(".")
}
if err := p.Tree(func(t *FieldDescriptorTree) error {
leafCount := 0
return t.DFS(func(n *treex.Node[*FieldDescriptor]) (bool, error) {
// Only leaf nodes (childrenless segments with field names) make up the path, the rest of the graph is just hierarchical information
if n.IsLeaf() {
desc := n.Value()
if leafCount > 0 {
b.WriteString(".")
}
b.WriteString(desc.field)
if desc.exp != nil {
expOpen := "["
expClose := "]"
if desc.exp.IsCondition() {
expOpen = "[?("
expClose = ")]"
}
if exp := desc.exp.JSONPathExpression(); exp != "" {
b.WriteString(expOpen)
b.WriteString(exp)
b.WriteString(expClose)
}
}
leafCount++
}
return false, nil
})
}); err != nil {
return ""
}
return b.String()
}
// FieldDescriptorTree is a wrapper around treex.Tree[*FieldDescriptor]
// with methods meaningful for a FieldDescriptor
type FieldDescriptorTree struct {
*treex.Tree[*FieldDescriptor]
}
// If called while walking the tree, you MUST start backtracking (return true)
// otherwise you'll keep walking the detached subtree!
func (t *FieldDescriptorTree) Swap(old, new *FieldDescriptor) error {
return t.Tree.SwapNode(old.node, new.node)
}
func (t *FieldDescriptorTree) Leafs() []*FieldDescriptor {
return lo.Map(t.Tree.Leafs(), func(n *treex.Node[*FieldDescriptor], _ int) *FieldDescriptor {
return n.Value()
})
}
// Tree returns a treex.Tree[*FieldDescriptor] from the FieldDescriptor so it can be traversed
func (s FieldDescriptor) Tree(cb func(t *FieldDescriptorTree) error) error {
tree, err := treex.NewTree(s.node)
if err != nil {
return err
}
return cb(&FieldDescriptorTree{Tree: tree})
}
func newFieldDescriptor() *FieldDescriptor {
desc := &FieldDescriptor{}
desc.node = treex.NewNode(desc)
return desc
}
func NewFieldSelector(field string) *FieldDescriptor {
desc := newFieldDescriptor()
desc.field = field
return desc
}
func NewFieldSelectorGroup(selectors ...*FieldDescriptor) *FieldDescriptor {
selectors = lo.Filter(selectors, func(item *FieldDescriptor, _ int) bool {
return item != nil
})
if len(selectors) == 0 {
return nil
}
desc := newFieldDescriptor()
for i := range selectors {
nd := selectors[i].node
desc.node.AddChild(nd)
}
return desc
}
|