Merge pull request #65 from ClusterCockpit/bug-59-nodelist-dash

Bug 59 nodelist dash
This commit is contained in:
Jan Eitzinger 2022-11-09 14:10:35 +01:00 committed by GitHub
commit 4f1ffe70aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View File

@ -92,6 +92,7 @@ func (nle NLExprIntRange) consume(input string) (next string, ok bool) {
func ParseNodeList(raw string) (NodeList, error) { func ParseNodeList(raw string) (NodeList, error) {
isLetter := func(r byte) bool { return ('a' <= r && r <= 'z') || ('A' <= r && r <= 'Z') } isLetter := func(r byte) bool { return ('a' <= r && r <= 'z') || ('A' <= r && r <= 'Z') }
isDigit := func(r byte) bool { return '0' <= r && r <= '9' } isDigit := func(r byte) bool { return '0' <= r && r <= '9' }
isDash := func(r byte) bool { return r == '-' }
rawterms := []string{} rawterms := []string{}
prevterm := 0 prevterm := 0
@ -117,23 +118,29 @@ func ParseNodeList(raw string) (NodeList, error) {
exprs := []interface { exprs := []interface {
consume(input string) (next string, ok bool) consume(input string) (next string, ok bool)
}{} }{}
for i := 0; i < len(rawterm); i++ { for i := 0; i < len(rawterm); i++ {
c := rawterm[i] c := rawterm[i]
if isLetter(c) || isDigit(c) { if isLetter(c) || isDigit(c) {
j := i j := i
for j < len(rawterm) && (isLetter(rawterm[j]) || isDigit(rawterm[j])) { for j < len(rawterm) &&
(isLetter(rawterm[j]) ||
isDigit(rawterm[j]) ||
isDash(rawterm[j])) {
j++ j++
} }
exprs = append(exprs, NLExprString(rawterm[i:j])) exprs = append(exprs, NLExprString(rawterm[i:j]))
i = j - 1 i = j - 1
} else if c == '[' { } else if c == '[' {
end := strings.Index(rawterm[i:], "]") end := strings.Index(rawterm[i:], "]")
if end == -1 { if end == -1 {
return nil, fmt.Errorf("node list: unclosed '['") return nil, fmt.Errorf("node list: unclosed '['")
} }
parts := strings.Split(rawterm[i+1:i+end], ",") parts := strings.Split(rawterm[i+1:i+end], ",")
nles := NLExprIntRanges{} nles := NLExprIntRanges{}
for _, part := range parts { for _, part := range parts {
minus := strings.Index(part, "-") minus := strings.Index(part, "-")
if minus == -1 { if minus == -1 {

View File

@ -57,3 +57,19 @@ func TestNodeListCommasInBrackets(t *testing.T) {
t.Fatal("4") t.Fatal("4")
} }
} }
func TestNodeListCommasOutsideBrackets(t *testing.T) {
nl, err := ParseNodeList("cn-0010,cn0011,cn-00[13-18,22-24]")
if err != nil {
t.Fatal(err)
}
if !nl.Contains("cn-0010") || !nl.Contains("cn0011") {
t.Fatal("1")
}
if !nl.Contains("cn-0013") ||
!nl.Contains("cn-0015") ||
!nl.Contains("cn-0022") ||
!nl.Contains("cn-0018") {
t.Fatal("2")
}
}

View File

@ -138,7 +138,7 @@
"kind": { "kind": {
"type": "string", "type": "string",
"enum": [ "enum": [
"influxdb-v2", "influxdb",
"prometheus", "prometheus",
"cc-metric-store", "cc-metric-store",
"test" "test"