promql: Make printer formatting less vintage (#3721)

- lower-case modifiers
- reverse order of aggregation modifiers and aggregated expression
- remove spacing before modifier parentheses
pull/3723/head
Julius Volz 7 years ago committed by GitHub
parent 1e943fc10a
commit 953af2c089
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -134,43 +134,45 @@ func (es Expressions) String() (s string) {
} }
func (node *AggregateExpr) String() string { func (node *AggregateExpr) String() string {
aggrString := fmt.Sprintf("%s(", node.Op) aggrString := node.Op.String()
if node.Op.isAggregatorWithParam() {
aggrString += fmt.Sprintf("%s, ", node.Param)
}
aggrString += fmt.Sprintf("%s)", node.Expr)
if node.Without { if node.Without {
aggrString = fmt.Sprintf("%s WITHOUT (%s)", aggrString, strings.Join(node.Grouping, ", ")) aggrString += fmt.Sprintf(" without(%s) ", strings.Join(node.Grouping, ", "))
} else { } else {
if len(node.Grouping) > 0 { if len(node.Grouping) > 0 {
aggrString = fmt.Sprintf("%s BY (%s)", aggrString, strings.Join(node.Grouping, ", ")) aggrString += fmt.Sprintf(" by(%s) ", strings.Join(node.Grouping, ", "))
} }
} }
aggrString += "("
if node.Op.isAggregatorWithParam() {
aggrString += fmt.Sprintf("%s, ", node.Param)
}
aggrString += fmt.Sprintf("%s)", node.Expr)
return aggrString return aggrString
} }
func (node *BinaryExpr) String() string { func (node *BinaryExpr) String() string {
returnBool := "" returnBool := ""
if node.ReturnBool { if node.ReturnBool {
returnBool = " BOOL" returnBool = " bool"
} }
matching := "" matching := ""
vm := node.VectorMatching vm := node.VectorMatching
if vm != nil && (len(vm.MatchingLabels) > 0 || vm.On) { if vm != nil && (len(vm.MatchingLabels) > 0 || vm.On) {
if vm.On { if vm.On {
matching = fmt.Sprintf(" ON(%s)", strings.Join(vm.MatchingLabels, ", ")) matching = fmt.Sprintf(" on(%s)", strings.Join(vm.MatchingLabels, ", "))
} else { } else {
matching = fmt.Sprintf(" IGNORING(%s)", strings.Join(vm.MatchingLabels, ", ")) matching = fmt.Sprintf(" ignoring(%s)", strings.Join(vm.MatchingLabels, ", "))
} }
if vm.Card == CardManyToOne || vm.Card == CardOneToMany { if vm.Card == CardManyToOne || vm.Card == CardOneToMany {
matching += " GROUP_" matching += " group_"
if vm.Card == CardManyToOne { if vm.Card == CardManyToOne {
matching += "LEFT" matching += "left"
} else { } else {
matching += "RIGHT" matching += "right"
} }
matching += fmt.Sprintf("(%s)", strings.Join(vm.Include, ", ")) matching += fmt.Sprintf("(%s)", strings.Join(vm.Include, ", "))
} }
@ -189,7 +191,7 @@ func (node *MatrixSelector) String() string {
} }
offset := "" offset := ""
if node.Offset != time.Duration(0) { if node.Offset != time.Duration(0) {
offset = fmt.Sprintf(" OFFSET %s", model.Duration(node.Offset)) offset = fmt.Sprintf(" offset %s", model.Duration(node.Offset))
} }
return fmt.Sprintf("%s[%s]%s", vecSelector.String(), model.Duration(node.Range), offset) return fmt.Sprintf("%s[%s]%s", vecSelector.String(), model.Duration(node.Range), offset)
} }
@ -221,7 +223,7 @@ func (node *VectorSelector) String() string {
} }
offset := "" offset := ""
if node.Offset != time.Duration(0) { if node.Offset != time.Duration(0) {
offset = fmt.Sprintf(" OFFSET %s", model.Duration(node.Offset)) offset = fmt.Sprintf(" offset %s", model.Duration(node.Offset))
} }
if len(labelStrings) == 0 { if len(labelStrings) == 0 {

@ -57,17 +57,17 @@ func TestExprString(t *testing.T) {
in, out string in, out string
}{ }{
{ {
in: `sum(task:errors:rate10s{job="s"}) BY ()`, in: `sum by() (task:errors:rate10s{job="s"})`,
out: `sum(task:errors:rate10s{job="s"})`, out: `sum(task:errors:rate10s{job="s"})`,
}, },
{ {
in: `sum(task:errors:rate10s{job="s"}) BY (code)`, in: `sum by(code) (task:errors:rate10s{job="s"})`,
}, },
{ {
in: `sum(task:errors:rate10s{job="s"}) WITHOUT ()`, in: `sum without() (task:errors:rate10s{job="s"})`,
}, },
{ {
in: `sum(task:errors:rate10s{job="s"}) WITHOUT (instance)`, in: `sum without(instance) (task:errors:rate10s{job="s"})`,
}, },
{ {
in: `topk(5, task:errors:rate10s{job="s"})`, in: `topk(5, task:errors:rate10s{job="s"})`,
@ -76,42 +76,42 @@ func TestExprString(t *testing.T) {
in: `count_values("value", task:errors:rate10s{job="s"})`, in: `count_values("value", task:errors:rate10s{job="s"})`,
}, },
{ {
in: `a - ON() c`, in: `a - on() c`,
}, },
{ {
in: `a - ON(b) c`, in: `a - on(b) c`,
}, },
{ {
in: `a - ON(b) GROUP_LEFT(x) c`, in: `a - on(b) group_left(x) c`,
}, },
{ {
in: `a - ON(b) GROUP_LEFT(x, y) c`, in: `a - on(b) group_left(x, y) c`,
}, },
{ {
in: `a - ON(b) GROUP_LEFT c`, in: `a - on(b) group_left c`,
out: `a - ON(b) GROUP_LEFT() c`, out: `a - on(b) group_left() c`,
}, },
{ {
in: `a - ON(b) GROUP_LEFT() (c)`, in: `a - on(b) group_left() (c)`,
}, },
{ {
in: `a - IGNORING(b) c`, in: `a - ignoring(b) c`,
}, },
{ {
in: `a - IGNORING() c`, in: `a - ignoring() c`,
out: `a - c`, out: `a - c`,
}, },
{ {
in: `up > BOOL 0`, in: `up > bool 0`,
}, },
{ {
in: `a OFFSET 1m`, in: `a offset 1m`,
}, },
{ {
in: `a{c="d"}[5m] OFFSET 1m`, in: `a{c="d"}[5m] offset 1m`,
}, },
{ {
in: `a[5m] OFFSET 1m`, in: `a[5m] offset 1m`,
}, },
} }

Loading…
Cancel
Save