-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_structs.go
77 lines (65 loc) · 1.66 KB
/
yaml_structs.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
package main
import (
"fmt"
"github.com/fatih/color"
"github.com/pkg/errors"
)
type KeyValuePairs map[string]string
type Request struct {
Methods []string `yaml:"methods"`
Path string `yaml:"path"`
PathPrefix string `yaml:"path-prefix"`
Query KeyValuePairs `yaml:"query"`
Headers KeyValuePairs `yaml:"headers"`
}
func (req *Request) Validate() error {
var err error
if len(req.Methods) == 0 {
err = errors.New("missing request methods")
} else if req.Path == "" && req.PathPrefix == "" {
err = errors.New("missing request path or path-prefix")
}
return err
}
func (req *Request) String() string {
var route string
if req.Path != "" {
route = req.Path
} else {
route = req.PathPrefix
}
return fmt.Sprintf("%s %s", color.MagentaString(fmt.Sprintf("%v", req.Methods)), color.GreenString(route))
}
type Response struct {
Headers KeyValuePairs `yaml:"headers"`
Latency int `yaml:"latency"`
File string `yaml:"file"`
Body string `yaml:"body"`
Status int `yaml:"status"`
}
func (res *Response) Validate() error {
var err error
if res.Status == 0 {
err = errors.New("missing response status")
}
return err
}
func (res *Response) String() string {
return color.YellowString(fmt.Sprintf("%d", res.Status))
}
type Stub struct {
Request Request `yaml:"request"`
Response Response `yaml:"response"`
}
func (s *Stub) String() string {
return fmt.Sprintf("%s %s %s", s.Request.String(), color.CyanString("->"), s.Response.String())
}
func (s *Stub) Validate() error {
var err error
err = s.Request.Validate()
if err != nil {
return err
}
err = s.Response.Validate()
return err
}