From 70b8191fab96d2529fb54e2d8b4e06d8cdc6385e Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 2 Dec 2019 13:51:13 +0200 Subject: [PATCH] lib/storage: generate more human-friendly result in TagFilters.String --- lib/storage/tag_filters.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/storage/tag_filters.go b/lib/storage/tag_filters.go index cdae49bbe..c2e9e1b80 100644 --- a/lib/storage/tag_filters.go +++ b/lib/storage/tag_filters.go @@ -68,10 +68,15 @@ func (tfs *TagFilters) Add(key, value []byte, isNegative, isRegexp bool) error { // String returns human-readable value for tfs. func (tfs *TagFilters) String() string { + if len(tfs.tfs) == 0 { + return "{}" + } var bb bytes.Buffer - for i := range tfs.tfs { + fmt.Fprintf(&bb, "{%s", tfs.tfs[0].String()) + for i := range tfs.tfs[1:] { fmt.Fprintf(&bb, ", %s", tfs.tfs[i].String()) } + fmt.Fprintf(&bb, "}") return bb.String() } @@ -110,11 +115,16 @@ type tagFilter struct { // String returns human-readable tf value. func (tf *tagFilter) String() string { - var bb bytes.Buffer - fmt.Fprintf(&bb, "[isNegative=%v, isRegexp=%v, prefix=%q", tf.isNegative, tf.isRegexp, tf.prefix) - fmt.Fprintf(&bb, ", orSuffixes=%v, reSuffixMatch=%p", tf.orSuffixes, tf.reSuffixMatch) - fmt.Fprintf(&bb, "]") - return bb.String() + op := "=" + if tf.isNegative { + op = "!=" + if tf.isRegexp { + op = "!~" + } + } else if tf.isRegexp { + op = "=~" + } + return fmt.Sprintf("%s%s%q", tf.key, op, tf.value) } // Marshal appends marshaled tf to dst