Repository navigation
Expand file tree
/
Copy pathparser.go
More file actions
191 lines (172 loc) · 4.48 KB
/
Copy pathparser.go
File metadata and controls
191 lines (172 loc) · 4.48 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
package dbgp
import (
"bytes"
"encoding/base64"
"encoding/xml"
"fmt"
"strings"
"unicode/utf8"
)
// Variable represents a parsed variable
type Variable struct {
Name string
Type string
Value string
Level int // nesting level for indentation
}
// ContextResponse wraps the XML response for context_get
type ContextResponse struct {
XMLName xml.Name `xml:"response"`
Properties []Property `xml:"property"`
}
// ParseVariables extracts top-level variables and their immediate children from a DBGp context_get response
func ParseVariables(xmlData string) []Variable {
var resp ContextResponse
decoder := xml.NewDecoder(bytes.NewReader([]byte(xmlData)))
decoder.CharsetReader = charsetReader
if err := decoder.Decode(&resp); err != nil {
return nil
}
return ParseVariablesFromProperties(resp.Properties)
}
// ParseVariablesFromProperties extracts top-level variables and their immediate children from parsed properties
func ParseVariablesFromProperties(properties []Property) []Variable {
var vars []Variable
for _, p := range properties {
vars = append(vars, Variable{
Name: displayName(p),
Type: p.Type,
Value: formatPropertyValue(p),
Level: 0,
})
// Add immediate child properties for objects/arrays
for _, child := range p.ChildProperties {
vars = append(vars, Variable{
Name: displayName(child),
Type: child.Type,
Value: formatPropertyValue(child),
Level: 1,
})
}
}
return vars
}
// displayName is a property's full name, or its name when it has none
// (e.g. the children of an eval result).
func displayName(p Property) string {
if p.FullName != "" {
return p.FullName
}
return p.Name
}
// ParseAllVariables extracts all variables including nested ones
func ParseAllVariables(xmlData string) []Variable {
var resp ContextResponse
decoder := xml.NewDecoder(bytes.NewReader([]byte(xmlData)))
decoder.CharsetReader = charsetReader
if err := decoder.Decode(&resp); err != nil {
return nil
}
return flattenProperties(resp.Properties, 0)
}
// flattenProperties recursively flattens nested properties
func flattenProperties(props []Property, level int) []Variable {
var vars []Variable
for _, p := range props {
vars = append(vars, Variable{
Name: displayName(p),
Type: p.Type,
Value: formatPropertyValue(p),
Level: level,
})
if len(p.ChildProperties) > 0 {
vars = append(vars, flattenProperties(p.ChildProperties, level+1)...)
}
}
return vars
}
// formatPropertyValue formats a property value for display
func formatPropertyValue(p Property) string {
// Handle types with children (check Children attr, ChildProperties, or raw XML in Value)
hasChildren := p.Children > 0 || len(p.ChildProperties) > 0
if hasChildren {
count := p.NumChildren
if count == 0 {
count = len(p.ChildProperties)
}
if count == 0 {
count = p.Children // fallback
}
if count == 0 {
count = 1 // at least one if we detected children
}
if p.Type == "object" && p.ClassName != "" {
return p.ClassName
}
return fmt.Sprintf("%s[%d]", p.Type, count)
}
// Decode base64 if needed
content := p.Value
if p.Encoding == "base64" && content != "" {
if decoded, err := base64.StdEncoding.DecodeString(content); err == nil {
content = string(decoded)
}
}
return formatSimpleValue(p.Type, content, p.ClassName)
}
// formatSimpleValue formats a value for display
func formatSimpleValue(typ, content, classname string) string {
switch typ {
case "null", "uninitialized":
return "null"
case "bool":
if content == "1" || content == "true" {
return "true"
}
return "false"
case "string":
if content == "" {
return `""`
}
if len(content) > 60 {
return `"` + truncate(content, 55) + `..."`
}
return `"` + content + `"`
case "int", "float":
return content
case "array":
return "array[]"
case "object":
if classname != "" {
return classname + "{}"
}
return "object{}"
default:
if content == "" {
return "<" + typ + ">"
}
if len(content) > 60 {
return truncate(content, 57) + "..."
}
return content
}
}
// truncate shortens s to at most n bytes without splitting a UTF-8 character.
func truncate(s string, n int) string {
if len(s) <= n {
return s
}
for n > 0 && !utf8.RuneStart(s[n]) {
n--
}
return s[:n]
}
// FormatVariable formats a variable for display
func FormatVariable(v Variable) string {
indent := strings.Repeat(" ", v.Level)
width := 30 - (v.Level * 2)
if width < 8 {
width = 8
}
return fmt.Sprintf("%s%-*s %-8s = %s", indent, width, v.Name, v.Type, v.Value)
}