Skip to content

Commit

Permalink
feat: support streaming key
Browse files Browse the repository at this point in the history
  • Loading branch information
Marina-Sakai committed Jul 3, 2024
1 parent 2d344ea commit cd1dbe0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 2 additions & 0 deletions conv/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
19 changes: 10 additions & 9 deletions conv/t2j/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit cd1dbe0

Please sign in to comment.