From f36f888f55a111ad2f79ae7387298de446742e73 Mon Sep 17 00:00:00 2001 From: Dhia Ayachi Date: Mon, 6 Feb 2023 14:12:43 -0500 Subject: [PATCH] Remove empty tags 2 (#16113) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add support for RemoveEmptyTags in API client * Add changelog --------- Co-authored-by: RĂ©mi Lapeyre --- .changelog/14244.txt | 3 +++ api/prepared_query.go | 6 +++++ api/prepared_query_test.go | 52 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 .changelog/14244.txt diff --git a/.changelog/14244.txt b/.changelog/14244.txt new file mode 100644 index 000000000..3edc4e7f5 --- /dev/null +++ b/.changelog/14244.txt @@ -0,0 +1,3 @@ +```release-note:improvement +client: add support for RemoveEmptyTags in Prepared Queries templates. +``` diff --git a/api/prepared_query.go b/api/prepared_query.go index 753aeb0ea..f47a58373 100644 --- a/api/prepared_query.go +++ b/api/prepared_query.go @@ -96,6 +96,12 @@ type QueryTemplate struct { // Regexp allows specifying a regex pattern to match against the name // of the query being executed. Regexp string + + // RemoveEmptyTags if set to true, will cause the Tags list inside + // the Service structure to be stripped of any empty strings. This is useful + // when interpolating into tags in a way where the tag is optional, and + // where searching for an empty tag would yield no results from the query. + RemoveEmptyTags bool } // PreparedQueryDefinition defines a complete prepared query. diff --git a/api/prepared_query_test.go b/api/prepared_query_test.go index 0e358d777..f684b26d3 100644 --- a/api/prepared_query_test.go +++ b/api/prepared_query_test.go @@ -180,3 +180,55 @@ func TestAPI_PreparedQuery(t *testing.T) { t.Fatalf("bad: %v", defs) } } + +func TestAPI_PreparedQueryRemoveEmptyTags(t *testing.T) { + t.Parallel() + c, s := makeClient(t) + defer s.Stop() + + def := &PreparedQueryDefinition{ + Name: "test", + Service: ServiceQuery{ + Service: "redis", + }, + Template: QueryTemplate{ + RemoveEmptyTags: false, + }, + } + + query := c.PreparedQuery() + var err error + def.ID, _, err = query.Create(def, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + queries, _, err := query.Get(def.ID, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + if len(queries) != 1 { + t.Fatalf("wrong length: %#v", queries) + } + if queries[0].Template.RemoveEmptyTags { + t.Fatalf("wrong value: %v", queries[0].Template.RemoveEmptyTags) + } + + def.Template.RemoveEmptyTags = true + _, err = query.Update(def, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + queries, _, err = query.Get(def.ID, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + if len(queries) != 1 { + t.Fatalf("wrong length: %#v", queries) + } + if !queries[0].Template.RemoveEmptyTags { + t.Fatalf("wrong value: %v", queries[0].Template.RemoveEmptyTags) + } + +}