-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
111 lines (89 loc) · 2.61 KB
/
model.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
package strest
import (
"encoding/json"
"os"
)
// Payload entire yaml configuration
type Payload struct {
Version string `yaml:"version"`
Request map[string]*Request `yaml:"requests"`
Async bool `yaml:"async"`
}
func marshal[K comparable, V any](m map[K]V) (map[K]V, error) {
stringify, err := json.Marshal(m)
if err != nil {
return nil, err
}
stringify = []byte(ParseField(string(stringify)))
var result map[K]V
if err := json.Unmarshal(stringify, &result); err != nil {
return nil, err
}
return result, nil
}
func (p *Payload) Load() {
for s, v := range p.Request {
GetLogger().Println("request", s)
for env, value := range v.Env {
err := os.Setenv(env, value.(string))
if err != nil {
continue
}
}
p.Request[s].URL = ParseField(v.URL)
p.Request[s].Method = ParseField(v.Method)
p.Request[s].Data.Raw = ParseField(v.Data.Raw)
if len(v.Data.Headers) != 0 {
headers, err := marshal[string, string](v.Data.Headers)
if err != nil {
continue
}
p.Request[s].Data.Headers = headers
}
if len(v.Data.Form) != 0 {
form, err := marshal[string, string](v.Data.Form)
if err != nil {
continue
}
p.Request[s].Data.Headers = form
}
if len(v.Data.JSON) != 0 {
requestJson, err := marshal[string, any](v.Data.JSON)
if err != nil {
continue
}
p.Request[s].Data.JSON = requestJson
}
for t, u := range v.Validation {
v.Validation[t].Body = ParseField(u.Body)
}
}
}
// RequestData Request data parameters
type RequestData struct {
Params map[string]interface{} `yaml:"params"`
Headers map[string]string `yaml:"headers"`
Form map[string]string `yaml:"form"`
JSON map[string]interface{} `yaml:"json"`
Raw string `yaml:"raw"`
}
// Request Information about the current request
type Request struct {
FailOnError bool `yaml:"failOnError"`
URL string `yaml:"url"`
Timeout int `yaml:"timeout"`
Method string `yaml:"method"`
Data *RequestData `yaml:"data"`
Log bool `yaml:"log"`
Validation map[string]*Validation `yaml:"validation"`
Repeat int `yaml:"repeat"`
Delay int `yaml:"delay"`
Async bool `yaml:"async"`
Env map[string]interface{} `yaml:"environment"`
DependsOn []string `yaml:"dependsOn"`
}
// Validation types of validation available on each request
type Validation struct {
Body string `yaml:"body"`
StatusCode int `yaml:"code"`
}