-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured.go
More file actions
277 lines (263 loc) · 8.05 KB
/
structured.go
File metadata and controls
277 lines (263 loc) · 8.05 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
package parser
import (
"bufio"
"encoding/json"
"strings"
yaml "gopkg.in/yaml.v3"
)
// ParsedEnvelope wraps a structured parse result in the same envelope shape
// the Java side uses (a Map<String, Object> with keys "type" and either
// "data" or "documents"). It is a typed alias for clarity; detectors consume
// it as a plain map[string]any (see detector.Context.ParsedData).
type ParsedEnvelope = map[string]any
// ParseStructured dispatches to the right structured parser based on
// Language. Returns nil for languages this parser does not handle. Errors
// are returned for true parse failures; an empty / non-applicable input
// yields ({"type":"yaml","data":{}}, nil) rather than nil/error.
func ParseStructured(lang Language, source []byte) (ParsedEnvelope, error) {
switch lang {
case LanguageYaml:
return parseYAML(source)
case LanguageJSON:
return parseJSON(source)
case LanguageTOML:
return parseTOML(source), nil
case LanguageINI:
return parseINI(source), nil
case LanguageProperties:
return parseProperties(source), nil
}
return nil, nil
}
// parseYAML parses a YAML document into the envelope shape. Multi-document
// YAML produces {"type":"yaml_multi","documents":[map,map,...]} ; a single
// document produces {"type":"yaml","data":map}.
func parseYAML(source []byte) (ParsedEnvelope, error) {
dec := yaml.NewDecoder(strings.NewReader(string(source)))
var docs []any
for {
var doc any
if err := dec.Decode(&doc); err != nil {
if err.Error() == "EOF" {
break
}
// Best-effort: skip bad documents and continue.
break
}
if doc != nil {
docs = append(docs, normalizeYAML(doc))
}
}
if len(docs) == 0 {
return ParsedEnvelope{"type": "yaml", "data": map[string]any{}}, nil
}
if len(docs) == 1 {
data, _ := docs[0].(map[string]any)
if data == nil {
data = map[string]any{}
}
return ParsedEnvelope{"type": "yaml", "data": data}, nil
}
return ParsedEnvelope{"type": "yaml_multi", "documents": docs}, nil
}
// normalizeYAML converts yaml.v3's map[interface{}]interface{} into
// map[string]any recursively. The default yaml.v3 unmarshal into any uses
// string keys already (unlike yaml.v2), but we still coerce bare booleans
// like `on:` / `off:` / `yes:` / `no:` back to their string form because
// Kubernetes / GitHub Actions YAMLs use them as keys and parsers downstream
// expect string keys.
func normalizeYAML(v any) any {
switch x := v.(type) {
case map[string]any:
out := make(map[string]any, len(x))
for k, val := range x {
out[k] = normalizeYAML(val)
}
return out
case map[any]any:
out := make(map[string]any, len(x))
for k, val := range x {
out[stringifyKey(k)] = normalizeYAML(val)
}
return out
case []any:
out := make([]any, len(x))
for i, e := range x {
out[i] = normalizeYAML(e)
}
return out
}
return v
}
func stringifyKey(k any) string {
switch v := k.(type) {
case string:
return v
case bool:
// SnakeYAML / yaml.v3 parses bare `on` / `off` / `yes` / `no` as
// booleans. Coerce back to the canonical lowercase string so callers
// can do string comparisons (GitHub Actions workflows use `on:`).
if v {
return "true"
}
return "false"
default:
// Fall back to fmt.Sprint behaviour via the type-switch default.
// We avoid an fmt import in the hot path by handling common types.
return jsonScalarString(k)
}
}
func jsonScalarString(v any) string {
// Cheap stringification covering int/float/nil cases. Avoids fmt.
b, err := json.Marshal(v)
if err != nil {
return ""
}
s := string(b)
if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
return s[1 : len(s)-1]
}
return s
}
// parseJSON unmarshals JSON content into the envelope shape. Non-object
// top-levels (arrays, scalars) yield {"type":"json","data":{}} rather than
// an error.
func parseJSON(source []byte) (ParsedEnvelope, error) {
if len(strings.TrimSpace(string(source))) == 0 {
return ParsedEnvelope{"type": "json", "data": map[string]any{}}, nil
}
var root any
if err := json.Unmarshal(source, &root); err != nil {
return ParsedEnvelope{"type": "json", "data": map[string]any{}}, nil
}
data, ok := root.(map[string]any)
if !ok {
data = map[string]any{}
}
return ParsedEnvelope{"type": "json", "data": data}, nil
}
// parseTOML implements minimal TOML parsing sufficient for the structured
// detectors: top-level key = value pairs and [section] / [section.sub]
// tables. No support for arrays-of-tables, inline tables, or multiline
// strings — the detectors only need section/key shape. The Java side uses
// SnakeYAML's TOML mode which is similarly shallow.
func parseTOML(source []byte) ParsedEnvelope {
data := map[string]any{}
var currentSection string
sc := bufio.NewScanner(strings.NewReader(string(source)))
sc.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
for sc.Scan() {
raw := strings.TrimSpace(sc.Text())
if raw == "" || strings.HasPrefix(raw, "#") {
continue
}
if strings.HasPrefix(raw, "[") && strings.HasSuffix(raw, "]") {
section := unquote(strings.TrimSpace(raw[1 : len(raw)-1]))
currentSection = section
// Walk into a nested map; only create the top-level section in
// data — nested namespacing is preserved by the dotted key.
top := topLevelOf(section)
if _, ok := data[top]; !ok {
data[top] = map[string]any{}
}
continue
}
eq := strings.Index(raw, "=")
if eq <= 0 {
continue
}
key := unquote(strings.TrimSpace(raw[:eq]))
val := strings.TrimSpace(raw[eq+1:])
val = unquote(val)
if currentSection == "" {
data[key] = val
} else {
top := topLevelOf(currentSection)
sub, ok := data[top].(map[string]any)
if !ok {
sub = map[string]any{}
data[top] = sub
}
sub[key] = val
}
}
return ParsedEnvelope{"type": "toml", "data": data}
}
func topLevelOf(section string) string {
if i := strings.IndexByte(section, '.'); i >= 0 {
return section[:i]
}
return section
}
func unquote(s string) string {
if len(s) >= 2 && (s[0] == '"' && s[len(s)-1] == '"' || s[0] == '\'' && s[len(s)-1] == '\'') {
return s[1 : len(s)-1]
}
return s
}
// parseINI implements minimal INI parsing: [section] headers and key = value
// pairs grouped under their section.
func parseINI(source []byte) ParsedEnvelope {
data := map[string]any{}
var currentSection string
sc := bufio.NewScanner(strings.NewReader(string(source)))
sc.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
for sc.Scan() {
raw := strings.TrimSpace(sc.Text())
if raw == "" || strings.HasPrefix(raw, "#") || strings.HasPrefix(raw, ";") {
continue
}
if strings.HasPrefix(raw, "[") && strings.HasSuffix(raw, "]") {
currentSection = strings.TrimSpace(raw[1 : len(raw)-1])
if _, ok := data[currentSection]; !ok {
data[currentSection] = map[string]any{}
}
continue
}
if currentSection == "" {
continue // INI requires a section in this shallow parser
}
eq := strings.Index(raw, "=")
if eq <= 0 {
continue
}
key := strings.TrimSpace(raw[:eq])
val := strings.TrimSpace(raw[eq+1:])
sect := data[currentSection].(map[string]any)
sect[key] = val
}
return ParsedEnvelope{"type": "ini", "data": data}
}
// parseProperties implements minimal .properties parsing: key=value pairs,
// '#' and '!' comments, trim whitespace around the separator. Mirrors the
// Java side's PropertiesLoader subset.
func parseProperties(source []byte) ParsedEnvelope {
data := map[string]any{}
sc := bufio.NewScanner(strings.NewReader(string(source)))
sc.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
for sc.Scan() {
raw := strings.TrimSpace(sc.Text())
if raw == "" || strings.HasPrefix(raw, "#") || strings.HasPrefix(raw, "!") {
continue
}
// Java .properties accepts '=', ':' and whitespace as separators.
idx := strings.IndexAny(raw, "=:")
if idx <= 0 {
// Whitespace-separated key/value
if i := strings.IndexAny(raw, " \t"); i > 0 {
key := strings.TrimSpace(raw[:i])
val := strings.TrimSpace(raw[i+1:])
if key != "" {
data[key] = val
}
}
continue
}
key := strings.TrimSpace(raw[:idx])
val := strings.TrimSpace(raw[idx+1:])
if key != "" {
data[key] = val
}
}
return ParsedEnvelope{"type": "properties", "data": data}
}