Skip to content

Commit

Permalink
feat: add Clone() function (#83)
Browse files Browse the repository at this point in the history
add new `csproto.Clone()` function and associated tests
add `go.work*` to `.gitignore`
  • Loading branch information
dylan-bourque authored Nov 14, 2022
1 parent 17e64cd commit 4b67217
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ dist/

# editor-related files
.vscode/

# Go workspace files
go.work*
25 changes: 25 additions & 0 deletions clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package csproto

import (
gogo "github.com/gogo/protobuf/proto"
google "github.com/golang/protobuf/proto" //nolint: staticcheck // we're using this deprecated package intentionally
googlev2 "google.golang.org/protobuf/proto"
)

// Clone returns a deep copy of m, delegating to the appropriate underlying Protobuf API based on
// the concrete type of m. Since the underlying runtimes return different types, this function returns
// interface{} and the caller will need to type-assert back to the concrete type of m.
//
// If m is not one of the supported message types, this function returns nil.
func Clone(m interface{}) interface{} {
switch MsgType(m) {
case MessageTypeGoogle:
return googlev2.Clone(m.(googlev2.Message))
case MessageTypeGoogleV1:
return google.Clone(m.(google.Message))
case MessageTypeGogo:
return gogo.Clone(m.(gogo.Message))
default:
return nil
}
}
10 changes: 10 additions & 0 deletions example/proto2_gogo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"
"time"
"unsafe"

"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
Expand Down Expand Up @@ -251,6 +252,15 @@ func TestProto2GogoEqual(t *testing.T) {
assert.True(t, csproto.Equal(m1, m2), "messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
}

func TestProto2GogoClone(t *testing.T) {
m1 := createTestProto2GogoMessage()
m2, ok := csproto.Clone(m1).(*gogo.BaseEvent)

assert.True(t, ok, "type assertion to *gogo.BaseEvent should succeed")
assert.True(t, csproto.Equal(m1, m2), "cloned messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
assert.NotEqual(t, unsafe.Pointer(m1), unsafe.Pointer(m2))
}

func createTestProto2GogoMessage() *gogo.BaseEvent {
now := uint64(time.Now().UTC().Unix())
et := gogo.EventType_EVENT_TYPE_ONE
Expand Down
10 changes: 10 additions & 0 deletions example/proto2_googlev1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"
"time"
"unsafe"

"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -278,6 +279,15 @@ func TestProto2GoogleV1Equal(t *testing.T) {
assert.True(t, csproto.Equal(m1, m2), "messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
}

func TestProto2GoogleV1Clone(t *testing.T) {
m1 := createTestProto2GoogleV1Message()
m2, ok := csproto.Clone(m1).(*googlev1.BaseEvent)

assert.True(t, ok, "type assertion to *gogo.BaseEvent should succeed")
assert.True(t, csproto.Equal(m1, m2), "cloned messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
assert.NotEqual(t, unsafe.Pointer(m1), unsafe.Pointer(m2))
}

func createTestProto2GoogleV1Message() *googlev1.BaseEvent {
now := uint64(time.Now().UTC().Unix())
et := googlev1.EventType_EVENT_TYPE_ONE
Expand Down
10 changes: 10 additions & 0 deletions example/proto2_googlev2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"
"time"
"unsafe"

"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -278,6 +279,15 @@ func TestProto2GoogleV2Equal(t *testing.T) {
assert.True(t, csproto.Equal(m1, m2), "messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
}

func TestProto2GoogleV2Clone(t *testing.T) {
m1 := createTestProto2GoogleV2Message()
m2, ok := csproto.Clone(m1).(*googlev2.BaseEvent)

assert.True(t, ok, "type assertion to *googlev2.BaseEvent should succeed")
assert.True(t, csproto.Equal(m1, m2), "cloned messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
assert.NotEqual(t, unsafe.Pointer(m1), unsafe.Pointer(m2))
}

func createTestProto2GoogleV2Message() *googlev2.BaseEvent {
now := uint64(time.Now().UTC().Unix())
et := googlev2.EventType_EVENT_TYPE_ONE
Expand Down
10 changes: 10 additions & 0 deletions example/proto3_gogo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"
"time"
"unsafe"

"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
Expand Down Expand Up @@ -248,6 +249,15 @@ func TestProto3GogoEqual(t *testing.T) {
assert.True(t, csproto.Equal(m1, m2), "messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
}

func TestProto3GogoClone(t *testing.T) {
m1 := createTestProto3GogoMessage()
m2, ok := csproto.Clone(m1).(*gogo.TestEvent)

assert.True(t, ok, "type assertion to *gogo.TestEvent should succeed")
assert.True(t, csproto.Equal(m1, m2), "cloned messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
assert.NotEqual(t, unsafe.Pointer(m1), unsafe.Pointer(m2))
}

func createTestProto3GogoMessage() *gogo.TestEvent {
event := gogo.TestEvent{
Name: "test",
Expand Down
10 changes: 10 additions & 0 deletions example/proto3_googlev1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"
"time"
"unsafe"

"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -256,6 +257,15 @@ func TestProto3GoogleV1Equal(t *testing.T) {
assert.True(t, csproto.Equal(m1, m2), "messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
}

func TestProto3GoogleV1Clone(t *testing.T) {
m1 := createTestProto3GoogleV1Message()
m2, ok := csproto.Clone(m1).(*googlev1.TestEvent)

assert.True(t, ok, "type assertion to *googlev1.TestEvent should succeed")
assert.True(t, csproto.Equal(m1, m2), "cloned messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
assert.NotEqual(t, unsafe.Pointer(m1), unsafe.Pointer(m2))
}

func createTestProto3GoogleV1Message() *googlev1.TestEvent {
event := googlev1.TestEvent{
Name: "test",
Expand Down
10 changes: 10 additions & 0 deletions example/proto3_googlev2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"
"time"
"unsafe"

"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -256,6 +257,15 @@ func TestProto3GoogleV2Equal(t *testing.T) {
assert.True(t, csproto.Equal(m1, m2), "messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
}

func TestProto3GoogleV2Clone(t *testing.T) {
m1 := createTestProto3GoogleV2Message()
m2, ok := csproto.Clone(m1).(*googlev2.TestEvent)

assert.True(t, ok, "type assertion to *googlev2.TestEvent should succeed")
assert.True(t, csproto.Equal(m1, m2), "cloned messages should be equal\nm1=%s\nm2=%s", m1.String(), m2.String())
assert.NotEqual(t, unsafe.Pointer(m1), unsafe.Pointer(m2))
}

func createTestProto3GoogleV2Message() *googlev2.TestEvent {
event := googlev2.TestEvent{
Name: "test",
Expand Down

0 comments on commit 4b67217

Please sign in to comment.