Skip to content

Commit

Permalink
fix: only add IsWithoutWrapping method
Browse files Browse the repository at this point in the history
  • Loading branch information
Marina-Sakai committed Nov 14, 2024
1 parent 6c88324 commit 4ef13e1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
28 changes: 6 additions & 22 deletions thrift/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ func (f FieldDescriptor) DefaultValue() *DefaultValue {
type FunctionDescriptor struct {
oneway bool
hasRequestBase bool
request *StructWrappedTypeDescriptor
response *StructWrappedTypeDescriptor
request *TypeDescriptor
response *TypeDescriptor
name string
endpoints []http.Endpoint
annotations []parser.Annotation
Expand All @@ -360,20 +360,12 @@ func (f FunctionDescriptor) HasRequestBase() bool {
// Request returns the request type descriptor of the function
// The request arguements is mapped with arguement id and name
func (f FunctionDescriptor) Request() *TypeDescriptor {
return f.request.tyDsc
return f.request
}

// Response returns the response type descriptor of the function
// The response arguements is mapped with arguement id
func (f FunctionDescriptor) Response() *TypeDescriptor {
return f.response.tyDsc
}

func (f FunctionDescriptor) StructWrappedRequest() *StructWrappedTypeDescriptor {
return f.request
}

func (f FunctionDescriptor) StructWrappedResponse() *StructWrappedTypeDescriptor {
return f.response
}

Expand All @@ -397,17 +389,9 @@ func (f FunctionDescriptor) IsServerStreaming() bool {
return f.isServerStreaming
}

type StructWrappedTypeDescriptor struct {
tyDsc *TypeDescriptor
isWrapped bool
}

func (s *StructWrappedTypeDescriptor) TypeDescriptor() *TypeDescriptor {
return s.tyDsc
}

func (s *StructWrappedTypeDescriptor) IsWrapped() bool {
return s.isWrapped
// IsWithoutWrapping returns if the request and response are not wrapped in struct
func (f FunctionDescriptor) IsWithoutWrapping() bool {
return f.isClientStreaming || f.isServerStreaming
}

// ServiceDescriptor is the runtime descriptor of a service
Expand Down
16 changes: 8 additions & 8 deletions thrift/idl.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ func addFunction(ctx context.Context, fn *parser.Function, tree *parser.Thrift,
isStreaming := st.ClientStreaming || st.ServerStreaming

var hasRequestBase bool
var req *StructWrappedTypeDescriptor
var resp *StructWrappedTypeDescriptor
var req *TypeDescriptor
var resp *TypeDescriptor

// parse request field
if opts.ParseFunctionMode != meta.ParseResponseOnly {
Expand Down Expand Up @@ -414,7 +414,7 @@ func addFunction(ctx context.Context, fn *parser.Function, tree *parser.Thrift,
return nil
}

func parseRequest(ctx context.Context, isStreaming bool, fn *parser.Function, tree *parser.Thrift, structsCache compilingCache, nextAnns []parser.Annotation, opts Options) (req *StructWrappedTypeDescriptor, hasRequestBase bool, err error) {
func parseRequest(ctx context.Context, isStreaming bool, fn *parser.Function, tree *parser.Thrift, structsCache compilingCache, nextAnns []parser.Annotation, opts Options) (req *TypeDescriptor, hasRequestBase bool, err error) {
// WARN: only support single argument
reqAst := fn.Arguments[0]
reqType, err := parseType(ctx, reqAst.Type, tree, structsCache, 0, opts, nextAnns, Request)
Expand All @@ -432,7 +432,7 @@ func parseRequest(ctx context.Context, isStreaming bool, fn *parser.Function, tr
}

if isStreaming {
return &StructWrappedTypeDescriptor{tyDsc: reqType, isWrapped: false}, hasRequestBase, nil
return reqType, hasRequestBase, nil
}

// wrap with a struct
Expand All @@ -453,18 +453,18 @@ func parseRequest(ctx context.Context, isStreaming bool, fn *parser.Function, tr
wrappedTyDsc.Struct().ids.Set(int32(reqAst.ID), unsafe.Pointer(reqField))
wrappedTyDsc.Struct().names.Set(reqAst.Name, unsafe.Pointer(reqField))
wrappedTyDsc.Struct().names.Build()
return &StructWrappedTypeDescriptor{tyDsc: wrappedTyDsc, isWrapped: true}, hasRequestBase, nil
return wrappedTyDsc, hasRequestBase, nil
}

func parseResponse(ctx context.Context, isStreaming bool, fn *parser.Function, tree *parser.Thrift, structsCache compilingCache, nextAnns []parser.Annotation, opts Options) (resp *StructWrappedTypeDescriptor, err error) {
func parseResponse(ctx context.Context, isStreaming bool, fn *parser.Function, tree *parser.Thrift, structsCache compilingCache, nextAnns []parser.Annotation, opts Options) (resp *TypeDescriptor, err error) {
respAst := fn.FunctionType
respType, err := parseType(ctx, respAst, tree, structsCache, 0, opts, nextAnns, Response)
if err != nil {
return nil, err
}

if isStreaming {
return &StructWrappedTypeDescriptor{tyDsc: respType, isWrapped: false}, nil
return respType, nil
}

wrappedResp := &TypeDescriptor{
Expand Down Expand Up @@ -502,7 +502,7 @@ func parseResponse(ctx context.Context, isStreaming bool, fn *parser.Function, t
wrappedResp.Struct().names.Set(exp.Name, unsafe.Pointer(exceptionField))
}
wrappedResp.Struct().names.Build()
return &StructWrappedTypeDescriptor{tyDsc: wrappedResp, isWrapped: true}, nil
return wrappedResp, nil
}

// reuse builtin types
Expand Down
4 changes: 2 additions & 2 deletions thrift/idl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,8 @@ func TestStreamingFunctionDescriptorFromContent(t *testing.T) {
require.Equal(t, true, dsc.Functions()["EchoServer"].IsServerStreaming())
require.Equal(t, false, dsc.Functions()["EchoUnary"].IsClientStreaming())
require.Equal(t, true, dsc.Functions()["EchoBizException"].IsClientStreaming())
require.Equal(t, false, dsc.Functions()["EchoClient"].StructWrappedRequest().IsWrapped())
require.Equal(t, true, dsc.Functions()["EchoClient"].IsWithoutWrapping())
require.Equal(t, "Request", dsc.Functions()["EchoClient"].Request().Struct().Name())
require.Equal(t, true, dsc.Functions()["EchoUnary"].StructWrappedRequest().IsWrapped())
require.Equal(t, false, dsc.Functions()["EchoUnary"].IsWithoutWrapping())
require.Equal(t, "", dsc.Functions()["EchoUnary"].Request().Struct().Name())
}

0 comments on commit 4ef13e1

Please sign in to comment.