From cd1dbe0e1f7349055173995eba59ccd266ab65e1 Mon Sep 17 00:00:00 2001 From: Marina-Sakai Date: Wed, 3 Jul 2024 13:57:32 +0800 Subject: [PATCH] feat: support streaming key --- conv/api.go | 2 ++ conv/t2j/impl.go | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/conv/api.go b/conv/api.go index 4a5d3725..7cbd3461 100644 --- a/conv/api.go +++ b/conv/api.go @@ -36,6 +36,8 @@ var ( CtxKeyThriftReqBase = &ContextKey{} // CtxKeyConvOptions is the key for Options in context CtxKeyConvOptions = &ContextKey{} + // CtxKeyStreaming is the key for streaming in context + CtxKeyStreaming = &ContextKey{} ) var ( diff --git a/conv/t2j/impl.go b/conv/t2j/impl.go index 6e51a018..7ca72810 100644 --- a/conv/t2j/impl.go +++ b/conv/t2j/impl.go @@ -73,7 +73,7 @@ func (self *BinaryConv) readResponseBase(ctx context.Context, p *thrift.BinaryPr func (self *BinaryConv) do(ctx context.Context, src []byte, desc *thrift.TypeDescriptor, out *[]byte, resp http.ResponseSetter) (err error) { //NOTICE: output buffer must be larger than src buffer rt.GuardSlice(out, len(src)*_GUARD_SLICE_FACTOR) - + var p = thrift.BinaryProtocol{ Buf: src, } @@ -151,7 +151,8 @@ func (self *BinaryConv) do(ctx context.Context, src []byte, desc *thrift.TypeDes *out = json.EncodeObjectColon(*out) // handle a thrift exception field. the id of a thrift exception field is non-zero - if self.opts.ConvertException && id != 0 { + // streaming doesn't support exception + if ctx.Value(conv.CtxKeyStreaming) == nil && self.opts.ConvertException && id != 0 { existExceptionField = true // reset out to get only exception field data *out = (*out)[:0] @@ -395,7 +396,7 @@ func (self *BinaryConv) handleUnsets(b *thrift.RequiresBitmap, desc *thrift.Stru var ok = false if hms := field.HTTPMappings(); self.opts.EnableHttpMapping && hms != nil { // make a default thrift value - p := thrift.BinaryProtocol{Buf: make([]byte, 0, conv.DefaulHttpValueBufferSizeForJSON)}; + p := thrift.BinaryProtocol{Buf: make([]byte, 0, conv.DefaulHttpValueBufferSizeForJSON)} if err := p.WriteDefaultOrEmpty(field); err != nil { return wrapError(meta.ErrWrite, fmt.Sprintf("encoding field '%s' default value failed", field.Name()), err) } @@ -517,10 +518,10 @@ func (self *BinaryConv) writeHttpValue(ctx context.Context, resp http.ResponseSe var thriftVal []byte var jsonVal []byte var textVal []byte - + for _, hm := range field.HTTPMappings() { var val []byte - enc := hm.Encoding(); + enc := hm.Encoding() if enc == meta.EncodingThriftBinary { // raw encoding, check if raw value is set @@ -543,10 +544,10 @@ func (self *BinaryConv) writeHttpValue(ctx context.Context, resp http.ResponseSe return false, unwrapError(fmt.Sprintf("reading thrift value of '%s' failed, thrift pos:%d", field.Name(), p.Read), err) } val = tmp - textVal = val + textVal = val } else { val = textVal - } + } } else if self.opts.UseKitexHttpEncoding { // kitex http encoding fallback if textVal == nil { @@ -558,7 +559,7 @@ func (self *BinaryConv) writeHttpValue(ctx context.Context, resp http.ResponseSe val = textVal } else { val = textVal - } + } } else if enc == meta.EncodingJSON { // for nested type, convert it to a new JSON string if jsonVal == nil { @@ -572,7 +573,7 @@ func (self *BinaryConv) writeHttpValue(ctx context.Context, resp http.ResponseSe } else { val = jsonVal } - + } else { return false, wrapError(meta.ErrConvert, fmt.Sprintf("unsuported http-value encoding %v of field '%s'", enc, field.Name()), nil) }