-
Notifications
You must be signed in to change notification settings - Fork 0
/
dasmaps_parser.go
285 lines (257 loc) · 8.52 KB
/
dasmaps_parser.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
package main
import (
"crypto/md5"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"runtime"
"strings"
"time"
"gopkg.in/yaml.v2"
)
// NOTE: The json package always orders keys when marshalling. Specifically:
// - Maps have their keys sorted lexicographically
// - Structs keys are marshalled in the order defined in the struct
// Params keeps DAS map parameters
type Params map[string]string
// DASMap represent generic DAS map, we use particular order to perform serialization
type DASMap struct {
Das_map []Params `yaml:"das_map" json:"das_map"`
Expire int `yaml:"expire" json:"expire"`
Format string `yaml:"format" json:"format"`
Hash string `yaml:"hash" json:"hash"`
Instances []string `yaml:"instances" json:"instances"`
Lookup string `yaml:"lookup" json:"lookup"`
Params Params `yaml:"params" json:"params"`
System string `yaml:"system" json:"system"`
TimeStamp int64 `yaml:"ts" json:"ts"`
Type string `yaml:"type" json:"type"`
Url string `yaml:"url" json:"url"`
Urn string `yaml:"urn" json:"urn"`
}
// String method provides string representation of DASMap
func (d *DASMap) String() string {
return recString(d)
}
// Notation represents notation DAS record
type Notation struct {
Api string `yaml:"api" json:"api"`
Api_output string `yaml:"api_output" json:"api_output"`
Rec_key string `yaml:"rec_key" json:"rec_key"`
}
// String method provides string representation of DASMap
func (n *Notation) String() string {
return recString(n)
}
// Notations represents collection of notation DAS records
type Notations struct {
Hash string `yaml:"hash" json:"hash"`
Notations []Notation `yaml:"notations" json:"notations"`
System string `yaml:"system" json:"system"`
TimeStamp int64 `yaml:"ts" json:"ts"`
Type string `yaml:"type" json:"type"`
}
// String method provides string representation of DASMap
func (n *Notations) String() string {
return recString(n)
}
// InputValue represents input_values DAS record
type InputValue struct {
Input string `yaml:"input" json:"input"`
Jsonpath_selector string `yaml:"jsonpath_selector" json:"jsonpath_selector"`
Test string `yaml:"test" json:"test"`
Url string `yaml:"url" json:"url"`
}
// String method provides string representation of DASMap
func (n *InputValue) String() string {
return recString(n)
}
// InputValues represents collection of input_values DAS records
type InputValues struct {
Hash string `yaml:"hash" json:"hash"`
InputValues []InputValue `yaml:"input_values" json:"input_values"`
TimeStamp int64 `yaml:"ts" json:"ts"`
Type string `yaml:"type" json:"type"`
}
// String method provides string representation of DASMap
func (n *InputValues) String() string {
return recString(n)
}
// Link represents Link record
type Link struct {
Name string `yaml:"name" json:"name"`
Query string `yaml:"query" json:"query"`
Url string `yaml:"url" json:"url"`
}
// String method provides string representation of Link structure
func (n *Link) String() string {
return recString(n)
}
// Item represents unique item in Presentation record
type Item struct {
Das string `yaml:"das" json:"das"`
Diff []string `yaml:"diff" json:"diff"`
Examples []string `yaml:"examples" json:"examples"`
Link []Link `yaml:"link" json:"link"`
Ui string `yaml:"ui" json:"ui"`
}
// String method provides string representation of Item structure
func (n *Item) String() string {
return recString(n)
}
type Presentation map[string][]Item
// String method provides string representation of Presentation structure
func (n *Presentation) String() string {
return recString(n)
}
// type PRecord map[string]Presentation
type PRecord struct {
Block []Item `yaml:"block" json:"block"`
Child []Item `yaml:"child" json:"child"`
City []Item `yaml:"city" json:"city"`
Config []Item `yaml:"config" json:"config"`
Das_query []Item `yaml:"das_query" json:"das_query"`
Dataset []Item `yaml:"dataset" json:"dataset"`
Date []Item `yaml:"date" json:"date"`
Events []Item `yaml:"events" json:"events"`
File []Item `yaml:"file" json:"file"`
Group []Item `yaml:"group" json:"group"`
Ip []Item `yaml:"ip" json:"ip"`
Jobsummary []Item `yaml:"jobsummary" json:"jobsummary"`
Lumi []Item `yaml:"lumi" json:"lumi"`
Mcm []Item `yaml:"mcm" json:"mcm"`
Monitor []Item `yaml:"monitor" json:"monitor"`
Parent []Item `yaml:"parent" json:"parent"`
Primary_dataset []Item `yaml:"primary_dataset" json:"primary_dataset"`
Records []Item `yaml:"records" json:"records"`
Release []Item `yaml:"release" json:"release"`
Rules []Item `yaml:"rules" json:"rules"`
Run []Item `yaml:"run" json:"run"`
Run_status []Item `yaml:"run_status" json:"run_status"`
Site []Item `yaml:"site" json:"site"`
Status []Item `yaml:"status" json:"status"`
Stream []Item `yaml:"stream" json:"stream"`
Summary []Item `yaml:"summary" json:"summary"`
Tier []Item `yaml:"tier" json:"tier"`
User []Item `yaml:"user" json:"user"`
}
// String method provides string representation of PresentationRecord structure
func (n *PRecord) String() string {
return recString(n)
}
type PresentationRecord struct {
Hash string `yaml:"hash" json:"hash"`
Presentation PRecord `yaml:"presentation" json:"presentation"`
TimeStamp int64 `yaml:"ts" json:"ts"`
Type string `yaml:"type" json:"type"`
}
// String method provides string representation of PresentationRecord structure
func (n *PresentationRecord) String() string {
return recString(n)
}
// String method provides string representation of DASMap
func recString(v interface{}) string {
r, e := json.Marshal(v)
if e != nil {
log.Fatalf("unable to marshal record: %v %v\n", v, e)
}
return string(r)
}
// helper function to parse input yaml file and produce DAS map records
func dasmaps(input string) {
data, err := ioutil.ReadFile(input)
if err != nil {
log.Fatalf("unable to read file %s %v\n", input, err)
}
var gRec DASMap
for i, r := range strings.Split(string(data), "---") {
if strings.Contains(input, "presentation") { // presentation map
n := PresentationRecord{}
err = yaml.Unmarshal([]byte(r), &n)
if err != nil {
log.Fatalf("record: %v, %v", r, err)
}
n.Type = "presentation"
n.TimeStamp = time.Now().Unix()
r, e := json.Marshal(n)
if e != nil {
log.Fatalf("unable to marshal record: %v, %v\n", n, e)
}
n.Hash = fmt.Sprintf("%x", md5.Sum(r))
fmt.Println(recString(n))
continue
}
if strings.Contains(r, "urn") || strings.Contains(r, "format") { // das record
n := DASMap{}
err = yaml.Unmarshal([]byte(r), &n)
if err != nil {
log.Fatalf("record: %v, %v", r, err)
}
if i == 0 {
gRec = n
continue
}
n.System = gRec.System
n.Format = gRec.Format
n.Instances = gRec.Instances
n.Type = "service"
n.TimeStamp = time.Now().Unix()
r, e := json.Marshal(n)
if e != nil {
log.Fatalf("unable to marshal record: %v, %v\n", n, e)
}
n.Hash = fmt.Sprintf("%x", md5.Sum(r))
fmt.Println(n.String())
} else if strings.Contains(r, "notations") {
n := Notations{}
err = yaml.Unmarshal([]byte(r), &n)
if err != nil {
log.Fatalf("record: %v, %v", r, err)
}
n.System = gRec.System
n.Type = "notation"
n.TimeStamp = time.Now().Unix()
r, e := json.Marshal(n)
if e != nil {
log.Fatalf("unable to marshal record: %v, %v\n", n, e)
}
n.Hash = fmt.Sprintf("%x", md5.Sum(r))
fmt.Println(n.String())
} else if strings.Contains(r, "input_values") {
n := InputValues{}
err = yaml.Unmarshal([]byte(r), &n)
if err != nil {
log.Fatalf("record: %v, %v", r, err)
}
n.Type = "input_values"
n.TimeStamp = time.Now().Unix()
r, e := json.Marshal(n)
if e != nil {
log.Fatalf("unable to marshal record: %v, %v\n", n, e)
}
n.Hash = fmt.Sprintf("%x", md5.Sum(r))
fmt.Println(n.String())
}
}
}
func info() string {
goVersion := runtime.Version()
tstamp := time.Now()
return fmt.Sprintf("git={{VERSION}} go=%s date=%s", goVersion, tstamp)
}
func main() {
var version bool
flag.BoolVar(&version, "version", false, "Show version")
var input string
flag.StringVar(&input, "input", "", "yam das map file")
var verbose int
flag.IntVar(&verbose, "verbose", 0, "verbosity level")
flag.Parse()
if version {
fmt.Println(info())
return
}
dasmaps(input)
}