forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
184 lines (150 loc) · 3.98 KB
/
string.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package eskip
import (
"fmt"
"io"
"math"
"strings"
)
func escape(s string, chars string) string {
s = strings.Replace(s, "\\", "\\\\", -1)
for i := 0; i < len(chars); i++ {
c := chars[i : i+1]
s = strings.Replace(s, c, "\\"+c, -1)
}
return s
}
func appendFmt(s []string, format string, args ...interface{}) []string {
return append(s, fmt.Sprintf(format, args...))
}
func appendFmtEscape(s []string, format string, escapeChars string, args ...interface{}) []string {
eargs := make([]interface{}, len(args))
for i, arg := range args {
eargs[i] = escape(fmt.Sprintf("%v", arg), escapeChars)
}
return appendFmt(s, format, eargs...)
}
func argsString(args []interface{}) string {
var sargs []string
for _, a := range args {
switch v := a.(type) {
case float64:
f := "%g"
// imprecise elimination of 0 decimals
// TODO: better fix this issue on parsing side
if math.Floor(v) == v {
f = "%.0f"
}
sargs = appendFmt(sargs, f, a)
case string:
sargs = appendFmtEscape(sargs, `"%s"`, `"`, a)
}
}
return strings.Join(sargs, ", ")
}
func (r *Route) predicateString() string {
var predicates []string
if r.Path != "" {
predicates = appendFmtEscape(predicates, `Path("%s")`, `"`, r.Path)
}
for _, h := range r.HostRegexps {
predicates = appendFmtEscape(predicates, "Host(/%s/)", "/", h)
}
for _, p := range r.PathRegexps {
predicates = appendFmtEscape(predicates, "PathRegexp(/%s/)", "/", p)
}
if r.Method != "" {
predicates = appendFmtEscape(predicates, `Method("%s")`, `"`, r.Method)
}
for k, v := range r.Headers {
predicates = appendFmtEscape(predicates, `Header("%s", "%s")`, `"`, k, v)
}
for k, rxs := range r.HeaderRegexps {
for _, rx := range rxs {
predicates = appendFmt(predicates, `HeaderRegexp("%s", /%s/)`, escape(k, `"`), escape(rx, "/"))
}
}
for _, p := range r.Predicates {
if p.Name != "Any" {
predicates = appendFmt(predicates, "%s(%s)", p.Name, argsString(p.Args))
}
}
if len(predicates) == 0 {
predicates = append(predicates, "*")
}
return strings.Join(predicates, " && ")
}
func (r *Route) filterString(pretty bool) string {
var sfilters []string
for _, f := range r.Filters {
sfilters = appendFmt(sfilters, "%s(%s)", f.Name, argsString(f.Args))
}
if pretty {
return strings.Join(sfilters, "\n -> ")
}
return strings.Join(sfilters, " -> ")
}
func (r *Route) backendString() string {
switch {
case r.Shunt, r.BackendType == ShuntBackend:
return "<shunt>"
case r.BackendType == LoopBackend:
return "<loopback>"
default:
return r.Backend
}
}
func (r *Route) backendStringQuoted() string {
s := r.backendString()
if r.BackendType == NetworkBackend && !r.Shunt {
s = fmt.Sprintf(`"%s"`, s)
}
return s
}
// Serializes a route expression. Omits the route id if any.
func (r *Route) String() string {
return r.Print(false)
}
func (r *Route) Print(pretty bool) string {
s := []string{r.predicateString()}
fs := r.filterString(pretty)
if fs != "" {
s = append(s, fs)
}
s = append(s, r.backendStringQuoted())
separator := " -> "
if pretty {
separator = "\n -> "
}
return strings.Join(s, separator)
}
// String is the same as Print but defaulting to pretty=false.
func String(routes ...*Route) string {
return Print(false, routes...)
}
// Print serializes a set of routes into a string. If there's only a
// single route, and its ID is not set, it prints only a route expression.
// If it has multiple routes with IDs, it prints full route definitions
// with the IDs and separated by ';'.
func Print(pretty bool, routes ...*Route) string {
if len(routes) == 1 && routes[0].Id == "" {
return routes[0].Print(pretty)
}
rs := make([]string, len(routes))
for i, r := range routes {
rs[i] = fmt.Sprintf("%s: %s", r.Id, r.Print(pretty))
}
sep := ";\n"
if pretty {
sep += "\n"
}
return strings.Join(rs, sep)
}
func Fprint(w io.Writer, pretty bool, routes ...*Route) {
f := "%s: %s;\n"
for i, r := range routes {
fmt.Fprintf(w, f, r.Id, r.Print(pretty))
if i == 0 {
f = "\n" + f
}
}
}