-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
44 lines (36 loc) · 941 Bytes
/
options.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
package fit
import (
"fmt"
"strings"
)
type Options struct {
regex map[string]string
name string
path string
}
// Where ...
func (r *Options) Where(constraints ...string) *Options {
regex, constraintLength := r.regex, len(constraints)
if regex == nil {
regex = make(map[string]string)
}
if constraintLength%2 != 0 {
panic("Constraint is missing")
}
for i := 0; i < constraintLength; i += 2 {
constraintName, constraintValue := constraints[i], constraints[i+1]
// Empty constraint(s)
if constraintValue == "" || constraintName == "" {
fmt.Println("Empty constraint was supplied. Ignoring")
continue
}
// If constraint does not exist in url path, we will ignore it
if !strings.Contains(r.path, constraintName) {
fmt.Printf("Constraint '%s' does not exist in path '%s'. Ignoring.\n", constraintName, r.path)
continue
}
regex[constraintName] = constraintValue
}
r.regex = regex
return r
}