Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not support unknown-fields for ReadAnyWithDesc() #31

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 66 additions & 65 deletions testdata/baseline_j2t_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,11 @@ func TestHTTP2Thrift_Nesting(t *testing.T) {
req := getSampleHttpRequest(stru2, nestingJSON)
ctx := context.WithValue(context.Background(), conv.CtxKeyHTTPRequest, req)
cv := j2t.NewBinaryConv(conv.Options{
WriteDefaultField: true,
EnableHttpMapping: true,
EnableValueMapping: true,
WriteDefaultField: true,
EnableHttpMapping: true,
EnableValueMapping: true,
TracebackRequredOrRootFields: true,
ReadHttpValueFallback: true,
ReadHttpValueFallback: true,
})
nj := convertI642StringNesting(nestingJSON, true)
out, err := cv.Do(ctx, nesting, []byte(nj))
Expand All @@ -561,12 +561,12 @@ func TestHTTP2Thrift_Nesting_Parallel(t *testing.T) {
// fmt.Printf("%#v", nesting)

cv := j2t.NewBinaryConv(conv.Options{
WriteDefaultField: true,
EnableHttpMapping: true,
EnableValueMapping: true,
WriteDefaultField: true,
EnableHttpMapping: true,
EnableValueMapping: true,
TracebackRequredOrRootFields: true,
ReadHttpValueFallback: true,
OmitHttpMappingErrors: true,
ReadHttpValueFallback: true,
OmitHttpMappingErrors: true,
})
nj := convertI642StringNesting(nestingJSON, true)
println(nj)
Expand Down Expand Up @@ -837,62 +837,62 @@ func BenchmarkHTTP2Thrift_DynamicGo_Raw(b *testing.B) {
})
}

func getKitexHttpRequest(req *descriptor.HTTPRequest) {
header := "你好"
req.Header.Set("String", header)
// exp.String_ = header
// h2 := "abcdefghijklmnopqrstuvwxyz"
// req.Header.Set("string_field", h2)
// exp.SimpleStruct.StringField = h2
// for i := range exp.ListSimple {
// exp.ListSimple[i].StringField = h2
// }
// for k := range exp.MapStringSimple {
// exp.MapStringSimple[k].StringField = h2
// }

c := []int64{-1, 0, math.MaxInt64, math.MinInt64}
cookie := ""
for i, v := range c {
cookie += strconv.Itoa(int(v))
if i != len(c)-1 {
cookie += ","
}
}
req.Cookies["list_i64"] = cookie
// exp.ListI64 = c

// param := math.MaxFloat64
// req.Params.Set("double", strconv.FormatFloat(param, 'f', -1, 64))
// exp.Double = param

q := []int32{-1, 0, math.MaxInt32, math.MinInt32}
query := ""
for i, v := range q {
query += strconv.Itoa(int(v))
if i != len(q)-1 {
query += ","
}
}
req.Query.Set("ListI32", query)
// exp.ListI32 = q
// func getKitexHttpRequest(req *descriptor.HTTPRequest) {
// header := "你好"
// req.Header.Set("String", header)
// // exp.String_ = header
// // h2 := "abcdefghijklmnopqrstuvwxyz"
// // req.Header.Set("string_field", h2)
// // exp.SimpleStruct.StringField = h2
// // for i := range exp.ListSimple {
// // exp.ListSimple[i].StringField = h2
// // }
// // for k := range exp.MapStringSimple {
// // exp.MapStringSimple[k].StringField = h2
// // }

// c := []int64{-1, 0, math.MaxInt64, math.MinInt64}
// cookie := ""
// for i, v := range c {
// cookie += strconv.Itoa(int(v))
// if i != len(c)-1 {
// cookie += ","
// }
// }
// req.Cookies["list_i64"] = cookie
// // exp.ListI64 = c

// // param := math.MaxFloat64
// // req.Params.Set("double", strconv.FormatFloat(param, 'f', -1, 64))
// // exp.Double = param

// q := []int32{-1, 0, math.MaxInt32, math.MinInt32}
// query := ""
// for i, v := range q {
// query += strconv.Itoa(int(v))
// if i != len(q)-1 {
// query += ","
// }
// }
// req.Query.Set("ListI32", query)
// // exp.ListI32 = q

// exp.I32 = 0
// // exp.I32 = 0

var helper = func(sim map[string]interface{}) {
sim["I32Field"] = int32(sim["I32Field"].(int64))
sim["ByteField"] = int8(sim["ByteField"].(int64))
}
for _, v := range req.Body["MapStringSimple"].(map[string]interface{}) {
helper(v.(map[string]interface{}))
}
for _, v := range req.Body["ListSimple"].([]interface{}) {
helper(v.(map[string]interface{}))
}
helper(req.Body["SimpleStruct"].(map[string]interface{}))
req.Body["Byte"] = int8(req.Body["Byte"].(int64))
// var helper = func(sim map[string]interface{}) {
// sim["I32Field"] = int32(sim["I32Field"].(int64))
// sim["ByteField"] = int8(sim["ByteField"].(int64))
// }
// for _, v := range req.Body["MapStringSimple"].(map[string]interface{}) {
// helper(v.(map[string]interface{}))
// }
// for _, v := range req.Body["ListSimple"].([]interface{}) {
// helper(v.(map[string]interface{}))
// }
// helper(req.Body["SimpleStruct"].(map[string]interface{}))
// req.Body["Byte"] = int8(req.Body["Byte"].(int64))

}
// }

func BenchmarkHTTP2Thrift_KitexGeneric(b *testing.B) {
p, err := generic.NewThriftFileProvider(idlPath)
Expand All @@ -904,9 +904,10 @@ func BenchmarkHTTP2Thrift_KitexGeneric(b *testing.B) {

b.Run("small", func(b *testing.B) {
codec := gthrift.NewWriteHTTPRequest(svcDsc)
req := &descriptor.HTTPRequest{
Method: "POST",
Path: "/simple",
req := &descriptor.HTTPRequest{}
req.Request, err = stdh.NewRequest("POST", "/simple", nil)
if err != nil {
b.Fatal(err)
}
jc := sonic.Config{
UseInt64: true,
Expand Down
8 changes: 5 additions & 3 deletions thrift/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,9 +1137,12 @@ func (p *BinaryProtocol) ReadAnyWithDesc(desc *TypeDescriptor, byteAsUint8 bool,
}
next := st.FieldById(id)
if next == nil {
if !disallowUnknonw {
if disallowUnknonw {
return nil, errUnknonwField
}
if err := p.Skip(typ, false); err != nil {
return nil, err
}
continue
}
vv, err := p.ReadAnyWithDesc(next.Type(), byteAsUint8, copyString, disallowUnknonw, useFieldName)
Expand Down Expand Up @@ -1592,7 +1595,6 @@ func (p *BinaryProtocol) WriteAnyWithDesc(desc *TypeDescriptor, val interface{},
return e
}
}
return nil
} else {
vs, ok := val.(map[FieldID]interface{})
if !ok {
Expand Down Expand Up @@ -1627,8 +1629,8 @@ func (p *BinaryProtocol) WriteAnyWithDesc(desc *TypeDescriptor, val interface{},
// return e
// }
// FreeRequiresBitmap(r)
return nil
}
return p.WriteStructEnd()
default:
return errUnsupportedType
}
Expand Down
33 changes: 32 additions & 1 deletion thrift/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package thrift

import (
"context"
"fmt"
"io/ioutil"
"os"
"runtime"
Expand Down Expand Up @@ -59,4 +60,34 @@ func getExampleData() []byte {
panic(err)
}
return out
}
}

func TestBinaryProtocol_ReadAnyWithDesc(t *testing.T) {
p1, err := NewDescritorFromPath(context.Background(), "../testdata/idl/example3.thrift")
if err != nil {
panic(err)
}
exp3partial := p1.Functions()["PartialMethod"].Response().Struct().FieldById(0).Type()
data, err := ioutil.ReadFile("../testdata/data/example3.bin")
if err != nil {
panic(err)
}

p := NewBinaryProtocol(data)
v, err := p.ReadAnyWithDesc(exp3partial, false, false, false, true)
if err != nil {
panic(err)
}
fmt.Printf("%#v", v)
p = NewBinaryProtocolBuffer()
err = p.WriteAnyWithDesc(exp3partial, v, true, true, true)
if err != nil {
panic(err)
}
fmt.Printf("%x", p.RawBuf())
v, err = p.ReadAnyWithDesc(exp3partial, false, false, false, true)
if err != nil {
panic(err)
}
fmt.Printf("%#v", v)
}
Loading