feat: id utils
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package trans
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func String(a string) *string {
|
||||
return &a
|
||||
@@ -527,3 +531,28 @@ func MapValues[TKey mapKeyValueType, TValue mapKeyValueType](source map[TKey]TVa
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
func ToUuidPtr(str *string) *uuid.UUID {
|
||||
var id *uuid.UUID
|
||||
if str != nil {
|
||||
_id, err := uuid.Parse(*str)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
id = &_id
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func ToUuid(str string) uuid.UUID {
|
||||
id, _ := uuid.Parse(str)
|
||||
return id
|
||||
}
|
||||
|
||||
func ToStringPtr(id *uuid.UUID) *string {
|
||||
var strUUID *string
|
||||
if id != nil {
|
||||
strUUID = Ptr(id.String())
|
||||
}
|
||||
return strUUID
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -167,3 +168,62 @@ func Test_Trans(t *testing.T) {
|
||||
assert.Equal(t, time.Now(), tmVal)
|
||||
assert.Equal(t, time.Now(), TimeValue(nil))
|
||||
}
|
||||
|
||||
func TestUUID(t *testing.T) {
|
||||
t.Run("ToUuidPtr_NilString", func(t *testing.T) {
|
||||
var str *string
|
||||
result := ToUuidPtr(str)
|
||||
if result != nil {
|
||||
t.Errorf("expected nil, got %v", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ToUuidPtr_ValidString", func(t *testing.T) {
|
||||
str := "550e8400-e29b-41d4-a716-446655440000"
|
||||
result := ToUuidPtr(&str)
|
||||
if result == nil || result.String() != str {
|
||||
t.Errorf("expected %v, got %v", str, result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ToUuidPtr_InvalidString", func(t *testing.T) {
|
||||
str := "invalid-uuid"
|
||||
result := ToUuidPtr(&str)
|
||||
if result != nil {
|
||||
t.Errorf("expected nil, got %v", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ToUuid_ValidString", func(t *testing.T) {
|
||||
str := "550e8400-e29b-41d4-a716-446655440000"
|
||||
result := ToUuid(str)
|
||||
if result.String() != str {
|
||||
t.Errorf("expected %v, got %v", str, result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ToUuid_InvalidString", func(t *testing.T) {
|
||||
str := "invalid-uuid"
|
||||
result := ToUuid(str)
|
||||
if result.String() == str {
|
||||
t.Errorf("expected invalid UUID, got %v", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ToStringPtr_NilUUID", func(t *testing.T) {
|
||||
var id *uuid.UUID
|
||||
result := ToStringPtr(id)
|
||||
if result != nil {
|
||||
t.Errorf("expected nil, got %v", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ToStringPtr_ValidUUID", func(t *testing.T) {
|
||||
id := uuid.MustParse("550e8400-e29b-41d4-a716-446655440000")
|
||||
result := ToStringPtr(&id)
|
||||
expected := "550e8400-e29b-41d4-a716-446655440000"
|
||||
if result == nil || *result != expected {
|
||||
t.Errorf("expected %v, got %v", expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user