-
Notifications
You must be signed in to change notification settings - Fork 19
/
collection.go
104 lines (85 loc) · 2.34 KB
/
collection.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
package postman
import (
"encoding/json"
"fmt"
"io"
)
type version string
const (
// V210 : v2.1.0
V210 version = "v2.1.0"
// V200 : v2.0.0
V200 version = "v2.0.0"
)
// Info stores data about the collection.
type Info struct {
Name string `json:"name"`
Description Description `json:"description"`
Version string `json:"version"`
Schema string `json:"schema"`
}
// Collection represents a Postman Collection.
type Collection struct {
Auth *Auth `json:"auth,omitempty"`
Info Info `json:"info"`
Items []*Items `json:"item"`
Events []*Event `json:"event,omitempty"`
Variables []*Variable `json:"variable,omitempty"`
}
// CreateCollection returns a new Collection.
func CreateCollection(name string, desc string) *Collection {
return &Collection{
Info: Info{
Name: name,
Description: Description{
Content: desc,
},
},
}
}
// AddItem appends an item (Item or ItemGroup) to the existing items slice.
func (c *Collection) AddItem(item *Items) {
c.Items = append(c.Items, item)
}
// AddItemGroup creates a new ItemGroup and appends it to the existing items slice.
func (c *Collection) AddItemGroup(name string) (f *Items) {
f = &Items{
Name: name,
Items: make([]*Items, 0),
}
c.Items = append(c.Items, f)
return
}
// Write encodes the Collection struct in JSON and writes it into the provided io.Writer.
func (c *Collection) Write(w io.Writer, v version) (err error) {
c.Info.Schema = fmt.Sprintf("https://schema.getpostman.com/json/collection/%s/collection.json", string(v))
setVersionForItems(c.Items, v)
file, _ := json.MarshalIndent(c, "", " ")
_, err = w.Write(file)
return
}
// Set the version on all structs that have a different behavior depending on the Postman Collection version.
func setVersionForItems(items []*Items, v version) {
for _, i := range items {
if i.Auth != nil {
i.Auth.setVersion(v)
}
if i.IsGroup() {
setVersionForItems(i.Items, v)
} else {
if i.Request != nil {
if i.Request.Auth != nil {
i.Request.Auth.setVersion(v)
}
if i.Request.URL != nil {
i.Request.URL.setVersion(v)
}
}
}
}
}
// ParseCollection parses the content of the provided data stream into a Collection object.
func ParseCollection(r io.Reader) (c *Collection, err error) {
err = json.NewDecoder(r).Decode(&c)
return
}