From 6324c9b944baf5d7d77c414e7eefdd6e2e688b54 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Fri, 12 Apr 2024 15:34:20 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20IsEmptyV?= =?UTF-8?q?alue()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- micro/microfunctions.go | 7 +++++++ micro/microfunctions_test.go | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/micro/microfunctions.go b/micro/microfunctions.go index 22e5c1f5..0860cb91 100644 --- a/micro/microfunctions.go +++ b/micro/microfunctions.go @@ -841,3 +841,10 @@ func StructDeepCopy(src, dist interface{}) (err error) { } return gob.NewDecoder(&buf).Decode(dist) } + +// IsEmptyValue - возвращает true если значение по умолчанию (0, пустая строка, пустой слайс) +func IsEmptyValue(v any) bool { + rv := reflect.ValueOf(v) + Otvet := !rv.IsValid() || reflect.DeepEqual(rv.Interface(), reflect.Zero(rv.Type()).Interface()) + return Otvet +} diff --git a/micro/microfunctions_test.go b/micro/microfunctions_test.go index 1bff12ab..3d62269e 100644 --- a/micro/microfunctions_test.go +++ b/micro/microfunctions_test.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "github.com/ManyakRus/starter/contextmain" + "github.com/google/uuid" "os" "reflect" "testing" @@ -634,3 +635,21 @@ func TestStructDeepCopy(t *testing.T) { t.Errorf("copied struct does not match original struct") } } + +func TestIsEmptyValue(t *testing.T) { + // Testing for integer zero value + if !IsEmptyValue(0) { + t.Error("Expected true for integer zero value") + } + + // Testing for empty string value + if !IsEmptyValue("") { + t.Error("Expected true for empty string value") + } + + // Testing for empty uuid value + uuid1 := uuid.Nil + if !IsEmptyValue(uuid1) { + t.Error("Expected true for empty uuid value") + } +}