Skip to content

Commit

Permalink
fix: revert base
Browse files Browse the repository at this point in the history
  • Loading branch information
Marina-Sakai committed Jul 30, 2024
1 parent 7f139dd commit de4d0a0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 42 deletions.
32 changes: 15 additions & 17 deletions conv/j2t/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"time"
"unsafe"

"github.com/cloudwego/gopkg/protocol/thrift/base"

sjson "github.com/bytedance/sonic/ast"
"github.com/cloudwego/dynamicgo/conv"
"github.com/cloudwego/dynamicgo/http"
Expand All @@ -47,7 +49,6 @@ import (
"github.com/cloudwego/dynamicgo/testdata/sample"
"github.com/cloudwego/dynamicgo/thrift"
"github.com/cloudwego/dynamicgo/thrift/annotation"
"github.com/cloudwego/dynamicgo/thrift/base"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -80,19 +81,18 @@ const (
)

func TestCases(t *testing.T) {
var tests = []struct{
name string
idl string
var tests = []struct {
name string
idl string
includes map[string]string
js string
opt conv.Options
want interface{}
err error
js string
opt conv.Options
want interface{}
err error
}{
{
name: "int2double_vm",
idl:
`struct Req {
idl: `struct Req {
1: optional double body (api.js_conv=""),
}
Expand All @@ -101,16 +101,15 @@ service SVR {
}
`,
includes: nil,
js: `{"body":"-1"}`,
opt: conv.Options{},
js: `{"body":"-1"}`,
opt: conv.Options{},
want: map[string]interface{}{
"body": float64(-1),
},
},
{
name: "int2double",
idl:
`struct Req {
idl: `struct Req {
1: optional double body,
}
Expand All @@ -119,8 +118,8 @@ service SVR {
}
`,
includes: nil,
js: `{"body":-2}`,
opt: conv.Options{EnableValueMapping: true},
js: `{"body":-2}`,
opt: conv.Options{EnableValueMapping: true},
want: map[string]interface{}{
"body": float64(-2),
},
Expand Down Expand Up @@ -154,7 +153,6 @@ service SVR {
}
}


func TestConvJSON2Thrift(t *testing.T) {
desc := getExampleDesc()
data := getExampleData()
Expand Down
11 changes: 6 additions & 5 deletions conv/j2t/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"runtime"
"unsafe"

"github.com/cloudwego/gopkg/protocol/thrift/base"

"github.com/cloudwego/dynamicgo/conv"
"github.com/cloudwego/dynamicgo/http"
"github.com/cloudwego/dynamicgo/internal/json"
Expand All @@ -30,7 +32,6 @@ import (
"github.com/cloudwego/dynamicgo/internal/rt"
"github.com/cloudwego/dynamicgo/meta"
"github.com/cloudwego/dynamicgo/thrift"
"github.com/cloudwego/dynamicgo/thrift/base"
)

const (
Expand Down Expand Up @@ -118,8 +119,8 @@ func isJsonString(val string) bool {
if len(val) < 2 {
return false
}
c := json.SkipBlank(val, 0)

c := json.SkipBlank(val, 0)
if c < 0 {
return false
}
Expand Down Expand Up @@ -164,7 +165,7 @@ func (self *BinaryConv) writeStringValue(ctx context.Context, buf *[]byte, f *th
if err := self.doNative(ctx, rt.Str2Mem(val), f.Type(), &p.Buf, req, false); err != nil {
return newError(meta.ErrConvert, fmt.Sprintf("failed to convert value of field '%s'", f.Name()), err)
}
// try text encoding, see thrift.EncodeText
// try text encoding, see thrift.EncodeText
} else {
return newError(meta.ErrConvert, fmt.Sprintf("unsupported http-mapping encoding %v for '%s'", enc, f.Name()), nil)
}
Expand All @@ -174,7 +175,7 @@ BACK:
return nil
}

func (self *BinaryConv)writeRequestBaseToThrift(ctx context.Context, buf *[]byte, field *thrift.FieldDescriptor) error {
func (self *BinaryConv) writeRequestBaseToThrift(ctx context.Context, buf *[]byte, field *thrift.FieldDescriptor) error {
var b *base.Base
if bobj := ctx.Value(conv.CtxKeyThriftReqBase); bobj != nil {
if v, ok := bobj.(*base.Base); ok && v != nil {
Expand Down
9 changes: 5 additions & 4 deletions conv/t2j/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"time"
"unsafe"

"github.com/cloudwego/gopkg/protocol/thrift/base"

sjson "github.com/bytedance/sonic/ast"
"github.com/cloudwego/dynamicgo/conv"
"github.com/cloudwego/dynamicgo/http"
Expand All @@ -40,7 +42,6 @@ import (
"github.com/cloudwego/dynamicgo/testdata/kitex_gen/example3"
"github.com/cloudwego/dynamicgo/thrift"
"github.com/cloudwego/dynamicgo/thrift/annotation"
"github.com/cloudwego/dynamicgo/thrift/base"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -687,7 +688,7 @@ func TestSimpleArgs(t *testing.T) {

func TestConvThrift2HTTP_KitexApiHeader(t *testing.T) {
// annotation.RegisterHttpMaping(annotation.APIHeader, annotation.HttpMapingHandler{Req:annotation.ApiHeaderRequest, Resp:annotation.ApiheaderKitexResponse, Enc:annotation.ApiHeaderKitexEncoding})

desc := thrift.FnResponse(thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "JSONStringMethod", thrift.Options{}))
exp := example3.NewExampleJSONString()
eobj := &example3.JSONObject{
Expand All @@ -705,7 +706,7 @@ func TestConvThrift2HTTP_KitexApiHeader(t *testing.T) {
EnableHttpMapping: true,
WriteHttpValueFallback: true,
OmitHttpMappingErrors: true,
UseKitexHttpEncoding: true,
UseKitexHttpEncoding: true,
})
ctx := context.Background()
resp := http.NewHTTPResponse()
Expand All @@ -721,4 +722,4 @@ func TestConvThrift2HTTP_KitexApiHeader(t *testing.T) {
require.Equal(t, "1,2", resp.Cookies()[1].Value)

// annotation.RegisterHttpMaping(annotation.APIHeader, annotation.HttpMapingHandler{Req:annotation.ApiHeaderRequest, Resp: annotation.ApiHeaderResponse, Enc:annotation.ApiHeaderEncoding})
}
}
19 changes: 10 additions & 9 deletions conv/t2j/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
"errors"
"fmt"

"github.com/cloudwego/gopkg/protocol/thrift/base"

"github.com/cloudwego/dynamicgo/conv"
"github.com/cloudwego/dynamicgo/http"
"github.com/cloudwego/dynamicgo/internal/json"
"github.com/cloudwego/dynamicgo/internal/primitive"
"github.com/cloudwego/dynamicgo/internal/rt"
"github.com/cloudwego/dynamicgo/meta"
"github.com/cloudwego/dynamicgo/thrift"
"github.com/cloudwego/dynamicgo/thrift/base"
)

const (
Expand Down Expand Up @@ -73,7 +74,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 @@ -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
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ require (
github.com/bytedance/sonic/loader v0.1.1
github.com/cloudwego/base64x v0.1.4
github.com/cloudwego/fastpb v0.0.4
github.com/cloudwego/gopkg v0.0.0-20240725095015-34d5327eebca
github.com/cloudwego/thriftgo v0.3.6
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/fatih/structtag v1.2.0
github.com/iancoleman/strcase v0.2.0
github.com/jhump/protoreflect v1.8.2
github.com/klauspost/cpuid/v2 v2.2.4
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.9.0
google.golang.org/protobuf v1.28.1
)

require (
github.com/bytedance/gopkg v0.0.0-20240711085056-a03554c296f8 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit de4d0a0

Please sign in to comment.