From b3c7613901a43b8f96edabc4d8dfb6a451c1db12 Mon Sep 17 00:00:00 2001 From: Yi Duan Date: Mon, 21 Aug 2023 12:33:35 +0800 Subject: [PATCH] fix: not support unknown-fields for `ReadAnyWithDesc()` (#31) * fix: not support unknown-fields for `ReadAnyWithDesc()` * fix CI --- testdata/baseline_j2t_test.go | 131 +++++++++++++++++----------------- thrift/binary.go | 8 ++- thrift/binary_test.go | 33 ++++++++- 3 files changed, 103 insertions(+), 69 deletions(-) diff --git a/testdata/baseline_j2t_test.go b/testdata/baseline_j2t_test.go index 1c541ae7..676dfef2 100644 --- a/testdata/baseline_j2t_test.go +++ b/testdata/baseline_j2t_test.go @@ -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)) @@ -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) @@ -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) @@ -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, diff --git a/thrift/binary.go b/thrift/binary.go index 9eb6e24b..27f920d3 100644 --- a/thrift/binary.go +++ b/thrift/binary.go @@ -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) @@ -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 { @@ -1627,8 +1629,8 @@ func (p *BinaryProtocol) WriteAnyWithDesc(desc *TypeDescriptor, val interface{}, // return e // } // FreeRequiresBitmap(r) - return nil } + return p.WriteStructEnd() default: return errUnsupportedType } diff --git a/thrift/binary_test.go b/thrift/binary_test.go index 9fe61bb4..e54a0431 100644 --- a/thrift/binary_test.go +++ b/thrift/binary_test.go @@ -18,6 +18,7 @@ package thrift import ( "context" + "fmt" "io/ioutil" "os" "runtime" @@ -59,4 +60,34 @@ func getExampleData() []byte { panic(err) } return out -} \ No newline at end of file +} + +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) +}