mirror of
https://github.com/zalando/postgres-operator.git
synced 2026-10-05 12:11:20 +02:00
make controllerinformer interface private;
use named regexp groups
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user