Skip to content

Commit b3c7613

Browse files
authored
fix: not support unknown-fields for ReadAnyWithDesc() (#31)
* fix: not support unknown-fields for `ReadAnyWithDesc()` * fix CI
1 parent 1942a99 commit b3c7613

File tree

3 files changed

+103
-69
lines changed

3 files changed

+103
-69
lines changed

testdata/baseline_j2t_test.go

Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -537,11 +537,11 @@ func TestHTTP2Thrift_Nesting(t *testing.T) {
537537
req := getSampleHttpRequest(stru2, nestingJSON)
538538
ctx := context.WithValue(context.Background(), conv.CtxKeyHTTPRequest, req)
539539
cv := j2t.NewBinaryConv(conv.Options{
540-
WriteDefaultField: true,
541-
EnableHttpMapping: true,
542-
EnableValueMapping: true,
540+
WriteDefaultField: true,
541+
EnableHttpMapping: true,
542+
EnableValueMapping: true,
543543
TracebackRequredOrRootFields: true,
544-
ReadHttpValueFallback: true,
544+
ReadHttpValueFallback: true,
545545
})
546546
nj := convertI642StringNesting(nestingJSON, true)
547547
out, err := cv.Do(ctx, nesting, []byte(nj))
@@ -561,12 +561,12 @@ func TestHTTP2Thrift_Nesting_Parallel(t *testing.T) {
561561
// fmt.Printf("%#v", nesting)
562562

563563
cv := j2t.NewBinaryConv(conv.Options{
564-
WriteDefaultField: true,
565-
EnableHttpMapping: true,
566-
EnableValueMapping: true,
564+
WriteDefaultField: true,
565+
EnableHttpMapping: true,
566+
EnableValueMapping: true,
567567
TracebackRequredOrRootFields: true,
568-
ReadHttpValueFallback: true,
569-
OmitHttpMappingErrors: true,
568+
ReadHttpValueFallback: true,
569+
OmitHttpMappingErrors: true,
570570
})
571571
nj := convertI642StringNesting(nestingJSON, true)
572572
println(nj)
@@ -837,62 +837,62 @@ func BenchmarkHTTP2Thrift_DynamicGo_Raw(b *testing.B) {
837837
})
838838
}
839839

840-
func getKitexHttpRequest(req *descriptor.HTTPRequest) {
841-
header := "你好"
842-
req.Header.Set("String", header)
843-
// exp.String_ = header
844-
// h2 := "abcdefghijklmnopqrstuvwxyz"
845-
// req.Header.Set("string_field", h2)
846-
// exp.SimpleStruct.StringField = h2
847-
// for i := range exp.ListSimple {
848-
// exp.ListSimple[i].StringField = h2
849-
// }
850-
// for k := range exp.MapStringSimple {
851-
// exp.MapStringSimple[k].StringField = h2
852-
// }
853-
854-
c := []int64{-1, 0, math.MaxInt64, math.MinInt64}
855-
cookie := ""
856-
for i, v := range c {
857-
cookie += strconv.Itoa(int(v))
858-
if i != len(c)-1 {
859-
cookie += ","
860-
}
861-
}
862-
req.Cookies["list_i64"] = cookie
863-
// exp.ListI64 = c
864-
865-
// param := math.MaxFloat64
866-
// req.Params.Set("double", strconv.FormatFloat(param, 'f', -1, 64))
867-
// exp.Double = param
868-
869-
q := []int32{-1, 0, math.MaxInt32, math.MinInt32}
870-
query := ""
871-
for i, v := range q {
872-
query += strconv.Itoa(int(v))
873-
if i != len(q)-1 {
874-
query += ","
875-
}
876-
}
877-
req.Query.Set("ListI32", query)
878-
// exp.ListI32 = q
840+
// func getKitexHttpRequest(req *descriptor.HTTPRequest) {
841+
// header := "你好"
842+
// req.Header.Set("String", header)
843+
// // exp.String_ = header
844+
// // h2 := "abcdefghijklmnopqrstuvwxyz"
845+
// // req.Header.Set("string_field", h2)
846+
// // exp.SimpleStruct.StringField = h2
847+
// // for i := range exp.ListSimple {
848+
// // exp.ListSimple[i].StringField = h2
849+
// // }
850+
// // for k := range exp.MapStringSimple {
851+
// // exp.MapStringSimple[k].StringField = h2
852+
// // }
853+
854+
// c := []int64{-1, 0, math.MaxInt64, math.MinInt64}
855+
// cookie := ""
856+
// for i, v := range c {
857+
// cookie += strconv.Itoa(int(v))
858+
// if i != len(c)-1 {
859+
// cookie += ","
860+
// }
861+
// }
862+
// req.Cookies["list_i64"] = cookie
863+
// // exp.ListI64 = c
864+
865+
// // param := math.MaxFloat64
866+
// // req.Params.Set("double", strconv.FormatFloat(param, 'f', -1, 64))
867+
// // exp.Double = param
868+
869+
// q := []int32{-1, 0, math.MaxInt32, math.MinInt32}
870+
// query := ""
871+
// for i, v := range q {
872+
// query += strconv.Itoa(int(v))
873+
// if i != len(q)-1 {
874+
// query += ","
875+
// }
876+
// }
877+
// req.Query.Set("ListI32", query)
878+
// // exp.ListI32 = q
879879

880-
// exp.I32 = 0
880+
// // exp.I32 = 0
881881

882-
var helper = func(sim map[string]interface{}) {
883-
sim["I32Field"] = int32(sim["I32Field"].(int64))
884-
sim["ByteField"] = int8(sim["ByteField"].(int64))
885-
}
886-
for _, v := range req.Body["MapStringSimple"].(map[string]interface{}) {
887-
helper(v.(map[string]interface{}))
888-
}
889-
for _, v := range req.Body["ListSimple"].([]interface{}) {
890-
helper(v.(map[string]interface{}))
891-
}
892-
helper(req.Body["SimpleStruct"].(map[string]interface{}))
893-
req.Body["Byte"] = int8(req.Body["Byte"].(int64))
882+
// var helper = func(sim map[string]interface{}) {
883+
// sim["I32Field"] = int32(sim["I32Field"].(int64))
884+
// sim["ByteField"] = int8(sim["ByteField"].(int64))
885+
// }
886+
// for _, v := range req.Body["MapStringSimple"].(map[string]interface{}) {
887+
// helper(v.(map[string]interface{}))
888+
// }
889+
// for _, v := range req.Body["ListSimple"].([]interface{}) {
890+
// helper(v.(map[string]interface{}))
891+
// }
892+
// helper(req.Body["SimpleStruct"].(map[string]interface{}))
893+
// req.Body["Byte"] = int8(req.Body["Byte"].(int64))
894894

895-
}
895+
// }
896896

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

905905
b.Run("small", func(b *testing.B) {
906906
codec := gthrift.NewWriteHTTPRequest(svcDsc)
907-
req := &descriptor.HTTPRequest{
908-
Method: "POST",
909-
Path: "/simple",
907+
req := &descriptor.HTTPRequest{}
908+
req.Request, err = stdh.NewRequest("POST", "/simple", nil)
909+
if err != nil {
910+
b.Fatal(err)
910911
}
911912
jc := sonic.Config{
912913
UseInt64: true,

thrift/binary.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,12 @@ func (p *BinaryProtocol) ReadAnyWithDesc(desc *TypeDescriptor, byteAsUint8 bool,
11371137
}
11381138
next := st.FieldById(id)
11391139
if next == nil {
1140-
if !disallowUnknonw {
1140+
if disallowUnknonw {
11411141
return nil, errUnknonwField
11421142
}
1143+
if err := p.Skip(typ, false); err != nil {
1144+
return nil, err
1145+
}
11431146
continue
11441147
}
11451148
vv, err := p.ReadAnyWithDesc(next.Type(), byteAsUint8, copyString, disallowUnknonw, useFieldName)
@@ -1592,7 +1595,6 @@ func (p *BinaryProtocol) WriteAnyWithDesc(desc *TypeDescriptor, val interface{},
15921595
return e
15931596
}
15941597
}
1595-
return nil
15961598
} else {
15971599
vs, ok := val.(map[FieldID]interface{})
15981600
if !ok {
@@ -1627,8 +1629,8 @@ func (p *BinaryProtocol) WriteAnyWithDesc(desc *TypeDescriptor, val interface{},
16271629
// return e
16281630
// }
16291631
// FreeRequiresBitmap(r)
1630-
return nil
16311632
}
1633+
return p.WriteStructEnd()
16321634
default:
16331635
return errUnsupportedType
16341636
}

thrift/binary_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package thrift
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"io/ioutil"
2223
"os"
2324
"runtime"
@@ -59,4 +60,34 @@ func getExampleData() []byte {
5960
panic(err)
6061
}
6162
return out
62-
}
63+
}
64+
65+
func TestBinaryProtocol_ReadAnyWithDesc(t *testing.T) {
66+
p1, err := NewDescritorFromPath(context.Background(), "../testdata/idl/example3.thrift")
67+
if err != nil {
68+
panic(err)
69+
}
70+
exp3partial := p1.Functions()["PartialMethod"].Response().Struct().FieldById(0).Type()
71+
data, err := ioutil.ReadFile("../testdata/data/example3.bin")
72+
if err != nil {
73+
panic(err)
74+
}
75+
76+
p := NewBinaryProtocol(data)
77+
v, err := p.ReadAnyWithDesc(exp3partial, false, false, false, true)
78+
if err != nil {
79+
panic(err)
80+
}
81+
fmt.Printf("%#v", v)
82+
p = NewBinaryProtocolBuffer()
83+
err = p.WriteAnyWithDesc(exp3partial, v, true, true, true)
84+
if err != nil {
85+
panic(err)
86+
}
87+
fmt.Printf("%x", p.RawBuf())
88+
v, err = p.ReadAnyWithDesc(exp3partial, false, false, false, true)
89+
if err != nil {
90+
panic(err)
91+
}
92+
fmt.Printf("%#v", v)
93+
}

0 commit comments

Comments
 (0)