make controllerinformer interface private;

use named regexp groups
This commit is contained in:
Murat Kabilov
2017-08-15 14:07:16 +02:00
parent 228639b839
commit 38e0ffecf7
3 changed files with 66 additions and 14 deletions
+28
View File
@@ -10,6 +10,8 @@ import (
"github.com/motomux/pretty"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"regexp"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
)
@@ -71,3 +73,29 @@ OUTER:
}
return result, len(result) == 0
}
func FindNamedStringSubmatch(r *regexp.Regexp, s string) map[string]string {
matches := r.FindStringSubmatch(s)
grNames := r.SubexpNames()
if matches == nil {
return nil
}
groupMatches := 0
res := make(map[string]string, len(grNames))
for i, n := range grNames {
if n == "" {
continue
}
res[n] = matches[i]
groupMatches++
}
if groupMatches == 0 {
return nil
}
return res
}
+22
View File
@@ -6,6 +6,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"regexp"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
)
@@ -44,6 +46,17 @@ var substractTest = []struct {
{[]string{"a", "b", "c", "d"}, []string{"a", "bb", "c", "d"}, []string{"b"}, false},
}
var substringMatch = []struct {
inRegex *regexp.Regexp
inStr string
out map[string]string
}{
{regexp.MustCompile(`aaaa (?P<num>\d+) bbbb`), "aaaa 123 bbbb", map[string]string{"num": "123"}},
{regexp.MustCompile(`aaaa (?P<num>\d+) bbbb`), "a aa 123 bbbb", nil},
{regexp.MustCompile(`aaaa \d+ bbbb`), "aaaa 123 bbbb", nil},
{regexp.MustCompile(`aaaa (\d+) bbbb`), "aaaa 123 bbbb", nil},
}
func TestRandomPassword(t *testing.T) {
const pwdLength = 10
pwd := RandomPassword(pwdLength)
@@ -100,3 +113,12 @@ func TestSubstractSlices(t *testing.T) {
}
}
}
func TestFindNamedStringSubmatch(t *testing.T) {
for _, tt := range substringMatch {
actualRes := FindNamedStringSubmatch(tt.inRegex, tt.inStr)
if !reflect.DeepEqual(actualRes, tt.out) {
t.Errorf("FindNamedStringSubmatch expected: %#v, got: %#v", tt.out, actualRes)
}
}
}