first commit
Made-with: Cursor
This commit is contained in:
88
internal/utils/pgtype.go
Normal file
88
internal/utils/pgtype.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func ToPgUUID(id uuid.UUID) pgtype.UUID {
|
||||
return pgtype.UUID{Bytes: id, Valid: true}
|
||||
}
|
||||
|
||||
func FromPgUUID(id pgtype.UUID) uuid.UUID {
|
||||
if !id.Valid {
|
||||
return uuid.Nil
|
||||
}
|
||||
return uuid.UUID(id.Bytes)
|
||||
}
|
||||
|
||||
func ToPgTimestamptz(t time.Time) pgtype.Timestamptz {
|
||||
return pgtype.Timestamptz{Time: t, Valid: true}
|
||||
}
|
||||
|
||||
func ToPgTimestamptzPtr(t *time.Time) pgtype.Timestamptz {
|
||||
if t == nil {
|
||||
return pgtype.Timestamptz{Valid: false}
|
||||
}
|
||||
return pgtype.Timestamptz{Time: *t, Valid: true}
|
||||
}
|
||||
|
||||
func FromPgTimestamptz(t pgtype.Timestamptz) time.Time {
|
||||
if !t.Valid {
|
||||
return time.Time{}
|
||||
}
|
||||
return t.Time
|
||||
}
|
||||
|
||||
func FromPgTimestamptzPtr(t pgtype.Timestamptz) *time.Time {
|
||||
if !t.Valid {
|
||||
return nil
|
||||
}
|
||||
return &t.Time
|
||||
}
|
||||
|
||||
func ToPgText(s string) pgtype.Text {
|
||||
if s == "" {
|
||||
return pgtype.Text{Valid: false}
|
||||
}
|
||||
return pgtype.Text{String: s, Valid: true}
|
||||
}
|
||||
|
||||
func ToPgTextPtr(s *string) pgtype.Text {
|
||||
if s == nil {
|
||||
return pgtype.Text{Valid: false}
|
||||
}
|
||||
return pgtype.Text{String: *s, Valid: true}
|
||||
}
|
||||
|
||||
func FromPgText(t pgtype.Text) *string {
|
||||
if !t.Valid {
|
||||
return nil
|
||||
}
|
||||
return &t.String
|
||||
}
|
||||
|
||||
func FromPgTextValue(t pgtype.Text) string {
|
||||
if !t.Valid {
|
||||
return ""
|
||||
}
|
||||
return t.String
|
||||
}
|
||||
|
||||
func ToPgBool(b bool) pgtype.Bool {
|
||||
return pgtype.Bool{Bool: b, Valid: true}
|
||||
}
|
||||
|
||||
func NullPgUUID() pgtype.UUID {
|
||||
return pgtype.UUID{Valid: false}
|
||||
}
|
||||
|
||||
func NullPgTimestamptz() pgtype.Timestamptz {
|
||||
return pgtype.Timestamptz{Valid: false}
|
||||
}
|
||||
|
||||
func NullPgText() pgtype.Text {
|
||||
return pgtype.Text{Valid: false}
|
||||
}
|
||||
Reference in New Issue
Block a user