/
dataview.go
320 lines (280 loc) · 7.86 KB
/
dataview.go
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package dataview
import (
"encoding/json"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/logger"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/matcher"
"github.com/tidwall/gjson"
)
var NamespaceSeparator = "::"
// Definition contains the view definition of when to use a specific view
type Definition struct {
FileName string `json:"-"`
Extension string `json:"-"`
Name string `json:"name,omitempty"`
Priority int `json:"priority,omitempty"`
Fragments []string `json:"fragments,omitempty"`
Type string `json:"type,omitempty"`
RequestPath string `json:"requestPath,omitempty"`
RequestMethod string `json:"requestMethod,omitempty"`
ContentType string `json:"contentType,omitempty"`
Self string `json:"self,omitempty"`
Columns []string `json:"columns,omitempty"`
}
func (d *Definition) FQDN() string {
if d.Extension != "" {
return fmt.Sprintf("%s%s%s", d.Extension, NamespaceSeparator, d.Name)
}
return d.Name
}
// DefinitionCollection collection of view definitions
type DefinitionCollection struct {
Definitions []Definition `json:"definitions,omitempty"`
}
// DataView data view containing pre-definied views
type DataView struct {
mu sync.RWMutex
Paths []string
Extension string
Pattern string
Definitions []Definition
Logger *logger.Logger
ActiveView *Definition
}
// NewDataView creates a new data view which selected a view based in json data
func NewDataView(pattern string, extension string, log *logger.Logger, paths ...string) (*DataView, error) {
if log == nil {
log = logger.NewDummyLogger("dataview")
}
view := &DataView{
mu: sync.RWMutex{},
Paths: paths,
Pattern: pattern,
Extension: extension,
Logger: log,
}
return view, nil
}
// LoadDefinitions load the view definitions
func (v *DataView) LoadDefinitions() error {
if len(v.GetDefinitions()) > 0 {
v.Logger.Debugf("Views already loaded")
return nil
}
v.mu.Lock()
defer v.mu.Unlock()
definitions := make([]Definition, 0)
v.Logger.Debugf("Looking for definitions in: %v", v.Paths)
for _, path := range v.Paths {
v.Logger.Debugf("Current view path: %s", path)
extName := ""
if strings.Contains(path, NamespaceSeparator) {
if b, a, ok := strings.Cut(path, NamespaceSeparator); ok {
extName = b
path = a
}
}
if stat, err := os.Stat(path); err != nil {
if extName == "" {
v.Logger.Debugf("Skipping view path because it does not exist. path=%s, error=%s", path, err)
}
continue
} else if !stat.IsDir() {
v.Logger.Debugf("Skipping view path because it is not a folder. path=%s", path)
continue
}
err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
// do not block walking folder
v.Logger.Warnf("Failed to walk path: %s, err=%s. file will be ignored", path, err)
return nil
}
if !d.IsDir() {
if !strings.EqualFold(filepath.Ext(d.Name()), v.Extension) {
return nil
}
if m, err := regexp.MatchString(v.Pattern, filepath.Base(d.Name())); err == nil && !m {
return nil
}
contents, err := os.ReadFile(path)
if err != nil {
return err
}
if extName != "" {
v.Logger.Debugf("Found view definition: %s | extension: %s", d.Name(), extName)
} else {
v.Logger.Debugf("Found view definition: %s", d.Name())
}
viewDefinition := &DefinitionCollection{}
if err := json.Unmarshal(contents, &viewDefinition); err != nil {
v.Logger.Warnf("Could not load view definitions. %s", err)
// do not prevent walking other folders
return nil
}
for i := range viewDefinition.Definitions {
viewDefinition.Definitions[i].FileName = d.Name()
viewDefinition.Definitions[i].Extension = extName
}
definitions = append(definitions, viewDefinition.Definitions...)
}
return nil
})
if err != nil {
v.Logger.Warnf("View discovery has errors. %s", err)
return err
}
v.Logger.Debugf("Loaded definitions: %d", len(definitions))
}
// sort by priority
sort.Slice(definitions, func(i, j int) bool {
return definitions[i].Priority < definitions[j].Priority
})
v.Definitions = definitions
return nil
}
// GetViewByName get view by name. Accepts wildcard name
func (v *DataView) GetViewByName(pattern string) ([]string, error) {
err := v.LoadDefinitions()
if err != nil {
return nil, err
}
var matchingDefinition *Definition
for _, definition := range v.GetDefinitions() {
if match, _ := matcher.MatchWithWildcards(definition.FQDN(), pattern); match {
matchingDefinition = &definition
break
}
}
if matchingDefinition == nil {
return nil, nil
}
return matchingDefinition.Columns, nil
}
// GetViews get a list of view names
func (v *DataView) GetViews(pattern string) ([]Definition, error) {
err := v.LoadDefinitions()
if err != nil {
return nil, err
}
matches := []Definition{}
for _, definition := range v.GetDefinitions() {
if match, _ := matcher.MatchWithWildcards(definition.FQDN(), pattern); match {
matches = append(matches, definition)
}
}
return matches, nil
}
func (v *DataView) GetDefinitions() []Definition {
v.mu.RLock()
defer v.mu.RUnlock()
return v.Definitions
}
// GetActiveView get the active view
func (v *DataView) GetActiveView() *Definition {
v.mu.RLock()
defer v.mu.RUnlock()
return v.ActiveView
}
// ClearActiveView clear the active view
func (v *DataView) ClearActiveView() {
v.mu.Lock()
defer v.mu.Unlock()
v.ActiveView = nil
}
type ViewData struct {
ResponseBody *gjson.Result
ContentType string
Response *http.Response
Request *http.Request
}
func (v *DataView) GetView(r *ViewData) ([]string, error) {
if view := v.GetActiveView(); view != nil {
v.Logger.Debugf("Using already active view")
return view.Columns, nil
}
data := r.ResponseBody
err := v.LoadDefinitions()
if err != nil {
return nil, err
}
if data.IsArray() {
if len(data.Array()) == 0 {
return nil, nil
}
data = &data.Array()[0]
}
definitions := v.GetDefinitions()
v.mu.Lock()
defer v.mu.Unlock()
var matchingDefinition *Definition
for _, definition := range definitions {
isMatch := true
for _, fragment := range definition.Fragments {
if result := data.Get(fragment); !result.Exists() {
// v.Logger.Debugf("Data did not contain fragment. view=%s, fragment=%s, input=%s", definition.FileName, fragment, data.Raw)
isMatch = false
break
}
}
if definition.Type != "" {
if v := data.Get("type"); v.Exists() {
if match, err := regexp.MatchString("(?i)"+definition.Type, v.Str); err == nil && !match {
isMatch = false
}
} else {
isMatch = false
}
}
if definition.ContentType != "" {
if match, err := regexp.MatchString("(?i)"+definition.ContentType, r.ContentType); err == nil && !match {
isMatch = false
}
}
if definition.Self != "" {
if v := data.Get("self"); v.Exists() {
if match, err := regexp.MatchString("(?i)"+definition.Self, v.Str); err == nil && !match {
isMatch = false
}
} else {
isMatch = false
}
}
if definition.RequestPath != "" {
if r.Request == nil {
isMatch = false
} else {
if match, err := regexp.MatchString("(?i)"+definition.RequestPath, r.Request.URL.Path); err == nil && !match {
isMatch = false
}
}
}
if definition.RequestMethod != "" {
if r.Request == nil {
isMatch = false
} else {
if match, err := regexp.MatchString("(?i)"+definition.RequestMethod, r.Request.Method); err == nil && !match {
isMatch = false
}
}
}
if isMatch {
matchingDefinition = &definition
break
}
}
if matchingDefinition != nil {
v.Logger.Debugf("Found matching view: name=%s", matchingDefinition.Name)
v.ActiveView = matchingDefinition
return matchingDefinition.Columns, nil
}
v.Logger.Debug("No matching view found")
return nil, nil
}