From dc4acc073004736392ad5b2a0aea54f1040450a8 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Sun, 24 Sep 2023 17:12:17 +0800 Subject: [PATCH] refactor(query-service): remove redundant nil check (#3614) From the Go specification [1]: "1. For a nil slice, the number of iterations is 0." "3. If the map is nil, the number of iterations is 0." Therefore, an additional nil check for before the loop is unnecessary. [1]: https://go.dev/ref/spec#For_range Signed-off-by: Eng Zer Jun --- pkg/query-service/dao/sqlite/rbac.go | 12 +++++------ pkg/query-service/model/v3/v3.go | 32 +++++++++++----------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/pkg/query-service/dao/sqlite/rbac.go b/pkg/query-service/dao/sqlite/rbac.go index 63dedf0a23..07e870fdff 100644 --- a/pkg/query-service/dao/sqlite/rbac.go +++ b/pkg/query-service/dao/sqlite/rbac.go @@ -563,13 +563,11 @@ func (mds *ModelDaoSqlite) UpdateUserFlags(ctx context.Context, userId string, f return nil, apiError } - if userPayload.Flags != nil { - for k, v := range userPayload.Flags { - if _, ok := flags[k]; !ok { - // insert only missing keys as we want to retain the - // flags in the db that are not part of this request - flags[k] = v - } + for k, v := range userPayload.Flags { + if _, ok := flags[k]; !ok { + // insert only missing keys as we want to retain the + // flags in the db that are not part of this request + flags[k] = v } } diff --git a/pkg/query-service/model/v3/v3.go b/pkg/query-service/model/v3/v3.go index 23819f81f7..bd9811fdaa 100644 --- a/pkg/query-service/model/v3/v3.go +++ b/pkg/query-service/model/v3/v3.go @@ -410,27 +410,21 @@ func (c *CompositeQuery) Validate() error { return fmt.Errorf("composite query must contain at least one query") } - if c.BuilderQueries != nil { - for name, query := range c.BuilderQueries { - if err := query.Validate(); err != nil { - return fmt.Errorf("builder query %s is invalid: %w", name, err) - } + for name, query := range c.BuilderQueries { + if err := query.Validate(); err != nil { + return fmt.Errorf("builder query %s is invalid: %w", name, err) } } - if c.ClickHouseQueries != nil { - for name, query := range c.ClickHouseQueries { - if err := query.Validate(); err != nil { - return fmt.Errorf("clickhouse query %s is invalid: %w", name, err) - } + for name, query := range c.ClickHouseQueries { + if err := query.Validate(); err != nil { + return fmt.Errorf("clickhouse query %s is invalid: %w", name, err) } } - if c.PromQueries != nil { - for name, query := range c.PromQueries { - if err := query.Validate(); err != nil { - return fmt.Errorf("prom query %s is invalid: %w", name, err) - } + for name, query := range c.PromQueries { + if err := query.Validate(); err != nil { + return fmt.Errorf("prom query %s is invalid: %w", name, err) } } @@ -515,11 +509,9 @@ func (b *BuilderQuery) Validate() error { } } - if b.SelectColumns != nil { - for _, selectColumn := range b.SelectColumns { - if err := selectColumn.Validate(); err != nil { - return fmt.Errorf("select column is invalid %w", err) - } + for _, selectColumn := range b.SelectColumns { + if err := selectColumn.Validate(); err != nil { + return fmt.Errorf("select column is invalid %w", err) } }