diff --git a/internal/mocks/thrift/fast/test.go b/internal/mocks/thrift/fast/test.go deleted file mode 100644 index ee1928076a..0000000000 --- a/internal/mocks/thrift/fast/test.go +++ /dev/null @@ -1,1548 +0,0 @@ -/* - * Copyright 2022 CloudWeGo Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Code generated by thriftgo (0.2.1). DO NOT EDIT. - -package fast - -import ( - "context" - "fmt" - "strings" - - "github.com/apache/thrift/lib/go/thrift" -) - -type MockReq struct { - Msg string `thrift:"Msg,1" json:"Msg"` - StrMap map[string]string `thrift:"strMap,2" json:"strMap"` - StrList []string `thrift:"strList,3" json:"strList"` -} - -func NewMockReq() *MockReq { - return &MockReq{} -} - -func (p *MockReq) GetMsg() (v string) { - return p.Msg -} - -func (p *MockReq) GetStrMap() (v map[string]string) { - return p.StrMap -} - -func (p *MockReq) GetStrList() (v []string) { - return p.StrList -} -func (p *MockReq) SetMsg(val string) { - p.Msg = val -} -func (p *MockReq) SetStrMap(val map[string]string) { - p.StrMap = val -} -func (p *MockReq) SetStrList(val []string) { - p.StrList = val -} - -var fieldIDToName_MockReq = map[int16]string{ - 1: "Msg", - 2: "strMap", - 3: "strList", -} - -func (p *MockReq) Read(iprot thrift.TProtocol) (err error) { - - var fieldTypeId thrift.TType - var fieldId int16 - - if _, err = iprot.ReadStructBegin(); err != nil { - goto ReadStructBeginError - } - - for { - _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin() - if err != nil { - goto ReadFieldBeginError - } - if fieldTypeId == thrift.STOP { - break - } - - switch fieldId { - case 1: - if fieldTypeId == thrift.STRING { - if err = p.ReadField1(iprot); err != nil { - goto ReadFieldError - } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - case 2: - if fieldTypeId == thrift.MAP { - if err = p.ReadField2(iprot); err != nil { - goto ReadFieldError - } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - case 3: - if fieldTypeId == thrift.LIST { - if err = p.ReadField3(iprot); err != nil { - goto ReadFieldError - } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - default: - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - - if err = iprot.ReadFieldEnd(); err != nil { - goto ReadFieldEndError - } - } - if err = iprot.ReadStructEnd(); err != nil { - goto ReadStructEndError - } - - return nil -ReadStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) -ReadFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) -ReadFieldError: - return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_MockReq[fieldId]), err) -SkipFieldError: - return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) - -ReadFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) -ReadStructEndError: - return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) -} - -func (p *MockReq) ReadField1(iprot thrift.TProtocol) error { - if v, err := iprot.ReadString(); err != nil { - return err - } else { - p.Msg = v - } - return nil -} - -func (p *MockReq) ReadField2(iprot thrift.TProtocol) error { - _, _, size, err := iprot.ReadMapBegin() - if err != nil { - return err - } - p.StrMap = make(map[string]string, size) - for i := 0; i < size; i++ { - var _key string - if v, err := iprot.ReadString(); err != nil { - return err - } else { - _key = v - } - - var _val string - if v, err := iprot.ReadString(); err != nil { - return err - } else { - _val = v - } - - p.StrMap[_key] = _val - } - if err := iprot.ReadMapEnd(); err != nil { - return err - } - return nil -} - -func (p *MockReq) ReadField3(iprot thrift.TProtocol) error { - _, size, err := iprot.ReadListBegin() - if err != nil { - return err - } - p.StrList = make([]string, 0, size) - for i := 0; i < size; i++ { - var _elem string - if v, err := iprot.ReadString(); err != nil { - return err - } else { - _elem = v - } - - p.StrList = append(p.StrList, _elem) - } - if err := iprot.ReadListEnd(); err != nil { - return err - } - return nil -} - -func (p *MockReq) Write(oprot thrift.TProtocol) (err error) { - var fieldId int16 - if err = oprot.WriteStructBegin("MockReq"); err != nil { - goto WriteStructBeginError - } - if p != nil { - if err = p.writeField1(oprot); err != nil { - fieldId = 1 - goto WriteFieldError - } - if err = p.writeField2(oprot); err != nil { - fieldId = 2 - goto WriteFieldError - } - if err = p.writeField3(oprot); err != nil { - fieldId = 3 - goto WriteFieldError - } - - } - if err = oprot.WriteFieldStop(); err != nil { - goto WriteFieldStopError - } - if err = oprot.WriteStructEnd(); err != nil { - goto WriteStructEndError - } - return nil -WriteStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) -WriteFieldError: - return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err) -WriteFieldStopError: - return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err) -WriteStructEndError: - return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err) -} - -func (p *MockReq) writeField1(oprot thrift.TProtocol) (err error) { - if err = oprot.WriteFieldBegin("Msg", thrift.STRING, 1); err != nil { - goto WriteFieldBeginError - } - if err := oprot.WriteString(p.Msg); err != nil { - return err - } - if err = oprot.WriteFieldEnd(); err != nil { - goto WriteFieldEndError - } - return nil -WriteFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err) -WriteFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err) -} - -func (p *MockReq) writeField2(oprot thrift.TProtocol) (err error) { - if err = oprot.WriteFieldBegin("strMap", thrift.MAP, 2); err != nil { - goto WriteFieldBeginError - } - if err := oprot.WriteMapBegin(thrift.STRING, thrift.STRING, len(p.StrMap)); err != nil { - return err - } - for k, v := range p.StrMap { - - if err := oprot.WriteString(k); err != nil { - return err - } - - if err := oprot.WriteString(v); err != nil { - return err - } - } - if err := oprot.WriteMapEnd(); err != nil { - return err - } - if err = oprot.WriteFieldEnd(); err != nil { - goto WriteFieldEndError - } - return nil -WriteFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T write field 2 begin error: ", p), err) -WriteFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T write field 2 end error: ", p), err) -} - -func (p *MockReq) writeField3(oprot thrift.TProtocol) (err error) { - if err = oprot.WriteFieldBegin("strList", thrift.LIST, 3); err != nil { - goto WriteFieldBeginError - } - if err := oprot.WriteListBegin(thrift.STRING, len(p.StrList)); err != nil { - return err - } - for _, v := range p.StrList { - if err := oprot.WriteString(v); err != nil { - return err - } - } - if err := oprot.WriteListEnd(); err != nil { - return err - } - if err = oprot.WriteFieldEnd(); err != nil { - goto WriteFieldEndError - } - return nil -WriteFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T write field 3 begin error: ", p), err) -WriteFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T write field 3 end error: ", p), err) -} - -func (p *MockReq) String() string { - if p == nil { - return "" - } - return fmt.Sprintf("MockReq(%+v)", *p) -} - -func (p *MockReq) DeepEqual(ano *MockReq) bool { - if p == ano { - return true - } else if p == nil || ano == nil { - return false - } - if !p.Field1DeepEqual(ano.Msg) { - return false - } - if !p.Field2DeepEqual(ano.StrMap) { - return false - } - if !p.Field3DeepEqual(ano.StrList) { - return false - } - return true -} - -func (p *MockReq) Field1DeepEqual(src string) bool { - - if strings.Compare(p.Msg, src) != 0 { - return false - } - return true -} -func (p *MockReq) Field2DeepEqual(src map[string]string) bool { - - if len(p.StrMap) != len(src) { - return false - } - for k, v := range p.StrMap { - _src := src[k] - if strings.Compare(v, _src) != 0 { - return false - } - } - return true -} -func (p *MockReq) Field3DeepEqual(src []string) bool { - - if len(p.StrList) != len(src) { - return false - } - for i, v := range p.StrList { - _src := src[i] - if strings.Compare(v, _src) != 0 { - return false - } - } - return true -} - -type Exception struct { - Code int32 `thrift:"code,1" json:"code"` - Msg string `thrift:"msg,255" json:"msg"` -} - -func NewException() *Exception { - return &Exception{} -} - -func (p *Exception) GetCode() (v int32) { - return p.Code -} - -func (p *Exception) GetMsg() (v string) { - return p.Msg -} -func (p *Exception) SetCode(val int32) { - p.Code = val -} -func (p *Exception) SetMsg(val string) { - p.Msg = val -} - -var fieldIDToName_Exception = map[int16]string{ - 1: "code", - 255: "msg", -} - -func (p *Exception) Read(iprot thrift.TProtocol) (err error) { - - var fieldTypeId thrift.TType - var fieldId int16 - - if _, err = iprot.ReadStructBegin(); err != nil { - goto ReadStructBeginError - } - - for { - _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin() - if err != nil { - goto ReadFieldBeginError - } - if fieldTypeId == thrift.STOP { - break - } - - switch fieldId { - case 1: - if fieldTypeId == thrift.I32 { - if err = p.ReadField1(iprot); err != nil { - goto ReadFieldError - } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - case 255: - if fieldTypeId == thrift.STRING { - if err = p.ReadField255(iprot); err != nil { - goto ReadFieldError - } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - default: - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - - if err = iprot.ReadFieldEnd(); err != nil { - goto ReadFieldEndError - } - } - if err = iprot.ReadStructEnd(); err != nil { - goto ReadStructEndError - } - - return nil -ReadStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) -ReadFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) -ReadFieldError: - return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_Exception[fieldId]), err) -SkipFieldError: - return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) - -ReadFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) -ReadStructEndError: - return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) -} - -func (p *Exception) ReadField1(iprot thrift.TProtocol) error { - if v, err := iprot.ReadI32(); err != nil { - return err - } else { - p.Code = v - } - return nil -} - -func (p *Exception) ReadField255(iprot thrift.TProtocol) error { - if v, err := iprot.ReadString(); err != nil { - return err - } else { - p.Msg = v - } - return nil -} - -func (p *Exception) Write(oprot thrift.TProtocol) (err error) { - var fieldId int16 - if err = oprot.WriteStructBegin("Exception"); err != nil { - goto WriteStructBeginError - } - if p != nil { - if err = p.writeField1(oprot); err != nil { - fieldId = 1 - goto WriteFieldError - } - if err = p.writeField255(oprot); err != nil { - fieldId = 255 - goto WriteFieldError - } - - } - if err = oprot.WriteFieldStop(); err != nil { - goto WriteFieldStopError - } - if err = oprot.WriteStructEnd(); err != nil { - goto WriteStructEndError - } - return nil -WriteStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) -WriteFieldError: - return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err) -WriteFieldStopError: - return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err) -WriteStructEndError: - return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err) -} - -func (p *Exception) writeField1(oprot thrift.TProtocol) (err error) { - if err = oprot.WriteFieldBegin("code", thrift.I32, 1); err != nil { - goto WriteFieldBeginError - } - if err := oprot.WriteI32(p.Code); err != nil { - return err - } - if err = oprot.WriteFieldEnd(); err != nil { - goto WriteFieldEndError - } - return nil -WriteFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err) -WriteFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err) -} - -func (p *Exception) writeField255(oprot thrift.TProtocol) (err error) { - if err = oprot.WriteFieldBegin("msg", thrift.STRING, 255); err != nil { - goto WriteFieldBeginError - } - if err := oprot.WriteString(p.Msg); err != nil { - return err - } - if err = oprot.WriteFieldEnd(); err != nil { - goto WriteFieldEndError - } - return nil -WriteFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T write field 255 begin error: ", p), err) -WriteFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T write field 255 end error: ", p), err) -} - -func (p *Exception) String() string { - if p == nil { - return "" - } - return fmt.Sprintf("Exception(%+v)", *p) -} -func (p *Exception) Error() string { - return p.String() -} - -func (p *Exception) DeepEqual(ano *Exception) bool { - if p == ano { - return true - } else if p == nil || ano == nil { - return false - } - if !p.Field1DeepEqual(ano.Code) { - return false - } - if !p.Field255DeepEqual(ano.Msg) { - return false - } - return true -} - -func (p *Exception) Field1DeepEqual(src int32) bool { - - if p.Code != src { - return false - } - return true -} -func (p *Exception) Field255DeepEqual(src string) bool { - - if strings.Compare(p.Msg, src) != 0 { - return false - } - return true -} - -type Mock interface { - Test(ctx context.Context, req *MockReq) (r string, err error) - - ExceptionTest(ctx context.Context, req *MockReq) (r string, err error) -} - -type MockClient struct { - c thrift.TClient -} - -func NewMockClientFactory(t thrift.TTransport, f thrift.TProtocolFactory) *MockClient { - return &MockClient{ - c: thrift.NewTStandardClient(f.GetProtocol(t), f.GetProtocol(t)), - } -} - -func NewMockClientProtocol(t thrift.TTransport, iprot thrift.TProtocol, oprot thrift.TProtocol) *MockClient { - return &MockClient{ - c: thrift.NewTStandardClient(iprot, oprot), - } -} - -func NewMockClient(c thrift.TClient) *MockClient { - return &MockClient{ - c: c, - } -} - -func (p *MockClient) Client_() thrift.TClient { - return p.c -} - -func (p *MockClient) Test(ctx context.Context, req *MockReq) (r string, err error) { - var _args MockTestArgs - _args.Req = req - var _result MockTestResult - if err = p.Client_().Call(ctx, "Test", &_args, &_result); err != nil { - return - } - return _result.GetSuccess(), nil -} -func (p *MockClient) ExceptionTest(ctx context.Context, req *MockReq) (r string, err error) { - var _args MockExceptionTestArgs - _args.Req = req - var _result MockExceptionTestResult - if err = p.Client_().Call(ctx, "ExceptionTest", &_args, &_result); err != nil { - return - } - switch { - case _result.Err != nil: - return r, _result.Err - } - return _result.GetSuccess(), nil -} - -type MockProcessor struct { - processorMap map[string]thrift.TProcessorFunction - handler Mock -} - -func (p *MockProcessor) AddToProcessorMap(key string, processor thrift.TProcessorFunction) { - p.processorMap[key] = processor -} - -func (p *MockProcessor) GetProcessorFunction(key string) (processor thrift.TProcessorFunction, ok bool) { - processor, ok = p.processorMap[key] - return processor, ok -} - -func (p *MockProcessor) ProcessorMap() map[string]thrift.TProcessorFunction { - return p.processorMap -} - -func NewMockProcessor(handler Mock) *MockProcessor { - self := &MockProcessor{handler: handler, processorMap: make(map[string]thrift.TProcessorFunction)} - self.AddToProcessorMap("Test", &mockProcessorTest{handler: handler}) - self.AddToProcessorMap("ExceptionTest", &mockProcessorExceptionTest{handler: handler}) - return self -} -func (p *MockProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { - name, _, seqId, err := iprot.ReadMessageBegin() - if err != nil { - return false, err - } - if processor, ok := p.GetProcessorFunction(name); ok { - return processor.Process(ctx, seqId, iprot, oprot) - } - iprot.Skip(thrift.STRUCT) - iprot.ReadMessageEnd() - x := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function "+name) - oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId) - x.Write(oprot) - oprot.WriteMessageEnd() - oprot.Flush(ctx) - return false, x -} - -type mockProcessorTest struct { - handler Mock -} - -func (p *mockProcessorTest) Process(ctx context.Context, seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { - args := MockTestArgs{} - if err = args.Read(iprot); err != nil { - iprot.ReadMessageEnd() - x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error()) - oprot.WriteMessageBegin("Test", thrift.EXCEPTION, seqId) - x.Write(oprot) - oprot.WriteMessageEnd() - oprot.Flush(ctx) - return false, err - } - - iprot.ReadMessageEnd() - var err2 error - result := MockTestResult{} - var retval string - if retval, err2 = p.handler.Test(ctx, args.Req); err2 != nil { - x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "Internal error processing Test: "+err2.Error()) - oprot.WriteMessageBegin("Test", thrift.EXCEPTION, seqId) - x.Write(oprot) - oprot.WriteMessageEnd() - oprot.Flush(ctx) - return true, err2 - } else { - result.Success = &retval - } - if err2 = oprot.WriteMessageBegin("Test", thrift.REPLY, seqId); err2 != nil { - err = err2 - } - if err2 = result.Write(oprot); err == nil && err2 != nil { - err = err2 - } - if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil { - err = err2 - } - if err2 = oprot.Flush(ctx); err == nil && err2 != nil { - err = err2 - } - if err != nil { - return - } - return true, err -} - -type mockProcessorExceptionTest struct { - handler Mock -} - -func (p *mockProcessorExceptionTest) Process(ctx context.Context, seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { - args := MockExceptionTestArgs{} - if err = args.Read(iprot); err != nil { - iprot.ReadMessageEnd() - x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error()) - oprot.WriteMessageBegin("ExceptionTest", thrift.EXCEPTION, seqId) - x.Write(oprot) - oprot.WriteMessageEnd() - oprot.Flush(ctx) - return false, err - } - - iprot.ReadMessageEnd() - var err2 error - result := MockExceptionTestResult{} - var retval string - if retval, err2 = p.handler.ExceptionTest(ctx, args.Req); err2 != nil { - switch v := err2.(type) { - case *Exception: - result.Err = v - default: - x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "Internal error processing ExceptionTest: "+err2.Error()) - oprot.WriteMessageBegin("ExceptionTest", thrift.EXCEPTION, seqId) - x.Write(oprot) - oprot.WriteMessageEnd() - oprot.Flush(ctx) - return true, err2 - } - } else { - result.Success = &retval - } - if err2 = oprot.WriteMessageBegin("ExceptionTest", thrift.REPLY, seqId); err2 != nil { - err = err2 - } - if err2 = result.Write(oprot); err == nil && err2 != nil { - err = err2 - } - if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil { - err = err2 - } - if err2 = oprot.Flush(ctx); err == nil && err2 != nil { - err = err2 - } - if err != nil { - return - } - return true, err -} - -type MockTestArgs struct { - Req *MockReq `thrift:"req,1" json:"req"` -} - -func NewMockTestArgs() *MockTestArgs { - return &MockTestArgs{} -} - -var MockTestArgs_Req_DEFAULT *MockReq - -func (p *MockTestArgs) GetReq() (v *MockReq) { - if !p.IsSetReq() { - return MockTestArgs_Req_DEFAULT - } - return p.Req -} -func (p *MockTestArgs) SetReq(val *MockReq) { - p.Req = val -} - -var fieldIDToName_MockTestArgs = map[int16]string{ - 1: "req", -} - -func (p *MockTestArgs) IsSetReq() bool { - return p.Req != nil -} - -func (p *MockTestArgs) Read(iprot thrift.TProtocol) (err error) { - - var fieldTypeId thrift.TType - var fieldId int16 - - if _, err = iprot.ReadStructBegin(); err != nil { - goto ReadStructBeginError - } - - for { - _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin() - if err != nil { - goto ReadFieldBeginError - } - if fieldTypeId == thrift.STOP { - break - } - - switch fieldId { - case 1: - if fieldTypeId == thrift.STRUCT { - if err = p.ReadField1(iprot); err != nil { - goto ReadFieldError - } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - default: - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - - if err = iprot.ReadFieldEnd(); err != nil { - goto ReadFieldEndError - } - } - if err = iprot.ReadStructEnd(); err != nil { - goto ReadStructEndError - } - - return nil -ReadStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) -ReadFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) -ReadFieldError: - return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_MockTestArgs[fieldId]), err) -SkipFieldError: - return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) - -ReadFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) -ReadStructEndError: - return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) -} - -func (p *MockTestArgs) ReadField1(iprot thrift.TProtocol) error { - p.Req = NewMockReq() - if err := p.Req.Read(iprot); err != nil { - return err - } - return nil -} - -func (p *MockTestArgs) Write(oprot thrift.TProtocol) (err error) { - var fieldId int16 - if err = oprot.WriteStructBegin("Test_args"); err != nil { - goto WriteStructBeginError - } - if p != nil { - if err = p.writeField1(oprot); err != nil { - fieldId = 1 - goto WriteFieldError - } - - } - if err = oprot.WriteFieldStop(); err != nil { - goto WriteFieldStopError - } - if err = oprot.WriteStructEnd(); err != nil { - goto WriteStructEndError - } - return nil -WriteStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) -WriteFieldError: - return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err) -WriteFieldStopError: - return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err) -WriteStructEndError: - return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err) -} - -func (p *MockTestArgs) writeField1(oprot thrift.TProtocol) (err error) { - if err = oprot.WriteFieldBegin("req", thrift.STRUCT, 1); err != nil { - goto WriteFieldBeginError - } - if err := p.Req.Write(oprot); err != nil { - return err - } - if err = oprot.WriteFieldEnd(); err != nil { - goto WriteFieldEndError - } - return nil -WriteFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err) -WriteFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err) -} - -func (p *MockTestArgs) String() string { - if p == nil { - return "" - } - return fmt.Sprintf("MockTestArgs(%+v)", *p) -} - -func (p *MockTestArgs) DeepEqual(ano *MockTestArgs) bool { - if p == ano { - return true - } else if p == nil || ano == nil { - return false - } - if !p.Field1DeepEqual(ano.Req) { - return false - } - return true -} - -func (p *MockTestArgs) Field1DeepEqual(src *MockReq) bool { - - if !p.Req.DeepEqual(src) { - return false - } - return true -} - -type MockTestResult struct { - Success *string `thrift:"success,0,optional" json:"success,omitempty"` -} - -func NewMockTestResult() *MockTestResult { - return &MockTestResult{} -} - -var MockTestResult_Success_DEFAULT string - -func (p *MockTestResult) GetSuccess() (v string) { - if !p.IsSetSuccess() { - return MockTestResult_Success_DEFAULT - } - return *p.Success -} -func (p *MockTestResult) SetSuccess(x interface{}) { - p.Success = x.(*string) -} - -var fieldIDToName_MockTestResult = map[int16]string{ - 0: "success", -} - -func (p *MockTestResult) IsSetSuccess() bool { - return p.Success != nil -} - -func (p *MockTestResult) Read(iprot thrift.TProtocol) (err error) { - - var fieldTypeId thrift.TType - var fieldId int16 - - if _, err = iprot.ReadStructBegin(); err != nil { - goto ReadStructBeginError - } - - for { - _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin() - if err != nil { - goto ReadFieldBeginError - } - if fieldTypeId == thrift.STOP { - break - } - - switch fieldId { - case 0: - if fieldTypeId == thrift.STRING { - if err = p.ReadField0(iprot); err != nil { - goto ReadFieldError - } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - default: - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - - if err = iprot.ReadFieldEnd(); err != nil { - goto ReadFieldEndError - } - } - if err = iprot.ReadStructEnd(); err != nil { - goto ReadStructEndError - } - - return nil -ReadStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) -ReadFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) -ReadFieldError: - return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_MockTestResult[fieldId]), err) -SkipFieldError: - return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) - -ReadFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) -ReadStructEndError: - return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) -} - -func (p *MockTestResult) ReadField0(iprot thrift.TProtocol) error { - if v, err := iprot.ReadString(); err != nil { - return err - } else { - p.Success = &v - } - return nil -} - -func (p *MockTestResult) Write(oprot thrift.TProtocol) (err error) { - var fieldId int16 - if err = oprot.WriteStructBegin("Test_result"); err != nil { - goto WriteStructBeginError - } - if p != nil { - if err = p.writeField0(oprot); err != nil { - fieldId = 0 - goto WriteFieldError - } - - } - if err = oprot.WriteFieldStop(); err != nil { - goto WriteFieldStopError - } - if err = oprot.WriteStructEnd(); err != nil { - goto WriteStructEndError - } - return nil -WriteStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) -WriteFieldError: - return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err) -WriteFieldStopError: - return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err) -WriteStructEndError: - return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err) -} - -func (p *MockTestResult) writeField0(oprot thrift.TProtocol) (err error) { - if p.IsSetSuccess() { - if err = oprot.WriteFieldBegin("success", thrift.STRING, 0); err != nil { - goto WriteFieldBeginError - } - if err := oprot.WriteString(*p.Success); err != nil { - return err - } - if err = oprot.WriteFieldEnd(); err != nil { - goto WriteFieldEndError - } - } - return nil -WriteFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T write field 0 begin error: ", p), err) -WriteFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T write field 0 end error: ", p), err) -} - -func (p *MockTestResult) String() string { - if p == nil { - return "" - } - return fmt.Sprintf("MockTestResult(%+v)", *p) -} - -func (p *MockTestResult) DeepEqual(ano *MockTestResult) bool { - if p == ano { - return true - } else if p == nil || ano == nil { - return false - } - if !p.Field0DeepEqual(ano.Success) { - return false - } - return true -} - -func (p *MockTestResult) Field0DeepEqual(src *string) bool { - - if p.Success == src { - return true - } else if p.Success == nil || src == nil { - return false - } - if strings.Compare(*p.Success, *src) != 0 { - return false - } - return true -} - -type MockExceptionTestArgs struct { - Req *MockReq `thrift:"req,1" json:"req"` -} - -func NewMockExceptionTestArgs() *MockExceptionTestArgs { - return &MockExceptionTestArgs{} -} - -var MockExceptionTestArgs_Req_DEFAULT *MockReq - -func (p *MockExceptionTestArgs) GetReq() (v *MockReq) { - if !p.IsSetReq() { - return MockExceptionTestArgs_Req_DEFAULT - } - return p.Req -} -func (p *MockExceptionTestArgs) SetReq(val *MockReq) { - p.Req = val -} - -var fieldIDToName_MockExceptionTestArgs = map[int16]string{ - 1: "req", -} - -func (p *MockExceptionTestArgs) IsSetReq() bool { - return p.Req != nil -} - -func (p *MockExceptionTestArgs) Read(iprot thrift.TProtocol) (err error) { - - var fieldTypeId thrift.TType - var fieldId int16 - - if _, err = iprot.ReadStructBegin(); err != nil { - goto ReadStructBeginError - } - - for { - _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin() - if err != nil { - goto ReadFieldBeginError - } - if fieldTypeId == thrift.STOP { - break - } - - switch fieldId { - case 1: - if fieldTypeId == thrift.STRUCT { - if err = p.ReadField1(iprot); err != nil { - goto ReadFieldError - } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - default: - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - - if err = iprot.ReadFieldEnd(); err != nil { - goto ReadFieldEndError - } - } - if err = iprot.ReadStructEnd(); err != nil { - goto ReadStructEndError - } - - return nil -ReadStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) -ReadFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) -ReadFieldError: - return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_MockExceptionTestArgs[fieldId]), err) -SkipFieldError: - return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) - -ReadFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) -ReadStructEndError: - return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) -} - -func (p *MockExceptionTestArgs) ReadField1(iprot thrift.TProtocol) error { - p.Req = NewMockReq() - if err := p.Req.Read(iprot); err != nil { - return err - } - return nil -} - -func (p *MockExceptionTestArgs) Write(oprot thrift.TProtocol) (err error) { - var fieldId int16 - if err = oprot.WriteStructBegin("ExceptionTest_args"); err != nil { - goto WriteStructBeginError - } - if p != nil { - if err = p.writeField1(oprot); err != nil { - fieldId = 1 - goto WriteFieldError - } - - } - if err = oprot.WriteFieldStop(); err != nil { - goto WriteFieldStopError - } - if err = oprot.WriteStructEnd(); err != nil { - goto WriteStructEndError - } - return nil -WriteStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) -WriteFieldError: - return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err) -WriteFieldStopError: - return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err) -WriteStructEndError: - return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err) -} - -func (p *MockExceptionTestArgs) writeField1(oprot thrift.TProtocol) (err error) { - if err = oprot.WriteFieldBegin("req", thrift.STRUCT, 1); err != nil { - goto WriteFieldBeginError - } - if err := p.Req.Write(oprot); err != nil { - return err - } - if err = oprot.WriteFieldEnd(); err != nil { - goto WriteFieldEndError - } - return nil -WriteFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err) -WriteFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err) -} - -func (p *MockExceptionTestArgs) String() string { - if p == nil { - return "" - } - return fmt.Sprintf("MockExceptionTestArgs(%+v)", *p) -} - -func (p *MockExceptionTestArgs) DeepEqual(ano *MockExceptionTestArgs) bool { - if p == ano { - return true - } else if p == nil || ano == nil { - return false - } - if !p.Field1DeepEqual(ano.Req) { - return false - } - return true -} - -func (p *MockExceptionTestArgs) Field1DeepEqual(src *MockReq) bool { - - if !p.Req.DeepEqual(src) { - return false - } - return true -} - -type MockExceptionTestResult struct { - Success *string `thrift:"success,0,optional" json:"success,omitempty"` - Err *Exception `thrift:"err,1,optional" json:"err,omitempty"` -} - -func NewMockExceptionTestResult() *MockExceptionTestResult { - return &MockExceptionTestResult{} -} - -var MockExceptionTestResult_Success_DEFAULT string - -func (p *MockExceptionTestResult) GetSuccess() (v string) { - if !p.IsSetSuccess() { - return MockExceptionTestResult_Success_DEFAULT - } - return *p.Success -} - -var MockExceptionTestResult_Err_DEFAULT *Exception - -func (p *MockExceptionTestResult) GetErr() (v *Exception) { - if !p.IsSetErr() { - return MockExceptionTestResult_Err_DEFAULT - } - return p.Err -} -func (p *MockExceptionTestResult) SetSuccess(x interface{}) { - p.Success = x.(*string) -} -func (p *MockExceptionTestResult) SetErr(val *Exception) { - p.Err = val -} - -var fieldIDToName_MockExceptionTestResult = map[int16]string{ - 0: "success", - 1: "err", -} - -func (p *MockExceptionTestResult) IsSetSuccess() bool { - return p.Success != nil -} - -func (p *MockExceptionTestResult) IsSetErr() bool { - return p.Err != nil -} - -func (p *MockExceptionTestResult) Read(iprot thrift.TProtocol) (err error) { - - var fieldTypeId thrift.TType - var fieldId int16 - - if _, err = iprot.ReadStructBegin(); err != nil { - goto ReadStructBeginError - } - - for { - _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin() - if err != nil { - goto ReadFieldBeginError - } - if fieldTypeId == thrift.STOP { - break - } - - switch fieldId { - case 0: - if fieldTypeId == thrift.STRING { - if err = p.ReadField0(iprot); err != nil { - goto ReadFieldError - } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - case 1: - if fieldTypeId == thrift.STRUCT { - if err = p.ReadField1(iprot); err != nil { - goto ReadFieldError - } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - default: - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } - } - - if err = iprot.ReadFieldEnd(); err != nil { - goto ReadFieldEndError - } - } - if err = iprot.ReadStructEnd(); err != nil { - goto ReadStructEndError - } - - return nil -ReadStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err) -ReadFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err) -ReadFieldError: - return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_MockExceptionTestResult[fieldId]), err) -SkipFieldError: - return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err) - -ReadFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err) -ReadStructEndError: - return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) -} - -func (p *MockExceptionTestResult) ReadField0(iprot thrift.TProtocol) error { - if v, err := iprot.ReadString(); err != nil { - return err - } else { - p.Success = &v - } - return nil -} - -func (p *MockExceptionTestResult) ReadField1(iprot thrift.TProtocol) error { - p.Err = NewException() - if err := p.Err.Read(iprot); err != nil { - return err - } - return nil -} - -func (p *MockExceptionTestResult) Write(oprot thrift.TProtocol) (err error) { - var fieldId int16 - if err = oprot.WriteStructBegin("ExceptionTest_result"); err != nil { - goto WriteStructBeginError - } - if p != nil { - if err = p.writeField0(oprot); err != nil { - fieldId = 0 - goto WriteFieldError - } - if err = p.writeField1(oprot); err != nil { - fieldId = 1 - goto WriteFieldError - } - - } - if err = oprot.WriteFieldStop(); err != nil { - goto WriteFieldStopError - } - if err = oprot.WriteStructEnd(); err != nil { - goto WriteStructEndError - } - return nil -WriteStructBeginError: - return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) -WriteFieldError: - return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err) -WriteFieldStopError: - return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err) -WriteStructEndError: - return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err) -} - -func (p *MockExceptionTestResult) writeField0(oprot thrift.TProtocol) (err error) { - if p.IsSetSuccess() { - if err = oprot.WriteFieldBegin("success", thrift.STRING, 0); err != nil { - goto WriteFieldBeginError - } - if err := oprot.WriteString(*p.Success); err != nil { - return err - } - if err = oprot.WriteFieldEnd(); err != nil { - goto WriteFieldEndError - } - } - return nil -WriteFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T write field 0 begin error: ", p), err) -WriteFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T write field 0 end error: ", p), err) -} - -func (p *MockExceptionTestResult) writeField1(oprot thrift.TProtocol) (err error) { - if p.IsSetErr() { - if err = oprot.WriteFieldBegin("err", thrift.STRUCT, 1); err != nil { - goto WriteFieldBeginError - } - if err := p.Err.Write(oprot); err != nil { - return err - } - if err = oprot.WriteFieldEnd(); err != nil { - goto WriteFieldEndError - } - } - return nil -WriteFieldBeginError: - return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err) -WriteFieldEndError: - return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err) -} - -func (p *MockExceptionTestResult) String() string { - if p == nil { - return "" - } - return fmt.Sprintf("MockExceptionTestResult(%+v)", *p) -} - -func (p *MockExceptionTestResult) DeepEqual(ano *MockExceptionTestResult) bool { - if p == ano { - return true - } else if p == nil || ano == nil { - return false - } - if !p.Field0DeepEqual(ano.Success) { - return false - } - if !p.Field1DeepEqual(ano.Err) { - return false - } - return true -} - -func (p *MockExceptionTestResult) Field0DeepEqual(src *string) bool { - - if p.Success == src { - return true - } else if p.Success == nil || src == nil { - return false - } - if strings.Compare(*p.Success, *src) != 0 { - return false - } - return true -} -func (p *MockExceptionTestResult) Field1DeepEqual(src *Exception) bool { - - if !p.Err.DeepEqual(src) { - return false - } - return true -} diff --git a/internal/mocks/thrift/gen.sh b/internal/mocks/thrift/gen.sh new file mode 100755 index 0000000000..df178c759b --- /dev/null +++ b/internal/mocks/thrift/gen.sh @@ -0,0 +1,5 @@ +#! /bin/bash + +kitex -module github.com/cloudwego/kitex -gen-path .. ./test.thrift + +rm -rf ./mock # not in use, rm it diff --git a/internal/mocks/thrift/k-consts.go b/internal/mocks/thrift/k-consts.go new file mode 100644 index 0000000000..84f8d35254 --- /dev/null +++ b/internal/mocks/thrift/k-consts.go @@ -0,0 +1,20 @@ +/* + * Copyright 2024 CloudWeGo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package thrift + +// KitexUnusedProtection is used to prevent 'imported and not used' error. +var KitexUnusedProtection = struct{}{} diff --git a/internal/mocks/thrift/fast/k-test.go b/internal/mocks/thrift/k-test.go similarity index 97% rename from internal/mocks/thrift/fast/k-test.go rename to internal/mocks/thrift/k-test.go index c31f69fd23..2020f27c4d 100644 --- a/internal/mocks/thrift/fast/k-test.go +++ b/internal/mocks/thrift/k-test.go @@ -1,5 +1,5 @@ /* - * Copyright 2022 CloudWeGo Authors + * Copyright 2024 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,9 +14,9 @@ * limitations under the License. */ -// Code generated by Kitex v0.4.3. DO NOT EDIT. +// Code generated by Kitex v0.10.1. DO NOT EDIT. -package fast +package thrift import ( "bytes" @@ -141,14 +141,16 @@ ReadStructEndError: func (p *MockReq) FastReadField1(buf []byte) (int, error) { offset := 0 + var _field string if v, l, err := bthrift.Binary.ReadString(buf[offset:]); err != nil { return offset, err } else { offset += l - p.Msg = v + _field = v } + p.Msg = _field return offset, nil } @@ -160,7 +162,7 @@ func (p *MockReq) FastReadField2(buf []byte) (int, error) { if err != nil { return offset, err } - p.StrMap = make(map[string]string, size) + _field := make(map[string]string, size) for i := 0; i < size; i++ { var _key string if v, l, err := bthrift.Binary.ReadString(buf[offset:]); err != nil { @@ -182,13 +184,14 @@ func (p *MockReq) FastReadField2(buf []byte) (int, error) { } - p.StrMap[_key] = _val + _field[_key] = _val } if l, err := bthrift.Binary.ReadMapEnd(buf[offset:]); err != nil { return offset, err } else { offset += l } + p.StrMap = _field return offset, nil } @@ -200,7 +203,7 @@ func (p *MockReq) FastReadField3(buf []byte) (int, error) { if err != nil { return offset, err } - p.StrList = make([]string, 0, size) + _field := make([]string, 0, size) for i := 0; i < size; i++ { var _elem string if v, l, err := bthrift.Binary.ReadString(buf[offset:]); err != nil { @@ -212,13 +215,14 @@ func (p *MockReq) FastReadField3(buf []byte) (int, error) { } - p.StrList = append(p.StrList, _elem) + _field = append(_field, _elem) } if l, err := bthrift.Binary.ReadListEnd(buf[offset:]); err != nil { return offset, err } else { offset += l } + p.StrList = _field return offset, nil } @@ -257,7 +261,6 @@ func (p *MockReq) fastWriteField1(buf []byte, binaryWriter bthrift.BinaryWriter) offset := 0 offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "Msg", thrift.STRING, 1) offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, p.Msg) - offset += bthrift.Binary.WriteFieldEnd(buf[offset:]) return offset } @@ -270,11 +273,8 @@ func (p *MockReq) fastWriteField2(buf []byte, binaryWriter bthrift.BinaryWriter) var length int for k, v := range p.StrMap { length++ - offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, k) - offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, v) - } bthrift.Binary.WriteMapBegin(buf[mapBeginOffset:], thrift.STRING, thrift.STRING, length) offset += bthrift.Binary.WriteMapEnd(buf[offset:]) @@ -291,7 +291,6 @@ func (p *MockReq) fastWriteField3(buf []byte, binaryWriter bthrift.BinaryWriter) for _, v := range p.StrList { length++ offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, v) - } bthrift.Binary.WriteListBegin(buf[listBeginOffset:], thrift.STRING, length) offset += bthrift.Binary.WriteListEnd(buf[offset:]) @@ -303,7 +302,6 @@ func (p *MockReq) field1Length() int { l := 0 l += bthrift.Binary.FieldBeginLength("Msg", thrift.STRING, 1) l += bthrift.Binary.StringLengthNocopy(p.Msg) - l += bthrift.Binary.FieldEndLength() return l } @@ -315,9 +313,7 @@ func (p *MockReq) field2Length() int { for k, v := range p.StrMap { l += bthrift.Binary.StringLengthNocopy(k) - l += bthrift.Binary.StringLengthNocopy(v) - } l += bthrift.Binary.MapEndLength() l += bthrift.Binary.FieldEndLength() @@ -330,7 +326,6 @@ func (p *MockReq) field3Length() int { l += bthrift.Binary.ListBeginLength(thrift.STRING, len(p.StrList)) for _, v := range p.StrList { l += bthrift.Binary.StringLengthNocopy(v) - } l += bthrift.Binary.ListEndLength() l += bthrift.Binary.FieldEndLength() @@ -425,28 +420,32 @@ ReadStructEndError: func (p *Exception) FastReadField1(buf []byte) (int, error) { offset := 0 + var _field int32 if v, l, err := bthrift.Binary.ReadI32(buf[offset:]); err != nil { return offset, err } else { offset += l - p.Code = v + _field = v } + p.Code = _field return offset, nil } func (p *Exception) FastReadField255(buf []byte) (int, error) { offset := 0 + var _field string if v, l, err := bthrift.Binary.ReadString(buf[offset:]); err != nil { return offset, err } else { offset += l - p.Msg = v + _field = v } + p.Msg = _field return offset, nil } @@ -483,7 +482,6 @@ func (p *Exception) fastWriteField1(buf []byte, binaryWriter bthrift.BinaryWrite offset := 0 offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "code", thrift.I32, 1) offset += bthrift.Binary.WriteI32(buf[offset:], p.Code) - offset += bthrift.Binary.WriteFieldEnd(buf[offset:]) return offset } @@ -492,7 +490,6 @@ func (p *Exception) fastWriteField255(buf []byte, binaryWriter bthrift.BinaryWri offset := 0 offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "msg", thrift.STRING, 255) offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, p.Msg) - offset += bthrift.Binary.WriteFieldEnd(buf[offset:]) return offset } @@ -501,7 +498,6 @@ func (p *Exception) field1Length() int { l := 0 l += bthrift.Binary.FieldBeginLength("code", thrift.I32, 1) l += bthrift.Binary.I32Length(p.Code) - l += bthrift.Binary.FieldEndLength() return l } @@ -510,7 +506,6 @@ func (p *Exception) field255Length() int { l := 0 l += bthrift.Binary.FieldBeginLength("msg", thrift.STRING, 255) l += bthrift.Binary.StringLengthNocopy(p.Msg) - l += bthrift.Binary.FieldEndLength() return l } @@ -588,14 +583,13 @@ ReadStructEndError: func (p *MockTestArgs) FastReadField1(buf []byte) (int, error) { offset := 0 - - tmp := NewMockReq() - if l, err := tmp.FastRead(buf[offset:]); err != nil { + _field := NewMockReq() + if l, err := _field.FastRead(buf[offset:]); err != nil { return offset, err } else { offset += l } - p.Req = tmp + p.Req = _field return offset, nil } @@ -716,13 +710,15 @@ ReadStructEndError: func (p *MockTestResult) FastReadField0(buf []byte) (int, error) { offset := 0 + var _field *string if v, l, err := bthrift.Binary.ReadString(buf[offset:]); err != nil { return offset, err } else { offset += l - p.Success = &v + _field = &v } + p.Success = _field return offset, nil } @@ -758,7 +754,6 @@ func (p *MockTestResult) fastWriteField0(buf []byte, binaryWriter bthrift.Binary if p.IsSetSuccess() { offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "success", thrift.STRING, 0) offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, *p.Success) - offset += bthrift.Binary.WriteFieldEnd(buf[offset:]) } return offset @@ -769,7 +764,6 @@ func (p *MockTestResult) field0Length() int { if p.IsSetSuccess() { l += bthrift.Binary.FieldBeginLength("success", thrift.STRING, 0) l += bthrift.Binary.StringLengthNocopy(*p.Success) - l += bthrift.Binary.FieldEndLength() } return l @@ -848,14 +842,13 @@ ReadStructEndError: func (p *MockExceptionTestArgs) FastReadField1(buf []byte) (int, error) { offset := 0 - - tmp := NewMockReq() - if l, err := tmp.FastRead(buf[offset:]); err != nil { + _field := NewMockReq() + if l, err := _field.FastRead(buf[offset:]); err != nil { return offset, err } else { offset += l } - p.Req = tmp + p.Req = _field return offset, nil } @@ -990,26 +983,27 @@ ReadStructEndError: func (p *MockExceptionTestResult) FastReadField0(buf []byte) (int, error) { offset := 0 + var _field *string if v, l, err := bthrift.Binary.ReadString(buf[offset:]); err != nil { return offset, err } else { offset += l - p.Success = &v + _field = &v } + p.Success = _field return offset, nil } func (p *MockExceptionTestResult) FastReadField1(buf []byte) (int, error) { offset := 0 - - tmp := NewException() - if l, err := tmp.FastRead(buf[offset:]); err != nil { + _field := NewException() + if l, err := _field.FastRead(buf[offset:]); err != nil { return offset, err } else { offset += l } - p.Err = tmp + p.Err = _field return offset, nil } @@ -1047,7 +1041,6 @@ func (p *MockExceptionTestResult) fastWriteField0(buf []byte, binaryWriter bthri if p.IsSetSuccess() { offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "success", thrift.STRING, 0) offset += bthrift.Binary.WriteStringNocopy(buf[offset:], binaryWriter, *p.Success) - offset += bthrift.Binary.WriteFieldEnd(buf[offset:]) } return offset @@ -1068,7 +1061,6 @@ func (p *MockExceptionTestResult) field0Length() int { if p.IsSetSuccess() { l += bthrift.Binary.FieldBeginLength("success", thrift.STRING, 0) l += bthrift.Binary.StringLengthNocopy(*p.Success) - l += bthrift.Binary.FieldEndLength() } return l diff --git a/internal/mocks/thrift/test.go b/internal/mocks/thrift/test.go index 4d8c4eb26c..6eba70afe6 100644 --- a/internal/mocks/thrift/test.go +++ b/internal/mocks/thrift/test.go @@ -1,5 +1,5 @@ /* - * Copyright 2022 CloudWeGo Authors + * Copyright 2024 CloudWeGo Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,28 +14,30 @@ * limitations under the License. */ -// Code generated by thriftgo (0.2.1). DO NOT EDIT. +// Code generated by thriftgo (0.3.13). DO NOT EDIT. package thrift import ( "context" "fmt" - "strings" - "github.com/apache/thrift/lib/go/thrift" + "strings" ) type MockReq struct { - Msg string `thrift:"Msg,1" json:"Msg"` - StrMap map[string]string `thrift:"strMap,2" json:"strMap"` - StrList []string `thrift:"strList,3" json:"strList"` + Msg string `thrift:"Msg,1" frugal:"1,default,string" json:"Msg"` + StrMap map[string]string `thrift:"strMap,2" frugal:"2,default,map" json:"strMap"` + StrList []string `thrift:"strList,3" frugal:"3,default,list" json:"strList"` } func NewMockReq() *MockReq { return &MockReq{} } +func (p *MockReq) InitDefault() { +} + func (p *MockReq) GetMsg() (v string) { return p.Msg } @@ -87,37 +89,30 @@ func (p *MockReq) Read(iprot thrift.TProtocol) (err error) { if err = p.ReadField1(iprot); err != nil { goto ReadFieldError } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } + } else if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError } case 2: if fieldTypeId == thrift.MAP { if err = p.ReadField2(iprot); err != nil { goto ReadFieldError } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } + } else if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError } case 3: if fieldTypeId == thrift.LIST { if err = p.ReadField3(iprot); err != nil { goto ReadFieldError } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } + } else if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError } default: if err = iprot.Skip(fieldTypeId); err != nil { goto SkipFieldError } } - if err = iprot.ReadFieldEnd(); err != nil { goto ReadFieldEndError } @@ -143,20 +138,22 @@ ReadStructEndError: } func (p *MockReq) ReadField1(iprot thrift.TProtocol) error { + + var _field string if v, err := iprot.ReadString(); err != nil { return err } else { - p.Msg = v + _field = v } + p.Msg = _field return nil } - func (p *MockReq) ReadField2(iprot thrift.TProtocol) error { _, _, size, err := iprot.ReadMapBegin() if err != nil { return err } - p.StrMap = make(map[string]string, size) + _field := make(map[string]string, size) for i := 0; i < size; i++ { var _key string if v, err := iprot.ReadString(); err != nil { @@ -172,21 +169,22 @@ func (p *MockReq) ReadField2(iprot thrift.TProtocol) error { _val = v } - p.StrMap[_key] = _val + _field[_key] = _val } if err := iprot.ReadMapEnd(); err != nil { return err } + p.StrMap = _field return nil } - func (p *MockReq) ReadField3(iprot thrift.TProtocol) error { _, size, err := iprot.ReadListBegin() if err != nil { return err } - p.StrList = make([]string, 0, size) + _field := make([]string, 0, size) for i := 0; i < size; i++ { + var _elem string if v, err := iprot.ReadString(); err != nil { return err @@ -194,11 +192,12 @@ func (p *MockReq) ReadField3(iprot thrift.TProtocol) error { _elem = v } - p.StrList = append(p.StrList, _elem) + _field = append(_field, _elem) } if err := iprot.ReadListEnd(); err != nil { return err } + p.StrList = _field return nil } @@ -220,7 +219,6 @@ func (p *MockReq) Write(oprot thrift.TProtocol) (err error) { fieldId = 3 goto WriteFieldError } - } if err = oprot.WriteFieldStop(); err != nil { goto WriteFieldStopError @@ -264,11 +262,9 @@ func (p *MockReq) writeField2(oprot thrift.TProtocol) (err error) { return err } for k, v := range p.StrMap { - if err := oprot.WriteString(k); err != nil { return err } - if err := oprot.WriteString(v); err != nil { return err } @@ -316,6 +312,7 @@ func (p *MockReq) String() string { return "" } return fmt.Sprintf("MockReq(%+v)", *p) + } func (p *MockReq) DeepEqual(ano *MockReq) bool { @@ -371,14 +368,17 @@ func (p *MockReq) Field3DeepEqual(src []string) bool { } type Exception struct { - Code int32 `thrift:"code,1" json:"code"` - Msg string `thrift:"msg,255" json:"msg"` + Code int32 `thrift:"code,1" frugal:"1,default,i32" json:"code"` + Msg string `thrift:"msg,255" frugal:"255,default,string" json:"msg"` } func NewException() *Exception { return &Exception{} } +func (p *Exception) InitDefault() { +} + func (p *Exception) GetCode() (v int32) { return p.Code } @@ -422,27 +422,22 @@ func (p *Exception) Read(iprot thrift.TProtocol) (err error) { if err = p.ReadField1(iprot); err != nil { goto ReadFieldError } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } + } else if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError } case 255: if fieldTypeId == thrift.STRING { if err = p.ReadField255(iprot); err != nil { goto ReadFieldError } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } + } else if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError } default: if err = iprot.Skip(fieldTypeId); err != nil { goto SkipFieldError } } - if err = iprot.ReadFieldEnd(); err != nil { goto ReadFieldEndError } @@ -468,20 +463,25 @@ ReadStructEndError: } func (p *Exception) ReadField1(iprot thrift.TProtocol) error { + + var _field int32 if v, err := iprot.ReadI32(); err != nil { return err } else { - p.Code = v + _field = v } + p.Code = _field return nil } - func (p *Exception) ReadField255(iprot thrift.TProtocol) error { + + var _field string if v, err := iprot.ReadString(); err != nil { return err } else { - p.Msg = v + _field = v } + p.Msg = _field return nil } @@ -499,7 +499,6 @@ func (p *Exception) Write(oprot thrift.TProtocol) (err error) { fieldId = 255 goto WriteFieldError } - } if err = oprot.WriteFieldStop(); err != nil { goto WriteFieldStopError @@ -557,6 +556,7 @@ func (p *Exception) String() string { return "" } return fmt.Sprintf("Exception(%+v)", *p) + } func (p *Exception) Error() string { return p.String() @@ -598,206 +598,17 @@ type Mock interface { ExceptionTest(ctx context.Context, req *MockReq) (r string, err error) } -type MockClient struct { - c thrift.TClient -} - -func NewMockClientFactory(t thrift.TTransport, f thrift.TProtocolFactory) *MockClient { - return &MockClient{ - c: thrift.NewTStandardClient(f.GetProtocol(t), f.GetProtocol(t)), - } -} - -func NewMockClientProtocol(t thrift.TTransport, iprot thrift.TProtocol, oprot thrift.TProtocol) *MockClient { - return &MockClient{ - c: thrift.NewTStandardClient(iprot, oprot), - } -} - -func NewMockClient(c thrift.TClient) *MockClient { - return &MockClient{ - c: c, - } -} - -func (p *MockClient) Client_() thrift.TClient { - return p.c -} - -func (p *MockClient) Test(ctx context.Context, req *MockReq) (r string, err error) { - var _args MockTestArgs - _args.Req = req - var _result MockTestResult - if err = p.Client_().Call(ctx, "Test", &_args, &_result); err != nil { - return - } - return _result.GetSuccess(), nil -} -func (p *MockClient) ExceptionTest(ctx context.Context, req *MockReq) (r string, err error) { - var _args MockExceptionTestArgs - _args.Req = req - var _result MockExceptionTestResult - if err = p.Client_().Call(ctx, "ExceptionTest", &_args, &_result); err != nil { - return - } - switch { - case _result.Err != nil: - return r, _result.Err - } - return _result.GetSuccess(), nil -} - -type MockProcessor struct { - processorMap map[string]thrift.TProcessorFunction - handler Mock -} - -func (p *MockProcessor) AddToProcessorMap(key string, processor thrift.TProcessorFunction) { - p.processorMap[key] = processor -} - -func (p *MockProcessor) GetProcessorFunction(key string) (processor thrift.TProcessorFunction, ok bool) { - processor, ok = p.processorMap[key] - return processor, ok -} - -func (p *MockProcessor) ProcessorMap() map[string]thrift.TProcessorFunction { - return p.processorMap -} - -func NewMockProcessor(handler Mock) *MockProcessor { - self := &MockProcessor{handler: handler, processorMap: make(map[string]thrift.TProcessorFunction)} - self.AddToProcessorMap("Test", &mockProcessorTest{handler: handler}) - self.AddToProcessorMap("ExceptionTest", &mockProcessorExceptionTest{handler: handler}) - return self -} -func (p *MockProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { - name, _, seqId, err := iprot.ReadMessageBegin() - if err != nil { - return false, err - } - if processor, ok := p.GetProcessorFunction(name); ok { - return processor.Process(ctx, seqId, iprot, oprot) - } - iprot.Skip(thrift.STRUCT) - iprot.ReadMessageEnd() - x := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function "+name) - oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId) - x.Write(oprot) - oprot.WriteMessageEnd() - oprot.Flush(ctx) - return false, x -} - -type mockProcessorTest struct { - handler Mock -} - -func (p *mockProcessorTest) Process(ctx context.Context, seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { - args := MockTestArgs{} - if err = args.Read(iprot); err != nil { - iprot.ReadMessageEnd() - x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error()) - oprot.WriteMessageBegin("Test", thrift.EXCEPTION, seqId) - x.Write(oprot) - oprot.WriteMessageEnd() - oprot.Flush(ctx) - return false, err - } - - iprot.ReadMessageEnd() - var err2 error - result := MockTestResult{} - var retval string - if retval, err2 = p.handler.Test(ctx, args.Req); err2 != nil { - x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "Internal error processing Test: "+err2.Error()) - oprot.WriteMessageBegin("Test", thrift.EXCEPTION, seqId) - x.Write(oprot) - oprot.WriteMessageEnd() - oprot.Flush(ctx) - return true, err2 - } else { - result.Success = &retval - } - if err2 = oprot.WriteMessageBegin("Test", thrift.REPLY, seqId); err2 != nil { - err = err2 - } - if err2 = result.Write(oprot); err == nil && err2 != nil { - err = err2 - } - if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil { - err = err2 - } - if err2 = oprot.Flush(ctx); err == nil && err2 != nil { - err = err2 - } - if err != nil { - return - } - return true, err -} - -type mockProcessorExceptionTest struct { - handler Mock -} - -func (p *mockProcessorExceptionTest) Process(ctx context.Context, seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { - args := MockExceptionTestArgs{} - if err = args.Read(iprot); err != nil { - iprot.ReadMessageEnd() - x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error()) - oprot.WriteMessageBegin("ExceptionTest", thrift.EXCEPTION, seqId) - x.Write(oprot) - oprot.WriteMessageEnd() - oprot.Flush(ctx) - return false, err - } - - iprot.ReadMessageEnd() - var err2 error - result := MockExceptionTestResult{} - var retval string - if retval, err2 = p.handler.ExceptionTest(ctx, args.Req); err2 != nil { - switch v := err2.(type) { - case *Exception: - result.Err = v - default: - x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "Internal error processing ExceptionTest: "+err2.Error()) - oprot.WriteMessageBegin("ExceptionTest", thrift.EXCEPTION, seqId) - x.Write(oprot) - oprot.WriteMessageEnd() - oprot.Flush(ctx) - return true, err2 - } - } else { - result.Success = &retval - } - if err2 = oprot.WriteMessageBegin("ExceptionTest", thrift.REPLY, seqId); err2 != nil { - err = err2 - } - if err2 = result.Write(oprot); err == nil && err2 != nil { - err = err2 - } - if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil { - err = err2 - } - if err2 = oprot.Flush(ctx); err == nil && err2 != nil { - err = err2 - } - if err != nil { - return - } - return true, err -} - type MockTestArgs struct { - Req *MockReq `thrift:"req,1" json:"req"` + Req *MockReq `thrift:"req,1" frugal:"1,default,MockReq" json:"req"` } func NewMockTestArgs() *MockTestArgs { return &MockTestArgs{} } +func (p *MockTestArgs) InitDefault() { +} + var MockTestArgs_Req_DEFAULT *MockReq func (p *MockTestArgs) GetReq() (v *MockReq) { @@ -810,10 +621,6 @@ func (p *MockTestArgs) SetReq(val *MockReq) { p.Req = val } -func (p *MockTestArgs) GetFirstArgument() (interface{}) { - return p.Req -} - var fieldIDToName_MockTestArgs = map[int16]string{ 1: "req", } @@ -846,17 +653,14 @@ func (p *MockTestArgs) Read(iprot thrift.TProtocol) (err error) { if err = p.ReadField1(iprot); err != nil { goto ReadFieldError } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } + } else if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError } default: if err = iprot.Skip(fieldTypeId); err != nil { goto SkipFieldError } } - if err = iprot.ReadFieldEnd(); err != nil { goto ReadFieldEndError } @@ -882,10 +686,11 @@ ReadStructEndError: } func (p *MockTestArgs) ReadField1(iprot thrift.TProtocol) error { - p.Req = NewMockReq() - if err := p.Req.Read(iprot); err != nil { + _field := NewMockReq() + if err := _field.Read(iprot); err != nil { return err } + p.Req = _field return nil } @@ -899,7 +704,6 @@ func (p *MockTestArgs) Write(oprot thrift.TProtocol) (err error) { fieldId = 1 goto WriteFieldError } - } if err = oprot.WriteFieldStop(); err != nil { goto WriteFieldStopError @@ -940,6 +744,7 @@ func (p *MockTestArgs) String() string { return "" } return fmt.Sprintf("MockTestArgs(%+v)", *p) + } func (p *MockTestArgs) DeepEqual(ano *MockTestArgs) bool { @@ -963,13 +768,16 @@ func (p *MockTestArgs) Field1DeepEqual(src *MockReq) bool { } type MockTestResult struct { - Success *string `thrift:"success,0,optional" json:"success,omitempty"` + Success *string `thrift:"success,0,optional" frugal:"0,optional,string" json:"success,omitempty"` } func NewMockTestResult() *MockTestResult { return &MockTestResult{} } +func (p *MockTestResult) InitDefault() { +} + var MockTestResult_Success_DEFAULT string func (p *MockTestResult) GetSuccess() (v string) { @@ -982,10 +790,6 @@ func (p *MockTestResult) SetSuccess(x interface{}) { p.Success = x.(*string) } -func (p *MockTestResult) GetResult() interface{} { - return p.Success -} - var fieldIDToName_MockTestResult = map[int16]string{ 0: "success", } @@ -1018,17 +822,14 @@ func (p *MockTestResult) Read(iprot thrift.TProtocol) (err error) { if err = p.ReadField0(iprot); err != nil { goto ReadFieldError } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } + } else if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError } default: if err = iprot.Skip(fieldTypeId); err != nil { goto SkipFieldError } } - if err = iprot.ReadFieldEnd(); err != nil { goto ReadFieldEndError } @@ -1054,11 +855,14 @@ ReadStructEndError: } func (p *MockTestResult) ReadField0(iprot thrift.TProtocol) error { + + var _field *string if v, err := iprot.ReadString(); err != nil { return err } else { - p.Success = &v + _field = &v } + p.Success = _field return nil } @@ -1072,7 +876,6 @@ func (p *MockTestResult) Write(oprot thrift.TProtocol) (err error) { fieldId = 0 goto WriteFieldError } - } if err = oprot.WriteFieldStop(); err != nil { goto WriteFieldStopError @@ -1115,6 +918,7 @@ func (p *MockTestResult) String() string { return "" } return fmt.Sprintf("MockTestResult(%+v)", *p) + } func (p *MockTestResult) DeepEqual(ano *MockTestResult) bool { @@ -1143,13 +947,16 @@ func (p *MockTestResult) Field0DeepEqual(src *string) bool { } type MockExceptionTestArgs struct { - Req *MockReq `thrift:"req,1" json:"req"` + Req *MockReq `thrift:"req,1" frugal:"1,default,MockReq" json:"req"` } func NewMockExceptionTestArgs() *MockExceptionTestArgs { return &MockExceptionTestArgs{} } +func (p *MockExceptionTestArgs) InitDefault() { +} + var MockExceptionTestArgs_Req_DEFAULT *MockReq func (p *MockExceptionTestArgs) GetReq() (v *MockReq) { @@ -1194,17 +1001,14 @@ func (p *MockExceptionTestArgs) Read(iprot thrift.TProtocol) (err error) { if err = p.ReadField1(iprot); err != nil { goto ReadFieldError } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } + } else if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError } default: if err = iprot.Skip(fieldTypeId); err != nil { goto SkipFieldError } } - if err = iprot.ReadFieldEnd(); err != nil { goto ReadFieldEndError } @@ -1230,10 +1034,11 @@ ReadStructEndError: } func (p *MockExceptionTestArgs) ReadField1(iprot thrift.TProtocol) error { - p.Req = NewMockReq() - if err := p.Req.Read(iprot); err != nil { + _field := NewMockReq() + if err := _field.Read(iprot); err != nil { return err } + p.Req = _field return nil } @@ -1247,7 +1052,6 @@ func (p *MockExceptionTestArgs) Write(oprot thrift.TProtocol) (err error) { fieldId = 1 goto WriteFieldError } - } if err = oprot.WriteFieldStop(); err != nil { goto WriteFieldStopError @@ -1288,6 +1092,7 @@ func (p *MockExceptionTestArgs) String() string { return "" } return fmt.Sprintf("MockExceptionTestArgs(%+v)", *p) + } func (p *MockExceptionTestArgs) DeepEqual(ano *MockExceptionTestArgs) bool { @@ -1311,14 +1116,17 @@ func (p *MockExceptionTestArgs) Field1DeepEqual(src *MockReq) bool { } type MockExceptionTestResult struct { - Success *string `thrift:"success,0,optional" json:"success,omitempty"` - Err *Exception `thrift:"err,1,optional" json:"err,omitempty"` + Success *string `thrift:"success,0,optional" frugal:"0,optional,string" json:"success,omitempty"` + Err *Exception `thrift:"err,1,optional" frugal:"1,optional,Exception" json:"err,omitempty"` } func NewMockExceptionTestResult() *MockExceptionTestResult { return &MockExceptionTestResult{} } +func (p *MockExceptionTestResult) InitDefault() { +} + var MockExceptionTestResult_Success_DEFAULT string func (p *MockExceptionTestResult) GetSuccess() (v string) { @@ -1380,27 +1188,22 @@ func (p *MockExceptionTestResult) Read(iprot thrift.TProtocol) (err error) { if err = p.ReadField0(iprot); err != nil { goto ReadFieldError } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } + } else if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError } case 1: if fieldTypeId == thrift.STRUCT { if err = p.ReadField1(iprot); err != nil { goto ReadFieldError } - } else { - if err = iprot.Skip(fieldTypeId); err != nil { - goto SkipFieldError - } + } else if err = iprot.Skip(fieldTypeId); err != nil { + goto SkipFieldError } default: if err = iprot.Skip(fieldTypeId); err != nil { goto SkipFieldError } } - if err = iprot.ReadFieldEnd(); err != nil { goto ReadFieldEndError } @@ -1426,19 +1229,22 @@ ReadStructEndError: } func (p *MockExceptionTestResult) ReadField0(iprot thrift.TProtocol) error { + + var _field *string if v, err := iprot.ReadString(); err != nil { return err } else { - p.Success = &v + _field = &v } + p.Success = _field return nil } - func (p *MockExceptionTestResult) ReadField1(iprot thrift.TProtocol) error { - p.Err = NewException() - if err := p.Err.Read(iprot); err != nil { + _field := NewException() + if err := _field.Read(iprot); err != nil { return err } + p.Err = _field return nil } @@ -1456,7 +1262,6 @@ func (p *MockExceptionTestResult) Write(oprot thrift.TProtocol) (err error) { fieldId = 1 goto WriteFieldError } - } if err = oprot.WriteFieldStop(); err != nil { goto WriteFieldStopError @@ -1518,6 +1323,7 @@ func (p *MockExceptionTestResult) String() string { return "" } return fmt.Sprintf("MockExceptionTestResult(%+v)", *p) + } func (p *MockExceptionTestResult) DeepEqual(ano *MockExceptionTestResult) bool { diff --git a/pkg/generic/binary_test/generic_init.go b/pkg/generic/binary_test/generic_init.go index d7f5bd4418..6b5ae69fe2 100644 --- a/pkg/generic/binary_test/generic_init.go +++ b/pkg/generic/binary_test/generic_init.go @@ -34,7 +34,7 @@ import ( thrift "github.com/cloudwego/kitex/pkg/protocol/bthrift/apache" "github.com/cloudwego/kitex/pkg/serviceinfo" "github.com/cloudwego/kitex/pkg/transmeta" - "github.com/cloudwego/kitex/pkg/utils" + "github.com/cloudwego/kitex/pkg/utils/fastthrift" "github.com/cloudwego/kitex/server" "github.com/cloudwego/kitex/server/genericserver" "github.com/cloudwego/kitex/transport" @@ -99,11 +99,10 @@ type GenericServiceMockImpl struct{} // GenericCall ... func (g *GenericServiceMockImpl) GenericCall(ctx context.Context, method string, request interface{}) (response interface{}, err error) { - rc := utils.NewThriftMessageCodec() buf := request.([]byte) var args2 kt.MockTestArgs - mth, seqID, err := rc.Decode(buf, &args2) + mth, seqID, err := fastthrift.UnmarshalMsg(buf, &args2) if err != nil { return nil, err } @@ -116,7 +115,7 @@ func (g *GenericServiceMockImpl) GenericCall(ctx context.Context, method string, result := kt.NewMockTestResult() result.Success = &resp - buf, err = rc.Encode(mth, thrift.REPLY, seqID, result) + buf, err = fastthrift.MarshalMsg(mth, fastthrift.REPLY, seqID, result) return buf, err } diff --git a/pkg/generic/binary_test/generic_test.go b/pkg/generic/binary_test/generic_test.go index f5d7613b64..57d0c34b56 100644 --- a/pkg/generic/binary_test/generic_test.go +++ b/pkg/generic/binary_test/generic_test.go @@ -34,7 +34,7 @@ import ( "github.com/cloudwego/kitex/pkg/generic" "github.com/cloudwego/kitex/pkg/kerrors" thrift "github.com/cloudwego/kitex/pkg/protocol/bthrift/apache" - "github.com/cloudwego/kitex/pkg/utils" + "github.com/cloudwego/kitex/pkg/utils/fastthrift" "github.com/cloudwego/kitex/server" ) @@ -111,8 +111,7 @@ func rawThriftBinaryMockReq(t *testing.T) { args.Req = req // encode - rc := utils.NewThriftMessageCodec() - buf, err := rc.Encode("Test", thrift.CALL, 100, args) + buf, err := fastthrift.MarshalMsg("Test", fastthrift.CALL, 100, args) test.Assert(t, err == nil, err) resp, err := cli.GenericCall(context.Background(), "Test", buf) @@ -121,7 +120,7 @@ func rawThriftBinaryMockReq(t *testing.T) { // decode buf = resp.([]byte) var result kt.MockTestResult - method, seqID, err := rc.Decode(buf, &result) + method, seqID, err := fastthrift.UnmarshalMsg(buf, &result) test.Assert(t, err == nil, err) test.Assert(t, method == "Test", method) test.Assert(t, seqID != 100, seqID) @@ -148,8 +147,7 @@ func rawThriftBinary2NormalServer(t *testing.T) { args.Req = req // encode - rc := utils.NewThriftMessageCodec() - buf, err := rc.Encode("Test", thrift.CALL, 100, args) + buf, err := fastthrift.MarshalMsg("Test", fastthrift.CALL, 100, args) test.Assert(t, err == nil, err) resp, err := cli.GenericCall(context.Background(), "Test", buf, callopt.WithRPCTimeout(100*time.Second)) @@ -158,7 +156,7 @@ func rawThriftBinary2NormalServer(t *testing.T) { // decode buf = resp.([]byte) var result kt.MockTestResult - method, seqID, err := rc.Decode(buf, &result) + method, seqID, err := fastthrift.UnmarshalMsg(buf, &result) test.Assert(t, err == nil, err) test.Assert(t, method == "Test", method) // seqID会在kitex中覆盖,避免TTHeader和Payload codec 不一致问题 diff --git a/pkg/generic/binarythrift_codec_test.go b/pkg/generic/binarythrift_codec_test.go index 5393b16efc..0a211d3994 100644 --- a/pkg/generic/binarythrift_codec_test.go +++ b/pkg/generic/binarythrift_codec_test.go @@ -22,11 +22,10 @@ import ( kt "github.com/cloudwego/kitex/internal/mocks/thrift" "github.com/cloudwego/kitex/internal/test" - thrift "github.com/cloudwego/kitex/pkg/protocol/bthrift/apache" "github.com/cloudwego/kitex/pkg/remote" "github.com/cloudwego/kitex/pkg/rpcinfo" "github.com/cloudwego/kitex/pkg/serviceinfo" - "github.com/cloudwego/kitex/pkg/utils" + "github.com/cloudwego/kitex/pkg/utils/fastthrift" ) func TestBinaryThriftCodec(t *testing.T) { @@ -34,8 +33,7 @@ func TestBinaryThriftCodec(t *testing.T) { args := kt.NewMockTestArgs() args.Req = req // encode - rc := utils.NewThriftMessageCodec() - buf, err := rc.Encode("mock", thrift.CALL, 100, args) + buf, err := fastthrift.MarshalMsg("mock", fastthrift.CALL, 100, args) test.Assert(t, err == nil, err) btc := &binaryThriftCodec{thriftCodec} @@ -92,7 +90,7 @@ func TestBinaryThriftCodec(t *testing.T) { test.Assert(t, seqID == 1, seqID) var req2 kt.MockTestArgs - method, seqID2, err2 := rc.Decode(reqBuf, &req2) + method, seqID2, err2 := fastthrift.UnmarshalMsg(reqBuf, &req2) test.Assert(t, err2 == nil, err) test.Assert(t, seqID2 == 1, seqID) test.Assert(t, method == "mock", method) diff --git a/pkg/protocol/bthrift/binary.go b/pkg/protocol/bthrift/binary.go index 0a1421ea8c..1d2bad2f34 100644 --- a/pkg/protocol/bthrift/binary.go +++ b/pkg/protocol/bthrift/binary.go @@ -172,9 +172,8 @@ func (binaryProtocol) WriteBinaryNocopy(buf []byte, binaryWriter BinaryWriter, v return l + len(value) } -func (binaryProtocol) MessageBeginLength(name string, typeID thrift.TMessageType, seqid int32) int { - version := uint32(thrift.VERSION_1) | uint32(typeID) - return Binary.I32Length(int32(version)) + Binary.StringLength(name) + Binary.I32Length(seqid) +func (binaryProtocol) MessageBeginLength(name string, _ thrift.TMessageType, _ int32) int { + return 4 + Binary.StringLength(name) + 4 } func (binaryProtocol) MessageEndLength() int { diff --git a/pkg/protocol/bthrift/exception.go b/pkg/protocol/bthrift/exception.go index e3ed256840..598a47ec9d 100644 --- a/pkg/protocol/bthrift/exception.go +++ b/pkg/protocol/bthrift/exception.go @@ -24,7 +24,7 @@ import ( ) // ApplicationException is for replacing apache.TApplicationException -// it implements ThriftMsgFastCodec interface. +// it implements ThriftFastCodec interface. type ApplicationException struct { t int32 m string @@ -180,7 +180,7 @@ func (e *ApplicationException) Error() string { } // TransportException is for replacing apache.TransportException -// it implements ThriftMsgFastCodec interface. +// it implements ThriftFastCodec interface. type TransportException struct { ApplicationException // same implementation ... } @@ -194,7 +194,7 @@ func NewTransportException(t int32, m string) *TransportException { } // ProtocolException is for replacing apache.ProtocolException -// it implements ThriftMsgFastCodec interface. +// it implements ThriftFastCodec interface. type ProtocolException struct { ApplicationException // same implementation ... } diff --git a/pkg/protocol/bthrift/interface.go b/pkg/protocol/bthrift/interface.go index f3922b1e64..62bbb23dc6 100644 --- a/pkg/protocol/bthrift/interface.go +++ b/pkg/protocol/bthrift/interface.go @@ -26,6 +26,13 @@ type BinaryWriter interface { WriteDirect(b []byte, remainCap int) error } +// ThriftFastCodec represents the interface of thrift fastcodec generated structs +type ThriftFastCodec interface { + BLength() int + FastWriteNocopy(buf []byte, binaryWriter BinaryWriter) int + FastRead(buf []byte) (int, error) +} + // BTProtocol . type BTProtocol interface { WriteMessageBegin(buf []byte, name string, typeID thrift.TMessageType, seqid int32) int diff --git a/pkg/remote/codec/thrift/thrift.go b/pkg/remote/codec/thrift/thrift.go index 0d65f224c7..9a9bab0b02 100644 --- a/pkg/remote/codec/thrift/thrift.go +++ b/pkg/remote/codec/thrift/thrift.go @@ -111,7 +111,7 @@ func (c thriftCodec) Marshal(ctx context.Context, message remote.Message, out re // encode with FastWrite if c.CodecType&FastWrite != 0 { - if msg, ok := data.(ThriftMsgFastCodec); ok { + if msg, ok := data.(bthrift.ThriftFastCodec); ok { return encodeFastThrift(out, methodName, msgType, seqID, msg) } } @@ -131,7 +131,7 @@ func (c thriftCodec) Marshal(ctx context.Context, message remote.Message, out re } // encodeFastThrift encode with the FastCodec way -func encodeFastThrift(out remote.ByteBuffer, methodName string, msgType remote.MessageType, seqID int32, msg ThriftMsgFastCodec) error { +func encodeFastThrift(out remote.ByteBuffer, methodName string, msgType remote.MessageType, seqID int32, msg bthrift.ThriftFastCodec) error { nw, _ := out.(remote.NocopyWrite) // nocopy write is a special implementation of linked buffer, only bytebuffer implement NocopyWrite do FastWrite msgBeginLen := bthrift.Binary.MessageBeginLength(methodName, thrift.TMessageType(msgType), seqID) @@ -259,11 +259,9 @@ type MessageReaderWithMethodWithContext interface { Read(ctx context.Context, method string, dataLen int, oprot thrift.TProtocol) error } -type ThriftMsgFastCodec interface { - BLength() int - FastWriteNocopy(buf []byte, binaryWriter bthrift.BinaryWriter) int - FastRead(buf []byte) (int, error) -} +// ThriftMsgFastCodec ... +// Deprecated: use `bthrift.ThriftFastCodec` +type ThriftMsgFastCodec = bthrift.ThriftFastCodec func getValidData(methodName string, message remote.Message) (interface{}, error) { if err := codec.NewDataIfNeeded(methodName, message); err != nil { diff --git a/pkg/remote/codec/thrift/thrift_data.go b/pkg/remote/codec/thrift/thrift_data.go index 548b634a68..7d42784d7a 100644 --- a/pkg/remote/codec/thrift/thrift_data.go +++ b/pkg/remote/codec/thrift/thrift_data.go @@ -51,7 +51,7 @@ func (c thriftCodec) marshalThriftData(ctx context.Context, data interface{}) ([ // encode with FastWrite if c.CodecType&FastWrite != 0 { - if msg, ok := data.(ThriftMsgFastCodec); ok { + if msg, ok := data.(bthrift.ThriftFastCodec); ok { payloadSize := msg.BLength() payload := mcache.Malloc(payloadSize) msg.FastWriteNocopy(payload, nil) @@ -146,12 +146,12 @@ func (c thriftCodec) fastMessageUnmarshalAvailable(data interface{}, payloadLen if payloadLen == 0 && c.CodecType&EnableSkipDecoder == 0 { return false } - _, ok := data.(ThriftMsgFastCodec) + _, ok := data.(bthrift.ThriftFastCodec) return ok } func (c thriftCodec) fastUnmarshal(tProt *BinaryProtocol, data interface{}, dataLen int) error { - msg := data.(ThriftMsgFastCodec) + msg := data.(bthrift.ThriftFastCodec) if dataLen > 0 { buf, err := tProt.next(dataLen) if err != nil { diff --git a/pkg/remote/codec/thrift/thrift_data_test.go b/pkg/remote/codec/thrift/thrift_data_test.go index 47f9ce5dcd..7ba4a5ae57 100644 --- a/pkg/remote/codec/thrift/thrift_data_test.go +++ b/pkg/remote/codec/thrift/thrift_data_test.go @@ -22,7 +22,7 @@ import ( "strings" "testing" - "github.com/cloudwego/kitex/internal/mocks/thrift/fast" + mocks "github.com/cloudwego/kitex/internal/mocks/thrift" "github.com/cloudwego/kitex/internal/test" "github.com/cloudwego/kitex/pkg/protocol/bthrift" thrift "github.com/cloudwego/kitex/pkg/protocol/bthrift/apache" @@ -30,7 +30,7 @@ import ( ) var ( - mockReq = &fast.MockReq{ + mockReq = &mocks.MockReq{ Msg: "hello", } mockReqThrift = []byte{ @@ -77,26 +77,26 @@ func TestMarshalThriftData(t *testing.T) { func Test_decodeBasicThriftData(t *testing.T) { t.Run("empty-input", func(t *testing.T) { - req := &fast.MockReq{} + req := &mocks.MockReq{} tProt := NewBinaryProtocol(remote.NewReaderBuffer([]byte{})) err := decodeBasicThriftData(context.Background(), tProt, "mock", -1, 0, req) test.Assert(t, err != nil, err) }) t.Run("invalid-input", func(t *testing.T) { - req := &fast.MockReq{} + req := &mocks.MockReq{} tProt := NewBinaryProtocol(remote.NewReaderBuffer([]byte{0xff})) err := decodeBasicThriftData(context.Background(), tProt, "mock", -1, 0, req) test.Assert(t, err != nil, err) }) t.Run("normal-input", func(t *testing.T) { - req := &fast.MockReq{} + req := &mocks.MockReq{} tProt := NewBinaryProtocol(remote.NewReaderBuffer(mockReqThrift)) err := decodeBasicThriftData(context.Background(), tProt, "mock", -1, 0, req) checkDecodeResult(t, err, req) }) } -func checkDecodeResult(t *testing.T, err error, req *fast.MockReq) { +func checkDecodeResult(t *testing.T, err error, req *mocks.MockReq) { test.Assert(t, err == nil, err) test.Assert(t, req.Msg == mockReq.Msg, req.Msg, mockReq.Msg) test.Assert(t, len(req.StrMap) == 0, req.StrMap) @@ -105,17 +105,17 @@ func checkDecodeResult(t *testing.T, err error, req *fast.MockReq) { func TestUnmarshalThriftData(t *testing.T) { t.Run("NoCodec(=FastCodec)", func(t *testing.T) { - req := &fast.MockReq{} + req := &mocks.MockReq{} err := UnmarshalThriftData(context.Background(), nil, "mock", mockReqThrift, req) checkDecodeResult(t, err, req) }) t.Run("FastCodec", func(t *testing.T) { - req := &fast.MockReq{} + req := &mocks.MockReq{} err := UnmarshalThriftData(context.Background(), NewThriftCodecWithConfig(FastRead|FastWrite), "mock", mockReqThrift, req) checkDecodeResult(t, err, req) }) t.Run("BasicCodec", func(t *testing.T) { - req := &fast.MockReq{} + req := &mocks.MockReq{} err := UnmarshalThriftData(context.Background(), NewThriftCodecWithConfig(Basic), "mock", mockReqThrift, req) checkDecodeResult(t, err, req) }) @@ -124,13 +124,13 @@ func TestUnmarshalThriftData(t *testing.T) { func TestThriftCodec_unmarshalThriftData(t *testing.T) { t.Run("FastCodec with SkipDecoder enabled", func(t *testing.T) { - req := &fast.MockReq{} + req := &mocks.MockReq{} codec := &thriftCodec{FastRead | EnableSkipDecoder} tProt := NewBinaryProtocol(remote.NewReaderBuffer(mockReqThrift)) defer tProt.Recycle() // specify dataLen with 0 so that skipDecoder works err := codec.unmarshalThriftData(context.Background(), tProt, "mock", req, -1, 0) - checkDecodeResult(t, err, &fast.MockReq{ + checkDecodeResult(t, err, &mocks.MockReq{ Msg: req.Msg, StrList: req.StrList, StrMap: req.StrMap, @@ -138,7 +138,7 @@ func TestThriftCodec_unmarshalThriftData(t *testing.T) { }) t.Run("FastCodec with SkipDecoder enabled, failed in using SkipDecoder Buffer", func(t *testing.T) { - req := &fast.MockReq{} + req := &mocks.MockReq{} codec := &thriftCodec{FastRead | EnableSkipDecoder} // these bytes are mapped to // Msg string `thrift:"Msg,1" json:"Msg"` diff --git a/pkg/remote/codec/thrift/thrift_frugal_test.go b/pkg/remote/codec/thrift/thrift_frugal_test.go index f2546da553..469bf23c0d 100644 --- a/pkg/remote/codec/thrift/thrift_frugal_test.go +++ b/pkg/remote/codec/thrift/thrift_frugal_test.go @@ -29,7 +29,7 @@ import ( "strings" "testing" - "github.com/cloudwego/kitex/internal/mocks/thrift/fast" + mocks "github.com/cloudwego/kitex/internal/mocks/thrift" "github.com/cloudwego/kitex/internal/test" "github.com/cloudwego/kitex/pkg/remote" "github.com/cloudwego/kitex/pkg/rpcinfo" @@ -220,7 +220,7 @@ func TestUnmarshalThriftDataFrugal(t *testing.T) { } for _, codec := range successfulCodecs { err := UnmarshalThriftData(context.Background(), codec, "mock", mockReqThrift, req) - checkDecodeResult(t, err, &fast.MockReq{ + checkDecodeResult(t, err, &mocks.MockReq{ Msg: req.Msg, StrList: req.StrList, StrMap: req.StrMap, @@ -241,7 +241,7 @@ func TestThriftCodec_unmarshalThriftDataFrugal(t *testing.T) { defer tProt.Recycle() // specify dataLen with 0 so that skipDecoder works err := codec.unmarshalThriftData(context.Background(), tProt, "mock", req, -1, 0) - checkDecodeResult(t, err, &fast.MockReq{ + checkDecodeResult(t, err, &mocks.MockReq{ Msg: req.Msg, StrList: req.StrList, StrMap: req.StrMap, diff --git a/pkg/remote/codec/thrift/thrift_test.go b/pkg/remote/codec/thrift/thrift_test.go index d7bef3b027..408bdfddda 100644 --- a/pkg/remote/codec/thrift/thrift_test.go +++ b/pkg/remote/codec/thrift/thrift_test.go @@ -24,7 +24,7 @@ import ( "github.com/cloudwego/netpoll" "github.com/cloudwego/kitex/internal/mocks" - mt "github.com/cloudwego/kitex/internal/mocks/thrift/fast" + mt "github.com/cloudwego/kitex/internal/mocks/thrift" "github.com/cloudwego/kitex/internal/test" "github.com/cloudwego/kitex/pkg/protocol/bthrift" thrift "github.com/cloudwego/kitex/pkg/protocol/bthrift/apache" diff --git a/pkg/utils/fastthrift/fast_thrift.go b/pkg/utils/fastthrift/fast_thrift.go deleted file mode 100644 index c15d40f4dc..0000000000 --- a/pkg/utils/fastthrift/fast_thrift.go +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2022 CloudWeGo Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package fastthrift - -import ( - "github.com/bytedance/gopkg/lang/dirtmake" - - "github.com/cloudwego/kitex/pkg/remote/codec/thrift" -) - -// FastMarshal marshals the msg to buf. The msg should be generated by Kitex tool and implement ThriftMsgFastCodec. -func FastMarshal(msg thrift.ThriftMsgFastCodec) []byte { - buf := dirtmake.Bytes(msg.BLength(), msg.BLength()) - msg.FastWriteNocopy(buf, nil) - return buf -} - -// FastUnmarshal unmarshal the buf into msg. The msg should be generated by Kitex tool and implement ThriftMsgFastCodec. -func FastUnmarshal(buf []byte, msg thrift.ThriftMsgFastCodec) error { - _, err := msg.FastRead(buf) - return err -} diff --git a/pkg/utils/fastthrift/fastthrift.go b/pkg/utils/fastthrift/fastthrift.go new file mode 100644 index 0000000000..0d27d849f1 --- /dev/null +++ b/pkg/utils/fastthrift/fastthrift.go @@ -0,0 +1,82 @@ +/* + * Copyright 2022 CloudWeGo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package fastthrift + +import ( + "errors" + + "github.com/bytedance/gopkg/lang/dirtmake" + + "github.com/cloudwego/kitex/pkg/protocol/bthrift" + thrift "github.com/cloudwego/kitex/pkg/protocol/bthrift/apache" +) + +// FastMarshal marshals the msg to buf. The msg should be generated by Kitex tool and implement ThriftFastCodec. +func FastMarshal(msg bthrift.ThriftFastCodec) []byte { + sz := msg.BLength() + buf := dirtmake.Bytes(sz, sz) + msg.FastWriteNocopy(buf, nil) + return buf +} + +// FastUnmarshal unmarshal the buf into msg. The msg should be generated by Kitex tool and implement ThriftFastCodec. +func FastUnmarshal(buf []byte, msg bthrift.ThriftFastCodec) error { + _, err := msg.FastRead(buf) + return err +} + +// for msgType of MarshalMsg +// Please use theses consts instead of relying on apache thrift.TMessageType +const ( + CALL = uint8(1) + REPLY = uint8(2) + EXCEPTION = uint8(3) + ONEWAY = uint8(4) +) + +// MarshalMsg encodes the given msg to buf for generic thrift RPC. +func MarshalMsg(method string, msgType uint8, seq int32, msg bthrift.ThriftFastCodec) ([]byte, error) { + if method == "" { + return nil, errors.New("method not set") + } + sz := bthrift.Binary.MessageBeginLength(method, thrift.TMessageType(msgType), seq) + msg.BLength() + b := dirtmake.Bytes(sz, sz) + i := bthrift.Binary.WriteMessageBegin(b, method, thrift.TMessageType(msgType), seq) + _ = msg.FastWriteNocopy(b[i:], nil) + return b, nil +} + +// UnmarshalMsg parses the given buf and stores the result to msg for generic thrift RPC. +// for EXCEPTION msgType, it will returns `err` with *bthrift.ApplicationException type without storing the result to msg. +func UnmarshalMsg(b []byte, msg bthrift.ThriftFastCodec) (method string, seq int32, err error) { + method, msgType, seq, i, err := bthrift.Binary.ReadMessageBegin(b) + if err != nil { + return "", 0, err + } + b = b[i:] + + if uint8(msgType) == EXCEPTION { + ex := bthrift.NewApplicationException(thrift.UNKNOWN_APPLICATION_EXCEPTION, "") + _, err = ex.FastRead(b) + if err != nil { + return method, seq, err + } + return method, seq, ex + } + _, err = msg.FastRead(b) + return method, seq, err +} diff --git a/pkg/utils/fastthrift/fast_thrift_test.go b/pkg/utils/fastthrift/fastthrift_test.go similarity index 60% rename from pkg/utils/fastthrift/fast_thrift_test.go rename to pkg/utils/fastthrift/fastthrift_test.go index 7336ed3839..31525da8fa 100644 --- a/pkg/utils/fastthrift/fast_thrift_test.go +++ b/pkg/utils/fastthrift/fastthrift_test.go @@ -19,8 +19,10 @@ package fastthrift import ( "testing" - mocks "github.com/cloudwego/kitex/internal/mocks/thrift/fast" + mocks "github.com/cloudwego/kitex/internal/mocks/thrift" "github.com/cloudwego/kitex/internal/test" + "github.com/cloudwego/kitex/pkg/protocol/bthrift" + thrift "github.com/cloudwego/kitex/pkg/protocol/bthrift/apache" ) var ( @@ -49,6 +51,35 @@ func TestFastThrift(t *testing.T) { test.Assert(t, len(req1.StrList) == len(req2.StrList)) } +func TestMarshalMsg(t *testing.T) { + // CALL and REPLY + + req := &mocks.MockReq{} + req.Msg = "Hello" + b, err := MarshalMsg("Echo", CALL, 1, req) + test.Assert(t, err == nil, err) + + resp := &mocks.MockReq{} + method, seq, err := UnmarshalMsg(b, resp) + test.Assert(t, err == nil, err) + test.Assert(t, method == "Echo", method) + test.Assert(t, seq == 1, seq) + test.Assert(t, resp.Msg == req.Msg, resp.Msg) + + // EXCEPTION + + ex := bthrift.NewApplicationException(thrift.WRONG_METHOD_NAME, "Ex!") + b, err = MarshalMsg("ExMethod", EXCEPTION, 2, ex) + test.Assert(t, err == nil, err) + method, seq, err = UnmarshalMsg(b, nil) + test.Assert(t, err != nil) + test.Assert(t, method == "ExMethod") + test.Assert(t, seq == 2) + e, ok := err.(*bthrift.ApplicationException) + test.Assert(t, ok) + test.Assert(t, e.TypeID() == ex.TypeID() && e.Error() == ex.Error()) +} + func BenchmarkFastUnmarshal(b *testing.B) { buf := FastMarshal(newRequest()) b.ResetTimer() diff --git a/pkg/utils/kitexutil/kitexutil_test.go b/pkg/utils/kitexutil/kitexutil_test.go index 5c76097494..6766aafcad 100644 --- a/pkg/utils/kitexutil/kitexutil_test.go +++ b/pkg/utils/kitexutil/kitexutil_test.go @@ -23,7 +23,7 @@ import ( "reflect" "testing" - mocks "github.com/cloudwego/kitex/internal/mocks/thrift/fast" + mocks "github.com/cloudwego/kitex/internal/mocks/thrift" "github.com/cloudwego/kitex/internal/test" "github.com/cloudwego/kitex/pkg/rpcinfo" "github.com/cloudwego/kitex/pkg/utils"