Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort all predicates #2208

Merged
merged 10 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 87 additions & 10 deletions eskip/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,85 @@ func argsString(args []interface{}) string {
return strings.Join(sargs, ", ")
}

func (r *Route) predicateString(sortPredicates bool) string {
func (r *Route) predicateStringSorted() string {
var predicates []string

cr := r.Copy()
AlexanderYastrebov marked this conversation as resolved.
Show resolved Hide resolved

if cr.Path != "" {
predicates = appendFmtEscape(predicates, `Path("%s")`, `"`, r.Path)
}

sort.SliceStable(cr.HostRegexps, func(i, j int) bool {
return cr.HostRegexps[i] > cr.HostRegexps[j]
})

for _, h := range cr.HostRegexps {
predicates = appendFmtEscape(predicates, "Host(/%s/)", "/", h)
}

sort.SliceStable(cr.PathRegexps, func(i, j int) bool {
return cr.PathRegexps[i] > cr.PathRegexps[j]
})

for _, p := range cr.PathRegexps {
predicates = appendFmtEscape(predicates, "PathRegexp(/%s/)", "/", p)
}

if r.Method != "" {
predicates = appendFmtEscape(predicates, `Method("%s")`, `"`, r.Method)
}

headerKeys := make([]string, 0, len(cr.Headers))

for k := range cr.Headers {
headerKeys = append(headerKeys, k)
}

sort.SliceStable(headerKeys, func(i, j int) bool {
return headerKeys[i] > headerKeys[j]
},
)

for _, key := range headerKeys {
predicates = appendFmtEscape(predicates, `Header("%s", "%s")`, `"`, key, cr.Headers[key])
}

headerKeys = make([]string, 0, len(cr.HeaderRegexps))

for k := range cr.HeaderRegexps {
headerKeys = append(headerKeys, k)
}

sort.SliceStable(headerKeys, func(i, j int) bool {
return headerKeys[i] > headerKeys[j]
},
)
MustafaSaber marked this conversation as resolved.
Show resolved Hide resolved

for _, k := range headerKeys {
for _, rx := range cr.HeaderRegexps[k] {
predicates = appendFmt(predicates, `HeaderRegexp("%s", /%s/)`, escape(k, `"`), escape(rx, "/"))
}
}

sort.SliceStable(cr.Predicates, func(i, j int) bool {
return cr.Predicates[i].String() < cr.Predicates[j].String()
})

for _, p := range cr.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, " && ")
MustafaSaber marked this conversation as resolved.
Show resolved Hide resolved
}

func (r *Route) predicateString() string {
var predicates []string

if r.Path != "" {
Expand Down Expand Up @@ -109,14 +187,7 @@ func (r *Route) predicateString(sortPredicates bool) string {
}
}

rp := r.Predicates
if sortPredicates {
rp = make([]*Predicate, len(r.Predicates))
copy(rp, r.Predicates)
sort.Slice(rp, func(i, j int) bool { return rp[i].String() < rp[j].String() })
}

for _, p := range rp {
for _, p := range r.Predicates {
if p.Name != "Any" {
predicates = appendFmt(predicates, "%s(%s)", p.Name, argsString(p.Args))
}
Expand Down Expand Up @@ -190,7 +261,13 @@ func (r *Route) String() string {
}

func (r *Route) Print(prettyPrintInfo PrettyPrintInfo) string {
s := []string{r.predicateString(prettyPrintInfo.SortPredicates)}
var s []string

if prettyPrintInfo.SortPredicates {
s = append(s, r.predicateStringSorted())
} else {
s = append(s, r.predicateString())
}

fs := r.filterString(prettyPrintInfo)
if fs != "" {
Expand Down
8 changes: 8 additions & 0 deletions eskip/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ func TestPrintSortedPredicates(t *testing.T) {
`routeWithDefaultPredicatesOnly: Header("Accept", "application/json") && Method("GET") -> "https://www.example.org"`,
`Method("GET") && Header("Accept", "application/json") -> "https://www.example.org"`,
},
{
`routeWithMultipleHeaders: Header("x-frontend-type", "mobile-app") && Header("X-Forwarded-Proto", "http") -> "https://www.example.org"`,
`Header("x-frontend-type", "mobile-app") && Header("X-Forwarded-Proto", "http") -> "https://www.example.org"`,
},
{
`routeWithMultipleHeadersRegex: HeaderRegexp("User-Agent", /Zelt-(.*)/) && HeaderRegexp("age", /\\d/) -> "https://www.example.org"`,
`HeaderRegexp("age", /\\d/) && HeaderRegexp("User-Agent", /Zelt-(.*)/) -> "https://www.example.org"`,
},
} {
testPrinting(item.route, item.expected, t, i, PrettyPrintInfo{Pretty: false, IndentStr: "", SortPredicates: true}, false)
}
Expand Down