forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
93 lines (80 loc) · 1.89 KB
/
group.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
package v1
import (
"fmt"
"go.k6.io/k6/lib"
)
type Check struct {
ID string `json:"id" yaml:"id"`
Path string `json:"path" yaml:"path"`
Name string `json:"name" yaml:"name"`
Passes int64 `json:"passes" yaml:"passes"`
Fails int64 `json:"fails" yaml:"fails"`
}
func NewCheck(c *lib.Check) Check {
return Check{
ID: c.ID,
Path: c.Path,
Name: c.Name,
Passes: c.Passes,
Fails: c.Fails,
}
}
type Group struct {
ID string `json:"-" yaml:"id"`
Path string `json:"path" yaml:"path"`
Name string `json:"name" yaml:"name"`
Checks []Check `json:"checks" yaml:"checks"`
Parent *Group `json:"-" yaml:"-"`
ParentID string `json:"-" yaml:"parent-id"`
Groups []*Group `json:"-" yaml:"-"`
GroupIDs []string `json:"-" yaml:"group-ids"`
}
func NewGroup(g *lib.Group, parent *Group) *Group {
group := &Group{
ID: g.ID,
Path: g.Path,
Name: g.Name,
}
if parent != nil {
group.Parent = parent
group.ParentID = parent.ID
} else if g.Parent != nil {
group.Parent = NewGroup(g.Parent, nil)
group.ParentID = g.Parent.ID
}
for _, gp := range g.Groups {
group.Groups = append(group.Groups, NewGroup(gp, group))
group.GroupIDs = append(group.GroupIDs, gp.ID)
}
for _, c := range g.Checks {
group.Checks = append(group.Checks, NewCheck(c))
}
return group
}
func (g *Group) SetToManyReferenceIDs(name string, ids []string) error {
switch name {
case "groups":
g.Groups = nil
g.GroupIDs = ids
return nil
default:
return fmt.Errorf("unknown to many relation: %s", name)
}
}
func (g *Group) SetToOneReferenceID(name, id string) error {
switch name {
case "parent":
g.Parent = nil
g.ParentID = id
return nil
default:
return fmt.Errorf("unknown to one relation: %s", name)
}
}
func FlattenGroup(g *Group) []*Group {
groups := []*Group{g}
for _, gp := range g.Groups {
groups = append(groups, FlattenGroup(gp)...)
}
return groups
}