-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.go
More file actions
168 lines (150 loc) · 4.79 KB
/
Copy pathcore.go
File metadata and controls
168 lines (150 loc) · 4.79 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
package main
import (
"encoding/json"
"fmt"
"io"
"sort"
"strings"
)
// measureWidth returns the display width of s, counting non-ASCII runes as 2.
func measureWidth(s string) int {
w := 0
for _, r := range s {
if r > 255 {
w += 2
} else {
w++
}
}
return w
}
// matchHeaders applies patterns to a list of headers.
// If isExclusion is true, it returns headers NOT matching the patterns.
// If isExclusion is false, it returns headers matching the patterns, in the order specified by patterns.
func matchHeaders(availableHeaders []string, patterns string, isExclusion bool) []string {
if patterns == "" {
if isExclusion {
return availableHeaders // No patterns to exclude, return all
}
return []string{} // No patterns to include, return empty
}
userPatterns := strings.Split(patterns, ",")
matched := make(map[string]bool)
resultOrder := []string{} // To maintain order for inclusion
// Create a map for quick lookup of available headers
availableHeadersMap := make(map[string]bool)
for _, h := range availableHeaders {
availableHeadersMap[h] = true
}
for _, pattern := range userPatterns {
trimmedPattern := strings.TrimSpace(pattern)
if trimmedPattern == "*" {
// Handle wildcard for remaining headers
// For inclusion: add all remaining available headers
// For exclusion: mark all remaining available headers as matched (to be excluded)
for _, header := range availableHeaders {
if availableHeadersMap[header] && !matched[header] { // Only consider headers not yet processed by explicit patterns
if isExclusion {
matched[header] = true
} else {
resultOrder = append(resultOrder, header)
matched[header] = true
}
}
}
} else if strings.HasSuffix(trimmedPattern, "*") {
// Prefix wildcard (e.g., "col*")
prefix := strings.TrimSuffix(trimmedPattern, "*")
var currentMatches []string
for _, header := range availableHeaders {
if strings.HasPrefix(header, prefix) && availableHeadersMap[header] {
currentMatches = append(currentMatches, header)
}
}
sort.Strings(currentMatches) // Sort prefix matches for deterministic behavior
for _, header := range currentMatches {
if !matched[header] {
if isExclusion {
matched[header] = true
} else {
resultOrder = append(resultOrder, header)
matched[header] = true
}
}
}
} else {
// Specific column name
if availableHeadersMap[trimmedPattern] && !matched[trimmedPattern] {
if isExclusion {
matched[trimmedPattern] = true
} else {
resultOrder = append(resultOrder, trimmedPattern)
matched[trimmedPattern] = true
}
}
}
}
if isExclusion {
// For exclusion, return headers that were NOT matched
finalHeaders := []string{}
for _, header := range availableHeaders {
if !matched[header] {
finalHeaders = append(finalHeaders, header)
}
}
return finalHeaders
} else {
// For inclusion, return headers that were matched, in order
return resultOrder
}
}
// parseJSON reads JSON from an io.Reader and converts it into a table structure,
// respecting the user-defined column order with advanced wildcards.
func parseJSON(r io.Reader, columnOrder string, excludeColumnOrder string) ([][]string, error) {
var data []map[string]any
decoder := json.NewDecoder(r)
if err := decoder.Decode(&data); err != nil {
return nil, fmt.Errorf("failed to decode json: %w", err)
}
if len(data) == 0 {
return [][]string{}, nil
}
// 1. Collect all unique keys from the data and sort them for deterministic order.
allHeadersSet := make(map[string]bool)
for _, row := range data {
for key := range row {
allHeadersSet[key] = true
}
}
allHeadersList := make([]string, 0, len(allHeadersSet))
for h := range allHeadersSet {
allHeadersList = append(allHeadersList, h)
}
sort.Strings(allHeadersList) // Ensure initial list is sorted
// 2. Apply exclusion patterns first
headersAfterExclusion := matchHeaders(allHeadersList, excludeColumnOrder, true)
var finalHeaders []string
if columnOrder == "" {
// Default behavior: use all headers remaining after exclusion, sorted alphabetically.
sort.Strings(headersAfterExclusion) // Ensure sorted after exclusion
finalHeaders = headersAfterExclusion
} else {
// Apply inclusion patterns to the headers remaining after exclusion
finalHeaders = matchHeaders(headersAfterExclusion, columnOrder, false)
}
// 3. Create the table data structure (headers + rows) using the final header order.
table := make([][]string, len(data)+1)
table[0] = finalHeaders
for i, rowMap := range data {
row := make([]string, len(finalHeaders))
for j, header := range finalHeaders {
if val, ok := rowMap[header]; ok {
row[j] = fmt.Sprintf("%v", val)
} else {
row[j] = "" // Handle missing keys for a given row
}
}
table[i+1] = row
}
return table, nil
}