From fb43f829246213c13d9060d69d82d628ef435f9a Mon Sep 17 00:00:00 2001 From: Kichiyaki Date: Wed, 6 Jan 2021 14:22:24 +0100 Subject: [PATCH] [WIP] add a new helper function - isZero --- models/helpers.go | 36 ++++++++++++++++++++++++++++++++++++ models/server.go | 16 ++++++++-------- models/server_stats.go | 10 +++++----- models/version.go | 12 ++++++------ 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/models/helpers.go b/models/helpers.go index 1452836..35861da 100644 --- a/models/helpers.go +++ b/models/helpers.go @@ -2,6 +2,7 @@ package models import ( "strings" + "time" ) func addAliasToColumnName(column, prefix string) string { @@ -52,3 +53,38 @@ func buildConditionArray(column string) string { func buildConditionNotInArray(column string) string { return "NOT (" + buildConditionArray(column) + ")" } + +func isZero(v interface{}) bool { + switch c := v.(type) { + case string: + return c == "" + case *string: + return c == nil + case []string: + return c == nil || len(c) == 0 + case int: + return c == 0 + case *int: + return c == nil + case []int: + return c == nil || len(c) == 0 + case float64: + return c == 0 + case *float64: + return c == nil + case float32: + return c == 0 + case *float32: + return c == nil + case bool: + return !c + case *bool: + return c == nil + case time.Time: + return c.IsZero() + case *time.Time: + return c == nil + default: + return false + } +} diff --git a/models/server.go b/models/server.go index 132fe6c..33cdba0 100644 --- a/models/server.go +++ b/models/server.go @@ -83,30 +83,30 @@ type ServerFilter struct { } func (f *ServerFilter) WhereWithAlias(q *orm.Query, alias string) (*orm.Query, error) { - if len(f.Key) > 0 { + if !isZero(f.Key) { q = q.Where(buildConditionArray(addAliasToColumnName("key", alias)), pg.Array(f.Key)) } - if len(f.KeyNEQ) > 0 { + if !isZero(f.KeyNEQ) { q = q.Where(buildConditionNotInArray(addAliasToColumnName("key", alias)), pg.Array(f.KeyNEQ)) } - if f.KeyMATCH != "" { + if !isZero(f.KeyMATCH) { q = q.Where(buildConditionMatch(addAliasToColumnName("key", alias)), f.KeyMATCH) } - if f.KeyIEQ != "" { + if !isZero(f.KeyIEQ) { q = q.Where(buildConditionIEQ(addAliasToColumnName("key", alias)), f.KeyIEQ) } - if len(f.Status) > 0 { + if !isZero(f.Status) { q = q.Where(buildConditionArray(addAliasToColumnName("status", alias)), pg.Array(f.Status)) } - if len(f.StatusNEQ) > 0 { + if !isZero(f.StatusNEQ) { q = q.Where(buildConditionNotInArray(addAliasToColumnName("status", alias)), pg.Array(f.StatusNEQ)) } - if len(f.VersionCode) > 0 { + if !isZero(f.VersionCode) { q = q.Where(buildConditionArray(addAliasToColumnName("version_code", alias)), pg.Array(f.VersionCode)) } - if len(f.VersionCodeNEQ) > 0 { + if !isZero(f.VersionCodeNEQ) { q = q.Where(buildConditionNotInArray(addAliasToColumnName("version_code", alias)), pg.Array(f.VersionCodeNEQ)) } diff --git a/models/server_stats.go b/models/server_stats.go index 1cac695..ce049e9 100644 --- a/models/server_stats.go +++ b/models/server_stats.go @@ -40,19 +40,19 @@ type ServerStatsFilter struct { } func (f *ServerStatsFilter) WhereWithAlias(q *orm.Query, alias string) (*orm.Query, error) { - if !f.CreateDate.IsZero() { + if !isZero(f.CreateDate) { q = q.Where(buildConditionEquals(addAliasToColumnName("create_date", alias)), f.CreateDate) } - if !f.CreateDateGT.IsZero() { + if !isZero(f.CreateDateGT) { q = q.Where(buildConditionGT(addAliasToColumnName("create_date", alias)), f.CreateDateGT) } - if !f.CreateDateGTE.IsZero() { + if !isZero(f.CreateDateGTE) { q = q.Where(buildConditionGTE(addAliasToColumnName("create_date", alias)), f.CreateDateGTE) } - if !f.CreateDateLT.IsZero() { + if !isZero(f.CreateDateLT) { q = q.Where(buildConditionLT(addAliasToColumnName("create_date", alias)), f.CreateDateLT) } - if !f.CreateDateLTE.IsZero() { + if !isZero(f.CreateDateLTE) { q = q.Where(buildConditionLTE(addAliasToColumnName("create_date", alias)), f.CreateDateLTE) } diff --git a/models/version.go b/models/version.go index 735e755..507cef0 100644 --- a/models/version.go +++ b/models/version.go @@ -102,23 +102,23 @@ type VersionFilter struct { } func (f *VersionFilter) WhereWithAlias(q *orm.Query, alias string) (*orm.Query, error) { - if len(f.Code) > 0 { + if !isZero(f.Code) { q = q.Where(buildConditionArray(addAliasToColumnName("code", alias)), pg.Array(f.Code)) } - if len(f.CodeNEQ) > 0 { + if !isZero(f.CodeNEQ) { q = q.Where(buildConditionNotInArray(addAliasToColumnName("code", alias)), pg.Array(f.CodeNEQ)) } - if len(f.Host) > 0 { + if !isZero(f.Host) { q = q.Where(buildConditionArray(addAliasToColumnName("host", alias)), pg.Array(f.Host)) } - if len(f.HostNEQ) > 0 { + if !isZero(f.HostNEQ) { q = q.Where(buildConditionNotInArray(addAliasToColumnName("host", alias)), pg.Array(f.HostNEQ)) } - if f.HostMATCH != "" { + if !isZero(f.HostMATCH) { q = q.Where(buildConditionMatch(addAliasToColumnName("host", alias)), f.HostMATCH) } - if f.HostIEQ != "" { + if !isZero(f.HostIEQ) { q = q.Where(buildConditionIEQ(addAliasToColumnName("host", alias)), f.HostIEQ) }