Skip to content

Commit

Permalink
Merge pull request #1 from xmtp/switch-to-google-proto
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas authored May 11, 2022
2 parents 3b2df50 + e356f21 commit 27ce145
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 82 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/libp2p/go-msgio
module github.com/xmtp/go-msgio

go 1.17

require (
github.com/gogo/protobuf v1.3.2
github.com/libp2p/go-buffer-pool v0.0.2
github.com/multiformats/go-varint v0.0.6
google.golang.org/protobuf v1.28.0
)
37 changes: 7 additions & 30 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
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/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs=
github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM=
github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY=
github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/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.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
2 changes: 1 addition & 1 deletion msgio/msgio.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"strings"

msgio "github.com/libp2p/go-msgio"
msgio "github.com/xmtp/go-msgio"
)

var Args ArgType
Expand Down
16 changes: 1 addition & 15 deletions protoio/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ package protoio
import (
"io"

"github.com/gogo/protobuf/proto"
"google.golang.org/protobuf/proto"
)

type Writer interface {
Expand All @@ -55,17 +55,3 @@ type ReadCloser interface {
Reader
io.Closer
}

func getSize(v interface{}) (int, bool) {
if sz, ok := v.(interface {
Size() (n int)
}); ok {
return sz.Size(), true
} else if sz, ok := v.(interface {
ProtoSize() (n int)
}); ok {
return sz.ProtoSize(), true
} else {
return 0, false
}
}
3 changes: 1 addition & 2 deletions protoio/uvarint_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ import (
"os"
"runtime/debug"

"github.com/gogo/protobuf/proto"

"github.com/multiformats/go-varint"
"google.golang.org/protobuf/proto"
)

type uvarintReader struct {
Expand Down
203 changes: 191 additions & 12 deletions protoio/uvarint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ package protoio_test

import (
"bytes"
"fmt"
"io"
"math/rand"
"testing"
"time"

"github.com/gogo/protobuf/test"
"github.com/xmtp/go-msgio/test"

"github.com/libp2p/go-msgio/protoio"
"github.com/multiformats/go-varint"
"github.com/xmtp/go-msgio/protoio"
)

func TestVarintNormal(t *testing.T) {
Expand Down Expand Up @@ -109,15 +110,7 @@ func iotest(writer protoio.WriteCloser, reader protoio.ReadCloser) error {
msgs := make([]*test.NinOptNative, size)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range msgs {
msgs[i] = test.NewPopulatedNinOptNative(r, true)
// https://github.com/gogo/protobuf/issues/31
if i == 5 {
msgs[i] = &test.NinOptNative{}
}
// https://github.com/gogo/protobuf/issues/31
if i == 999 {
msgs[i] = &test.NinOptNative{}
}
msgs[i] = NewPopulatedNinOptNative(r, true)
err := writer.WriteMsg(msgs[i])
if err != nil {
return err
Expand All @@ -135,7 +128,7 @@ func iotest(writer protoio.WriteCloser, reader protoio.ReadCloser) error {
}
return err
}
if err := msg.VerboseEqual(msgs[i]); err != nil {
if err := verboseEqual(msg, msgs[i]); err != nil {
return err
}
i++
Expand All @@ -148,3 +141,189 @@ func iotest(writer protoio.WriteCloser, reader protoio.ReadCloser) error {
}
return nil
}

// Everything below this line is borrowed from the generated test code in gogoproto
// --------------------------------------------------------------------------------
type randyThetest interface {
Float32() float32
Float64() float64
Int63() int64
Int31() int32
Uint32() uint32
Intn(n int) int
}

func verboseEqual(this *test.NinOptNative, that1 *test.NinOptNative) error {
if that1 == nil {
if this == nil {
return nil
}
return fmt.Errorf("that == nil && this != nil")
}
if that1 == nil {
if this == nil {
return nil
}
return fmt.Errorf("that is type *NidOptNative but is nil && this != nil")
} else if this == nil {
return fmt.Errorf("that is type *NidOptNative but is not nil && this == nil")
}
if this.GetField1() != that1.GetField1() {
return fmt.Errorf("Field1 this(%v) Not Equal that(%v)", this.Field1, that1.Field1)
}
if this.GetField2() != that1.GetField2() {
return fmt.Errorf("Field2 this(%v) Not Equal that(%v)", this.Field2, that1.Field2)
}
if this.GetField3() != that1.GetField3() {
return fmt.Errorf("GetField3() this(%v) Not Equal that(%v)", this.GetField3(), that1.GetField3())
}
if this.GetField4() != that1.GetField4() {
return fmt.Errorf("GetField4() this(%v) Not Equal that(%v)", this.GetField4(), that1.GetField4())
}
if this.GetField5() != that1.GetField5() {
return fmt.Errorf("GetField5() this(%v) Not Equal that(%v)", this.GetField5(), that1.GetField5())
}
if this.GetField6() != that1.GetField6() {
return fmt.Errorf("GetField6() this(%v) Not Equal that(%v)", this.GetField6(), that1.GetField6())
}
if this.GetField7() != that1.GetField7() {
return fmt.Errorf("GetField7() this(%v) Not Equal that(%v)", this.GetField7(), that1.GetField7())
}
if this.GetField8() != that1.GetField8() {
return fmt.Errorf("GetField8() this(%v) Not Equal that(%v)", this.GetField8(), that1.GetField8())
}
if this.GetField9() != that1.GetField9() {
return fmt.Errorf("GetField9() this(%v) Not Equal that(%v)", this.GetField9(), that1.GetField9())
}
if this.GetField10() != that1.GetField10() {
return fmt.Errorf("GetField10() this(%v) Not Equal that(%v)", this.GetField10(), that1.GetField10())
}
if this.GetField11() != that1.GetField11() {
return fmt.Errorf("GetField11() this(%v) Not Equal that(%v)", this.GetField11(), that1.GetField11())
}
if this.GetField12() != that1.GetField12() {
return fmt.Errorf("GetField12() this(%v) Not Equal that(%v)", this.GetField12(), that1.GetField12())
}
if this.GetField13() != that1.GetField13() {
return fmt.Errorf("GetField13() this(%v) Not Equal that(%v)", this.GetField13(), that1.GetField13())
}
if this.GetField14() != that1.GetField14() {
return fmt.Errorf("GetField14() this(%v) Not Equal that(%v)", this.GetField14(), that1.GetField14())
}
if !bytes.Equal(this.Field15, that1.Field15) {
return fmt.Errorf("Field15 this(%v) Not Equal that(%v)", this.Field15, that1.Field15)
}
return nil
}

func NewPopulatedNinOptNative(r randyThetest, easy bool) *test.NinOptNative {
this := &test.NinOptNative{}
if r.Intn(5) != 0 {
v2 := float64(r.Float64())
if r.Intn(2) == 0 {
v2 *= -1
}
this.Field1 = &v2
}
if r.Intn(5) != 0 {
v3 := float32(r.Float32())
if r.Intn(2) == 0 {
v3 *= -1
}
this.Field2 = &v3
}
if r.Intn(5) != 0 {
v4 := int32(r.Int31())
if r.Intn(2) == 0 {
v4 *= -1
}
this.Field3 = &v4
}
if r.Intn(5) != 0 {
v5 := int64(r.Int63())
if r.Intn(2) == 0 {
v5 *= -1
}
this.Field4 = &v5
}
if r.Intn(5) != 0 {
v6 := uint32(r.Uint32())
this.Field5 = &v6
}
if r.Intn(5) != 0 {
v7 := uint64(uint64(r.Uint32()))
this.Field6 = &v7
}
if r.Intn(5) != 0 {
v8 := int32(r.Int31())
if r.Intn(2) == 0 {
v8 *= -1
}
this.Field7 = &v8
}
if r.Intn(5) != 0 {
v9 := int64(r.Int63())
if r.Intn(2) == 0 {
v9 *= -1
}
this.Field8 = &v9
}
if r.Intn(5) != 0 {
v10 := uint32(r.Uint32())
this.Field9 = &v10
}
if r.Intn(5) != 0 {
v11 := int32(r.Int31())
if r.Intn(2) == 0 {
v11 *= -1
}
this.Field10 = &v11
}
if r.Intn(5) != 0 {
v12 := uint64(uint64(r.Uint32()))
this.Field11 = &v12
}
if r.Intn(5) != 0 {
v13 := int64(r.Int63())
if r.Intn(2) == 0 {
v13 *= -1
}
this.Field12 = &v13
}
if r.Intn(5) != 0 {
v14 := bool(bool(r.Intn(2) == 0))
this.Field13 = &v14
}
if r.Intn(5) != 0 {
v15 := string(randStringThetest(r))
this.Field14 = &v15
}
if r.Intn(5) != 0 {
v16 := r.Intn(100)
this.Field15 = make([]byte, v16)
for i := 0; i < v16; i++ {
this.Field15[i] = byte(r.Intn(256))
}
}

return this
}

func randStringThetest(r randyThetest) string {
v258 := r.Intn(100)
tmps := make([]rune, v258)
for i := 0; i < v258; i++ {
tmps[i] = randUTF8RuneThetest(r)
}
return string(tmps)
}

func randUTF8RuneThetest(r randyThetest) rune {
ru := r.Intn(62)
if ru < 10 {
return rune(ru + 48)
} else if ru < 36 {
return rune(ru + 55)
}
return rune(ru + 61)
}
21 changes: 1 addition & 20 deletions protoio/uvarint_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"os"
"runtime/debug"

"github.com/gogo/protobuf/proto"
"google.golang.org/protobuf/proto"

"github.com/multiformats/go-varint"
)
Expand All @@ -62,25 +62,6 @@ func (uw *uvarintWriter) WriteMsg(msg proto.Message) (err error) {
}()

var data []byte
if m, ok := msg.(interface {
MarshalTo(data []byte) (n int, err error)
}); ok {
n, ok := getSize(m)
if ok {
if n+varint.MaxLenUvarint63 >= len(uw.buffer) {
uw.buffer = make([]byte, n+varint.MaxLenUvarint63)
}
lenOff := varint.PutUvarint(uw.buffer, uint64(n))
_, err = m.MarshalTo(uw.buffer[lenOff:])
if err != nil {
return err
}
_, err = uw.w.Write(uw.buffer[:lenOff+n])
return err
}
}

// fallback
data, err = proto.Marshal(msg)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 27ce145

Please sign in to comment.