-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode.go
More file actions
304 lines (273 loc) · 8.31 KB
/
Copy pathnode.go
File metadata and controls
304 lines (273 loc) · 8.31 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package protobufquery
import (
"bytes"
"google.golang.org/protobuf/reflect/protoreflect"
)
// A NodeType is the type of a Node.
type NodeType uint
const (
// DocumentNode is a document object that, as the root of the document tree,
// provides access to the entire XML document.
DocumentNode NodeType = iota
// ElementNode is an element.
ElementNode
// TextNode is the text content of a node.
TextNode
)
// A Node consists of a NodeType and some Data (tag name for
// element nodes, content for text) and are part of a tree of Nodes.
type Node struct {
Parent, PrevSibling, NextSibling, FirstChild, LastChild *Node
Type NodeType
Name string
Data *protoreflect.Value
level int
}
// Options controls the behavior of protobuf parsing
type Options struct {
// PopulateDefaults determines whether to populate default values for proto3 messages
PopulateDefaults bool
}
// DefaultOptions provides sensible defaults for parsing
// PopulateDefaults is false by default to maintain backward compatibility
var DefaultOptions = Options{
PopulateDefaults: false, // Preserve existing behavior by default
}
// ChildNodes gets all child nodes of the node.
func (n *Node) ChildNodes() []*Node {
var a []*Node
for nn := n.FirstChild; nn != nil; nn = nn.NextSibling {
a = append(a, nn)
}
return a
}
// InnerText gets the value of the node and all its child nodes.
func (n *Node) InnerText() string {
var output func(*bytes.Buffer, *Node)
output = func(buf *bytes.Buffer, n *Node) {
if n.Type == TextNode {
buf.WriteString(n.Data.String())
return
}
for child := n.FirstChild; child != nil; child = child.NextSibling {
output(buf, child)
}
}
var buf bytes.Buffer
output(&buf, n)
return buf.String()
}
func outputXML(buf *bytes.Buffer, n *Node) {
if n.Type == TextNode {
buf.WriteString(n.Data.String())
return
}
name := "element"
if n.Name != "" {
name = n.Name
}
buf.WriteString("<" + name + ">")
for child := n.FirstChild; child != nil; child = child.NextSibling {
outputXML(buf, child)
}
buf.WriteString("</" + name + ">")
}
// OutputXML prints the XML string.
func (n *Node) OutputXML() string {
var buf bytes.Buffer
buf.WriteString(`<?xml version="1.0"?>`)
for n := n.FirstChild; n != nil; n = n.NextSibling {
outputXML(&buf, n)
}
return buf.String()
}
// SelectElement finds the first of child elements with the
// specified name.
func (n *Node) SelectElement(name string) *Node {
for nn := n.FirstChild; nn != nil; nn = nn.NextSibling {
if nn.Name == name {
return nn
}
}
return nil
}
// Value return the value of the node itself or its 'TextElement' children.
// If `nil`, the value is either really `nil` or there is no matching child.
func (n *Node) Value() interface{} {
if n.Type == TextNode {
if n.Data == nil {
return nil
}
return n.Data.Interface()
}
result := make([]interface{}, 0)
for child := n.FirstChild; child != nil; child = child.NextSibling {
if child.Type != TextNode || child.Data == nil {
continue
}
result = append(result, child.Data.Interface())
}
if len(result) == 0 {
return nil
} else if len(result) == 1 {
return result[0]
}
return result
}
// Parse ProtocolBuffer message with default options.
func Parse(msg protoreflect.Message) (*Node, error) {
return ParseWithOptions(msg, DefaultOptions)
}
// ParseWithOptions parses a ProtocolBuffer message with custom options.
func ParseWithOptions(msg protoreflect.Message, opts Options) (*Node, error) {
doc := &Node{Type: DocumentNode}
visit(doc, msg, 1, opts)
return doc, nil
}
func visit(parent *Node, msg protoreflect.Message, level int, opts Options) {
desc := msg.Descriptor()
fields := desc.Fields()
// Process all fields in the message descriptor, not just present ones
for i := 0; i < fields.Len(); i++ {
field := fields.Get(i)
if msg.Has(field) {
// Field is present, use its actual value
value := msg.Get(field)
traverse(parent, field, value, level, opts)
} else if opts.PopulateDefaults && desc.Syntax() == protoreflect.Proto3 {
// Field is not present in proto3, use default value
defaultValue := getDefaultValue(field)
if defaultValue.IsValid() {
traverse(parent, field, defaultValue, level, opts)
}
}
// For proto2 or when PopulateDefaults is false, skip unset fields (existing behavior)
}
}
func traverse(parent *Node, field protoreflect.FieldDescriptor, value protoreflect.Value, level int, opts Options) {
node := &Node{Type: ElementNode, Name: string(field.Name()), level: level}
nodeChildren := 0
switch {
case field.IsList():
l := value.List()
for i := 0; i < l.Len(); i++ {
subNode := handleValue(field.Kind(), l.Get(i), level+1, opts)
if subNode.Type == ElementNode {
// Add element nodes directly to the parent
subNode.Name = node.Name
addNode(parent, subNode)
} else {
// Add basic nodes to the local collection node
elementNode := &Node{Type: ElementNode, level: level + 1}
subNode.level += 2
addNode(elementNode, subNode)
addNode(node, elementNode)
nodeChildren++
}
}
case field.IsMap():
key := field.MapKey()
value.Map().Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
subNode := handleValue(key.Kind(), v, level+1, opts)
if subNode.Type == ElementNode {
// Add element nodes directly to the parent
subNode.Name = k.String()
addNode(parent, subNode)
} else {
// Add basic nodes to the local collection node
elementNode := &Node{Type: ElementNode, Name: k.String(), level: level + 1}
subNode.level += 2
addNode(elementNode, subNode)
addNode(node, elementNode)
nodeChildren++
}
return true
})
default:
subNode := handleValue(field.Kind(), value, level+1, opts)
if subNode.Type == ElementNode {
// Add element nodes directly to the parent
subNode.Name = node.Name
addNode(parent, subNode)
} else {
// Add basic nodes to the local collection node
addNode(node, subNode)
nodeChildren++
}
}
// Only add the node if it has children
if nodeChildren > 0 {
addNode(parent, node)
}
}
func handleValue(kind protoreflect.Kind, value protoreflect.Value, level int, opts Options) *Node {
var node *Node
switch kind {
case protoreflect.MessageKind:
node = &Node{Type: ElementNode, level: level}
visit(node, value.Message(), level+1, opts)
default:
node = &Node{Type: TextNode, Data: &value, level: level}
}
return node
}
func addNode(top, n *Node) {
if n.level == top.level {
top.NextSibling = n
n.PrevSibling = top
n.Parent = top.Parent
if top.Parent != nil {
top.Parent.LastChild = n
}
} else if n.level > top.level {
n.Parent = top
if top.FirstChild == nil {
top.FirstChild = n
top.LastChild = n
} else {
t := top.LastChild
t.NextSibling = n
n.PrevSibling = t
top.LastChild = n
}
}
}
// getDefaultValue returns the appropriate default value for a field
func getDefaultValue(field protoreflect.FieldDescriptor) protoreflect.Value {
// Skip lists and maps - they're empty by default and don't need explicit defaults
if field.IsList() || field.IsMap() {
return protoreflect.Value{}
}
switch field.Kind() {
case protoreflect.BoolKind:
return protoreflect.ValueOfBool(false)
case protoreflect.EnumKind:
enumDesc := field.Enum()
if enumDesc.Values().Len() > 0 {
return protoreflect.ValueOfEnum(enumDesc.Values().Get(0).Number())
}
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
return protoreflect.ValueOfInt32(0)
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
return protoreflect.ValueOfInt64(0)
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
return protoreflect.ValueOfUint32(0)
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
return protoreflect.ValueOfUint64(0)
case protoreflect.FloatKind:
return protoreflect.ValueOfFloat32(0.0)
case protoreflect.DoubleKind:
return protoreflect.ValueOfFloat64(0.0)
case protoreflect.StringKind:
return protoreflect.ValueOfString("")
case protoreflect.BytesKind:
return protoreflect.ValueOfBytes([]byte{})
case protoreflect.MessageKind, protoreflect.GroupKind:
// For nested messages, we don't populate defaults automatically
// to avoid infinite recursion and unnecessary complexity.
// If users need nested message defaults, they can call ParseWithOptions
// on the nested message separately.
return protoreflect.Value{}
}
return protoreflect.Value{}
}