Skip to content

Commit

Permalink
Consolidate IRequest/IResponse/WritableStruct/ReadableStruct
Browse files Browse the repository at this point in the history
Summary:
No need for two sets of identical interfaces.
Keep only one for clarity and consistency.

Reviewed By: podtserkovskiy

Differential Revision: D69505948

fbshipit-source-id: 3de3d9751e8bc38557c4ebc545de92b0fb1c6b7c
  • Loading branch information
echistyakov authored and facebook-github-bot committed Feb 12, 2025
1 parent 9157e92 commit c8604d1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 34 deletions.
6 changes: 2 additions & 4 deletions third-party/thrift/src/thrift/lib/go/thrift/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ type Exception = types.Exception

type WritableStruct = types.WritableStruct

type ReadableStruct = types.ReadableStruct

type Encoder = types.Encoder

type ApplicationException = types.ApplicationException

type Type = types.Type

type IRequest = types.IRequest

type IResponse = types.IResponse

type FormatID = types.ProtocolID

type Format = types.Format
Expand Down
4 changes: 2 additions & 2 deletions third-party/thrift/src/thrift/lib/go/thrift/clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (cc *ClientConn) Close() error {
}

// SendMsg sends a request to a given thrift endpoint
func (cc *ClientConn) SendMsg(ctx context.Context, method string, req types.IRequest, msgType types.MessageType) error {
func (cc *ClientConn) SendMsg(ctx context.Context, method string, req types.WritableStruct, msgType types.MessageType) error {
cc.seqID++

if err := types.SetRequestHeaders(ctx, cc.proto); err != nil {
Expand All @@ -65,7 +65,7 @@ func (cc *ClientConn) SendMsg(ctx context.Context, method string, req types.IReq
}

// RecvMsg receives the response from a call to a thrift endpoint
func (cc *ClientConn) RecvMsg(ctx context.Context, method string, res types.IResponse) error {
func (cc *ClientConn) RecvMsg(ctx context.Context, method string, res types.ReadableStruct) error {
recvMethod, mTypeID, seqID, err := cc.proto.ReadMessageBegin()

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ var errFakeProtoWriteMessageEnd = errors.New("error writing message end from Fak
var errFakeProtoFlush = errors.New("error flushing FakeProto")

type fakeResponse struct {
types.IResponse
types.ReadableStruct
shouldReturnError bool
}

type fakeRequest struct {
types.IRequest
types.WritableStruct
shouldReturnError bool
}

Expand Down Expand Up @@ -100,7 +100,7 @@ func (f *fakeResponse) Read(proto types.Decoder) error {
func TestSendMsgError(t *testing.T) {
testCases := []struct {
proto types.Protocol
request types.IRequest
request types.WritableStruct
expected error
}{
// Bad WriteMessageBegin
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestSendMsgError(t *testing.T) {
func TestRecvMsgError(t *testing.T) {
testCases := []struct {
proto types.Protocol
response types.IResponse
response types.ReadableStruct
expected error
}{
// Error reading message begin
Expand Down
10 changes: 0 additions & 10 deletions third-party/thrift/src/thrift/lib/go/thrift/types/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ type Encoder interface {
Flush() (err error)
}

// IRequest represents a request to be sent to a thrift endpoint
type IRequest interface {
Write(p Encoder) error
}

// IResponse represents a response received from a thrift call
type IResponse interface {
Read(p Decoder) error
}

// The maximum recursive depth the skip() function will traverse
const DEFAULT_RECURSION_DEPTH = 64

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ import "context"
type RequestChannel interface {
ClientInterface

Call(ctx context.Context, method string, request IRequest, response IResponse) error
Oneway(ctx context.Context, method string, request IRequest) error
Call(ctx context.Context, method string, request WritableStruct, response ReadableStruct) error
Oneway(ctx context.Context, method string, request WritableStruct) error
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewSerialChannel(protocol Protocol) *SerialChannel {
}
}

func (c *SerialChannel) sendMsg(ctx context.Context, method string, request IRequest, msgType MessageType) (int32, error) {
func (c *SerialChannel) sendMsg(ctx context.Context, method string, request WritableStruct, msgType MessageType) (int32, error) {
c.seqID++
seqID := c.seqID

Expand All @@ -61,7 +61,7 @@ func (c *SerialChannel) sendMsg(ctx context.Context, method string, request IReq
return seqID, c.protocol.Flush()
}

func (c *SerialChannel) recvMsg(method string, seqID int32, response IResponse) error {
func (c *SerialChannel) recvMsg(method string, seqID int32, response ReadableStruct) error {
// TODO: Implement per-call cancellation for a SerialChannel
recvMethod, mTypeID, msgSeqID, err := c.protocol.ReadMessageBegin()

Expand Down Expand Up @@ -111,7 +111,7 @@ func (c *SerialChannel) Close() error {

// Call will call the given method with the given thrift struct, and read the response
// into the given response struct. It only allows one outstanding request at once, but is thread-safe.
func (c *SerialChannel) Call(ctx context.Context, method string, request IRequest, response IResponse) error {
func (c *SerialChannel) Call(ctx context.Context, method string, request WritableStruct, response ReadableStruct) error {
c.lock.Lock()
defer c.lock.Unlock()

Expand All @@ -130,7 +130,7 @@ func (c *SerialChannel) Call(ctx context.Context, method string, request IReques

// Oneway will call the given method with the given thrift struct. It returns immediately when the request is sent.
// It only allows one outstanding request at once, but is thread-safe.
func (c *SerialChannel) Oneway(ctx context.Context, method string, request IRequest) error {
func (c *SerialChannel) Oneway(ctx context.Context, method string, request WritableStruct) error {
c.lock.Lock()
defer c.lock.Unlock()

Expand Down
21 changes: 13 additions & 8 deletions third-party/thrift/src/thrift/lib/go/thrift/types/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@

package types

// WritableStruct is an interface used to encapsulate a message that can be written to a protocol
// WritableStruct is an interface used to encapsulate a message that can be written to an Encoder
type WritableStruct interface {
Write(p Encoder) error
Write(Encoder) error
}

// ReadableStruct is an interface used to encapsulate a message that can be read from a Decoder
type ReadableStruct interface {
Read(Decoder) error
}

// Struct is the interface used to encapsulate a message that can be read and written to/from Encoder/Decoder
type Struct interface {
WritableStruct
ReadableStruct
}

// WritableException is an interface used to encapsulate an exception that can be written to a protocol
Expand All @@ -32,9 +43,3 @@ type WritableResult interface {
WritableStruct
Exception() WritableException
}

// Struct is the interface used to encapsulate a message that can be read and written to a protocol
type Struct interface {
Write(p Encoder) error
Read(p Decoder) error
}

0 comments on commit c8604d1

Please sign in to comment.