-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathslice.go
121 lines (109 loc) · 2.49 KB
/
slice.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
package proute
import (
"bufio"
"io"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/segmentio/ksuid"
)
type PRouteOptions struct {
HeaderCrumbs []Crumb
QueryCrumbs []Crumb
PathCrumbs []Crumb
BodyCrumbs []Crumb
Method string
ID string
ContentType []ContentType
}
type PRouteOption func(o *PRouteOptions)
func OptHeader(c Crumb) PRouteOption {
return func(o *PRouteOptions) {
o.HeaderCrumbs = append(o.HeaderCrumbs, c)
}
}
func OptQuery(c Crumb) PRouteOption {
return func(o *PRouteOptions) {
o.QueryCrumbs = append(o.QueryCrumbs, c)
}
}
func OptPath(c Crumb) PRouteOption {
return func(o *PRouteOptions) {
o.PathCrumbs = append(o.PathCrumbs, c)
}
}
func OptBody(c Crumb) PRouteOption {
return func(o *PRouteOptions) {
o.BodyCrumbs = append(o.BodyCrumbs, c)
}
}
func OptID(ID string) PRouteOption {
return func(o *PRouteOptions) {
o.ID = ID
}
}
func OptMethod(method string) PRouteOption {
return func(o *PRouteOptions) {
o.Method = method
}
}
func OptContentType(v string) PRouteOption {
return func(o *PRouteOptions) {
o.ContentType = append(o.ContentType, ContentType(v))
}
}
func FromStringSliceReader(r io.Reader, source string, opts...PRouteOption) (API, error) {
scanner := bufio.NewScanner(r)
lines := make([]string, 0)
for scanner.Scan() {
lines = append(lines, strings.Trim(scanner.Text(), " "))
}
return FromStringSlice(lines, source, opts...)
}
// FromStringSlice will convert a string slice of paths into a Proute API. You can provide options to configure
// all the proutes at the same time, but not any individually
func FromStringSlice(in []string, source string, opts ...PRouteOption) (API, error) {
ret := API{
URL: source,
}
o := &PRouteOptions{
Method: "GET",
ID: ksuid.New().String(),
}
for _, v := range opts {
v(o)
}
ret.ID = o.ID
for _, v := range in {
if len(v) == 0 {
continue
}
if v[0] != '/' {
v = "/" + v
}
route := Route{
TemplatePath: v,
Method: o.Method,
ContentType: o.ContentType,
HeaderCrumbs: o.HeaderCrumbs,
PathCrumbs: o.PathCrumbs,
BodyCrumbs: o.BodyCrumbs,
QueryCrumbs: o.QueryCrumbs,
}
ret.Routes = append(ret.Routes, route)
}
return ret, nil
}
func (a APIS) EncodeStringSlice(w io.Writer) (error) {
var merr *multierror.Error
for _, api := range a {
for _, route := range api.Routes {
path, err:= route.Path(true)
if err != nil {
multierror.Append(merr, err)
continue
}
w.Write([]byte(path + "\n"))
}
}
return merr.ErrorOrNil()
}