-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader.go
More file actions
294 lines (250 loc) · 7.62 KB
/
Copy pathheader.go
File metadata and controls
294 lines (250 loc) · 7.62 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
package csvpp
import (
"fmt"
"strings"
"unicode/utf8"
)
// parseColumnHeader parses a single column header string according to IETF CSV++ Section 2.2.
// ABNF (Section 2.2):
//
// field = simple-field / array-field / struct-field / array-struct-field
func parseColumnHeader(s string) (*ColumnHeader, error) {
return parseColumnHeaderWithDepth(s, 0, DefaultMaxNestingDepth)
}
// parseColumnHeaderWithDepth parses a column header with depth limit.
func parseColumnHeaderWithDepth(s string, depth, maxDepth int) (*ColumnHeader, error) {
if depth > maxDepth {
return nil, fmt.Errorf("%w: depth %d exceeds max %d", ErrNestingTooDeep, depth, maxDepth)
}
if s == "" {
return nil, fmt.Errorf("%w: empty column header", ErrInvalidHeader)
}
h := &ColumnHeader{
Kind: SimpleField,
}
remaining := s
// 1. Extract name
name, rest, err := parseName(remaining)
if err != nil {
return nil, err
}
h.Name = name
remaining = rest
if remaining == "" {
return h, nil
}
// 2. Extract array delimiter if "[" is present
// ABNF: array-field = name "[" [delimiter] "]"
if strings.HasPrefix(remaining, "[") {
delim, rest, err := parseArrayDelimiterWithDepth(remaining, depth)
if err != nil {
return nil, err
}
h.ArrayDelimiter = delim
h.Kind = ArrayField
remaining = rest
}
if remaining == "" {
return h, nil
}
// 3. Extract component delimiter and component list if "(" is present
// ABNF: struct-field = name [component-delim] "(" component-list ")"
if len(remaining) > 0 {
compDelim, compList, err := parseStructuredPartWithDepth(remaining, depth, maxDepth)
if err != nil {
return nil, err
}
h.ComponentDelimiter = compDelim
h.Components = compList
if h.Kind == ArrayField {
h.Kind = ArrayStructuredField
} else {
h.Kind = StructuredField
}
}
return h, nil
}
// parseName extracts the name part according to IETF CSV++ Section 2.2.
// ABNF (Section 2.2):
//
// name = 1*field-char
// field-char = ALPHA / DIGIT / "_" / "-"
//
// Note: Header names are restricted to ASCII characters per the IETF specification.
func parseName(s string) (name, rest string, err error) {
var i int
for i < len(s) {
r, size := utf8.DecodeRuneInString(s[i:])
if !isFieldChar(r) {
break
}
i += size
}
if i == 0 {
return "", "", fmt.Errorf("%w: name is required", ErrInvalidHeader)
}
return s[:i], s[i:], nil
}
// isFieldChar checks if the rune is a valid field-char per IETF CSV++ Section 2.2.
// ABNF: field-char = ALPHA / DIGIT / "_" / "-"
// This restricts header names to ASCII alphanumeric characters, underscore, and hyphen.
func isFieldChar(r rune) bool {
return (r >= 'a' && r <= 'z') ||
(r >= 'A' && r <= 'Z') ||
(r >= '0' && r <= '9') ||
r == '_' || r == '-'
}
// parseArrayDelimiterWithDepth extracts the "[" delimiter "]" part per IETF CSV++ Section 2.2.2.
// ABNF: array-field = name "[" [delimiter] "]"
// If no delimiter is specified at the top level (depth == 0), DefaultArrayDelimiter (~) is used.
// Per draft-02, nested arrays (depth > 0) MUST explicitly specify a delimiter;
// empty brackets are invalid because the default tilde is already consumed by the outer level.
func parseArrayDelimiterWithDepth(s string, depth int) (delim rune, rest string, err error) {
if !strings.HasPrefix(s, "[") {
return 0, s, nil
}
// Skip "["
s = s[1:]
// Find "]"
before, after, ok := strings.Cut(s, "]")
if !ok {
return 0, "", fmt.Errorf("%w: missing closing bracket ']'", ErrInvalidHeader)
}
raw := before
rest = after
if raw == "" {
if depth > 0 {
return 0, "", fmt.Errorf("%w: nested arrays must specify an explicit delimiter (empty brackets are invalid at depth > 0)", ErrInvalidHeader)
}
// Use default delimiter only at top level
delim = DefaultArrayDelimiter
} else {
r, size := utf8.DecodeRuneInString(raw)
if size != len(raw) {
return 0, "", fmt.Errorf("%w: array delimiter must be a single character", ErrInvalidHeader)
}
delim = r
}
return delim, rest, nil
}
// parseStructuredPartWithDepth parses the structured part with depth limit per IETF CSV++ Section 2.2.3.
// ABNF: struct-field = name [component-delim] "(" component-list ")"
// If no component delimiter is specified, DefaultComponentDelimiter (^) is used.
func parseStructuredPartWithDepth(s string, depth, maxDepth int) (compDelim rune, components []*ColumnHeader, err error) {
if s == "" {
return 0, nil, fmt.Errorf("%w: unexpected end of header", ErrInvalidHeader)
}
// Extract component delimiter (character before "(")
parenIdx := strings.Index(s, "(")
if parenIdx == -1 {
return 0, nil, fmt.Errorf("%w: missing opening parenthesis '('", ErrInvalidHeader)
}
if parenIdx == 0 {
// Use default component delimiter
compDelim = DefaultComponentDelimiter
} else {
// Character before "(" is the component delimiter
raw := s[:parenIdx]
r, size := utf8.DecodeRuneInString(raw)
if size != len(raw) {
return 0, nil, fmt.Errorf("%w: component delimiter must be a single character", ErrInvalidHeader)
}
compDelim = r
}
// Parse after "("
s = s[parenIdx+1:]
// Find matching ")" (considering nesting)
closeIdx := findClosingParen(s)
if closeIdx == -1 {
return 0, nil, fmt.Errorf("%w: missing closing parenthesis ')'", ErrInvalidHeader)
}
inner := s[:closeIdx]
rest := s[closeIdx+1:]
if rest != "" {
return 0, nil, fmt.Errorf("%w: unexpected characters after closing parenthesis", ErrInvalidHeader)
}
// Parse component list (depth + 1)
components, err = parseComponentListWithDepth(inner, compDelim, depth+1, maxDepth)
if err != nil {
return 0, nil, err
}
return compDelim, components, nil
}
// findClosingParen returns the index of the matching closing parenthesis.
func findClosingParen(s string) int {
depth := 1
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
switch r {
case '(':
depth++
case ')':
depth--
if depth == 0 {
return i
}
}
i += size
}
return -1
}
// parseComponentListWithDepth parses a component list with depth limit per IETF CSV++ Section 2.2.3.
// ABNF: component-list = component *(component-delim component)
// Each component can itself be any field type, enabling nested structures.
func parseComponentListWithDepth(s string, delim rune, depth, maxDepth int) ([]*ColumnHeader, error) {
if s == "" {
return nil, fmt.Errorf("%w: component list is empty", ErrInvalidHeader)
}
parts := splitByDelimiter(s, delim)
components := make([]*ColumnHeader, 0, len(parts))
for _, part := range parts {
comp, err := parseColumnHeaderWithDepth(part, depth, maxDepth)
if err != nil {
return nil, err
}
components = append(components, comp)
}
return components, nil
}
// splitByDelimiter splits a string by delimiter (ignoring content inside parentheses).
func splitByDelimiter(s string, delim rune) []string {
var parts []string
var current strings.Builder
depth := 0
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
switch {
case r == '(':
depth++
current.WriteRune(r)
case r == ')':
depth--
current.WriteRune(r)
case r == delim && depth == 0:
parts = append(parts, current.String())
current.Reset()
default:
current.WriteRune(r)
}
i += size
}
if current.Len() > 0 {
parts = append(parts, current.String())
}
return parts
}
// parseHeaderRecordWithMaxDepth parses an entire header row with depth limit.
func parseHeaderRecordWithMaxDepth(fields []string, maxDepth int) ([]*ColumnHeader, error) {
if len(fields) == 0 {
return nil, ErrNoHeader
}
headers := make([]*ColumnHeader, 0, len(fields))
for _, field := range fields {
h, err := parseColumnHeaderWithDepth(field, 0, maxDepth)
if err != nil {
return nil, err
}
headers = append(headers, h)
}
return headers, nil
}