From 5c89f387f291389fc17d80097c4ec34c488cd768 Mon Sep 17 00:00:00 2001 From: HeyJavaBean Date: Thu, 21 Nov 2024 16:20:24 +0800 Subject: [PATCH] optimize: add interface for new kitex version --- apache_adaptor/adaptor.go | 83 +++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/apache_adaptor/adaptor.go b/apache_adaptor/adaptor.go index 8703bfa..1ca629d 100644 --- a/apache_adaptor/adaptor.go +++ b/apache_adaptor/adaptor.go @@ -24,6 +24,11 @@ import ( "github.com/cloudwego/gopkg/protocol/thrift" ) +type bufioxReaderWriter interface { + GetBufioxReader() bufiox.Reader + GetBufioxWriter() bufiox.Writer +} + type ByteBuffer interface { // Next reads the next n bytes sequentially and returns the original buffer. Next(n int) (p []byte, err error) @@ -58,32 +63,28 @@ func next2Reader(n ByteBuffer) io.Reader { } func AdaptRead(iprot interface{}, readFunc func(buf []byte) (int, error)) error { - // 通过过渡手段先让用户的 Apache Codec 变成冷门路径 - - // todo - // 先给 kitex 新版本 TProtocol 补全接口 - // 尝试类型断言(对下一个新版本有效) - var br bufiox.Reader - fieldNames := []string{"br", "trans"} - for _, fn := range fieldNames { - reader, exist, err := getUnexportField(iprot, fn) - if err != nil { - return err - } - if exist { - switch r := reader.(type) { - case bufiox.Reader: - br = r - // case io.Reader: - // br = bufiox.NewDefaultReader(r) - case ByteBuffer: - rd := next2Reader(r) - br = bufiox.NewDefaultReader(rd) - default: - return fmt.Errorf("reader not ok") + if bp, ok := iprot.(bufioxReaderWriter); ok { + br = bp.GetBufioxReader() + } else { + fieldNames := []string{"br", "trans"} + for _, fn := range fieldNames { + reader, exist, err := getUnexportField(iprot, fn) + if err != nil { + return err + } + if exist { + switch r := reader.(type) { + case bufiox.Reader: + br = r + case ByteBuffer: + rd := next2Reader(r) + br = bufiox.NewDefaultReader(rd) + default: + return fmt.Errorf("reader not ok") + } + break } - break } } if br == nil { @@ -99,22 +100,26 @@ func AdaptRead(iprot interface{}, readFunc func(buf []byte) (int, error)) error func AdaptWrite(oprot interface{}, writeFunc func() []byte) error { var bw bufiox.Writer - fieldNames := []string{"bw", "trans"} - for _, fn := range fieldNames { - writer, exist, err := getUnexportField(oprot, fn) - if err != nil { - return err - } - if exist { - switch w := writer.(type) { - case bufiox.Writer: - bw = w - case io.Writer: - bw = bufiox.NewDefaultWriter(w) - default: - return fmt.Errorf("writer type not ok") + if bp, ok := oprot.(bufioxReaderWriter); ok { + bw = bp.GetBufioxWriter() + } else { + fieldNames := []string{"bw", "trans"} + for _, fn := range fieldNames { + writer, exist, err := getUnexportField(oprot, fn) + if err != nil { + return err + } + if exist { + switch w := writer.(type) { + case bufiox.Writer: + bw = w + case io.Writer: + bw = bufiox.NewDefaultWriter(w) + default: + return fmt.Errorf("writer type not ok") + } + break } - break } } if bw == nil {