1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-26 23:10:42 +02:00

сделал IsEmptyValue()

This commit is contained in:
Nikitin Aleksandr
2024-04-12 15:34:20 +03:00
parent 5cfb8cbc93
commit 6324c9b944
2 changed files with 26 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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")
}
}