This is an automated email from the ASF dual-hosted git repository.

jiayuliu pushed a commit to branch add-uuid-go
in repository https://gitbox.apache.org/repos/asf/thrift.git

commit 9a701cb851b6cee535dc3cf1de2ccc611d69de43
Author: Jiayu Liu <[email protected]>
AuthorDate: Sun Oct 9 02:21:15 2022 +0000

    add uuid support in go
---
 go.mod                                |  2 ++
 go.sum                                |  2 ++
 lib/go/thrift/binary_protocol.go      | 13 +++++++++++++
 lib/go/thrift/compact_protocol.go     | 17 +++++++++++++++++
 lib/go/thrift/debug_protocol.go       | 22 ++++++++++++++++++++++
 lib/go/thrift/header_protocol.go      | 10 ++++++++++
 lib/go/thrift/json_protocol.go        | 15 ++++++++++++++-
 lib/go/thrift/protocol.go             |  4 ++++
 lib/go/thrift/simple_json_protocol.go | 15 ++++++++++++++-
 lib/go/thrift/type.go                 |  7 ++++---
 10 files changed, 102 insertions(+), 5 deletions(-)

diff --git a/go.mod b/go.mod
index 2d3d1c662..b160ad181 100644
--- a/go.mod
+++ b/go.mod
@@ -3,3 +3,5 @@ module github.com/apache/thrift
 go 1.18
 
 require github.com/golang/mock v1.5.0
+
+require github.com/google/uuid v1.3.0 // indirect
diff --git a/go.sum b/go.sum
index cfde58466..3f616cfae 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,7 @@
 github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
 github.com/golang/mock v1.5.0/go.mod 
h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
+github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
+github.com/google/uuid v1.3.0/go.mod 
h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 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/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
diff --git a/lib/go/thrift/binary_protocol.go b/lib/go/thrift/binary_protocol.go
index c6fae0f59..6c1641805 100644
--- a/lib/go/thrift/binary_protocol.go
+++ b/lib/go/thrift/binary_protocol.go
@@ -26,6 +26,8 @@ import (
        "fmt"
        "io"
        "math"
+
+       "github.com/google/uuid"
 )
 
 type TBinaryProtocol struct {
@@ -242,6 +244,11 @@ func (p *TBinaryProtocol) WriteDouble(ctx context.Context, 
value float64) error
        return p.WriteI64(ctx, int64(math.Float64bits(value)))
 }
 
+func (p *TBinaryProtocol) WriteUuid(ctx context.Context, value uuid.UUID) 
error {
+       _, err := p.trans.Write(value[:])
+       return NewTProtocolException(err)
+}
+
 func (p *TBinaryProtocol) WriteString(ctx context.Context, value string) error 
{
        e := p.WriteI32(ctx, int32(len(value)))
        if e != nil {
@@ -453,6 +460,12 @@ func (p *TBinaryProtocol) ReadDouble(ctx context.Context) 
(value float64, err er
        return value, err
 }
 
+func (p *TBinaryProtocol) ReadUuid(ctx context.Context) (value uuid.UUID, err 
error) {
+       buf := p.buffer[0:16]
+       err = p.readAll(ctx, buf)
+       return uuid.ParseBytes(buf)
+}
+
 func (p *TBinaryProtocol) ReadString(ctx context.Context) (value string, err 
error) {
        size, e := p.ReadI32(ctx)
        if e != nil {
diff --git a/lib/go/thrift/compact_protocol.go 
b/lib/go/thrift/compact_protocol.go
index dc86fe605..04d0a53d0 100644
--- a/lib/go/thrift/compact_protocol.go
+++ b/lib/go/thrift/compact_protocol.go
@@ -26,6 +26,8 @@ import (
        "fmt"
        "io"
        "math"
+
+       "github.com/google/uuid"
 )
 
 const (
@@ -328,6 +330,11 @@ func (p *TCompactProtocol) WriteDouble(ctx 
context.Context, value float64) error
        return NewTProtocolException(err)
 }
 
+func (p *TCompactProtocol) WriteUuid(ctx context.Context, value uuid.UUID) 
error {
+       _, err := p.trans.Write(value[:])
+       return NewTProtocolException(err)
+}
+
 // Write a string to the wire with a varint size preceding.
 func (p *TCompactProtocol) WriteString(ctx context.Context, value string) 
error {
        _, e := p.writeVarint32(int32(len(value)))
@@ -621,6 +628,16 @@ func (p *TCompactProtocol) ReadString(ctx context.Context) 
(value string, err er
        return string(buf), NewTProtocolException(e)
 }
 
+func (p *TCompactProtocol) ReadUuid(ctx context.Context) (value uuid.UUID, err 
error) {
+       if buf, e := safeReadBytes(16, p.trans); e != nil {
+               return uuid.Nil, NewTProtocolException(e)
+       } else if value, e := uuid.ParseBytes(buf); e != nil {
+               return uuid.Nil, NewTProtocolException(e)
+       } else {
+               return value, nil
+       }
+}
+
 // Read a []byte from the wire.
 func (p *TCompactProtocol) ReadBinary(ctx context.Context) (value []byte, err 
error) {
        length, e := p.readVarint32()
diff --git a/lib/go/thrift/debug_protocol.go b/lib/go/thrift/debug_protocol.go
index c1f4fab4c..65bcb5663 100644
--- a/lib/go/thrift/debug_protocol.go
+++ b/lib/go/thrift/debug_protocol.go
@@ -22,6 +22,8 @@ package thrift
 import (
        "context"
        "fmt"
+
+       "github.com/google/uuid"
 )
 
 type TDebugProtocol struct {
@@ -234,6 +236,16 @@ func (tdp *TDebugProtocol) WriteI64(ctx context.Context, 
value int64) error {
        }
        return err
 }
+
+func (tdp *TDebugProtocol) WriteUuid(ctx context.Context, value uuid.UUID) 
error {
+       err := tdp.Delegate.WriteUuid(ctx, value)
+       tdp.logf("%sWriteUuid(value=%#v) => %#v", tdp.LogPrefix, value, err)
+       if tdp.DuplicateTo != nil {
+               tdp.DuplicateTo.WriteUuid(ctx, value)
+       }
+       return err
+}
+
 func (tdp *TDebugProtocol) WriteDouble(ctx context.Context, value float64) 
error {
        err := tdp.Delegate.WriteDouble(ctx, value)
        tdp.logf("%sWriteDouble(value=%#v) => %#v", tdp.LogPrefix, value, err)
@@ -411,6 +423,16 @@ func (tdp *TDebugProtocol) ReadString(ctx context.Context) 
(value string, err er
        }
        return
 }
+
+func (tdp *TDebugProtocol) ReadUuid(ctx context.Context) (value uuid.UUID, err 
error) {
+       value, err = tdp.Delegate.ReadUuid(ctx)
+       tdp.logf("%sReadUuid() (value=%#v, err=%#v)", tdp.LogPrefix, value, err)
+       if tdp.DuplicateTo != nil {
+               tdp.DuplicateTo.WriteUuid(ctx, value)
+       }
+       return
+}
+
 func (tdp *TDebugProtocol) ReadBinary(ctx context.Context) (value []byte, err 
error) {
        value, err = tdp.Delegate.ReadBinary(ctx)
        tdp.logf("%sReadBinary() (value=%#v, err=%#v)", tdp.LogPrefix, value, 
err)
diff --git a/lib/go/thrift/header_protocol.go b/lib/go/thrift/header_protocol.go
index 878041f8d..3b3563859 100644
--- a/lib/go/thrift/header_protocol.go
+++ b/lib/go/thrift/header_protocol.go
@@ -22,6 +22,8 @@ package thrift
 import (
        "context"
        "errors"
+
+       "github.com/google/uuid"
 )
 
 // THeaderProtocol is a thrift protocol that implements THeader:
@@ -213,6 +215,10 @@ func (p *THeaderProtocol) WriteDouble(ctx context.Context, 
value float64) error
        return p.protocol.WriteDouble(ctx, value)
 }
 
+func (p *THeaderProtocol) WriteUuid(ctx context.Context, value uuid.UUID) 
error {
+       return p.protocol.WriteUuid(ctx, value)
+}
+
 func (p *THeaderProtocol) WriteString(ctx context.Context, value string) error 
{
        return p.protocol.WriteString(ctx, value)
 }
@@ -326,6 +332,10 @@ func (p *THeaderProtocol) ReadDouble(ctx context.Context) 
(value float64, err er
        return p.protocol.ReadDouble(ctx)
 }
 
+func (p *THeaderProtocol) ReadUuid(ctx context.Context) (value uuid.UUID, err 
error) {
+       return p.protocol.ReadUuid(ctx)
+}
+
 func (p *THeaderProtocol) ReadString(ctx context.Context) (value string, err 
error) {
        return p.protocol.ReadString(ctx)
 }
diff --git a/lib/go/thrift/json_protocol.go b/lib/go/thrift/json_protocol.go
index d248ecfee..a75647a7b 100644
--- a/lib/go/thrift/json_protocol.go
+++ b/lib/go/thrift/json_protocol.go
@@ -23,6 +23,8 @@ import (
        "context"
        "encoding/base64"
        "fmt"
+
+       "github.com/google/uuid"
 )
 
 const (
@@ -33,7 +35,6 @@ const (
 
 // JSON protocol implementation for thrift.
 // Utilizes Simple JSON protocol
-//
 type TJSONProtocol struct {
        *TSimpleJSONProtocol
 }
@@ -189,6 +190,10 @@ func (p *TJSONProtocol) WriteDouble(ctx context.Context, v 
float64) error {
        return p.OutputF64(v)
 }
 
+func (p *TJSONProtocol) WriteUuid(ctx context.Context, uuid uuid.UUID) error {
+       return p.WriteString(ctx, uuid.String())
+}
+
 func (p *TJSONProtocol) WriteString(ctx context.Context, v string) error {
        return p.OutputString(v)
 }
@@ -379,6 +384,14 @@ func (p *TJSONProtocol) ReadDouble(ctx context.Context) 
(float64, error) {
        return v, err
 }
 
+func (p *TJSONProtocol) ReadUuid(ctx context.Context) (uuid.UUID, error) {
+       if s, err := p.ReadString(ctx); err != nil {
+               return uuid.Nil, err
+       } else {
+               return uuid.Parse(s)
+       }
+}
+
 func (p *TJSONProtocol) ReadString(ctx context.Context) (string, error) {
        var v string
        if err := p.ParsePreValue(); err != nil {
diff --git a/lib/go/thrift/protocol.go b/lib/go/thrift/protocol.go
index 647c0bdd6..7ba86196f 100644
--- a/lib/go/thrift/protocol.go
+++ b/lib/go/thrift/protocol.go
@@ -23,6 +23,8 @@ import (
        "context"
        "errors"
        "fmt"
+
+       "github.com/google/uuid"
 )
 
 const (
@@ -51,6 +53,7 @@ type TProtocol interface {
        WriteI64(ctx context.Context, value int64) error
        WriteDouble(ctx context.Context, value float64) error
        WriteString(ctx context.Context, value string) error
+       WriteUuid(ctx context.Context, value uuid.UUID) error
        WriteBinary(ctx context.Context, value []byte) error
 
        ReadMessageBegin(ctx context.Context) (name string, typeId 
TMessageType, seqid int32, err error)
@@ -71,6 +74,7 @@ type TProtocol interface {
        ReadI32(ctx context.Context) (value int32, err error)
        ReadI64(ctx context.Context) (value int64, err error)
        ReadDouble(ctx context.Context) (value float64, err error)
+       ReadUuid(ctx context.Context) (value uuid.UUID, err error)
        ReadString(ctx context.Context) (value string, err error)
        ReadBinary(ctx context.Context) (value []byte, err error)
 
diff --git a/lib/go/thrift/simple_json_protocol.go 
b/lib/go/thrift/simple_json_protocol.go
index 5cefb600a..c299c9570 100644
--- a/lib/go/thrift/simple_json_protocol.go
+++ b/lib/go/thrift/simple_json_protocol.go
@@ -30,6 +30,8 @@ import (
        "io"
        "math"
        "strconv"
+
+       "github.com/google/uuid"
 )
 
 type _ParseContext int
@@ -93,7 +95,6 @@ var errEmptyJSONContextStack = 
NewTProtocolExceptionWithType(INVALID_DATA, error
 // This protocol produces/consumes a simple output format
 // suitable for parsing by scripting languages.  It should not be
 // confused with the full-featured TJSONProtocol.
-//
 type TSimpleJSONProtocol struct {
        trans TTransport
 
@@ -316,6 +317,10 @@ func (p *TSimpleJSONProtocol) WriteString(ctx 
context.Context, v string) error {
        return p.OutputString(v)
 }
 
+func (p *TSimpleJSONProtocol) WriteUuid(ctx context.Context, v uuid.UUID) 
error {
+       return p.WriteString(ctx, v.String())
+}
+
 func (p *TSimpleJSONProtocol) WriteBinary(ctx context.Context, v []byte) error 
{
        // JSON library only takes in a string,
        // not an arbitrary byte array, to ensure bytes are transmitted
@@ -534,6 +539,14 @@ func (p *TSimpleJSONProtocol) ReadDouble(ctx 
context.Context) (float64, error) {
        return v, err
 }
 
+func (p *TSimpleJSONProtocol) ReadUuid(ctx context.Context) (uuid.UUID, error) 
{
+       if s, err := p.ReadString(ctx); err != nil {
+               return uuid.Nil, err
+       } else {
+               return uuid.Parse(s)
+       }
+}
+
 func (p *TSimpleJSONProtocol) ReadString(ctx context.Context) (string, error) {
        var v string
        if err := p.ParsePreValue(); err != nil {
diff --git a/lib/go/thrift/type.go b/lib/go/thrift/type.go
index 4292ffcad..8c9046d47 100644
--- a/lib/go/thrift/type.go
+++ b/lib/go/thrift/type.go
@@ -39,8 +39,9 @@ const (
        SET    = 14
        LIST   = 15
        UTF8   = 16
-       UTF16  = 17
-       //BINARY = 18   wrong and unusued
+       UUID   = 17
+       //UTF16  = 17   wrong and unused
+       //BINARY = 18   wrong and unused
 )
 
 var typeNames = map[int]string{
@@ -58,7 +59,7 @@ var typeNames = map[int]string{
        SET:    "SET",
        LIST:   "LIST",
        UTF8:   "UTF8",
-       UTF16:  "UTF16",
+       UUID:   "UUID",
 }
 
 func (p TType) String() string {

Reply via email to