Fix PrintList implementation, add NodeCount method

This commit is contained in:
Christoph Kluge 2023-04-06 11:38:10 +02:00
parent 2401a2b940
commit f0cbb8c5dc
2 changed files with 46 additions and 18 deletions

View File

@ -287,8 +287,7 @@ func (r *subClusterResolver) NumberOfNodes(ctx context.Context, obj *schema.SubC
if err != nil { if err != nil {
return 0, err return 0, err
} }
stringList := nodeList.PrintList() return nodeList.NodeCount(), nil
return len(stringList), nil
} }
// Cluster returns generated.ClusterResolver implementation. // Cluster returns generated.ClusterResolver implementation.

View File

@ -14,7 +14,7 @@ import (
type NodeList [][]interface { type NodeList [][]interface {
consume(input string) (next string, ok bool) consume(input string) (next string, ok bool)
limits() []map[string]int64 limits() []map[string]int
prefix() string prefix() string
} }
@ -40,12 +40,35 @@ func (nl *NodeList) Contains(name string) bool {
func (nl *NodeList) PrintList() []string { func (nl *NodeList) PrintList() []string {
var out []string var out []string
for _, term := range *nl { for _, term := range *nl {
// Get String-Part first
prefix := term[0].prefix() prefix := term[0].prefix()
if len(term) == 1 { // If only String-Part in Term: Single Node Name -> Use as provided
out = append(out, prefix)
} else { // Else: Numeric start-end definition with x digits zeroPadded
limitArr := term[1].limits() limitArr := term[1].limits()
for _, inner := range limitArr { for _, inner := range limitArr {
for i := inner["start"]; i < inner["end"]+1; i++ { for i := inner["start"]; i < inner["end"]+1; i++ {
node := fmt.Sprintf("%s%02d", prefix, i) if inner["zeroPadded"] == 1 {
out = append(out, node) out = append(out, fmt.Sprintf("%s%0*d", prefix, inner["digits"], i))
} else {
log.Error("node list: only zero-padded ranges are allowed")
}
}
}
}
}
return out
}
func (nl *NodeList) NodeCount() int {
var out int = 0
for _, term := range *nl {
if len(term) == 1 { // If only String-Part in Term: Single Node Name -> add one
out += 1
} else { // Else: Numeric start-end definition -> add difference + 1
limitArr := term[1].limits()
for _, inner := range limitArr {
out += (inner["end"] - inner["start"]) + 1
} }
} }
} }
@ -62,9 +85,9 @@ func (nle NLExprString) consume(input string) (next string, ok bool) {
return "", false return "", false
} }
func (nle NLExprString) limits() []map[string]int64 { func (nle NLExprString) limits() []map[string]int {
// Null implementation to fullfill interface requirement // Null implementation to fullfill interface requirement
l := make([]map[string]int64, 0) l := make([]map[string]int, 0)
return l return l
} }
@ -83,8 +106,8 @@ func (nles NLExprIntRanges) consume(input string) (next string, ok bool) {
return "", false return "", false
} }
func (nles NLExprIntRanges) limits() []map[string]int64 { func (nles NLExprIntRanges) limits() []map[string]int {
l := make([]map[string]int64, 0) l := make([]map[string]int, 0)
for _, nle := range nles { for _, nle := range nles {
inner := nle.limits() inner := nle.limits()
l = append(l, inner[0]) l = append(l, inner[0])
@ -131,11 +154,17 @@ func (nle NLExprIntRange) consume(input string) (next string, ok bool) {
return "", false return "", false
} }
func (nle NLExprIntRange) limits() []map[string]int64 { func (nle NLExprIntRange) limits() []map[string]int {
l := make([]map[string]int64, 0) l := make([]map[string]int, 0)
m := make(map[string]int64) m := make(map[string]int)
m["start"] = nle.start m["start"] = int(nle.start)
m["end"] = nle.end m["end"] = int(nle.end)
m["digits"] = int(nle.digits)
if nle.zeroPadded == true {
m["zeroPadded"] = 1
} else {
m["zeroPadded"] = 0
}
l = append(l, m) l = append(l, m)
return l return l
} }
@ -173,7 +202,7 @@ func ParseNodeList(raw string) (NodeList, error) {
for _, rawterm := range rawterms { for _, rawterm := range rawterms {
exprs := []interface { exprs := []interface {
consume(input string) (next string, ok bool) consume(input string) (next string, ok bool)
limits() []map[string]int64 limits() []map[string]int
prefix() string prefix() string
}{} }{}
for i := 0; i < len(rawterm); i++ { for i := 0; i < len(rawterm); i++ {