From 03cf6141d4b12655410392ef46cc74adba6ff3f4 Mon Sep 17 00:00:00 2001 From: Oleg Zaytsev Date: Thu, 13 Jun 2024 18:46:35 +0200 Subject: [PATCH] Fix Matcher.String() with empty label name When the label name is empty, which can happen now with quoted label name, it should be quoted when printed as a string again. Signed-off-by: Oleg Zaytsev --- model/labels/matcher.go | 2 +- promql/parser/printer_test.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/model/labels/matcher.go b/model/labels/matcher.go index 8e220e392..a09c838e3 100644 --- a/model/labels/matcher.go +++ b/model/labels/matcher.go @@ -101,7 +101,7 @@ func (m *Matcher) shouldQuoteName() bool { } return true } - return false + return len(m.Name) == 0 } // Matches returns whether the matcher matches the given string value. diff --git a/promql/parser/printer_test.go b/promql/parser/printer_test.go index f224d841d..d2e301a88 100644 --- a/promql/parser/printer_test.go +++ b/promql/parser/printer_test.go @@ -148,6 +148,13 @@ func TestExprString(t *testing.T) { in: `{"_0"="1"}`, out: `{_0="1"}`, }, + { + in: `{""="0"}`, + }, + { + in: "{``=\"0\"}", + out: `{""="0"}`, + }, } for _, test := range inputs {