-
Notifications
You must be signed in to change notification settings - Fork 19
/
items.go
124 lines (109 loc) · 4.01 KB
/
items.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
package postman
import "encoding/json"
// Items are the basic unit for a Postman collection.
// It can either be a request (Item) or a folder (ItemGroup).
type Items struct {
// Common fields.
Name string `json:"name"`
Description string `json:"description,omitempty"`
Variables []*Variable `json:"variable,omitempty"`
Events []*Event `json:"event,omitempty"`
ProtocolProfileBehavior interface{} `json:"protocolProfileBehavior,omitempty"`
// Fields specific to Item
ID string `json:"id,omitempty"`
Request *Request `json:"request,omitempty"`
Responses []*Response `json:"response,omitempty"`
// Fields specific to ItemGroup
Items []*Items `json:"item"`
Auth *Auth `json:"auth,omitempty"`
}
// An Item is an entity which contain an actual HTTP request, and sample responses attached to it.
type Item struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Variables []*Variable `json:"variable,omitempty"`
Events []*Event `json:"event,omitempty"`
ProtocolProfileBehavior interface{} `json:"protocolProfileBehavior,omitempty"`
ID string `json:"id,omitempty"`
Request *Request `json:"request,omitempty"`
Responses []*Response `json:"response,omitempty"`
}
// A ItemGroup is an ordered set of requests.
type ItemGroup struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Variables []*Variable `json:"variable,omitempty"`
Events []*Event `json:"event,omitempty"`
ProtocolProfileBehavior interface{} `json:"protocolProfileBehavior,omitempty"`
Items []*Items `json:"item"`
Auth *Auth `json:"auth,omitempty"`
}
// CreateItem is a helper to create a new Item.
func CreateItem(i Item) *Items {
return &Items{
Name: i.Name,
Description: i.Description,
Variables: i.Variables,
Events: i.Events,
ProtocolProfileBehavior: i.ProtocolProfileBehavior,
ID: i.ID,
Request: i.Request,
Responses: i.Responses,
}
}
// CreateItemGroup is a helper to create a new ItemGroup.
func CreateItemGroup(ig ItemGroup) *Items {
return &Items{
Name: ig.Name,
Description: ig.Description,
Variables: ig.Variables,
Events: ig.Events,
ProtocolProfileBehavior: ig.ProtocolProfileBehavior,
Items: ig.Items,
Auth: ig.Auth,
}
}
// IsGroup returns false as an Item is not a group.
func (i Items) IsGroup() bool {
if i.Items != nil {
return true
}
return false
}
// AddItem appends an item to the existing items slice.
func (i *Items) AddItem(item *Items) {
i.Items = append(i.Items, item)
}
// AddItemGroup creates a new Item folder and appends it to the existing items slice.
func (i *Items) AddItemGroup(name string) (f *Items) {
f = &Items{
Name: name,
Items: make([]*Items, 0),
}
i.Items = append(i.Items, f)
return
}
// MarshalJSON returns the JSON encoding of an Item/ItemGroup.
func (i Items) MarshalJSON() ([]byte, error) {
if i.IsGroup() {
return json.Marshal(ItemGroup{
Name: i.Name,
Description: i.Description,
Variables: i.Variables,
Events: i.Events,
ProtocolProfileBehavior: i.ProtocolProfileBehavior,
Items: i.Items,
Auth: i.Auth,
})
}
return json.Marshal(Item{
Name: i.Name,
Description: i.Description,
Variables: i.Variables,
Events: i.Events,
ProtocolProfileBehavior: i.ProtocolProfileBehavior,
ID: i.ID,
Request: i.Request,
Responses: i.Responses,
})
}