Skip to content

Commit

Permalink
Merge pull request #1 from nikitaksv/feat/add-uuid
Browse files Browse the repository at this point in the history
add uuid wrapper and string valuer add parse uuid
  • Loading branch information
nikitaksv authored Jul 17, 2024
2 parents cf2d213 + 668d236 commit f0076e1
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 7 deletions.
7 changes: 7 additions & 0 deletions convpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"time"

"github.com/google/uuid"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -136,3 +137,9 @@ func SQLNullString(value sql.NullString) StringWrapper {
}
return String(value.String)
}
func UUID(id uuid.UUID) UUIDWrapper {
return &uuidWrapper{value: uuid.NullUUID{Valid: true, UUID: id}}
}
func NullUUID(id uuid.NullUUID) UUIDWrapper {
return &uuidWrapper{value: id}
}
46 changes: 46 additions & 0 deletions convpb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package convpb
import (
"database/sql"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/wrapperspb"
)

func TestSQL(t *testing.T) {
Expand Down Expand Up @@ -58,3 +63,44 @@ func TestNumberWrapper_ToStringValue(t *testing.T) {
}
}
}

func TestUUIDWrapper(t *testing.T) {
id := uuid.New()
w := UUID(id)
assert.False(t, w.IsNil())
assert.Equal(t, w.ToStringValue(), wrapperspb.String(id.String()))
assert.Equal(t, w.ToString(), id.String())
assert.True(t, w.CondSetNil(id).IsNil())
}

func TestStringValuer_UUID(t *testing.T) {
id := uuid.New()
s := wrapperspb.String(id.String())
v := StringValue(s)

pid, err := v.ToUUID()
require.NoError(t, err)
assert.Equal(t, pid, id)
assert.Equal(t, v.ToUUIDOrNil(), id)

pnid, err := v.ToNullUUID()
require.NoError(t, err)
assert.Equal(t, pnid, uuid.NullUUID{UUID: id, Valid: true})
assert.Equal(t, v.ToNullUUIDOrNil(), uuid.NullUUID{UUID: id, Valid: true})
}

func TestStringValuer_UUID_error(t *testing.T) {
id := "asd"
s := wrapperspb.String(id)
v := StringValue(s)

pid, err := v.ToUUID()
require.Error(t, err)
assert.Equal(t, pid, uuid.Nil)
assert.Equal(t, v.ToUUIDOrNil(), uuid.Nil)

pnid, err := v.ToNullUUID()
require.Error(t, err)
assert.Equal(t, pnid, uuid.NullUUID{})
assert.Equal(t, v.ToNullUUIDOrNil(), uuid.NullUUID{})
}
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ module github.com/nikitaksv/convpb

go 1.18

require google.golang.org/protobuf v1.28.1
require (
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.9.0
google.golang.org/protobuf v1.34.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 14 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
30 changes: 30 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

"github.com/google/uuid"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
Expand Down Expand Up @@ -81,6 +82,10 @@ type StringValuer interface {
ToBuffer() *bytes.Buffer
// ToBytes returned slice of bytes
ToBytes() []byte
ToUUID() (uuid.UUID, error)
ToUUIDOrNil() uuid.UUID
ToNullUUID() (uuid.NullUUID, error)
ToNullUUIDOrNil() uuid.NullUUID
}

type stringValuer struct {
Expand Down Expand Up @@ -162,3 +167,28 @@ func (s *stringValuer) ToBytes() []byte {

return []byte(s.ToString())
}
func (s *stringValuer) ToUUID() (uuid.UUID, error) {
if s.IsNil() {
return uuid.Nil, nil
}

return uuid.Parse(s.value.GetValue())
}
func (s *stringValuer) ToUUIDOrNil() uuid.UUID {
id, _ := s.ToUUID() //nolint:errcheck
return id
}
func (s *stringValuer) ToNullUUID() (uuid.NullUUID, error) {
if s.IsNil() {
return uuid.NullUUID{}, nil
}
id, err := uuid.Parse(s.value.GetValue())
if err != nil {
return uuid.NullUUID{}, err
}
return uuid.NullUUID{UUID: id, Valid: true}, nil
}
func (s *stringValuer) ToNullUUIDOrNil() uuid.NullUUID {
id, _ := s.ToNullUUID() //nolint:errcheck
return id
}
60 changes: 60 additions & 0 deletions uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package convpb

import (
"github.com/google/uuid"
"google.golang.org/protobuf/types/known/wrapperspb"
)

type UUIDWrapper interface {
Commoner[UUIDWrapper]
CondSetNil(cond uuid.UUID) UUIDWrapper
ToStringValue() *wrapperspb.StringValue
ToString() string
}

type uuidWrapper struct {
value uuid.NullUUID
}

func (s *uuidWrapper) IsNil() bool {
return s == nil || !s.value.Valid
}
func (s *uuidWrapper) Clone() UUIDWrapper {
if s.IsNil() {
return &uuidWrapper{}
}
var c uuid.UUID
copy(c[:], s.value.UUID[:])
return &uuidWrapper{value: uuid.NullUUID{UUID: c, Valid: true}}
}

func (s *uuidWrapper) EmptySetNil() UUIDWrapper {
if s.IsNil() {
return s
}
if s.value.UUID == uuid.Nil {
s.value = uuid.NullUUID{}
}
return s
}
func (s *uuidWrapper) CondSetNil(cond uuid.UUID) UUIDWrapper {
if s.IsNil() {
return s
}
if s.value.UUID == cond {
s.value = uuid.NullUUID{}
}
return s
}
func (s *uuidWrapper) ToStringValue() *wrapperspb.StringValue {
if s.IsNil() {
return nil
}
return wrapperspb.String(s.value.UUID.String())
}
func (s *uuidWrapper) ToString() string {
if s.IsNil() {
return ""
}
return s.value.UUID.String()
}

0 comments on commit f0076e1

Please sign in to comment.