From 1e6bfcabfa6ece7a21a102df741c844738f0a963 Mon Sep 17 00:00:00 2001 From: Yi Duan Date: Wed, 31 Jul 2024 11:10:13 +0800 Subject: [PATCH] feat: support go1.23 (#66) * chore: support go1.23 * update sonic * opt: recover to print more context * opt: make more panic msg * update mod * update ci * add go1.23 ci * fix: checkptr * remove all API docs onto go.dev * update --- .github/workflows/go_latest_test.yml | 48 + ...linux-compat.yml => push-check-compat.yml} | 4 +- README.md | 3 + conv/README.md | 113 - conv/j2p/README.md | 122 - conv/j2t/README.md | 191 -- conv/j2t/conv_test.go | 81 +- conv/j2t/error.go | 42 +- conv/j2t/impl.go | 15 +- conv/p2j/README.md | 139 -- conv/t2j/README.md | 178 -- go.mod | 5 +- go.sum | 19 +- http/README.md | 352 --- http/http.go | 8 + internal/json/api_compat.go | 18 +- internal/native/types/types.go | 51 +- internal/rt/table.go | 118 + proto/README.md | 893 ------- scripts/go_latest.sh | 11 + testdata/test/go.mod | 4 +- testdata/test/go.sum | 7 +- thrift/README.md | 2092 ----------------- thrift/generic/README.md | 1217 ---------- 24 files changed, 380 insertions(+), 5351 deletions(-) create mode 100644 .github/workflows/go_latest_test.yml rename .github/workflows/{push-check-linux-compat.yml => push-check-compat.yml} (94%) delete mode 100644 conv/README.md delete mode 100644 conv/j2p/README.md delete mode 100644 conv/j2t/README.md delete mode 100644 conv/p2j/README.md delete mode 100644 conv/t2j/README.md delete mode 100644 http/README.md create mode 100644 internal/rt/table.go delete mode 100644 proto/README.md create mode 100755 scripts/go_latest.sh delete mode 100644 thrift/README.md delete mode 100644 thrift/generic/README.md diff --git a/.github/workflows/go_latest_test.yml b/.github/workflows/go_latest_test.yml new file mode 100644 index 00000000..f111224a --- /dev/null +++ b/.github/workflows/go_latest_test.yml @@ -0,0 +1,48 @@ +name: Go Latest + +on: push + +jobs: + build: + strategy: + matrix: + os: [arm, X64] + runs-on: ${{ matrix.os }} + steps: + - name: Clear repository + run: sudo rm -fr $GITHUB_WORKSPACE && mkdir $GITHUB_WORKSPACE + + - uses: actions/checkout@v2 + + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.22.4 + + - uses: actions/cache@v2 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: build go + run: sh ./scripts/go_latest.sh go1.23 + continue-on-error: true + + - name: test + run: | + PWD=$(pwd) + export GOROOT=$PWD/go1.23 + export PATH=$GOROOT/bin:$PATH + go version + go test -race github.com/cloudwego/dynamicgo/thrift + go test -race github.com/cloudwego/dynamicgo/thrift/annotation + go test -race github.com/cloudwego/dynamicgo/thrift/generic + go test -race github.com/cloudwego/dynamicgo/conv/t2j + go test -race github.com/cloudwego/dynamicgo/http + go test -race github.com/cloudwego/dynamicgo/internal/json + go test -race github.com/cloudwego/dynamicgo/conv/j2p + go test -race github.com/cloudwego/dynamicgo/conv/p2j + go test -race github.com/cloudwego/dynamicgo/proto/generic + diff --git a/.github/workflows/push-check-linux-compat.yml b/.github/workflows/push-check-compat.yml similarity index 94% rename from .github/workflows/push-check-linux-compat.yml rename to .github/workflows/push-check-compat.yml index 68ca572b..8694476d 100644 --- a/.github/workflows/push-check-linux-compat.yml +++ b/.github/workflows/push-check-compat.yml @@ -6,14 +6,14 @@ jobs: build: strategy: matrix: - go-version: [1.17.x, 1.20.x] + go-version: [1.17.x, 1.22.x] os: [ARM64, X64] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: go-version: ${{ matrix.go-version }} diff --git a/README.md b/README.md index 25e7d449..5e0c48f9 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,9 @@ Dynamic-Go for Thrift protocol: [introduction.md](introduction.md). Dynamic-Go for Protobuf protocol: [introduction.md](./proto/INTRODUCTION.md) ## Usage + +[![GoDoc](https://godoc.org/github.com/cloudwego/dynamicgo?status.svg)](https://pkg.go.dev/github.com/cloudwego/dynamicgo?tab=doc) + ### thrift Thrift IDL parser and message operators. It can parse thrift IDL in runtime and handle thrift data in generic way. [DOC](thrift/README.md) diff --git a/conv/README.md b/conv/README.md deleted file mode 100644 index 53cd0e94..00000000 --- a/conv/README.md +++ /dev/null @@ -1,113 +0,0 @@ - - -# conv - -```go -import "github.com/cloudwego/dynamicgo/conv" -``` - -## Index - -- [Variables](<#variables>) -- [func FreeBytes(b *[]byte)](<#func-freebytes>) -- [func NewBytes() *[]byte](<#func-newbytes>) -- [type ContextKey](<#type-contextkey>) -- [type Options](<#type-options>) - - -## Variables - -```go -var ( - // CtxKeyHTTPResponse is the key for http.ResponseSetter in context - CtxKeyHTTPResponse = &ContextKey{} - // CtxKeyHTTPRequest is the key for http.RequestGetter in context - CtxKeyHTTPRequest = &ContextKey{} - // CtxKeyThriftRespBase is the key for base.Base in context - CtxKeyThriftRespBase = &ContextKey{} - // CtxKeyThriftReqBase is the key for base.BaseResp in context - CtxKeyThriftReqBase = &ContextKey{} - // CtxKeyConvOptions is the key for Options in context - CtxKeyConvOptions = &ContextKey{} -) -``` - -```go -var ( - // DefaultBufferSize is the default buffer size for conversion - DefaultBufferSize = 4096 - // DefaultHttpValueBufferSize is the default buffer size for copying a json value - DefaulHttpValueBufferSizeForJSON = 1024 - // DefaultHttpValueBufferSize is the default buffer size for copying a http value - DefaulHttpValueBufferSizeForScalar = 64 -) -``` - -## func FreeBytes - -```go -func FreeBytes(b *[]byte) -``` - -FreeBytes returns a byte slice to pool and reset it - -## func NewBytes - -```go -func NewBytes() *[]byte -``` - -NewBytes returns a new byte slice from pool - -## type ContextKey - -ContextKey is the key type for context arguments - -```go -type ContextKey struct { - // contains filtered or unexported fields -} -``` - -## type Options - -```go -type Options struct { - // DisallowUnknownField indicates if unknown fields should be skipped - DisallowUnknownField bool - // WriteDefaultField indicates if default-requireness fields should be written if - WriteDefaultField bool - // EnableValueMapping indicates if value mapping (api.js_conv...) should be enabled - EnableValueMapping bool - // EnableHttpMapping indicates if http mapping (api.query|api.header...) should be enabled - EnableHttpMapping bool - // HttpMappingAsExtra indicates if continuing convert the same field after http mapping - HttpMappingAsExtra bool - // EnableThriftBase indicates if thrift/base should be recoginized and mapping to/from context - EnableThriftBase bool - // UseNativeSkip indicates if use thrift.SkipNative() or thrift.SkipGo() - UseNativeSkip bool - // Int64AsString indicates if string value cane be read as **Int8/Int16/Int32/Int64/Float64**, - // or in response a **Int64** value can be written as string - String2Int64 bool - // WriteRequireField indicates if required-requireness fields should be written empty value if - // not found - WriteRequireField bool - // NoBase64Binary indicates if base64 string shoud be Encode/Decode as []byte - NoBase64Binary bool - // ByteAsUint8 indicates if byte should be conv as uint8 (default is int8), this only works for t2j now - ByteAsUint8 bool - // WriteOptionalField indicates if optional-requireness fields should be written when not given - WriteOptionalField bool - // TracebackRequredOrRootFields indicates if required-requireness - // or root-level fields should be seeking on http-values when reading failed from current layer of json. - // this option is only used in j2t now. - TracebackRequredOrRootFields bool - // NoCopyString indicates if string should be copied or just referenced (if possible) - NoCopyString bool -} -``` - - - -Generated by [gomarkdoc]() diff --git a/conv/j2p/README.md b/conv/j2p/README.md deleted file mode 100644 index d215a598..00000000 --- a/conv/j2p/README.md +++ /dev/null @@ -1,122 +0,0 @@ - - -# j2p - -```go -import "github.com/cloudwego/dynamicgo/conv/j2p" -``` - -## Index - -- [type BinaryConv](<#BinaryConv>) - - [func NewBinaryConv(opts conv.Options) BinaryConv](<#NewBinaryConv>) - - [func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte) (tbytes []byte, err error)](<#BinaryConv.Do>) - - [func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte, buf *[]byte) error](<#BinaryConv.DoInto>) - - - -## type [BinaryConv]() - -BinaryConv is a converter from json to protobuf binary - -```go -type BinaryConv struct { - // contains filtered or unexported fields -} -``` - - -### func [NewBinaryConv]() - -```go -func NewBinaryConv(opts conv.Options) BinaryConv -``` - -NewBinaryConv returns a new BinaryConv - - -### func (*BinaryConv) [Do]() - -```go -func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte) (tbytes []byte, err error) -``` - -Do converts json bytes (jbytes) to protobuf binary (tbytes) desc is the protobuf type descriptor of the protobuf binary, usually it the request Message type - -
Example -

- - - -```go -package main - -import ( - "context" - "encoding/json" - "reflect" - - "github.com/cloudwego/dynamicgo/conv" - "github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/example2" - "google.golang.org/protobuf/encoding/protowire" -) - -var opts = conv.Options{} - -func main() { - // get descriptor and data - desc := getExampleDesc() - data := getExampleData() - - // make BinaryConv - cv := NewBinaryConv(opts) - - // do conversion - out, err := cv.Do(context.Background(), desc, data) - if err != nil { - panic(err) - } - - // validate result - exp := &example2.ExampleReq{} - err = json.Unmarshal(data, exp) - if err != nil { - panic(err) - } - act := &example2.ExampleReq{} - l := 0 - dataLen := len(out) - // fastRead to get target struct - for l < dataLen { - id, wtyp, tagLen := protowire.ConsumeTag(out) - if tagLen < 0 { - panic("parseTag failed") - } - l += tagLen - out = out[tagLen:] - offset, err := act.FastRead(out, int8(wtyp), int32(id)) - if err != nil { - panic(err) - } - out = out[offset:] - l += offset - } - if !reflect.DeepEqual(exp, act) { - panic("not equal") - } -} -``` - -

-
- - -### func (*BinaryConv) [DoInto]() - -```go -func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, jbytes []byte, buf *[]byte) error -``` - -DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer - -Generated by [gomarkdoc]() diff --git a/conv/j2t/README.md b/conv/j2t/README.md deleted file mode 100644 index 551fe19a..00000000 --- a/conv/j2t/README.md +++ /dev/null @@ -1,191 +0,0 @@ - - -# j2t - -```go -import "github.com/cloudwego/dynamicgo/conv/j2t" -``` - -## Index - -- [type BinaryConv](<#type-binaryconv>) - - [func NewBinaryConv(opts conv.Options) BinaryConv](<#func-newbinaryconv>) - - [func (self *BinaryConv) Do(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte) (tbytes []byte, err error)](<#func-binaryconv-do>) - - [func (self *BinaryConv) DoInto(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte, buf *[]byte) (err error)](<#func-binaryconv-dointo>) - - [func (self *BinaryConv) SetOptions(opts conv.Options)](<#func-binaryconv-setoptions>) -- [type HTTPConv](<#type-httpconv>) - - [func NewHTTPConv(proto meta.Encoding, fnDesc *thrift.FunctionDescriptor) *HTTPConv](<#func-newhttpconv>) - - [func (h HTTPConv) Do(ctx context.Context, req http.RequestGetter, opt conv.Options) (tbytes []byte, err error)](<#func-httpconv-do>) - - [func (h HTTPConv) DoInto(ctx context.Context, req http.RequestGetter, buf *[]byte, opt conv.Options) (err error)](<#func-httpconv-dointo>) - - -## type BinaryConv - -BinaryConv is a converter from json to thrift binary - -```go -type BinaryConv struct { - // contains filtered or unexported fields -} -``` - -### func NewBinaryConv - -```go -func NewBinaryConv(opts conv.Options) BinaryConv -``` - -NewBinaryConv returns a new BinaryConv - -### func \(\*BinaryConv\) Do - -```go -func (self *BinaryConv) Do(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte) (tbytes []byte, err error) -``` - -#### Do converts json bytes \(jbytes\) to thrift binary \(tbytes\) - -desc is the thrift type descriptor of the thrift binary, usually it the request STRUCT type ctx is the context, which can be used to pass arguments as below: \- conv.CtxKeyHTTPRequest: http.RequestGetter as http request \- conv.CtxKeyThriftRespBase: thrift.Base as base metadata of thrift response - -
Example -

- -```go -{ - - desc := getExampleDesc() - data := getExampleData() - - cv := NewBinaryConv(opts) - - out, err := cv.Do(context.Background(), desc, data) - if err != nil { - panic(err) - } - - exp := example3.NewExampleReq() - err = json.Unmarshal(data, exp) - if err != nil { - panic(err) - } - act := example3.NewExampleReq() - _, err = act.FastRead(out) - if err != nil { - panic(err) - } - if !reflect.DeepEqual(exp, act) { - panic("not equal") - } -} -``` - -

-
- -### func \(\*BinaryConv\) DoInto - -```go -func (self *BinaryConv) DoInto(ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte, buf *[]byte) (err error) -``` - -DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer - -### func \(\*BinaryConv\) SetOptions - -```go -func (self *BinaryConv) SetOptions(opts conv.Options) -``` - -SetOptions sets options - -## type HTTPConv - -HTTPConv is a converter from http request to thrift message - -```go -type HTTPConv struct { - // contains filtered or unexported fields -} -``` - -### func NewHTTPConv - -```go -func NewHTTPConv(proto meta.Encoding, fnDesc *thrift.FunctionDescriptor) *HTTPConv -``` - -NewHTTPConv returns a new HTTPConv, which contains the thrift message header and footer - -proto is specified thrift encoding protocol \(meta.EncodingThriftBinary|meta.EncodingThriftCompact\) fnDesc is the thrift method descriptor corresponding to the http request url - -### func \(HTTPConv\) Do - -```go -func (h HTTPConv) Do(ctx context.Context, req http.RequestGetter, opt conv.Options) (tbytes []byte, err error) -``` - -Do converts http request into thrift message. req body must be one of following: \- json \(application/json\) \- url encoded form \(application/x\-www\-form\-urlencoded\) \- empty - -### func \(HTTPConv\) DoInto - -```go -func (h HTTPConv) DoInto(ctx context.Context, req http.RequestGetter, buf *[]byte, opt conv.Options) (err error) -``` - -
Example -

- -```go -{ - - svc, err := thrift.NewDescritorFromPath(context.Background(), exampleIDLPath) - if err != nil { - panic(err) - } - fn := svc.Functions()["ExampleMethod"] - - cv := NewHTTPConv(meta.EncodingThriftBinary, fn) - - jdata := `{"msg":"hello","InnerBase":{}}` - stdreq, err := stdhttp.NewRequest("POST", - "http://localhost:8080/example?query=1,2,3&inner_query=中文", - strings.NewReader(jdata)) - if err != nil { - panic(err) - } - stdreq.Header.Set("Content-Type", "application/json") - stdreq.Header.Set("heeader", "true") - stdreq.Header.Set("inner_string", "igorned") - - req, err := http.NewHTTPRequestFromStdReq( - stdreq, - http.Param{Key: "path", Value: "OK"}, - http.Param{Key: "inner_string", Value: "priority"}, - ) - - buf := make([]byte, 0, len(jdata)*2/3) - - err = cv.DoInto(context.Background(), req, &buf, opts) - if err != nil { - panic(err) - } - - p := thrift.NewBinaryProtocol(buf) - method, mType, seqID, reqID, stru, err := p.UnwrapBody() - println(method, mType, seqID, reqID) - - act := example3.NewExampleReq() - _, err = act.FastRead(stru) - if err != nil { - panic(err) - } - spew.Dump(act) -} -``` - -

-
- - - -Generated by [gomarkdoc]() diff --git a/conv/j2t/conv_test.go b/conv/j2t/conv_test.go index b4b064ab..07dca6d0 100644 --- a/conv/j2t/conv_test.go +++ b/conv/j2t/conv_test.go @@ -171,6 +171,32 @@ func TestConvJSON2Thrift(t *testing.T) { require.Equal(t, exp, act) } +func TestPanicRecover(t *testing.T) { + desc := getExampleDesc() + data := getExampleData() + exp := example3.NewExampleReq() + err := json.Unmarshal(data, exp) + require.Nil(t, err) + req := getExampleReq(exp, true, data) + cv := NewBinaryConv(conv.Options{ + EnableHttpMapping: true, + }) + ctx := context.Background() + ctx = context.WithValue(ctx, conv.CtxKeyHTTPRequest, req) + buf := make([]byte, 0, 1) + mock := MockConv{ + panic:2, + } + defer func() { + if v := recover(); v == nil { + t.Fatal("not panic") + } else { + t.Log(v) + } + }() + _ = mock.do(&cv, ctx, data, desc, &buf, req, true) +} + func TestConvHTTP2Thrift(t *testing.T) { desc := getExampleDesc() data := getExampleData() @@ -683,7 +709,6 @@ func TestError(t *testing.T) { sp: types.MAX_RECURSE + 1, reqsCache: 1, keyCache: 1, - dcap: 800, } err := mock.do(&cv, ctx, src, desc, &buf, nil, true) require.Error(t, err) @@ -724,8 +749,8 @@ type MockConv struct { sp int reqsCache int keyCache int - dcap int - fc int + fieldCache int + panic int } func (mock *MockConv) Do(self *BinaryConv, ctx context.Context, desc *thrift.TypeDescriptor, jbytes []byte) (tbytes []byte, err error) { @@ -759,26 +784,46 @@ func (mock *MockConv) Do(self *BinaryConv, ctx context.Context, desc *thrift.Typ func (mock MockConv) do(self *BinaryConv, ctx context.Context, src []byte, desc *thrift.TypeDescriptor, buf *[]byte, req http.RequestGetter, top bool) (err error) { flags := toFlags(self.opts) jp := rt.Mem2Str(src) - tmp := make([]byte, 0, mock.dcap) - fsm := &types.J2TStateMachine{ - SP: mock.sp, - JT: types.JsonState{ - Dbuf: *(**byte)(unsafe.Pointer(&tmp)), - Dcap: mock.dcap, - }, - ReqsCache: make([]byte, 0, mock.reqsCache), - KeyCache: make([]byte, 0, mock.keyCache), - SM: types.StateMachine{}, - VT: [types.MAX_RECURSE]types.J2TState{}, - FieldCache: make([]int32, 0, mock.fc), + // tmp := make([]byte, 0, mock.dcap) + fsm := types.NewJ2TStateMachine() + if mock.reqsCache != 0 { + fsm.ReqsCache = make([]byte, 0, mock.reqsCache) + } + if mock.keyCache != 0 { + fsm.KeyCache = make([]byte, 0, mock.keyCache) + } + if mock.fieldCache != 0 { + fsm.FieldCache = make([]int32, 0, mock.fieldCache) } + // fsm := &types.J2TStateMachine{ + // SP: mock.sp, + // JT: types.JsonState{ + // Dbuf: *(**byte)(unsafe.Pointer(&tmp)), + // Dcap: mock.dcap, + // }, + // ReqsCache: make([]byte, 0, mock.reqsCache), + // KeyCache: make([]byte, 0, mock.keyCache), + // SM: types.StateMachine{}, + // VT: [types.MAX_RECURSE]types.J2TState{}, + // FieldCache: make([]int32, 0, mock.fc), + // } fsm.Init(0, unsafe.Pointer(desc)) if mock.sp != 0 { fsm.SP = mock.sp } + var ret uint64 + defer func() { + if msg := recover(); msg != nil { + panic(makePanicMsg(msg, src, desc, buf, req, self.flags, fsm, ret)) + } + }() exec: - ret := native.J2T_FSM(fsm, buf, &jp, flags) + mock.panic -= 1 + if mock.panic == 0 { + panic("test!") + } + ret = native.J2T_FSM(fsm, buf, &jp, flags) if ret != 0 { cont, e := self.handleError(ctx, fsm, buf, src, req, ret, top) if cont && e == nil { @@ -807,7 +852,6 @@ func TestStateMachineOOM(t *testing.T) { sp: 0, reqsCache: 1, keyCache: 1, - dcap: 800, } err := mock.do(&cv, ctx, src, desc, &buf, nil, true) require.Nil(t, err) @@ -844,8 +888,7 @@ func TestStateMachineOOM(t *testing.T) { sp: 1, reqsCache: 1, keyCache: 1, - dcap: 800, - fc: 0, + fieldCache: 0, } cv := NewBinaryConv(conv.Options{ EnableHttpMapping: true, diff --git a/conv/j2t/error.go b/conv/j2t/error.go index be87cf31..a1018711 100644 --- a/conv/j2t/error.go +++ b/conv/j2t/error.go @@ -55,7 +55,7 @@ func getJ2TExtraStruct(fsm *types.J2TStateMachine, offset int) (td *thrift.TypeD } func (self BinaryConv) handleError(ctx context.Context, fsm *types.J2TStateMachine, buf *[]byte, src []byte, req http.RequestGetter, ret uint64, top bool) (cont bool, err error) { - e := types.ParsingError(ret & ((1 << types.ERR_WRAP_SHIFT_CODE) - 1)) + e := getErrCode(ret) p := int(ret >> types.ERR_WRAP_SHIFT_CODE) switch e { @@ -131,12 +131,25 @@ func (self BinaryConv) handleError(ctx context.Context, fsm *types.J2TStateMachi } } - return false, explainNativeError(e, src, p) + return false, explainNativeError(ret, src) } -func explainNativeError(e types.ParsingError, in []byte, v int) error { - ip := v & ((1 << types.ERR_WRAP_SHIFT_POS) - 1) - v = v >> types.ERR_WRAP_SHIFT_POS +func getPos(e uint64) int { + return int(e >> types.ERR_WRAP_SHIFT_CODE) & ((1 << types.ERR_WRAP_SHIFT_POS) - 1) +} + +func getErrCode(e uint64) types.ParsingError { + return types.ParsingError(e & ((1 << types.ERR_WRAP_SHIFT_CODE) - 1)) +} + +func getValue(e uint64) int { + return int(e >> (types.ERR_WRAP_SHIFT_CODE + types.ERR_WRAP_SHIFT_POS)) +} + +func explainNativeError(ret uint64, in []byte) error { + ip := getPos(ret) + v := getValue(ret) + e := getErrCode(ret) switch e { case types.ERR_INVALID_CHAR: ch, st := v>>types.ERR_WRAP_SHIFT_CODE, v&((1< +%s + +Desc: %s + +%s + +Ret: %x`, msg, flags, string(src), *buf, fsm.String(), desc.Name(), re, ret) +} diff --git a/conv/j2t/impl.go b/conv/j2t/impl.go index 46cd7a4a..23961dea 100644 --- a/conv/j2t/impl.go +++ b/conv/j2t/impl.go @@ -91,22 +91,29 @@ func (self *BinaryConv) do(ctx context.Context, src []byte, desc *thrift.TypeDes } func (self *BinaryConv) doNative(ctx context.Context, src []byte, desc *thrift.TypeDescriptor, buf *[]byte, req http.RequestGetter, top bool) (err error) { - jp := rt.Mem2Str(src) fsm := types.NewJ2TStateMachine() + var ret uint64 + defer func() { + if msg := recover(); msg != nil { + panic(makePanicMsg(msg, src, desc, buf, req, self.flags, fsm, ret)) + } + }() + + jp := rt.Mem2Str(src) fsm.Init(0, unsafe.Pointer(desc)) exec: - ret := native.J2T_FSM(fsm, buf, &jp, self.flags) + ret = native.J2T_FSM(fsm, buf, &jp, self.flags) if ret != 0 { cont, e := self.handleError(ctx, fsm, buf, src, req, ret, top) if cont && e == nil { goto exec } err = e - goto ret + goto final } -ret: +final: types.FreeJ2TStateMachine(fsm) runtime.KeepAlive(desc) runtime.KeepAlive(src) diff --git a/conv/p2j/README.md b/conv/p2j/README.md deleted file mode 100644 index 388bf790..00000000 --- a/conv/p2j/README.md +++ /dev/null @@ -1,139 +0,0 @@ - - -# p2j - -```go -import "github.com/cloudwego/dynamicgo/conv/p2j" -``` - -## Index - -- [type BinaryConv](<#BinaryConv>) - - [func NewBinaryConv(opts conv.Options) BinaryConv](<#NewBinaryConv>) - - [func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, pbytes []byte) (json []byte, err error)](<#BinaryConv.Do>) - - [func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, pbytes []byte, buf *[]byte) (err error)](<#BinaryConv.DoInto>) - - [func (self *BinaryConv) SetOptions(opts conv.Options)](<#BinaryConv.SetOptions>) - - - -## type [BinaryConv]() - - - -```go -type BinaryConv struct { - // contains filtered or unexported fields -} -``` - - -### func [NewBinaryConv]() - -```go -func NewBinaryConv(opts conv.Options) BinaryConv -``` - -NewBinaryConv returns a new BinaryConv - - -### func (*BinaryConv) [Do]() - -```go -func (self *BinaryConv) Do(ctx context.Context, desc *proto.TypeDescriptor, pbytes []byte) (json []byte, err error) -``` - -Do converts protobuf binary (pbytes) to json bytes (jbytes) desc is the protobuf type descriptor of the protobuf binary, usually it is a response Message type - -
Example -

- - - -```go -package main - -import ( - "context" - "encoding/json" - "reflect" - - "github.com/cloudwego/dynamicgo/conv" - "github.com/cloudwego/dynamicgo/internal/util_test" - "github.com/cloudwego/dynamicgo/proto" - "github.com/cloudwego/dynamicgo/testdata/kitex_gen/pb/example2" - goprotowire "google.golang.org/protobuf/encoding/protowire" -) - -var opts = conv.Options{} - -func main() { - // get descriptor and data - includeDirs := util_test.MustGitPath("testdata/idl/") // includeDirs is used to find the include files. - desc := proto.FnRequest(proto.GetFnDescFromFile(exampleIDLPath, "ExampleMethod", proto.Options{}, includeDirs)) - - // make BinaryConv - cv := NewBinaryConv(conv.Options{}) - in := readExampleReqProtoBufData() - - // do conversion - out, err := cv.Do(context.Background(), desc, in) - if err != nil { - panic(err) - } - exp := example2.ExampleReq{} - - // use kitex_util to check proto data validity - l := 0 - dataLen := len(in) - for l < dataLen { - id, wtyp, tagLen := goprotowire.ConsumeTag(in) - if tagLen < 0 { - panic("proto data error format") - } - l += tagLen - in = in[tagLen:] - offset, err := exp.FastRead(in, int8(wtyp), int32(id)) - if err != nil { - panic(err) - } - in = in[offset:] - l += offset - } - if len(in) != 0 { - panic("proto data error format") - } - - // validate result - var act example2.ExampleReq - err = json.Unmarshal([]byte(out), &act) - if err != nil { - panic(err) - } - if !reflect.DeepEqual(exp, act) { - panic("not equal") - } -} -``` - -

-
- - -### func (*BinaryConv) [DoInto]() - -```go -func (self *BinaryConv) DoInto(ctx context.Context, desc *proto.TypeDescriptor, pbytes []byte, buf *[]byte) (err error) -``` - -DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer - - -### func (*BinaryConv) [SetOptions]() - -```go -func (self *BinaryConv) SetOptions(opts conv.Options) -``` - -SetOptions sets options - -Generated by [gomarkdoc]() diff --git a/conv/t2j/README.md b/conv/t2j/README.md deleted file mode 100644 index 7f0e7f0e..00000000 --- a/conv/t2j/README.md +++ /dev/null @@ -1,178 +0,0 @@ - - -# t2j - -```go -import "github.com/cloudwego/dynamicgo/conv/t2j" -``` - -## Index - -- [type BinaryConv](<#type-binaryconv>) - - [func NewBinaryConv(opts conv.Options) BinaryConv](<#func-newbinaryconv>) - - [func (self *BinaryConv) Do(ctx context.Context, desc *thrift.TypeDescriptor, tbytes []byte) (json []byte, err error)](<#func-binaryconv-do>) - - [func (self *BinaryConv) DoInto(ctx context.Context, desc *thrift.TypeDescriptor, tbytes []byte, buf *[]byte) (err error)](<#func-binaryconv-dointo>) - - [func (self *BinaryConv) SetOptions(opts conv.Options)](<#func-binaryconv-setoptions>) -- [type HTTPConv](<#type-httpconv>) - - [func NewHTTPConv(proto meta.Encoding, desc *thrift.FunctionDescriptor) *HTTPConv](<#func-newhttpconv>) - - [func (h HTTPConv) Do(ctx context.Context, resp http.ResponseSetter, tbytes []byte, opt conv.Options) (err error)](<#func-httpconv-do>) - - [func (h HTTPConv) DoInto(ctx context.Context, resp http.ResponseSetter, tbytes []byte, buf *[]byte, opt conv.Options) (err error)](<#func-httpconv-dointo>) - - -## type BinaryConv - -BinaryConv is a converter from thrift binary to json - -```go -type BinaryConv struct { - // contains filtered or unexported fields -} -``` - -### func NewBinaryConv - -```go -func NewBinaryConv(opts conv.Options) BinaryConv -``` - -NewBinaryConv returns a new BinaryConv - -### func \(\*BinaryConv\) Do - -```go -func (self *BinaryConv) Do(ctx context.Context, desc *thrift.TypeDescriptor, tbytes []byte) (json []byte, err error) -``` - -#### Do converts thrift binary \(tbytes\) to json bytes \(jbytes\) - -desc is the thrift type descriptor of the thrift binary, usually it is a response STRUCT type ctx is the context, which can be used to pass arguments as below: \- conv.CtxKeyHTTPResponse: http.ResponseSetter as http request \- conv.CtxKeyThriftRespBase: thrift.Base as base metadata of thrift response - -
Example -

- -```go -{ - - desc := thrift.FnResponse(thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{})) - data := getExample3Data() - - cv := NewBinaryConv(opts) - - out, err := cv.Do(context.Background(), desc, data) - if err != nil { - panic(err) - } - - // validate result - var exp, act example3.ExampleResp - _, err = exp.FastRead(data) - if err != nil { - panic(err) - } - err = json.Unmarshal(out, &act) - if err != nil { - panic(err) - } - if !reflect.DeepEqual(exp, act) { - panic("not equal") - } -} -``` - -

-
- -### func \(\*BinaryConv\) DoInto - -```go -func (self *BinaryConv) DoInto(ctx context.Context, desc *thrift.TypeDescriptor, tbytes []byte, buf *[]byte) (err error) -``` - -DoInto behaves like Do, but it writes the result to buffer directly instead of returning a new buffer - -### func \(\*BinaryConv\) SetOptions - -```go -func (self *BinaryConv) SetOptions(opts conv.Options) -``` - -SetOptions sets options - -## type HTTPConv - -HTTPConv is a converter from thrift message to http response - -```go -type HTTPConv struct { - // contains filtered or unexported fields -} -``` - -### func NewHTTPConv - -```go -func NewHTTPConv(proto meta.Encoding, desc *thrift.FunctionDescriptor) *HTTPConv -``` - -NewHTTPConv returns a new HTTPConv - -### func \(HTTPConv\) Do - -```go -func (h HTTPConv) Do(ctx context.Context, resp http.ResponseSetter, tbytes []byte, opt conv.Options) (err error) -``` - -Do converts thrift message \(tbytes\) into http response. resp body is set as json protocol if has - -### func \(HTTPConv\) DoInto - -```go -func (h HTTPConv) DoInto(ctx context.Context, resp http.ResponseSetter, tbytes []byte, buf *[]byte, opt conv.Options) (err error) -``` - -DoInto converts the thrift message \(tbytes\) to into buf in JSON protocol, as well as other http response arguments. WARN: This will set buf to resp, thus DONOT reuse the buf afterward. - -
Example -

- -```go -{ - - desc := thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{}) - - data := getExample3Data() - in, err := thrift.WrapBinaryBody(data, "ExampleMethod", thrift.REPLY, thrift.FieldID(0), 1) - if err != nil { - panic(err) - } - - resp := http.NewHTTPResponse() - resp.StatusCode = 200 - - cv := NewHTTPConv(meta.EncodingThriftBinary, desc) - - buf := make([]byte, 0, len(data)*2) - err = cv.DoInto(context.Background(), resp, in, &buf, opts) - if err != nil { - panic(err) - } - - // validate result - var act example3.ExampleResp - err = json.Unmarshal(buf, &act) - if err != nil { - panic(err) - } - - spew.Dump(act) - - spew.Dump(resp) -} -``` - -

-
- - - -Generated by [gomarkdoc]() diff --git a/go.mod b/go.mod index dcab61f7..1c45f406 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.17 require ( github.com/apache/thrift v0.13.0 - github.com/bytedance/sonic v1.11.8 - github.com/bytedance/sonic/loader v0.1.1 + github.com/bytedance/sonic v1.12.0 + github.com/bytedance/sonic/loader v0.2.0 github.com/cloudwego/base64x v0.1.4 github.com/cloudwego/fastpb v0.0.4 github.com/cloudwego/thriftgo v0.3.6 @@ -25,6 +25,7 @@ require ( 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/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 ) diff --git a/go.sum b/go.sum index b7d948eb..a51b0de5 100644 --- a/go.sum +++ b/go.sum @@ -2,10 +2,11 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI= github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= -github.com/bytedance/sonic v1.11.8 h1:Zw/j1KfiS+OYTi9lyB3bb0CFxPJVkM17k1wyDG32LRA= -github.com/bytedance/sonic v1.11.8/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= -github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic v1.12.0 h1:YGPgxF9xzaCNvd/ZKdQ28yRovhfMFZQjuk6fKBzZ3ls= +github.com/bytedance/sonic v1.12.0/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= +github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= @@ -25,6 +26,7 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4= github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -51,6 +53,7 @@ github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHL github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/jhump/protoreflect v1.8.2 h1:k2xE7wcUomeqwY0LDCYA16y4WWfyTcMx5mKhk0d4ua0= github.com/jhump/protoreflect v1.8.2/go.mod h1:7GcYQDdMU/O/BBrl/cX6PNHpXh6cenjd8pneu5yW7Tg= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= @@ -79,6 +82,7 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= @@ -103,6 +107,7 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= @@ -112,11 +117,13 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -138,12 +145,15 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -164,6 +174,7 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.1-0.20200805231151-a709e31e5d12/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/http/README.md b/http/README.md deleted file mode 100644 index ab9ed4bd..00000000 --- a/http/README.md +++ /dev/null @@ -1,352 +0,0 @@ - - -# http - -```go -import "github.com/cloudwego/dynamicgo/http" -``` - -## Index - -- [Constants](<#constants>) -- [Variables](<#variables>) -- [func AnnoToMethod(annoKey string) string](<#func-annotomethod>) -- [type Endpoint](<#type-endpoint>) -- [type HTTPRequest](<#type-httprequest>) - - [func NewHTTPRequest() *HTTPRequest](<#func-newhttprequest>) - - [func NewHTTPRequestFromStdReq(req *http.Request, params ...Param) (ret *HTTPRequest, err error)](<#func-newhttprequestfromstdreq>) - - [func NewHTTPRequestFromUrl(method, url string, body io.Reader, params ...Param) (*HTTPRequest, error)](<#func-newhttprequestfromurl>) - - [func (self HTTPRequest) Body() []byte](<#func-httprequest-body>) - - [func (self HTTPRequest) Cookie(key string) string](<#func-httprequest-cookie>) - - [func (self HTTPRequest) Header(key string) string](<#func-httprequest-header>) - - [func (self HTTPRequest) Host() string](<#func-httprequest-host>) - - [func (self *HTTPRequest) MapBody(key string) string](<#func-httprequest-mapbody>) - - [func (self HTTPRequest) Method() string](<#func-httprequest-method>) - - [func (self HTTPRequest) Param(key string) string](<#func-httprequest-param>) - - [func (self HTTPRequest) Path() string](<#func-httprequest-path>) - - [func (self HTTPRequest) PostForm(key string) string](<#func-httprequest-postform>) - - [func (self HTTPRequest) Query(key string) string](<#func-httprequest-query>) - - [func (self HTTPRequest) Uri() string](<#func-httprequest-uri>) -- [type HTTPResponse](<#type-httpresponse>) - - [func NewHTTPResponse() *HTTPResponse](<#func-newhttpresponse>) - - [func (self HTTPResponse) SetCookie(key string, val string) error](<#func-httpresponse-setcookie>) - - [func (self HTTPResponse) SetHeader(key string, val string) error](<#func-httpresponse-setheader>) - - [func (self HTTPResponse) SetRawBody(body []byte) error](<#func-httpresponse-setrawbody>) - - [func (self HTTPResponse) SetStatusCode(code int) error](<#func-httpresponse-setstatuscode>) -- [type Param](<#type-param>) -- [type Params](<#type-params>) - - [func (ps *Params) ByName(name string) string](<#func-params-byname>) - - [func (ps *Params) Recycle()](<#func-params-recycle>) - - [func (ps *Params) Set(name string, val string) bool](<#func-params-set>) -- [type RequestGetter](<#type-requestgetter>) -- [type ResponseSetter](<#type-responsesetter>) - - -## Constants - -```go -const ( - // HeaderContentType is the key of Content-Type header - HeaderContentType = "Content-Type" - // HeaderSetCookie is the key of Set-Cookie header - HeaderSetCookie = "Set-Cookie" -) -``` - -## Variables - -```go -var ( - // DefaultJsonPairSize is the default size of json.Pair slice. - DefaultJsonPairSize = 16 -) -``` - -## func AnnoToMethod - -```go -func AnnoToMethod(annoKey string) string -``` - -AnnoToMethod maps annotation to corresponding http method - -## type Endpoint - -Endpoint a http endpoint. - -```go -type Endpoint struct { - Method, Path string -} -``` - -## type HTTPRequest - -Request is a implementation of RequestGetter. It wraps http.Request. - -```go -type HTTPRequest struct { - *http.Request - - Params Params - BodyMap interface{} - // contains filtered or unexported fields -} -``` - -### func NewHTTPRequest - -```go -func NewHTTPRequest() *HTTPRequest -``` - -NewHTTPRequest creates a new HTTPRequest. - -### func NewHTTPRequestFromStdReq - -```go -func NewHTTPRequestFromStdReq(req *http.Request, params ...Param) (ret *HTTPRequest, err error) -``` - -NewHTTPRequestFromStdReq creates a new HTTPRequest from http.Request. It will check the content\-type of the request and parse the body if the type one of following: \- application/json \- application/x\-www\-form\-urlencoded - -### func NewHTTPRequestFromUrl - -```go -func NewHTTPRequestFromUrl(method, url string, body io.Reader, params ...Param) (*HTTPRequest, error) -``` - -NewHTTPRequestFromUrl creates a new HTTPRequest from url, body and url\-path param. - -### func \(HTTPRequest\) Body - -```go -func (self HTTPRequest) Body() []byte -``` - -Body implements RequestGetter.Body. - -### func \(HTTPRequest\) Cookie - -```go -func (self HTTPRequest) Cookie(key string) string -``` - -Cookie implements RequestGetter.Cookie. - -### func \(HTTPRequest\) Header - -```go -func (self HTTPRequest) Header(key string) string -``` - -Header implements RequestGetter.Header. - -### func \(HTTPRequest\) Host - -```go -func (self HTTPRequest) Host() string -``` - -Host implements RequestGetter.Host. - -### func \(\*HTTPRequest\) MapBody - -```go -func (self *HTTPRequest) MapBody(key string) string -``` - -MapBody implements RequestGetter.MapBody. - -### func \(HTTPRequest\) Method - -```go -func (self HTTPRequest) Method() string -``` - -Method implements RequestGetter.Method. - -### func \(HTTPRequest\) Param - -```go -func (self HTTPRequest) Param(key string) string -``` - -Param implements RequestGetter.Param. - -### func \(HTTPRequest\) Path - -```go -func (self HTTPRequest) Path() string -``` - -Path implements RequestGetter.Path. - -### func \(HTTPRequest\) PostForm - -```go -func (self HTTPRequest) PostForm(key string) string -``` - -PostForm implements RequestGetter.PostForm. - -### func \(HTTPRequest\) Query - -```go -func (self HTTPRequest) Query(key string) string -``` - -Query implements RequestGetter.Query. - -### func \(HTTPRequest\) Uri - -```go -func (self HTTPRequest) Uri() string -``` - -Uri implements RequestGetter.Uri. - -## type HTTPResponse - -HTTPResponse is an implementation of ResponseSetter - -```go -type HTTPResponse struct { - *http.Response -} -``` - -### func NewHTTPResponse - -```go -func NewHTTPResponse() *HTTPResponse -``` - -NewHTTPResponse creates a new HTTPResponse - -### func \(HTTPResponse\) SetCookie - -```go -func (self HTTPResponse) SetCookie(key string, val string) error -``` - -SetCookie implements ResponseSetter.SetCookie - -### func \(HTTPResponse\) SetHeader - -```go -func (self HTTPResponse) SetHeader(key string, val string) error -``` - -SetHeader implements ResponseSetter.SetHeader - -### func \(HTTPResponse\) SetRawBody - -```go -func (self HTTPResponse) SetRawBody(body []byte) error -``` - -### func \(HTTPResponse\) SetStatusCode - -```go -func (self HTTPResponse) SetStatusCode(code int) error -``` - -SetStatusCode implements ResponseSetter.SetStatusCode - -## type Param - -### Param in url path - -e.g. /user/:id \+ /user/123 =\> Param\{Key: "id", Value: "123"\} - -```go -type Param struct { - Key string - Value string -} -``` - -## type Params - -Http url\-path params - -```go -type Params struct { - // contains filtered or unexported fields -} -``` - -### func \(\*Params\) ByName - -```go -func (ps *Params) ByName(name string) string -``` - -ByName search Param by given name - -### func \(\*Params\) Recycle - -```go -func (ps *Params) Recycle() -``` - -Recycle the Params - -### func \(\*Params\) Set - -```go -func (ps *Params) Set(name string, val string) bool -``` - -Set set Param by given name and value, return true if Param exists - -## type RequestGetter - -RequestGetter is a interface for getting request parameters - -```go -type RequestGetter interface { - // Method returns the http method. - Method() string - // Host returns the host. - Host() string - // Uri returns entire uri. - Uri() string - // Header returns the value of the header with the given key. - Header(string) string - // Cookie returns the value of the cookie with the given key. - Cookie(string) string - // Query returns the value of the query with the given key. - Query(string) string - // Param returns the value of the url-path param with the given key. - Param(string) string - // PostForm returns the value of the post-form body with the given key. - PostForm(string) string - // MapBody returns the value of body with the given key. - MapBody(string) string - // Body returns the raw body in bytes. - Body() []byte -} -``` - -## type ResponseSetter - -ResponseSetter is a interface for setting response parameters - -```go -type ResponseSetter interface { - // SetStatusCode sets the status code of the response - SetStatusCode(int) error - // SetHeader sets the header of the response - SetHeader(string, string) error - // SetCookie sets the cookie of the response - SetCookie(string, string) error - // SetRawBody sets the raw body of the response - SetRawBody([]byte) error -} -``` - - - -Generated by [gomarkdoc]() diff --git a/http/http.go b/http/http.go index 956c0337..847945b4 100644 --- a/http/http.go +++ b/http/http.go @@ -18,6 +18,7 @@ package http import ( "bytes" + "fmt" "io" "io/ioutil" "net/http" @@ -91,6 +92,13 @@ type HTTPRequest struct { BodyMap interface{} } +func (h *HTTPRequest) String() string { + return fmt.Sprintf(`URL: %s +Headers: %v +Body: %s +BodyMap: %v`, h.URL.String(), h.Header, string(h.rawBody), h.BodyMap) +} + // NewHTTPRequest creates a new HTTPRequest. func NewHTTPRequest() *HTTPRequest { return &HTTPRequest{} diff --git a/internal/json/api_compat.go b/internal/json/api_compat.go index e2e1e374..4953fd54 100644 --- a/internal/json/api_compat.go +++ b/internal/json/api_compat.go @@ -22,25 +22,19 @@ import ( "strconv" "unicode/utf8" _ "unsafe" + + "github.com/cloudwego/dynamicgo/internal/rt" ) func NoQuote(buf *[]byte, val string) { quoteString(buf, val) } -var ( - //go:linkname safeSet encoding/json.safeSet - safeSet [utf8.RuneSelf]bool - - //go:linkname hex encoding/json.hex - hex string -) - func quoteString(e *[]byte, s string) { start := 0 for i := 0; i < len(s); { if b := s[i]; b < utf8.RuneSelf { - if safeSet[b] { + if rt.SafeSet[b] { i++ continue } @@ -64,8 +58,8 @@ func quoteString(e *[]byte, s string) { // user-controlled strings are rendered into JSON // and served to some browsers. *e = append(*e, `u00`...) - *e = append(*e, hex[b>>4]) - *e = append(*e, hex[b&0xF]) + *e = append(*e, rt.Hex[b>>4]) + *e = append(*e, rt.Hex[b&0xF]) } i++ start = i @@ -86,7 +80,7 @@ func quoteString(e *[]byte, s string) { *e = append(*e, s[start:i]...) } *e = append(*e, `\u202`...) - *e = append(*e, hex[c&0xF]) + *e = append(*e, rt.Hex[c&0xF]) i += size start = i continue diff --git a/internal/native/types/types.go b/internal/native/types/types.go index e4d67a6c..5a6ff115 100644 --- a/internal/native/types/types.go +++ b/internal/native/types/types.go @@ -20,7 +20,6 @@ import ( "fmt" "sync" "unsafe" - ) const ( @@ -174,6 +173,10 @@ type JsonState struct { Dcap int } +func (s JsonState) String() string { + return fmt.Sprintf(`{Vt: %d, Dv: %f, Iv: %d, Ep: %d}`, s.Vt, s.Dv, s.Iv, s.Ep) +} + type StateMachine struct { Sp int Vt [MAX_RECURSE]int64 @@ -204,6 +207,27 @@ type J2TState struct { Extra J2TExtra } +//go:nocheckptr +func (s J2TState) String() string { + name := "" + typ := 0 + if s.TypeDesc != 0 { + desc := (*tTypeDesc)(unsafe.Pointer(s.TypeDesc)) + name = desc.name + typ = int(desc.ttype) + } + return fmt.Sprintf("{State: %x, JsonPos: %d, TypeDesc: %s(%d), Extra:%v}", s.State, s.JsonPos, name, typ, s.Extra) +} + +type tTypeDesc struct +{ + ttype uint8; + name string; + key *tTypeDesc; + elem *tTypeDesc; + st unsafe.Pointer; +}; + func (self *J2TState) TdPointer() uintptr { return uintptr(self.TypeDesc) } @@ -255,6 +279,31 @@ type J2TStateMachine struct { FieldValueCache FieldValue } +func (fsm *J2TStateMachine) String() string { + var vt1 *J2TState + if fsm.SP > 0 { + vt1 = &fsm.VT[fsm.SP-1] + } + var vt2 *J2TState + if fsm.SP > 1 { + vt2 = &fsm.VT[fsm.SP-2] + } + var svt int64 + if fsm.SM.Sp > 0 { + svt = fsm.SM.Vt[fsm.SM.Sp-1] + } + return fmt.Sprintf(`SP: %d +JsonState: %v +VT[SP-1]: %v +VT[SP-2]: %v +SM.SP: %d +SM.VT[SP-1]: %x +ReqsCache: %v +KeyCache: %s +FieldCache: %v +FieldValue: %#v`, fsm.SP, fsm.JT, vt1, vt2, fsm.SM.Sp, svt, fsm.ReqsCache, fsm.KeyCache, fsm.FieldCache, fsm.FieldValueCache) +} + type FieldValue struct { FieldID int32 ValBegin uint32 diff --git a/internal/rt/table.go b/internal/rt/table.go new file mode 100644 index 00000000..7d49f90e --- /dev/null +++ b/internal/rt/table.go @@ -0,0 +1,118 @@ +// Copyright 2024 CloudWeGo Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package rt + +import "unicode/utf8" + +var SafeSet = [utf8.RuneSelf]bool{ + ' ': true, + '!': true, + '"': false, + '#': true, + '$': true, + '%': true, + '&': true, + '\'': true, + '(': true, + ')': true, + '*': true, + '+': true, + ',': true, + '-': true, + '.': true, + '/': true, + '0': true, + '1': true, + '2': true, + '3': true, + '4': true, + '5': true, + '6': true, + '7': true, + '8': true, + '9': true, + ':': true, + ';': true, + '<': true, + '=': true, + '>': true, + '?': true, + '@': true, + 'A': true, + 'B': true, + 'C': true, + 'D': true, + 'E': true, + 'F': true, + 'G': true, + 'H': true, + 'I': true, + 'J': true, + 'K': true, + 'L': true, + 'M': true, + 'N': true, + 'O': true, + 'P': true, + 'Q': true, + 'R': true, + 'S': true, + 'T': true, + 'U': true, + 'V': true, + 'W': true, + 'X': true, + 'Y': true, + 'Z': true, + '[': true, + '\\': false, + ']': true, + '^': true, + '_': true, + '`': true, + 'a': true, + 'b': true, + 'c': true, + 'd': true, + 'e': true, + 'f': true, + 'g': true, + 'h': true, + 'i': true, + 'j': true, + 'k': true, + 'l': true, + 'm': true, + 'n': true, + 'o': true, + 'p': true, + 'q': true, + 'r': true, + 's': true, + 't': true, + 'u': true, + 'v': true, + 'w': true, + 'x': true, + 'y': true, + 'z': true, + '{': true, + '|': true, + '}': true, + '~': true, + '\u007f': true, +} + +var Hex = "0123456789abcdef" diff --git a/proto/README.md b/proto/README.md deleted file mode 100644 index dd9b2c1a..00000000 --- a/proto/README.md +++ /dev/null @@ -1,893 +0,0 @@ - - -# proto - -```go -import "github.com/cloudwego/dynamicgo/proto" -``` - -## Index - -- [proto](#proto) - - [Index](#index) - - [Variables](#variables) - - [type EnumNumber](#type-enumnumber) - - [type FieldDescriptor](#type-fielddescriptor) - - [func (\*FieldDescriptor) IsList](#func-fielddescriptor-islist) - - [func (\*FieldDescriptor) IsMap](#func-fielddescriptor-ismap) - - [func (\*FieldDescriptor) JSONName](#func-fielddescriptor-jsonname) - - [func (\*FieldDescriptor) Kind](#func-fielddescriptor-kind) - - [func (\*FieldDescriptor) MapKey](#func-fielddescriptor-mapkey) - - [func (\*FieldDescriptor) MapValue](#func-fielddescriptor-mapvalue) - - [func (\*FieldDescriptor) Message](#func-fielddescriptor-message) - - [func (\*FieldDescriptor) Name](#func-fielddescriptor-name) - - [func (\*FieldDescriptor) Number](#func-fielddescriptor-number) - - [func (\*FieldDescriptor) Type](#func-fielddescriptor-type) - - [type FieldNameMap](#type-fieldnamemap) - - [func (FieldNameMap) All](#func-fieldnamemap-all) - - [func (\*FieldNameMap) Build](#func-fieldnamemap-build) - - [func (FieldNameMap) Get](#func-fieldnamemap-get) - - [func (\*FieldNameMap) Set](#func-fieldnamemap-set) - - [func (FieldNameMap) Size](#func-fieldnamemap-size) - - [type FieldNumber](#type-fieldnumber) - - [type FieldNumberMap](#type-fieldnumbermap) - - [func (FieldNumberMap) All](#func-fieldnumbermap-all) - - [func (FieldNumberMap) Get](#func-fieldnumbermap-get) - - [func (\*FieldNumberMap) Set](#func-fieldnumbermap-set) - - [func (FieldNumberMap) Size](#func-fieldnumbermap-size) - - [type MessageDescriptor](#type-messagedescriptor) - - [func (\*MessageDescriptor) ByJSONName](#func-messagedescriptor-byjsonname) - - [func (\*MessageDescriptor) ByName](#func-messagedescriptor-byname) - - [func (\*MessageDescriptor) ByNumber](#func-messagedescriptor-bynumber) - - [func (\*MessageDescriptor) FieldsCount](#func-messagedescriptor-fieldscount) - - [func (\*MessageDescriptor) Name](#func-messagedescriptor-name) - - [type MethodDescriptor](#type-methoddescriptor) - - [func GetFnDescFromFile](#func-getfndescfromfile) - - [func (\*MethodDescriptor) Input](#func-methoddescriptor-input) - - [func (\*MethodDescriptor) Name](#func-methoddescriptor-name) - - [func (\*MethodDescriptor) Output](#func-methoddescriptor-output) - - [type Number](#type-number) - - [type Options](#type-options) - - [func NewDefaultOptions](#func-newdefaultoptions) - - [func (Options) NewDesccriptorFromContent](#func-options-newdesccriptorfromcontent) - - [func (Options) NewDescriptorFromPath](#func-options-newdescriptorfrompath) - - [type ParseTarget](#type-parsetarget) - - [type ProtoKind](#type-protokind) - - [type ServiceDescriptor](#type-servicedescriptor) - - [func NewDescritorFromContent](#func-newdescritorfromcontent) - - [func NewDescritorFromPath](#func-newdescritorfrompath) - - [func (\*ServiceDescriptor) LookupMethodByName](#func-servicedescriptor-lookupmethodbyname) - - [func (\*ServiceDescriptor) Methods](#func-servicedescriptor-methods) - - [func (\*ServiceDescriptor) Name](#func-servicedescriptor-name) - - [type Type](#type-type) - - [func FromProtoKindToType](#func-fromprotokindtotype) - - [func (Type) IsComplex](#func-type-iscomplex) - - [func (Type) IsInt](#func-type-isint) - - [func (Type) IsPacked](#func-type-ispacked) - - [func (Type) IsUint](#func-type-isuint) - - [func (Type) NeedVarint](#func-type-needvarint) - - [func (Type) String](#func-type-string) - - [func (Type) TypeToKind](#func-type-typetokind) - - [func (Type) Valid](#func-type-valid) - - [type TypeDescriptor](#type-typedescriptor) - - [func FnRequest](#func-fnrequest) - - [func FnResponse](#func-fnresponse) - - [func (\*TypeDescriptor) BaseId](#func-typedescriptor-baseid) - - [func (\*TypeDescriptor) Elem](#func-typedescriptor-elem) - - [func (\*TypeDescriptor) IsList](#func-typedescriptor-islist) - - [func (\*TypeDescriptor) IsMap](#func-typedescriptor-ismap) - - [func (\*TypeDescriptor) IsPacked](#func-typedescriptor-ispacked) - - [func (\*TypeDescriptor) Key](#func-typedescriptor-key) - - [func (\*TypeDescriptor) Message](#func-typedescriptor-message) - - [func (\*TypeDescriptor) Name](#func-typedescriptor-name) - - [func (\*TypeDescriptor) Type](#func-typedescriptor-type) - - [func (\*TypeDescriptor) WireType](#func-typedescriptor-wiretype) - - [type WireType](#type-wiretype) - - [func (WireType) String](#func-wiretype-string) - - -## Variables - -map from proto.ProtoKind to proto.WireType - -```go -var Kind2Wire = map[ProtoKind]WireType{ - BoolKind: VarintType, - EnumKind: VarintType, - Int32Kind: VarintType, - Sint32Kind: VarintType, - Uint32Kind: VarintType, - Int64Kind: VarintType, - Sint64Kind: VarintType, - Uint64Kind: VarintType, - Sfixed32Kind: Fixed32Type, - Fixed32Kind: Fixed32Type, - FloatKind: Fixed32Type, - Sfixed64Kind: Fixed64Type, - Fixed64Kind: Fixed64Type, - DoubleKind: Fixed64Type, - StringKind: BytesType, - BytesKind: BytesType, - MessageKind: BytesType, - GroupKind: StartGroupType, -} -``` - - -## type [EnumNumber]() - - - -```go -type EnumNumber int32 -``` - - -## type [FieldDescriptor]() - - - -```go -type FieldDescriptor struct { - // contains filtered or unexported fields -} -``` - - -### func (*FieldDescriptor) [IsList]() - -```go -func (f *FieldDescriptor) IsList() bool -``` - - - - -### func (*FieldDescriptor) [IsMap]() - -```go -func (f *FieldDescriptor) IsMap() bool -``` - - - - -### func (*FieldDescriptor) [JSONName]() - -```go -func (f *FieldDescriptor) JSONName() string -``` - - - - -### func (*FieldDescriptor) [Kind]() - -```go -func (f *FieldDescriptor) Kind() ProtoKind -``` - - - - -### func (*FieldDescriptor) [MapKey]() - -```go -func (f *FieldDescriptor) MapKey() *TypeDescriptor -``` - - - - -### func (*FieldDescriptor) [MapValue]() - -```go -func (f *FieldDescriptor) MapValue() *TypeDescriptor -``` - - - - -### func (*FieldDescriptor) [Message]() - -```go -func (f *FieldDescriptor) Message() *MessageDescriptor -``` - -when List+Message it can get message element descriptor when Map it can get map key-value entry massage descriptor when Message it can get sub message descriptor - - -### func (*FieldDescriptor) [Name]() - -```go -func (f *FieldDescriptor) Name() string -``` - - - - -### func (*FieldDescriptor) [Number]() - -```go -func (f *FieldDescriptor) Number() FieldNumber -``` - - - - -### func (*FieldDescriptor) [Type]() - -```go -func (f *FieldDescriptor) Type() *TypeDescriptor -``` - - - - -## type [FieldNameMap]() - -FieldNameMap is a map for field name and field descriptor - -```go -type FieldNameMap struct { - // contains filtered or unexported fields -} -``` - - -### func (FieldNameMap) [All]() - -```go -func (ft FieldNameMap) All() []*FieldDescriptor -``` - -All returns all field descriptors - - -### func (*FieldNameMap) [Build]() - -```go -func (ft *FieldNameMap) Build() -``` - -Build builds the map. It will try to build a trie tree if the dispersion of keys is higher enough (min). - - -### func (FieldNameMap) [Get]() - -```go -func (ft FieldNameMap) Get(k string) *FieldDescriptor -``` - -Get gets the field descriptor for the given key - - -### func (*FieldNameMap) [Set]() - -```go -func (ft *FieldNameMap) Set(key string, field *FieldDescriptor) (exist bool) -``` - -Set sets the field descriptor for the given key - - -### func (FieldNameMap) [Size]() - -```go -func (ft FieldNameMap) Size() int -``` - -Size returns the size of the map - - -## type [FieldNumber]() - - - -```go -type FieldNumber int32 -``` - -reserved field number min-max ranges in a proto message - -```go -const ( - MinValidNumber FieldNumber = 1 - FirstReservedNumber FieldNumber = 19000 - LastReservedNumber FieldNumber = 19999 - MaxValidNumber FieldNumber = 1<<29 - 1 - DefaultRecursionLimit = 10000 -) -``` - - -## type [FieldNumberMap]() - -FieldIDMap is a map from field id to field descriptor - -```go -type FieldNumberMap struct { - // contains filtered or unexported fields -} -``` - - -### func (FieldNumberMap) [All]() - -```go -func (fd FieldNumberMap) All() (ret []*FieldDescriptor) -``` - -All returns all field descriptors - - -### func (FieldNumberMap) [Get]() - -```go -func (fd FieldNumberMap) Get(id FieldNumber) *FieldDescriptor -``` - -Get gets the field descriptor for the given id - - -### func (*FieldNumberMap) [Set]() - -```go -func (fd *FieldNumberMap) Set(id FieldNumber, f *FieldDescriptor) -``` - -Set sets the field descriptor for the given id - - -### func (FieldNumberMap) [Size]() - -```go -func (fd FieldNumberMap) Size() int -``` - -Size returns the size of the map - - -## type [MessageDescriptor]() - - - -```go -type MessageDescriptor struct { - // contains filtered or unexported fields -} -``` - - -### func (*MessageDescriptor) [ByJSONName]() - -```go -func (m *MessageDescriptor) ByJSONName(name string) *FieldDescriptor -``` - - - - -### func (*MessageDescriptor) [ByName]() - -```go -func (m *MessageDescriptor) ByName(name string) *FieldDescriptor -``` - - - - -### func (*MessageDescriptor) [ByNumber]() - -```go -func (m *MessageDescriptor) ByNumber(id FieldNumber) *FieldDescriptor -``` - - - - -### func (*MessageDescriptor) [FieldsCount]() - -```go -func (m *MessageDescriptor) FieldsCount() int -``` - - - - -### func (*MessageDescriptor) [Name]() - -```go -func (m *MessageDescriptor) Name() string -``` - - - - -## type [MethodDescriptor]() - - - -```go -type MethodDescriptor struct { - // contains filtered or unexported fields -} -``` - - -### func [GetFnDescFromFile]() - -```go -func GetFnDescFromFile(filePath, fnName string, opts Options, includeDirs ...string) *MethodDescriptor -``` - -GetFnDescFromFile get a fucntion descriptor from idl path (relative to your git root) and the function name - - -### func (*MethodDescriptor) [Input]() - -```go -func (m *MethodDescriptor) Input() *TypeDescriptor -``` - - - - -### func (*MethodDescriptor) [Name]() - -```go -func (m *MethodDescriptor) Name() string -``` - - - - -### func (*MethodDescriptor) [Output]() - -```go -func (m *MethodDescriptor) Output() *TypeDescriptor -``` - - - - -## type [Number]() - -define Number = protowire.Number (int32) - -```go -type Number = protowire.Number -``` - - -## type [Options]() - -Options is options for parsing thrift IDL. - -```go -type Options struct { - // ParseServiceMode indicates how to parse service. - ParseServiceMode meta.ParseServiceMode - - MapFieldWay meta.MapFieldWay // not implemented. - - ParseFieldRandomRate float64 // not implemented. - - ParseEnumAsInt64 bool // not implemented. - - SetOptionalBitmap bool // not implemented. - - UseDefaultValue bool // not implemented. - - ParseFunctionMode meta.ParseFunctionMode // not implemented. - - EnableProtoBase bool // not implemented. -} -``` - - -### func [NewDefaultOptions]() - -```go -func NewDefaultOptions() Options -``` - -NewDefaultOptions creates a default Options. - - -### func (Options) [NewDesccriptorFromContent]() - -```go -func (opts Options) NewDesccriptorFromContent(ctx context.Context, path, content string, includes map[string]string, importDirs ...string) (*ServiceDescriptor, error) -``` - - - - -### func (Options) [NewDescriptorFromPath]() - -```go -func (opts Options) NewDescriptorFromPath(ctx context.Context, path string, importDirs ...string) (*ServiceDescriptor, error) -``` - -NewDescritorFromContent creates a ServiceDescriptor from a proto path and its imports, which uses the given options. The importDirs is used to find the include files. - - -## type [ParseTarget]() - -ParseTarget indicates the target to parse - -```go -type ParseTarget uint8 -``` - - - -```go -const ( - Request ParseTarget = iota - Response - Exception -) -``` - - -## type [ProtoKind]() - -define ProtoKind = protoreflect.Kind (int8) - -```go -type ProtoKind = protoreflect.Kind -``` - - - -```go -const ( - DoubleKind ProtoKind = iota + 1 - FloatKind - Int64Kind - Uint64Kind - Int32Kind - Fixed64Kind - Fixed32Kind - BoolKind - StringKind - GroupKind - MessageKind - BytesKind - Uint32Kind - EnumKind - Sfixed32Kind - Sfixed64Kind - Sint32Kind - Sint64Kind -) -``` - - -## type [ServiceDescriptor]() - - - -```go -type ServiceDescriptor struct { - // contains filtered or unexported fields -} -``` - - -### func [NewDescritorFromContent]() - -```go -func NewDescritorFromContent(ctx context.Context, path, content string, includes map[string]string, importDirs ...string) (*ServiceDescriptor, error) -``` - -NewDescritorFromContent behaviors like NewDescritorFromPath, besides it uses DefaultOptions. - - -### func [NewDescritorFromPath]() - -```go -func NewDescritorFromPath(ctx context.Context, path string, importDirs ...string) (*ServiceDescriptor, error) -``` - -NewDescritorFromPath behaviors like NewDescritorFromPath, besides it uses DefaultOptions. - - -### func (*ServiceDescriptor) [LookupMethodByName]() - -```go -func (s *ServiceDescriptor) LookupMethodByName(name string) *MethodDescriptor -``` - - - - -### func (*ServiceDescriptor) [Methods]() - -```go -func (s *ServiceDescriptor) Methods() map[string]*MethodDescriptor -``` - - - - -### func (*ServiceDescriptor) [Name]() - -```go -func (s *ServiceDescriptor) Name() string -``` - - - - -## type [Type]() - -Node type (uint8) mapping ProtoKind the same value, except for UNKNOWN, LIST, MAP, ERROR - -```go -type Type uint8 -``` - - - -```go -const ( - UNKNOWN Type = 0 // unknown field type - DOUBLE Type = 1 - FLOAT Type = 2 - INT64 Type = 3 - UINT64 Type = 4 - INT32 Type = 5 - FIX64 Type = 6 - FIX32 Type = 7 - BOOL Type = 8 - STRING Type = 9 - GROUP Type = 10 // deprecated - MESSAGE Type = 11 - BYTE Type = 12 - UINT32 Type = 13 - ENUM Type = 14 - SFIX32 Type = 15 - SFIX64 Type = 16 - SINT32 Type = 17 - SINT64 Type = 18 - LIST Type = 19 - MAP Type = 20 - ERROR Type = 255 -) -``` - - -### func [FromProtoKindToType]() - -```go -func FromProtoKindToType(kind ProtoKind, isList bool, isMap bool) Type -``` - -FromProtoKindTType converts ProtoKind to Type - - -### func (Type) [IsComplex]() - -```go -func (p Type) IsComplex() bool -``` - -IsComplex tells if the type is one of STRUCT, MAP, SET, LIST - - -### func (Type) [IsInt]() - -```go -func (p Type) IsInt() bool -``` - -IsInt containing isUint - - -### func (Type) [IsPacked]() - -```go -func (p Type) IsPacked() bool -``` - - - - -### func (Type) [IsUint]() - -```go -func (p Type) IsUint() bool -``` - - - - -### func (Type) [NeedVarint]() - -```go -func (p Type) NeedVarint() bool -``` - -check if the type need Varint encoding - - -### func (Type) [String]() - -```go -func (p Type) String() string -``` - -String for format and print - - -### func (Type) [TypeToKind]() - -```go -func (p Type) TypeToKind() ProtoKind -``` - - - - -### func (Type) [Valid]() - -```go -func (p Type) Valid() bool -``` - - - - -## type [TypeDescriptor]() - - - -```go -type TypeDescriptor struct { - // contains filtered or unexported fields -} -``` - - -### func [FnRequest]() - -```go -func FnRequest(fn *MethodDescriptor) *TypeDescriptor -``` - -FnRequest get the normal requestDescriptor - - -### func [FnResponse]() - -```go -func FnResponse(fn *MethodDescriptor) *TypeDescriptor -``` - -FnResponse get hte normal responseDescriptor - - -### func (*TypeDescriptor) [BaseId]() - -```go -func (t *TypeDescriptor) BaseId() FieldNumber -``` - - - - -### func (*TypeDescriptor) [Elem]() - -```go -func (t *TypeDescriptor) Elem() *TypeDescriptor -``` - - - - -### func (*TypeDescriptor) [IsList]() - -```go -func (f *TypeDescriptor) IsList() bool -``` - - - - -### func (*TypeDescriptor) [IsMap]() - -```go -func (f *TypeDescriptor) IsMap() bool -``` - - - - -### func (*TypeDescriptor) [IsPacked]() - -```go -func (t *TypeDescriptor) IsPacked() bool -``` - - - - -### func (*TypeDescriptor) [Key]() - -```go -func (t *TypeDescriptor) Key() *TypeDescriptor -``` - - - - -### func (*TypeDescriptor) [Message]() - -```go -func (t *TypeDescriptor) Message() *MessageDescriptor -``` - - - - -### func (*TypeDescriptor) [Name]() - -```go -func (f *TypeDescriptor) Name() string -``` - - - - -### func (*TypeDescriptor) [Type]() - -```go -func (t *TypeDescriptor) Type() Type -``` - - - - -### func (*TypeDescriptor) [WireType]() - -```go -func (f *TypeDescriptor) WireType() WireType -``` - - - - -## type [WireType]() - -protobuf encoding wire type - -```go -type WireType int8 -``` - - - -```go -const ( - VarintType WireType = 0 - Fixed32Type WireType = 5 - Fixed64Type WireType = 1 - BytesType WireType = 2 - StartGroupType WireType = 3 // deprecated - EndGroupType WireType = 4 // deprecated -) -``` - - -### func (WireType) [String]() - -```go -func (p WireType) String() string -``` - - - -Generated by [gomarkdoc]() diff --git a/scripts/go_latest.sh b/scripts/go_latest.sh new file mode 100755 index 00000000..86f3037e --- /dev/null +++ b/scripts/go_latest.sh @@ -0,0 +1,11 @@ +#!/bin/bash +TAG=$1 + +git clone -b release-branch.$TAG https://github.com/golang/go.git $TAG + +cd $TAG/src + +./all.bash + + + diff --git a/testdata/test/go.mod b/testdata/test/go.mod index d225bbc9..f677b08d 100644 --- a/testdata/test/go.mod +++ b/testdata/test/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/apache/thrift v0.20.0 - github.com/bytedance/sonic v1.11.8 + github.com/bytedance/sonic v1.12.0 github.com/cloudwego/dynamicgo v0.2.6 github.com/cloudwego/kitex v0.9.3-rc2 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc @@ -16,7 +16,7 @@ require ( require ( github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect - github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/choleraehyq/pid v0.0.18 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/configmanager v0.2.2 // indirect diff --git a/testdata/test/go.sum b/testdata/test/go.sum index da4eb848..2aec8af1 100644 --- a/testdata/test/go.sum +++ b/testdata/test/go.sum @@ -18,10 +18,11 @@ github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b/go.mod h1:FtQG3YbQ github.com/bytedance/mockey v1.2.7 h1:8j4yCqS5OmMe2dQCxPit4FVkwTK9nrykIgbOZN3s28o= github.com/bytedance/mockey v1.2.7/go.mod h1:bNrUnI1u7+pAc0TYDgPATM+wF2yzHxmNH+iDXg4AOCU= github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= -github.com/bytedance/sonic v1.11.8 h1:Zw/j1KfiS+OYTi9lyB3bb0CFxPJVkM17k1wyDG32LRA= -github.com/bytedance/sonic v1.11.8/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= -github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic v1.12.0 h1:YGPgxF9xzaCNvd/ZKdQ28yRovhfMFZQjuk6fKBzZ3ls= +github.com/bytedance/sonic v1.12.0/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= +github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/choleraehyq/pid v0.0.18 h1:O7LLxPoOyt3YtonlCC8BmNrF9P6Hc8B509UOqlPSVhw= github.com/choleraehyq/pid v0.0.18/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U= diff --git a/thrift/README.md b/thrift/README.md deleted file mode 100644 index 91f2a2d3..00000000 --- a/thrift/README.md +++ /dev/null @@ -1,2092 +0,0 @@ - - -# thrift - -```go -import "github.com/cloudwego/dynamicgo/thrift" -``` - -## Index - -- [Constants](<#constants>) -- [Variables](<#variables>) -- [func FreeBinaryProtocolBuffer(bp *BinaryProtocol)](<#func-freebinaryprotocolbuffer>) -- [func FreeRequiresBitmap(b *RequiresBitmap)](<#func-freerequiresbitmap>) -- [func GetBinaryMessageHeaderAndFooter(methodName string, msgTyp TMessageType, structID FieldID, seqID int32) (header []byte, footer []byte, err error)](<#func-getbinarymessageheaderandfooter>) -- [func RegisterAnnotation(an Annotation, keys ...string)](<#func-registerannotation>) -- [func RegisterAnnotationMapper(scope AnnoScope, mapper AnnotationMapper, keys ...string)](<#func-registerannotationmapper>) -- [func TypeSize(t Type) int](<#func-typesize>) -- [func UnwrapBinaryMessage(proto meta.Encoding, buf []byte) (name string, callType TMessageType, seqID int32, structID FieldID, body []byte, err error)](<#func-unwrapbinarymessage>) -- [func WrapBinaryBody(body []byte, methodName string, msgTyp TMessageType, structID FieldID, seqID int32) ([]byte, error)](<#func-wrapbinarybody>) -- [type AnnoID](<#type-annoid>) - - [func MakeAnnoID(kind AnnoKind, scope AnnoScope, typ AnnoType) AnnoID](<#func-makeannoid>) - - [func (t AnnoID) Kind() AnnoKind](<#func-annoid-kind>) - - [func (t AnnoID) Scope() AnnoScope](<#func-annoid-scope>) - - [func (t AnnoID) Type() AnnoType](<#func-annoid-type>) -- [type AnnoKind](<#type-annokind>) -- [type AnnoScope](<#type-annoscope>) -- [type AnnoType](<#type-annotype>) -- [type Annotation](<#type-annotation>) - - [func FindAnnotation(key string, scope AnnoScope) Annotation](<#func-findannotation>) -- [type AnnotationMapper](<#type-annotationmapper>) - - [func FindAnnotationMapper(key string, scope AnnoScope) AnnotationMapper](<#func-findannotationmapper>) -- [type BinaryEncoding](<#type-binaryencoding>) - - [func (BinaryEncoding) DecodeBool(b []byte) bool](<#func-binaryencoding-decodebool>) - - [func (BinaryEncoding) DecodeByte(b []byte) byte](<#func-binaryencoding-decodebyte>) - - [func (d BinaryEncoding) DecodeBytes(b []byte) (value []byte)](<#func-binaryencoding-decodebytes>) - - [func (BinaryEncoding) DecodeDouble(b []byte) float64](<#func-binaryencoding-decodedouble>) - - [func (BinaryEncoding) DecodeInt16(b []byte) int16](<#func-binaryencoding-decodeint16>) - - [func (BinaryEncoding) DecodeInt32(b []byte) int32](<#func-binaryencoding-decodeint32>) - - [func (BinaryEncoding) DecodeInt64(b []byte) int64](<#func-binaryencoding-decodeint64>) - - [func (d BinaryEncoding) DecodeString(b []byte) (value string)](<#func-binaryencoding-decodestring>) - - [func (BinaryEncoding) EncodeBinary(b []byte, v []byte)](<#func-binaryencoding-encodebinary>) - - [func (BinaryEncoding) EncodeBool(b []byte, v bool)](<#func-binaryencoding-encodebool>) - - [func (BinaryEncoding) EncodeByte(b []byte, v byte)](<#func-binaryencoding-encodebyte>) - - [func (BinaryEncoding) EncodeDouble(b []byte, v float64)](<#func-binaryencoding-encodedouble>) - - [func (BinaryEncoding) EncodeFieldBegin(b []byte, t Type, id FieldID)](<#func-binaryencoding-encodefieldbegin>) - - [func (BinaryEncoding) EncodeInt16(b []byte, v int16)](<#func-binaryencoding-encodeint16>) - - [func (BinaryEncoding) EncodeInt32(b []byte, v int32)](<#func-binaryencoding-encodeint32>) - - [func (BinaryEncoding) EncodeInt64(b []byte, v int64)](<#func-binaryencoding-encodeint64>) - - [func (BinaryEncoding) EncodeString(b []byte, v string)](<#func-binaryencoding-encodestring>) -- [type BinaryProtocol](<#type-binaryprotocol>) - - [func NewBinaryProtocol(buf []byte) *BinaryProtocol](<#func-newbinaryprotocol>) - - [func NewBinaryProtocolBuffer() *BinaryProtocol](<#func-newbinaryprotocolbuffer>) - - [func (p *BinaryProtocol) DecodeText(val string, desc *TypeDescriptor, disallowUnknown bool, base64Binary bool, useFieldName bool, asJson bool) error](<#func-binaryprotocol-decodetext>) - - [func (p *BinaryProtocol) EncodeText(desc *TypeDescriptor, buf *[]byte, byteAsUint8 bool, disallowUnknown bool, base64Binary bool, useFieldName bool, asJson bool) error](<#func-binaryprotocol-encodetext>) - - [func (p BinaryProtocol) Left() int](<#func-binaryprotocol-left>) - - [func (p *BinaryProtocol) ModifyI32(pos int, value int32) error](<#func-binaryprotocol-modifyi32>) - - [func (p BinaryProtocol) RawBuf() []byte](<#func-binaryprotocol-rawbuf>) - - [func (p *BinaryProtocol) ReadAnyWithDesc(desc *TypeDescriptor, copyString bool, disallowUnknonw bool, useFieldName bool) (interface{}, error)](<#func-binaryprotocol-readanywithdesc>) - - [func (p *BinaryProtocol) ReadBinary(copyBytes bool) (value []byte, err error)](<#func-binaryprotocol-readbinary>) - - [func (p *BinaryProtocol) ReadBool() (bool, error)](<#func-binaryprotocol-readbool>) - - [func (p *BinaryProtocol) ReadByte() (value byte, err error)](<#func-binaryprotocol-readbyte>) - - [func (p *BinaryProtocol) ReadDouble() (value float64, err error)](<#func-binaryprotocol-readdouble>) - - [func (p *BinaryProtocol) ReadFieldBegin() (name string, typeID Type, id FieldID, err error)](<#func-binaryprotocol-readfieldbegin>) - - [func (p *BinaryProtocol) ReadFieldEnd() error](<#func-binaryprotocol-readfieldend>) - - [func (p *BinaryProtocol) ReadI16() (value int16, err error)](<#func-binaryprotocol-readi16>) - - [func (p *BinaryProtocol) ReadI32() (value int32, err error)](<#func-binaryprotocol-readi32>) - - [func (p *BinaryProtocol) ReadI64() (value int64, err error)](<#func-binaryprotocol-readi64>) - - [func (p *BinaryProtocol) ReadInt(t Type) (value int, err error)](<#func-binaryprotocol-readint>) - - [func (p *BinaryProtocol) ReadListBegin() (elemType Type, size int, err error)](<#func-binaryprotocol-readlistbegin>) - - [func (p *BinaryProtocol) ReadListEnd() error](<#func-binaryprotocol-readlistend>) - - [func (p *BinaryProtocol) ReadMapBegin() (kType, vType Type, size int, err error)](<#func-binaryprotocol-readmapbegin>) - - [func (p *BinaryProtocol) ReadMapEnd() error](<#func-binaryprotocol-readmapend>) - - [func (p *BinaryProtocol) ReadMessageBegin(copyString bool) (name string, typeID TMessageType, seqID int32, err error)](<#func-binaryprotocol-readmessagebegin>) - - [func (p *BinaryProtocol) ReadMessageEnd() error](<#func-binaryprotocol-readmessageend>) - - [func (p *BinaryProtocol) ReadSetBegin() (elemType Type, size int, err error)](<#func-binaryprotocol-readsetbegin>) - - [func (p *BinaryProtocol) ReadSetEnd() error](<#func-binaryprotocol-readsetend>) - - [func (p *BinaryProtocol) ReadString(copy bool) (value string, err error)](<#func-binaryprotocol-readstring>) - - [func (p *BinaryProtocol) ReadStringWithDesc(desc *TypeDescriptor, buf *[]byte, byteAsUint8 bool, disallowUnknown bool, base64Binary bool) error](<#func-binaryprotocol-readstringwithdesc>) - - [func (p *BinaryProtocol) ReadStructBegin() (name string, err error)](<#func-binaryprotocol-readstructbegin>) - - [func (p *BinaryProtocol) ReadStructEnd() error](<#func-binaryprotocol-readstructend>) - - [func (p *BinaryProtocol) Recycle()](<#func-binaryprotocol-recycle>) - - [func (p *BinaryProtocol) Reset()](<#func-binaryprotocol-reset>) - - [func (p *BinaryProtocol) Skip(fieldType Type, useNative bool) (err error)](<#func-binaryprotocol-skip>) - - [func (p *BinaryProtocol) SkipGo(fieldType Type, maxDepth int) (err error)](<#func-binaryprotocol-skipgo>) - - [func (p *BinaryProtocol) SkipNative(fieldType Type, maxDepth int) (err error)](<#func-binaryprotocol-skipnative>) - - [func (p BinaryProtocol) UnwrapBody() (string, TMessageType, int32, FieldID, []byte, error)](<#func-binaryprotocol-unwrapbody>) - - [func (p *BinaryProtocol) WriteAnyWithDesc(desc *TypeDescriptor, val interface{}, cast bool, disallowUnknown bool, useFieldName bool) error](<#func-binaryprotocol-writeanywithdesc>) - - [func (p *BinaryProtocol) WriteBinary(value []byte) error](<#func-binaryprotocol-writebinary>) - - [func (p *BinaryProtocol) WriteBool(value bool) error](<#func-binaryprotocol-writebool>) - - [func (p *BinaryProtocol) WriteByte(value byte) error](<#func-binaryprotocol-writebyte>) - - [func (p *BinaryProtocol) WriteDefaultOrEmpty(field *FieldDescriptor) error](<#func-binaryprotocol-writedefaultorempty>) - - [func (p *BinaryProtocol) WriteDouble(value float64) error](<#func-binaryprotocol-writedouble>) - - [func (p *BinaryProtocol) WriteEmpty(desc *TypeDescriptor) error](<#func-binaryprotocol-writeempty>) - - [func (p *BinaryProtocol) WriteFieldBegin(name string, typeID Type, id FieldID) error](<#func-binaryprotocol-writefieldbegin>) - - [func (p *BinaryProtocol) WriteFieldEnd() error](<#func-binaryprotocol-writefieldend>) - - [func (p *BinaryProtocol) WriteFieldStop() error](<#func-binaryprotocol-writefieldstop>) - - [func (p *BinaryProtocol) WriteI16(value int16) error](<#func-binaryprotocol-writei16>) - - [func (p *BinaryProtocol) WriteI32(value int32) error](<#func-binaryprotocol-writei32>) - - [func (p *BinaryProtocol) WriteI64(value int64) error](<#func-binaryprotocol-writei64>) - - [func (p *BinaryProtocol) WriteInt(t Type, value int) error](<#func-binaryprotocol-writeint>) - - [func (p *BinaryProtocol) WriteListBegin(elemType Type, size int) error](<#func-binaryprotocol-writelistbegin>) - - [func (p *BinaryProtocol) WriteListBeginWithSizePos(elemType Type, size int) (int, error)](<#func-binaryprotocol-writelistbeginwithsizepos>) - - [func (p *BinaryProtocol) WriteListEnd() error](<#func-binaryprotocol-writelistend>) - - [func (p *BinaryProtocol) WriteMapBegin(keyType, valueType Type, size int) error](<#func-binaryprotocol-writemapbegin>) - - [func (p *BinaryProtocol) WriteMapBeginWithSizePos(keyType, valueType Type, size int) (int, error)](<#func-binaryprotocol-writemapbeginwithsizepos>) - - [func (p *BinaryProtocol) WriteMapEnd() error](<#func-binaryprotocol-writemapend>) - - [func (p *BinaryProtocol) WriteMessageBegin(name string, typeID TMessageType, seqID int32) error](<#func-binaryprotocol-writemessagebegin>) - - [func (p *BinaryProtocol) WriteMessageEnd() error](<#func-binaryprotocol-writemessageend>) - - [func (p *BinaryProtocol) WriteSetBegin(elemType Type, size int) error](<#func-binaryprotocol-writesetbegin>) - - [func (p *BinaryProtocol) WriteSetEnd() error](<#func-binaryprotocol-writesetend>) - - [func (p *BinaryProtocol) WriteString(value string) error](<#func-binaryprotocol-writestring>) - - [func (p *BinaryProtocol) WriteStringWithDesc(val string, desc *TypeDescriptor, disallowUnknown bool, base64Binary bool) error](<#func-binaryprotocol-writestringwithdesc>) - - [func (p *BinaryProtocol) WriteStructBegin(name string) error](<#func-binaryprotocol-writestructbegin>) - - [func (p *BinaryProtocol) WriteStructEnd() error](<#func-binaryprotocol-writestructend>) -- [type DefaultValue](<#type-defaultvalue>) - - [func (d DefaultValue) GoValue() interface{}](<#func-defaultvalue-govalue>) - - [func (d DefaultValue) JSONValue() string](<#func-defaultvalue-jsonvalue>) - - [func (d DefaultValue) ThriftBinary() string](<#func-defaultvalue-thriftbinary>) -- [type FieldDescriptor](<#type-fielddescriptor>) - - [func (f FieldDescriptor) Alias() string](<#func-fielddescriptor-alias>) - - [func (f FieldDescriptor) DefaultValue() *DefaultValue](<#func-fielddescriptor-defaultvalue>) - - [func (f FieldDescriptor) HTTPMappings() []HttpMapping](<#func-fielddescriptor-httpmappings>) - - [func (f FieldDescriptor) ID() FieldID](<#func-fielddescriptor-id>) - - [func (f FieldDescriptor) IsRequestBase() bool](<#func-fielddescriptor-isrequestbase>) - - [func (f FieldDescriptor) IsResponseBase() bool](<#func-fielddescriptor-isresponsebase>) - - [func (f FieldDescriptor) Name() string](<#func-fielddescriptor-name>) - - [func (f FieldDescriptor) Required() Requireness](<#func-fielddescriptor-required>) - - [func (f FieldDescriptor) Type() *TypeDescriptor](<#func-fielddescriptor-type>) - - [func (f FieldDescriptor) ValueMapping() ValueMapping](<#func-fielddescriptor-valuemapping>) - - [func (f FieldDescriptor) ValueMappingType() AnnoType](<#func-fielddescriptor-valuemappingtype>) -- [type FieldID](<#type-fieldid>) -- [type FieldIDMap](<#type-fieldidmap>) - - [func (fd FieldIDMap) All() (ret []*FieldDescriptor)](<#func-fieldidmap-all>) - - [func (fd FieldIDMap) Get(id FieldID) *FieldDescriptor](<#func-fieldidmap-get>) - - [func (fd *FieldIDMap) Set(id FieldID, f *FieldDescriptor)](<#func-fieldidmap-set>) - - [func (fd FieldIDMap) Size() int](<#func-fieldidmap-size>) -- [type FieldNameMap](<#type-fieldnamemap>) - - [func (ft FieldNameMap) All() []*FieldDescriptor](<#func-fieldnamemap-all>) - - [func (ft *FieldNameMap) Build()](<#func-fieldnamemap-build>) - - [func (ft FieldNameMap) Get(k string) *FieldDescriptor](<#func-fieldnamemap-get>) - - [func (ft *FieldNameMap) Set(key string, field *FieldDescriptor) (exist bool)](<#func-fieldnamemap-set>) - - [func (ft FieldNameMap) Size() int](<#func-fieldnamemap-size>) -- [type FunctionDescriptor](<#type-functiondescriptor>) - - [func GetFnDescFromFile(filePath, fnName string, opts Options) *FunctionDescriptor](<#func-getfndescfromfile>) - - [func (f FunctionDescriptor) Annotations() map[string][]string](<#func-functiondescriptor-annotations>) - - [func (f FunctionDescriptor) Endpoints() []http.Endpoint](<#func-functiondescriptor-endpoints>) - - [func (f FunctionDescriptor) HasRequestBase() bool](<#func-functiondescriptor-hasrequestbase>) - - [func (f FunctionDescriptor) Name() string](<#func-functiondescriptor-name>) - - [func (f FunctionDescriptor) Oneway() bool](<#func-functiondescriptor-oneway>) - - [func (f FunctionDescriptor) Request() *TypeDescriptor](<#func-functiondescriptor-request>) - - [func (f FunctionDescriptor) Response() *TypeDescriptor](<#func-functiondescriptor-response>) -- [type HttpMapping](<#type-httpmapping>) -- [type KeyMapping](<#type-keymapping>) -- [type OptionMapping](<#type-optionmapping>) -- [type Options](<#type-options>) - - [func NewDefaultOptions() Options](<#func-newdefaultoptions>) - - [func (opts Options) NewDescriptorFromContentWithMethod(ctx context.Context, path, content string, includes map[string]string, isAbsIncludePath bool, methods ...string) (*ServiceDescriptor, error)](<#func-options-newdescriptorfromcontentwithmethod>) - - [func (opts Options) NewDescriptorFromPathWithMethod(ctx context.Context, path string, includeDirs []string, methods ...string) (*ServiceDescriptor, error)](<#func-options-newdescriptorfrompathwithmethod>) - - [func (opts Options) NewDescritorFromContent(ctx context.Context, path, content string, includes map[string]string, isAbsIncludePath bool) (*ServiceDescriptor, error)](<#func-options-newdescritorfromcontent>) - - [func (opts Options) NewDescritorFromPath(ctx context.Context, path string, includeDirs ...string) (*ServiceDescriptor, error)](<#func-options-newdescritorfrompath>) -- [type ParseTarget](<#type-parsetarget>) -- [type Requireness](<#type-requireness>) -- [type RequiresBitmap](<#type-requiresbitmap>) - - [func NewRequiresBitmap() *RequiresBitmap](<#func-newrequiresbitmap>) - - [func (b RequiresBitmap) CheckRequires(desc *StructDescriptor, writeDefault bool, handler func(field *FieldDescriptor) error) error](<#func-requiresbitmap-checkrequires>) - - [func (b RequiresBitmap) CopyTo(to *RequiresBitmap)](<#func-requiresbitmap-copyto>) - - [func (b RequiresBitmap) HandleRequires(desc *StructDescriptor, writeRquired bool, writeDefault bool, writeOptional bool, handler func(field *FieldDescriptor) error) error](<#func-requiresbitmap-handlerequires>) - - [func (b RequiresBitmap) IsSet(id FieldID) bool](<#func-requiresbitmap-isset>) - - [func (b *RequiresBitmap) Set(id FieldID, val Requireness)](<#func-requiresbitmap-set>) -- [type ServiceDescriptor](<#type-servicedescriptor>) - - [func NewDescritorFromContent(ctx context.Context, path, content string, includes map[string]string, isAbsIncludePath bool) (*ServiceDescriptor, error)](<#func-newdescritorfromcontent>) - - [func NewDescritorFromPath(ctx context.Context, path string, includeDirs ...string) (*ServiceDescriptor, error)](<#func-newdescritorfrompath>) - - [func (s ServiceDescriptor) Annotations() map[string][]string](<#func-servicedescriptor-annotations>) - - [func (s ServiceDescriptor) Functions() map[string]*FunctionDescriptor](<#func-servicedescriptor-functions>) - - [func (s *ServiceDescriptor) LookupFunctionByMethod(method string) (*FunctionDescriptor, error)](<#func-servicedescriptor-lookupfunctionbymethod>) - - [func (s ServiceDescriptor) Name() string](<#func-servicedescriptor-name>) -- [type StructDescriptor](<#type-structdescriptor>) - - [func (s StructDescriptor) FieldById(id FieldID) *FieldDescriptor](<#func-structdescriptor-fieldbyid>) - - [func (s StructDescriptor) FieldByKey(k string) (field *FieldDescriptor)](<#func-structdescriptor-fieldbykey>) - - [func (s StructDescriptor) Fields() []*FieldDescriptor](<#func-structdescriptor-fields>) - - [func (s StructDescriptor) GetRequestBase() *FieldDescriptor](<#func-structdescriptor-getrequestbase>) - - [func (s StructDescriptor) GetResponseBase() *FieldDescriptor](<#func-structdescriptor-getresponsebase>) - - [func (s StructDescriptor) HttpMappingFields() []*FieldDescriptor](<#func-structdescriptor-httpmappingfields>) - - [func (s StructDescriptor) Len() int](<#func-structdescriptor-len>) - - [func (s StructDescriptor) Name() string](<#func-structdescriptor-name>) - - [func (s StructDescriptor) Requires() RequiresBitmap](<#func-structdescriptor-requires>) -- [type TMessageType](<#type-tmessagetype>) -- [type Type](<#type-type>) - - [func FromThriftTType(t thrift.TType) Type](<#func-fromthriftttype>) - - [func (p Type) IsComplex() bool](<#func-type-iscomplex>) - - [func (p Type) IsInt() bool](<#func-type-isint>) - - [func (p Type) String() string](<#func-type-string>) - - [func (p Type) ToThriftTType() thrift.TType](<#func-type-tothriftttype>) - - [func (p Type) Valid() bool](<#func-type-valid>) -- [type TypeDescriptor](<#type-typedescriptor>) - - [func FnRequest(fn *FunctionDescriptor) *TypeDescriptor](<#func-fnrequest>) - - [func FnResponse(fn *FunctionDescriptor) *TypeDescriptor](<#func-fnresponse>) - - [func (d TypeDescriptor) Elem() *TypeDescriptor](<#func-typedescriptor-elem>) - - [func (d TypeDescriptor) IsBinary() bool](<#func-typedescriptor-isbinary>) - - [func (d TypeDescriptor) Key() *TypeDescriptor](<#func-typedescriptor-key>) - - [func (d TypeDescriptor) Name() string](<#func-typedescriptor-name>) - - [func (d TypeDescriptor) Struct() *StructDescriptor](<#func-typedescriptor-struct>) - - [func (d TypeDescriptor) Type() Type](<#func-typedescriptor-type>) -- [type ValueMapping](<#type-valuemapping>) - - -## Constants - -```go -const ( - // AnnoKeyDynamicGoDeprecated is used to mark a description as deprecated - AnnoKeyDynamicGoDeprecated = "dynamicgo.deprecated" - // AnnoKeyDynamicGoApiNone is used to deal with http response field with api.none annotation - AnnoKeyDynamicGoApiNone = "api.none" -) -``` - -```go -const ( - VERSION_MASK = 0xffff0000 - VERSION_1 = 0x80010000 -) -``` - -```go -const MaxSkipDepth = types.TB_SKIP_STACK_SIZE - 1 -``` - -## Variables - -```go -var ( - CtxKeyIsBodyRoot = &ctxIsBodyRoot -) -``` - -## func FreeBinaryProtocolBuffer - -```go -func FreeBinaryProtocolBuffer(bp *BinaryProtocol) -``` - -FreeBinaryProtocol resets the buffer and puts the binary protocol back to sync.Pool - -## func FreeRequiresBitmap - -```go -func FreeRequiresBitmap(b *RequiresBitmap) -``` - -FreeRequiresBitmap free the bitmap, but not clear its memory - -## func GetBinaryMessageHeaderAndFooter - -```go -func GetBinaryMessageHeaderAndFooter(methodName string, msgTyp TMessageType, structID FieldID, seqID int32) (header []byte, footer []byte, err error) -``` - -GetBinaryMessageHeaderAndFooter writes the message parameters into header and footer - -## func RegisterAnnotation - -```go -func RegisterAnnotation(an Annotation, keys ...string) -``` - -RegisterAnnotation register an annotation on specific AnnoScope - -## func RegisterAnnotationMapper - -```go -func RegisterAnnotationMapper(scope AnnoScope, mapper AnnotationMapper, keys ...string) -``` - -RegisterAnnotationMapper register a annotation mapper on specific scope - -## func TypeSize - -```go -func TypeSize(t Type) int -``` - -TypeSize returns the size of the given type. \-1 means variable size \(LIST, SET, MAP, STRING\) 0 means unknown type - -## func UnwrapBinaryMessage - -```go -func UnwrapBinaryMessage(proto meta.Encoding, buf []byte) (name string, callType TMessageType, seqID int32, structID FieldID, body []byte, err error) -``` - -UnwrapBinaryMessage unwraps the message parameters from the buf - -## func WrapBinaryBody - -```go -func WrapBinaryBody(body []byte, methodName string, msgTyp TMessageType, structID FieldID, seqID int32) ([]byte, error) -``` - -WrapBinaryMessage wraps the message with header and footer and body - -## type AnnoID - -AnnoID is the unique id of an annotation, which is composed of kind, scope and type: - -``` -0xff000000: AnnoKind -0x00ff0000: AnnoScope -0x0000ffff: AnnoType -``` - -```go -type AnnoID uint32 -``` - -### func MakeAnnoID - -```go -func MakeAnnoID(kind AnnoKind, scope AnnoScope, typ AnnoType) AnnoID -``` - -### func \(AnnoID\) Kind - -```go -func (t AnnoID) Kind() AnnoKind -``` - -Kind returns the kind of the annotation - -### func \(AnnoID\) Scope - -```go -func (t AnnoID) Scope() AnnoScope -``` - -Scope returns the scope of the annotation - -### func \(AnnoID\) Type - -```go -func (t AnnoID) Type() AnnoType -``` - -Type returns the type of the annotation - -## type AnnoKind - -AnnoKind is the kind of annotation, which defines the result handler of Annotation.Make\(\) - -```go -type AnnoKind uint8 -``` - -```go -const ( - // AnnoKindHttpMappping is the kind of http mapping annotation - // These annotations Make() will return HTTPMapping - AnnoKindHttpMappping AnnoKind = iota + 1 - - // AnnotationKindKeyMapping is the kind of key mapping annotation - // These annotations Make() will return KeyMapping - AnnoKindValueMapping - - // AnnotationKindValueMapping is the kind of value mapping annotation - // These annotations Make() will return ValueMapping - AnnoKindOptionMapping - - // AnnotationKindOptionMapping is the kind of option mapping annotation - // These annotations Make() will return OptionMapping - AnnoKindKeyMapping -) -``` - -## type AnnoScope - -AnnoScope is effective scope of annotation - -```go -type AnnoScope uint8 -``` - -```go -const ( - // AnnoScopeService works on service description - AnnoScopeService AnnoScope = iota + 1 - - // AnnoScopeFunction works on function description - AnnoScopeFunction - - // AnnoScopeStruct works on struct description - AnnoScopeStruct - - // AnnoScopeField works on field description - AnnoScopeField -) -``` - -## type AnnoType - -AnnoType is the specific type of an annotation - -```go -type AnnoType uint16 -``` - -## type Annotation - -Annotation idl annotation interface - -```go -type Annotation interface { - // unique id of the Annotation - ID() AnnoID - - // Make makes the handler function under specific values and idl description - // - // desc is specific type to its registered AnnoScope: - // AnnoScopeService: desc is *parser.Service - // AnnoScopeFunction: desc is *parser.Function - // AnnoScopeStruct: desc is *parser.StructLike - // AnnoScopeField: desc is *parser.Field - // - // returned handler SHOULD BE one interface according to its AnnoKind: - // AnnoKindHttpMappping: HTTPMapping interface - // AnnoKindKeyMapping: KeyMapping interface - // AnnoKindKeyMapping: ValueMapping interface - // AnnoKindOptionMapping: OptionMapping interface - Make(ctx context.Context, values []parser.Annotation, desc interface{}) (handler interface{}, err error) -} -``` - -### func FindAnnotation - -```go -func FindAnnotation(key string, scope AnnoScope) Annotation -``` - -## type AnnotationMapper - -AnnotationMapper is used to convert a annotation to equivalent annotations desc is specific to its registered AnnoScope: AnnoScopeService: desc is \*parser.Service AnnoScopeFunction: desc is \*parser.Function AnnoScopeStruct: desc is \*parser.StructLike AnnoScopeField: desc is \*parser.Field - -```go -type AnnotationMapper interface { - // Map map a annotation to equivalent annotations - Map(ctx context.Context, ann []parser.Annotation, desc interface{}, opt Options) (cur []parser.Annotation, next []parser.Annotation, err error) -} -``` - -### func FindAnnotationMapper - -```go -func FindAnnotationMapper(key string, scope AnnoScope) AnnotationMapper -``` - -## type BinaryEncoding - -BinaryEncoding is the implementation of Encoding for binary encoding. - -```go -type BinaryEncoding struct{} -``` - -### func \(BinaryEncoding\) DecodeBool - -```go -func (BinaryEncoding) DecodeBool(b []byte) bool -``` - -EncodeFieldEnd encodes a field end. - -### func \(BinaryEncoding\) DecodeByte - -```go -func (BinaryEncoding) DecodeByte(b []byte) byte -``` - -DecodeByte decodes a byte value. - -### func \(BinaryEncoding\) DecodeBytes - -```go -func (d BinaryEncoding) DecodeBytes(b []byte) (value []byte) -``` - -DecodeBinary decodes a binary value. - -### func \(BinaryEncoding\) DecodeDouble - -```go -func (BinaryEncoding) DecodeDouble(b []byte) float64 -``` - -DecodeDouble decodes a double value. - -### func \(BinaryEncoding\) DecodeInt16 - -```go -func (BinaryEncoding) DecodeInt16(b []byte) int16 -``` - -DecodeInt16 decodes a int16 value. - -### func \(BinaryEncoding\) DecodeInt32 - -```go -func (BinaryEncoding) DecodeInt32(b []byte) int32 -``` - -DecodeInt32 decodes a int32 value. - -### func \(BinaryEncoding\) DecodeInt64 - -```go -func (BinaryEncoding) DecodeInt64(b []byte) int64 -``` - -DecodeInt64 decodes a int64 value. - -### func \(BinaryEncoding\) DecodeString - -```go -func (d BinaryEncoding) DecodeString(b []byte) (value string) -``` - -DecodeString decodes a string value. - -### func \(BinaryEncoding\) EncodeBinary - -```go -func (BinaryEncoding) EncodeBinary(b []byte, v []byte) -``` - -EncodeBinary encodes a binary value. - -### func \(BinaryEncoding\) EncodeBool - -```go -func (BinaryEncoding) EncodeBool(b []byte, v bool) -``` - -EncodeBool encodes a bool value. - -### func \(BinaryEncoding\) EncodeByte - -```go -func (BinaryEncoding) EncodeByte(b []byte, v byte) -``` - -EncodeByte encodes a byte value. - -### func \(BinaryEncoding\) EncodeDouble - -```go -func (BinaryEncoding) EncodeDouble(b []byte, v float64) -``` - -### func \(BinaryEncoding\) EncodeFieldBegin - -```go -func (BinaryEncoding) EncodeFieldBegin(b []byte, t Type, id FieldID) -``` - -EncodeFieldBegin encodes a field begin. - -### func \(BinaryEncoding\) EncodeInt16 - -```go -func (BinaryEncoding) EncodeInt16(b []byte, v int16) -``` - -EncodeInt16 encodes a int16 value. - -### func \(BinaryEncoding\) EncodeInt32 - -```go -func (BinaryEncoding) EncodeInt32(b []byte, v int32) -``` - -EncodeInt32 encodes a int32 value. - -### func \(BinaryEncoding\) EncodeInt64 - -```go -func (BinaryEncoding) EncodeInt64(b []byte, v int64) -``` - -EncodeInt64 encodes a int64 value. - -### func \(BinaryEncoding\) EncodeString - -```go -func (BinaryEncoding) EncodeString(b []byte, v string) -``` - -EncodeString encodes a string value. - -## type BinaryProtocol - -BinaryProtocol implements the BinaryProtocol see https://github.com/apache/thrift/blob/master/doc/specs/thrift-binary-protocol.md - -```go -type BinaryProtocol struct { - Buf []byte - Read int -} -``` - -### func NewBinaryProtocol - -```go -func NewBinaryProtocol(buf []byte) *BinaryProtocol -``` - -NewBinaryProtocol get a new binary protocol from sync.Pool. - -### func NewBinaryProtocolBuffer - -```go -func NewBinaryProtocolBuffer() *BinaryProtocol -``` - -NewBinaryProtocolBuffer gets a new binary protocol from sync.Pool and reuse the buffer in pool - -### func \(\*BinaryProtocol\) DecodeText - -```go -func (p *BinaryProtocol) DecodeText(val string, desc *TypeDescriptor, disallowUnknown bool, base64Binary bool, useFieldName bool, asJson bool) error -``` - -DecodeText decode special text\-encoded val with desc and write it into buffer The encoding of val should be compatible with \`EncodeText\(\)\` WARNING: this function is not fully implemented, only support json\-encoded string for LIST/MAP/SET/STRUCT - -### func \(\*BinaryProtocol\) EncodeText - -```go -func (p *BinaryProtocol) EncodeText(desc *TypeDescriptor, buf *[]byte, byteAsUint8 bool, disallowUnknown bool, base64Binary bool, useFieldName bool, asJson bool) error -``` - -EncodeText reads thrift data with descriptor, and converts it to a specail text\-protocol string: This protocol is similar to JSON, excepts its key \(or field id\) IS NOT QUOTED unless it is a string type: \- LIST/SET's all elements will be joined with ',', and if asJson is true the entiry value will be wrapped by '\[' \(start\) and '\]' \(end\). \- MAP's each pair of key and value will be binded with ':', all elements will be joined with ',', and if asJson is true the entiry value will be wrapped by '\{' \(start\) and '\}' \(end\). \- STRUCT's each pair of field \(name or id\) and value will be binded with ':', all elements will be joined with ',', and if asJson is true the entiry value will be wrapped by '\{' \(start\) and '\}' \(end\). \- STRING \(including key\) will be wrapped by '"' if asJson is true. - -### func \(BinaryProtocol\) Left - -```go -func (p BinaryProtocol) Left() int -``` - -Left returns the left bytes to read - -### func \(\*BinaryProtocol\) ModifyI32 - -```go -func (p *BinaryProtocol) ModifyI32(pos int, value int32) error -``` - -ModifyI16 write int32 into the buffer at the given position - -### func \(BinaryProtocol\) RawBuf - -```go -func (p BinaryProtocol) RawBuf() []byte -``` - -RawBuf returns the raw buffer of the protocol - -### func \(\*BinaryProtocol\) ReadAnyWithDesc - -```go -func (p *BinaryProtocol) ReadAnyWithDesc(desc *TypeDescriptor, copyString bool, disallowUnknonw bool, useFieldName bool) (interface{}, error) -``` - -ReadAnyWithDesc explains thrift data with descriptor and converts it to go interface\{\} \- LIST/SET will be converted to \[\]interface\{\} \- MAP will be converted to map\[string\]interface\{\} or map\[int\]interface\{\} or map\[interface\{\}\]interface \(depends on its key type\) \- STRUCT will be converted to map\[FieldID\]interface\{\} - -
Example -

- -```go -{ - p1, err := NewDescritorFromPath(context.Background(), "../testdata/idl/example2.thrift") - if err != nil { - panic(err) - } - example2ReqDesc := p1.Functions()["ExampleMethod"].Request().Struct().FieldById(1).Type() - data, err := ioutil.ReadFile("../testdata/data/example2.bin") - if err != nil { - panic(err) - } - - p := NewBinaryProtocol(data) - v, err := p.ReadAnyWithDesc(example2ReqDesc, false, false, true) - if err != nil { - panic(err) - } - fmt.Printf("%#v", v) - p = NewBinaryProtocolBuffer() - err = p.WriteAnyWithDesc(example2ReqDesc, v, true, true, true) - if err != nil { - panic(err) - } - fmt.Printf("%x", p.RawBuf()) -} -``` - -

-
- -### func \(\*BinaryProtocol\) ReadBinary - -```go -func (p *BinaryProtocol) ReadBinary(copyBytes bool) (value []byte, err error) -``` - -ReadBinary ... - -### func \(\*BinaryProtocol\) ReadBool - -```go -func (p *BinaryProtocol) ReadBool() (bool, error) -``` - -ReadBool ... - -### func \(\*BinaryProtocol\) ReadByte - -```go -func (p *BinaryProtocol) ReadByte() (value byte, err error) -``` - -ReadByte ... - -### func \(\*BinaryProtocol\) ReadDouble - -```go -func (p *BinaryProtocol) ReadDouble() (value float64, err error) -``` - -ReadDouble ... - -### func \(\*BinaryProtocol\) ReadFieldBegin - -```go -func (p *BinaryProtocol) ReadFieldBegin() (name string, typeID Type, id FieldID, err error) -``` - -ReadFieldBegin ... - -### func \(\*BinaryProtocol\) ReadFieldEnd - -```go -func (p *BinaryProtocol) ReadFieldEnd() error -``` - -ReadFieldEnd ... - -### func \(\*BinaryProtocol\) ReadI16 - -```go -func (p *BinaryProtocol) ReadI16() (value int16, err error) -``` - -ReadI16 ... - -### func \(\*BinaryProtocol\) ReadI32 - -```go -func (p *BinaryProtocol) ReadI32() (value int32, err error) -``` - -ReadI32 ... - -### func \(\*BinaryProtocol\) ReadI64 - -```go -func (p *BinaryProtocol) ReadI64() (value int64, err error) -``` - -ReadI64 ... - -### func \(\*BinaryProtocol\) ReadInt - -```go -func (p *BinaryProtocol) ReadInt(t Type) (value int, err error) -``` - -ReadInt ... - -### func \(\*BinaryProtocol\) ReadListBegin - -```go -func (p *BinaryProtocol) ReadListBegin() (elemType Type, size int, err error) -``` - -ReadListBegin ... - -### func \(\*BinaryProtocol\) ReadListEnd - -```go -func (p *BinaryProtocol) ReadListEnd() error -``` - -ReadListEnd ... - -### func \(\*BinaryProtocol\) ReadMapBegin - -```go -func (p *BinaryProtocol) ReadMapBegin() (kType, vType Type, size int, err error) -``` - -ReadMapBegin ... - -### func \(\*BinaryProtocol\) ReadMapEnd - -```go -func (p *BinaryProtocol) ReadMapEnd() error -``` - -ReadMapEnd ... - -### func \(\*BinaryProtocol\) ReadMessageBegin - -```go -func (p *BinaryProtocol) ReadMessageBegin(copyString bool) (name string, typeID TMessageType, seqID int32, err error) -``` - -ReadMessageBegin ... - -### func \(\*BinaryProtocol\) ReadMessageEnd - -```go -func (p *BinaryProtocol) ReadMessageEnd() error -``` - -ReadMessageEnd ... - -### func \(\*BinaryProtocol\) ReadSetBegin - -```go -func (p *BinaryProtocol) ReadSetBegin() (elemType Type, size int, err error) -``` - -ReadSetBegin ... - -### func \(\*BinaryProtocol\) ReadSetEnd - -```go -func (p *BinaryProtocol) ReadSetEnd() error -``` - -ReadSetEnd ... - -### func \(\*BinaryProtocol\) ReadString - -```go -func (p *BinaryProtocol) ReadString(copy bool) (value string, err error) -``` - -ReadString ... - -### func \(\*BinaryProtocol\) ReadStringWithDesc - -```go -func (p *BinaryProtocol) ReadStringWithDesc(desc *TypeDescriptor, buf *[]byte, byteAsUint8 bool, disallowUnknown bool, base64Binary bool) error -``` - -ReadStringWithDesc explains thrift data with desc and converts to simple string - -### func \(\*BinaryProtocol\) ReadStructBegin - -```go -func (p *BinaryProtocol) ReadStructBegin() (name string, err error) -``` - -ReadStructBegin ... - -### func \(\*BinaryProtocol\) ReadStructEnd - -```go -func (p *BinaryProtocol) ReadStructEnd() error -``` - -ReadStructEnd ... - -### func \(\*BinaryProtocol\) Recycle - -```go -func (p *BinaryProtocol) Recycle() -``` - -Recycle put the protocol back to sync.Pool - -### func \(\*BinaryProtocol\) Reset - -```go -func (p *BinaryProtocol) Reset() -``` - -Reset resets the buffer and read position - -### func \(\*BinaryProtocol\) Skip - -```go -func (p *BinaryProtocol) Skip(fieldType Type, useNative bool) (err error) -``` - -Skip skips over the value for the given type. - -### func \(\*BinaryProtocol\) SkipGo - -```go -func (p *BinaryProtocol) SkipGo(fieldType Type, maxDepth int) (err error) -``` - -SkipGo skips over the value for the given type using Go implementation. - -### func \(\*BinaryProtocol\) SkipNative - -```go -func (p *BinaryProtocol) SkipNative(fieldType Type, maxDepth int) (err error) -``` - -Skip skips over teh value for the given type using native C implementation. - -### func \(BinaryProtocol\) UnwrapBody - -```go -func (p BinaryProtocol) UnwrapBody() (string, TMessageType, int32, FieldID, []byte, error) -``` - -UnwrapBody unwraps the message parameters from its buf - -### func \(\*BinaryProtocol\) WriteAnyWithDesc - -```go -func (p *BinaryProtocol) WriteAnyWithDesc(desc *TypeDescriptor, val interface{}, cast bool, disallowUnknown bool, useFieldName bool) error -``` - -WriteAnyWithDesc explain desc and val and write them into buffer \- LIST/SET will be converted from \[\]interface\{\} \- MAP will be converted from map\[string\]interface\{\} or map\[int\]interface\{\} \- STRUCT will be converted from map\[FieldID\]interface\{\} - -### func \(\*BinaryProtocol\) WriteBinary - -```go -func (p *BinaryProtocol) WriteBinary(value []byte) error -``` - -WriteBinary ... - -### func \(\*BinaryProtocol\) WriteBool - -```go -func (p *BinaryProtocol) WriteBool(value bool) error -``` - -WriteBool ... - -### func \(\*BinaryProtocol\) WriteByte - -```go -func (p *BinaryProtocol) WriteByte(value byte) error -``` - -WriteByte ... - -### func \(\*BinaryProtocol\) WriteDefaultOrEmpty - -```go -func (p *BinaryProtocol) WriteDefaultOrEmpty(field *FieldDescriptor) error -``` - -WriteDefaultOrEmpty write default value if any, otherwise write zero value - -### func \(\*BinaryProtocol\) WriteDouble - -```go -func (p *BinaryProtocol) WriteDouble(value float64) error -``` - -WriteDouble ... - -### func \(\*BinaryProtocol\) WriteEmpty - -```go -func (p *BinaryProtocol) WriteEmpty(desc *TypeDescriptor) error -``` - -WriteEmpty write zero value - -### func \(\*BinaryProtocol\) WriteFieldBegin - -```go -func (p *BinaryProtocol) WriteFieldBegin(name string, typeID Type, id FieldID) error -``` - -WriteFieldBegin ... - -### func \(\*BinaryProtocol\) WriteFieldEnd - -```go -func (p *BinaryProtocol) WriteFieldEnd() error -``` - -WriteFieldEnd ... - -### func \(\*BinaryProtocol\) WriteFieldStop - -```go -func (p *BinaryProtocol) WriteFieldStop() error -``` - -WriteFieldStop ... - -### func \(\*BinaryProtocol\) WriteI16 - -```go -func (p *BinaryProtocol) WriteI16(value int16) error -``` - -WriteI16 ... - -### func \(\*BinaryProtocol\) WriteI32 - -```go -func (p *BinaryProtocol) WriteI32(value int32) error -``` - -WriteI32 ... - -### func \(\*BinaryProtocol\) WriteI64 - -```go -func (p *BinaryProtocol) WriteI64(value int64) error -``` - -WriteI64 ... - -### func \(\*BinaryProtocol\) WriteInt - -```go -func (p *BinaryProtocol) WriteInt(t Type, value int) error -``` - -WriteInt ... - -### func \(\*BinaryProtocol\) WriteListBegin - -```go -func (p *BinaryProtocol) WriteListBegin(elemType Type, size int) error -``` - -WriteListBegin ... - -### func \(\*BinaryProtocol\) WriteListBeginWithSizePos - -```go -func (p *BinaryProtocol) WriteListBeginWithSizePos(elemType Type, size int) (int, error) -``` - -WriteListBeginWithSizePos writes the list begin, and return the buffer position of the size data - -### func \(\*BinaryProtocol\) WriteListEnd - -```go -func (p *BinaryProtocol) WriteListEnd() error -``` - -WriteListEnd ... - -### func \(\*BinaryProtocol\) WriteMapBegin - -```go -func (p *BinaryProtocol) WriteMapBegin(keyType, valueType Type, size int) error -``` - -WriteMapBegin ... - -### func \(\*BinaryProtocol\) WriteMapBeginWithSizePos - -```go -func (p *BinaryProtocol) WriteMapBeginWithSizePos(keyType, valueType Type, size int) (int, error) -``` - -WriteMapBeginWithSizePos writes the map begin, and return the buffer position of the size data - -### func \(\*BinaryProtocol\) WriteMapEnd - -```go -func (p *BinaryProtocol) WriteMapEnd() error -``` - -WriteMapEnd ... - -### func \(\*BinaryProtocol\) WriteMessageBegin - -```go -func (p *BinaryProtocol) WriteMessageBegin(name string, typeID TMessageType, seqID int32) error -``` - -WriteMessageBegin ... - -### func \(\*BinaryProtocol\) WriteMessageEnd - -```go -func (p *BinaryProtocol) WriteMessageEnd() error -``` - -WriteMessageEnd ... - -### func \(\*BinaryProtocol\) WriteSetBegin - -```go -func (p *BinaryProtocol) WriteSetBegin(elemType Type, size int) error -``` - -WriteSetBegin ... - -### func \(\*BinaryProtocol\) WriteSetEnd - -```go -func (p *BinaryProtocol) WriteSetEnd() error -``` - -WriteSetEnd ... - -### func \(\*BinaryProtocol\) WriteString - -```go -func (p *BinaryProtocol) WriteString(value string) error -``` - -WriteString ... - -### func \(\*BinaryProtocol\) WriteStringWithDesc - -```go -func (p *BinaryProtocol) WriteStringWithDesc(val string, desc *TypeDescriptor, disallowUnknown bool, base64Binary bool) error -``` - -WriteStringWithDesc explain simple string val with desc and convert to thrift data - -### func \(\*BinaryProtocol\) WriteStructBegin - -```go -func (p *BinaryProtocol) WriteStructBegin(name string) error -``` - -WriteStructBegin ... - -### func \(\*BinaryProtocol\) WriteStructEnd - -```go -func (p *BinaryProtocol) WriteStructEnd() error -``` - -WriteStructEnd ... - -## type DefaultValue - -DefaultValue is the default value of a field - -```go -type DefaultValue struct { - // contains filtered or unexported fields -} -``` - -### func \(DefaultValue\) GoValue - -```go -func (d DefaultValue) GoValue() interface{} -``` - -GoValue return the go runtime representation of the default value - -### func \(DefaultValue\) JSONValue - -```go -func (d DefaultValue) JSONValue() string -``` - -JSONValue return the json\-encoded representation of the default value - -### func \(DefaultValue\) ThriftBinary - -```go -func (d DefaultValue) ThriftBinary() string -``` - -ThriftBinary return the thrift\-binary\-encoded representation of the default value - -## type FieldDescriptor - -FieldDescriptor is the runtime descriptor of a field in a struct - -```go -type FieldDescriptor struct { - // contains filtered or unexported fields -} -``` - -### func \(FieldDescriptor\) Alias - -```go -func (f FieldDescriptor) Alias() string -``` - -Alias returns the alias of a field - -### func \(FieldDescriptor\) DefaultValue - -```go -func (f FieldDescriptor) DefaultValue() *DefaultValue -``` - -DefaultValue returns the default value of a field - -### func \(FieldDescriptor\) HTTPMappings - -```go -func (f FieldDescriptor) HTTPMappings() []HttpMapping -``` - -HTTPMappings returns the http\-mapping annotations of a field - -### func \(FieldDescriptor\) ID - -```go -func (f FieldDescriptor) ID() FieldID -``` - -ID returns the id of a field - -### func \(FieldDescriptor\) IsRequestBase - -```go -func (f FieldDescriptor) IsRequestBase() bool -``` - -IsRequestBase tells if the field is base.Base - -### func \(FieldDescriptor\) IsResponseBase - -```go -func (f FieldDescriptor) IsResponseBase() bool -``` - -IsResponseBase tells if the field is base.BaseResp - -### func \(FieldDescriptor\) Name - -```go -func (f FieldDescriptor) Name() string -``` - -Name returns the name of a field - -### func \(FieldDescriptor\) Required - -```go -func (f FieldDescriptor) Required() Requireness -``` - -Required return the requiredness of a field - -### func \(FieldDescriptor\) Type - -```go -func (f FieldDescriptor) Type() *TypeDescriptor -``` - -Type returns the type descriptor of a field - -### func \(FieldDescriptor\) ValueMapping - -```go -func (f FieldDescriptor) ValueMapping() ValueMapping -``` - -ValueMapping returns the value\-mapping annotation of a field - -### func \(FieldDescriptor\) ValueMappingType - -```go -func (f FieldDescriptor) ValueMappingType() AnnoType -``` - -ValueMappingType returns the value\-mapping annotation's type of a field - -## type FieldID - -FieldID is used to identify a field in a struct - -```go -type FieldID uint16 -``` - -## type FieldIDMap - -FieldIDMap is a map from field id to field descriptor - -```go -type FieldIDMap struct { - // contains filtered or unexported fields -} -``` - -### func \(FieldIDMap\) All - -```go -func (fd FieldIDMap) All() (ret []*FieldDescriptor) -``` - -All returns all field descriptors - -### func \(FieldIDMap\) Get - -```go -func (fd FieldIDMap) Get(id FieldID) *FieldDescriptor -``` - -Get gets the field descriptor for the given id - -### func \(\*FieldIDMap\) Set - -```go -func (fd *FieldIDMap) Set(id FieldID, f *FieldDescriptor) -``` - -Set sets the field descriptor for the given id - -### func \(FieldIDMap\) Size - -```go -func (fd FieldIDMap) Size() int -``` - -Size returns the size of the map - -## type FieldNameMap - -FieldNameMap is a map for field name and field descriptor - -```go -type FieldNameMap struct { - // contains filtered or unexported fields -} -``` - -### func \(FieldNameMap\) All - -```go -func (ft FieldNameMap) All() []*FieldDescriptor -``` - -All returns all field descriptors - -### func \(\*FieldNameMap\) Build - -```go -func (ft *FieldNameMap) Build() -``` - -Build builds the map. It will try to build a trie tree if the dispersion of keys is higher enough \(min\). - -### func \(FieldNameMap\) Get - -```go -func (ft FieldNameMap) Get(k string) *FieldDescriptor -``` - -Get gets the field descriptor for the given key - -### func \(\*FieldNameMap\) Set - -```go -func (ft *FieldNameMap) Set(key string, field *FieldDescriptor) (exist bool) -``` - -Set sets the field descriptor for the given key - -### func \(FieldNameMap\) Size - -```go -func (ft FieldNameMap) Size() int -``` - -Size returns the size of the map - -## type FunctionDescriptor - -FunctionDescriptor idl function descriptor - -```go -type FunctionDescriptor struct { - // contains filtered or unexported fields -} -``` - -### func GetFnDescFromFile - -```go -func GetFnDescFromFile(filePath, fnName string, opts Options) *FunctionDescriptor -``` - -GetFnDescFromFile get a fucntion descriptor from idl path \(relative to your git root\) and the function name - -### func \(FunctionDescriptor\) Annotations - -```go -func (f FunctionDescriptor) Annotations() map[string][]string -``` - -Annotations returns the annotations of the function - -### func \(FunctionDescriptor\) Endpoints - -```go -func (f FunctionDescriptor) Endpoints() []http.Endpoint -``` - -Endpoints returns the http endpoints of the function - -### func \(FunctionDescriptor\) HasRequestBase - -```go -func (f FunctionDescriptor) HasRequestBase() bool -``` - -HasRequestBase tells if the function has a base.Base field - -### func \(FunctionDescriptor\) Name - -```go -func (f FunctionDescriptor) Name() string -``` - -Name returns the name of the function - -### func \(FunctionDescriptor\) Oneway - -```go -func (f FunctionDescriptor) Oneway() bool -``` - -Oneway tells if the function is oneway type - -### func \(FunctionDescriptor\) Request - -```go -func (f FunctionDescriptor) Request() *TypeDescriptor -``` - -Request returns the request type descriptor of the function The request arguements is mapped with arguement id and name - -### func \(FunctionDescriptor\) Response - -```go -func (f FunctionDescriptor) Response() *TypeDescriptor -``` - -Response returns the response type descriptor of the function The response arguements is mapped with arguement id - -## type HttpMapping - -HTTPMapping is used to convert http value while running convertion. See also: thrift/annotation/http\_mapping.go - -```go -type HttpMapping interface { - // Request get a http value from req - Request(ctx context.Context, req http.RequestGetter, field *FieldDescriptor) (string, error) - - // Response set a http value into resp - Response(ctx context.Context, resp http.ResponseSetter, field *FieldDescriptor, val string) error - - // RawEncoding indicates the encoding of the value, it should be meta.EncodingText by default - Encoding() meta.Encoding -} -``` - -## type KeyMapping - -KeyMapping is used to convert field key while parsing idl. See also: thrift/annotation/key\_mapping.go - -```go -type KeyMapping interface { - // Map key to new key - Map(ctx context.Context, key string) string -} -``` - -## type OptionMapping - -OptionMapping is used to convert thrift.Options while parsing idl. See also: thrift/annotation/option\_mapping.go - -```go -type OptionMapping interface { - // Map options to new options - Map(ctx context.Context, opts Options) Options -} -``` - -## type Options - -Options is options for parsing thrift IDL. - -```go -type Options struct { - // ParseServiceMode indicates how to parse service. - ParseServiceMode meta.ParseServiceMode - - // MapFieldWay indicates StructDescriptor.FieldByKey() uses alias to map field. - // By default, we use alias to map, and alias always equals to field name if not given. - MapFieldWay meta.MapFieldWay - - // ParseFieldRandomRate indicates whether to parse partial fields and is only used for mock test. - // The value means the possibility of randomly parse and embed one field into StructDescriptor. - // It must be within (0, 1], and 0 means always parse all fields. - ParseFieldRandomRate float64 - - // ParseEnumAsInt64 indicates whether to parse enum as I64 (default I32). - ParseEnumAsInt64 bool - - // SetOptionalBitmap indicates to set bitmap for optional fields - SetOptionalBitmap bool - - // UseDefaultValue indicates to parse and store default value defined on IDL fields. - UseDefaultValue bool - - // ParseFunctionMode indicates to parse only response or request for a function - ParseFunctionMode meta.ParseFunctionMode - - // EnableThriftBase indicates to explictly handle thrift/base (see README.md) fields. - // One field is identified as a thrift base if it satisfies **BOTH** of the following conditions: - // 1. Its type is 'base.Base' (for request base) or 'base.BaseResp' (for response base); - // 2. it is on the top layer of the root struct of one function. - EnableThriftBase bool -} -``` - -### func NewDefaultOptions - -```go -func NewDefaultOptions() Options -``` - -NewDefaultOptions creates a default Options. - -### func \(Options\) NewDescriptorFromContentWithMethod - -```go -func (opts Options) NewDescriptorFromContentWithMethod(ctx context.Context, path, content string, includes map[string]string, isAbsIncludePath bool, methods ...string) (*ServiceDescriptor, error) -``` - -NewDescritorFromContentWithMethod creates a ServiceDescriptor from a thrift content and its includes, but only parse specific methods. - -### func \(Options\) NewDescriptorFromPathWithMethod - -```go -func (opts Options) NewDescriptorFromPathWithMethod(ctx context.Context, path string, includeDirs []string, methods ...string) (*ServiceDescriptor, error) -``` - -NewDescritorFromContent creates a ServiceDescriptor from a thrift path and its includes, with specific methods. If methods is empty, all methods will be parsed. The includeDirs is used to find the include files. - -### func \(Options\) NewDescritorFromContent - -```go -func (opts Options) NewDescritorFromContent(ctx context.Context, path, content string, includes map[string]string, isAbsIncludePath bool) (*ServiceDescriptor, error) -``` - -NewDescritorFromContent creates a ServiceDescriptor from a thrift content and its includes, which uses the default options. path is the main thrift file path, content is the main thrift file content. includes is the thrift file content map, and its keys are specific including thrift file path. isAbsIncludePath indicates whether these keys of includes are absolute path. If true, the include path will be joined with the main thrift file path. - -### func \(Options\) NewDescritorFromPath - -```go -func (opts Options) NewDescritorFromPath(ctx context.Context, path string, includeDirs ...string) (*ServiceDescriptor, error) -``` - -NewDescritorFromContent creates a ServiceDescriptor from a thrift path and its includes, which uses the given options. The includeDirs is used to find the include files. - -## type ParseTarget - -ParseTarget indicates the target to parse - -```go -type ParseTarget uint8 -``` - -```go -const ( - Request ParseTarget = iota - Response - Exception -) -``` - -## type Requireness - -Requireness is the requireness of a field. See also https://thrift.apache.org/docs/idl.html - -```go -type Requireness uint8 -``` - -```go -const ( - // OptionalRequireness means the field is optional - OptionalRequireness Requireness = 0 - // DefaultRequireness means the field is default-requireness - DefaultRequireness Requireness = 1 - // RequiredRequireness means the field is required - RequiredRequireness Requireness = 2 -) -``` - -## type RequiresBitmap - -RequiresBitmap is a bitmap to mark fields - -```go -type RequiresBitmap []uint64 -``` - -### func NewRequiresBitmap - -```go -func NewRequiresBitmap() *RequiresBitmap -``` - -NewRequiresBitmap get bitmap from pool, if pool is empty, create a new one WARN: memory from pool maybe dirty\! - -### func \(RequiresBitmap\) CheckRequires - -```go -func (b RequiresBitmap) CheckRequires(desc *StructDescriptor, writeDefault bool, handler func(field *FieldDescriptor) error) error -``` - -CheckRequires scan every bit of the bitmap. When a bit is marked, it will: \- if the corresponding field is required\-requireness, it reports error \- if the corresponding is not required\-requireness but writeDefault is true, it will call handler to handle this field - -### func \(RequiresBitmap\) CopyTo - -```go -func (b RequiresBitmap) CopyTo(to *RequiresBitmap) -``` - -CopyTo copy the bitmap to a given bitmap - -### func \(RequiresBitmap\) HandleRequires - -```go -func (b RequiresBitmap) HandleRequires(desc *StructDescriptor, writeRquired bool, writeDefault bool, writeOptional bool, handler func(field *FieldDescriptor) error) error -``` - -CheckRequires scan every bit of the bitmap. When a bit is marked, it will: \- if the corresponding field is required\-requireness and writeRquired is true, it will call handler to handle this field, otherwise report error \- if the corresponding is default\-requireness and writeDefault is true, it will call handler to handle this field \- if the corresponding is optional\-requireness and writeOptional is true, it will call handler to handle this field - -### func \(RequiresBitmap\) IsSet - -```go -func (b RequiresBitmap) IsSet(id FieldID) bool -``` - -IsSet tells if the bit corresponding the given id is marked - -### func \(\*RequiresBitmap\) Set - -```go -func (b *RequiresBitmap) Set(id FieldID, val Requireness) -``` - -Set mark the bit corresponding the given id, with the given requireness \- RequiredRequireness|DefaultRequireness mark the bit as 1 \- OptionalRequireness mark the bit as 0 - -## type ServiceDescriptor - -ServiceDescriptor is the runtime descriptor of a service - -```go -type ServiceDescriptor struct { - // contains filtered or unexported fields -} -``` - -### func NewDescritorFromContent - -```go -func NewDescritorFromContent(ctx context.Context, path, content string, includes map[string]string, isAbsIncludePath bool) (*ServiceDescriptor, error) -``` - -NewDescritorFromContent behaviors like NewDescritorFromPath, besides it uses DefaultOptions. - -
Example -

- -```go -{ - path := "a/b/main.thrift" - content := ` - include "base/base.thrift" - namespace go test.server - - service InboxService { - base.BaseResp ExampleMethod(1: base.Base req) - }` - base := ` - namespace py base - namespace go base - namespace java com.bytedance.thrift.base - - struct TrafficEnv { - 1: bool Open = false, - 2: string Env = "", - } - - struct Base { - 1: string LogID = "", - 2: string Caller = "", - 3: string Addr = "", - 4: string Client = "", - 5: optional TrafficEnv TrafficEnv, - 6: optional map Extra, - } - - struct BaseResp { - 1: string StatusMessage = "", - 2: i32 StatusCode = 0, - 3: optional map Extra, - } - ` - - includes := map[string]string{ - path: content, - "a/b/base/base.thrift": base, - } - - p1, err := NewDescritorFromContent(context.Background(), path, content, includes, true) - if err != nil { - panic(err) - } - r1, _ := p1.LookupFunctionByMethod("ExampleMethod") - fmt.Printf("%#v\n", r1.Response()) - - delete(includes, "a/b/base/base.thrift") - includes["base/base.thrift"] = base - - p2, err := Options{ - ParseFunctionMode: meta.ParseRequestOnly, - }.NewDescritorFromContent(context.Background(), path, content, includes, false) - if err != nil { - panic(err) - } - r2, _ := p2.LookupFunctionByMethod("ExampleMethod") - fmt.Printf("%#v\n", r2.Response()) -} -``` - -

-
- -### func NewDescritorFromPath - -```go -func NewDescritorFromPath(ctx context.Context, path string, includeDirs ...string) (*ServiceDescriptor, error) -``` - -NewDescritorFromPath behaviors like NewDescritorFromPath, besides it uses DefaultOptions. - -
Example -

- -```go -{ - - p1, err := NewDescritorFromPath(context.Background(), "../testdata/idl/example.thrift") - if err != nil { - panic(err) - } - r1, _ := p1.LookupFunctionByMethod("ExampleMethod") - fmt.Printf("%#v\n", r1.Response()) - - p2, err := Options{ - ParseFunctionMode: meta.ParseRequestOnly, - }.NewDescritorFromPath(context.Background(), "../testdata/idl/example.thrift") - if err != nil { - panic(err) - } - r2, _ := p2.LookupFunctionByMethod("ExampleMethod") - fmt.Printf("%#v\n", r2.Response()) -} -``` - -

-
- -### func \(ServiceDescriptor\) Annotations - -```go -func (s ServiceDescriptor) Annotations() map[string][]string -``` - -Annotations returns the annotations of a service - -### func \(ServiceDescriptor\) Functions - -```go -func (s ServiceDescriptor) Functions() map[string]*FunctionDescriptor -``` - -Functions returns all functions in the service - -### func \(\*ServiceDescriptor\) LookupFunctionByMethod - -```go -func (s *ServiceDescriptor) LookupFunctionByMethod(method string) (*FunctionDescriptor, error) -``` - -LookupFunctionByMethod lookup function by method name - -### func \(ServiceDescriptor\) Name - -```go -func (s ServiceDescriptor) Name() string -``` - -Name returns the name of the service - -## type StructDescriptor - -StructDescriptor is the runtime descriptor of a STRUCT type - -```go -type StructDescriptor struct { - // contains filtered or unexported fields -} -``` - -### func \(StructDescriptor\) FieldById - -```go -func (s StructDescriptor) FieldById(id FieldID) *FieldDescriptor -``` - -FieldById finds the field by field id - -### func \(StructDescriptor\) FieldByKey - -```go -func (s StructDescriptor) FieldByKey(k string) (field *FieldDescriptor) -``` - -#### FieldByName finds the field by key - -NOTICE: Options.MapFieldWay can influence the behavior of this method. ep: if Options.MapFieldWay is MapFieldWayName, then field names should be used as key. - -### func \(StructDescriptor\) Fields - -```go -func (s StructDescriptor) Fields() []*FieldDescriptor -``` - -Fields returns all fields in the struct - -### func \(StructDescriptor\) GetRequestBase - -```go -func (s StructDescriptor) GetRequestBase() *FieldDescriptor -``` - -GetRequestBase returns the base.Base field of a STRUCT - -### func \(StructDescriptor\) GetResponseBase - -```go -func (s StructDescriptor) GetResponseBase() *FieldDescriptor -``` - -GetResponseBase returns the base.BaseResp field of a STRUCT - -### func \(StructDescriptor\) HttpMappingFields - -```go -func (s StructDescriptor) HttpMappingFields() []*FieldDescriptor -``` - -GetHttpMappingFields returns the fields with http\-mapping annotations - -### func \(StructDescriptor\) Len - -```go -func (s StructDescriptor) Len() int -``` - -Len returns the number of fields in the struct - -### func \(StructDescriptor\) Name - -```go -func (s StructDescriptor) Name() string -``` - -Name returns the name of the struct - -### func \(StructDescriptor\) Requires - -```go -func (s StructDescriptor) Requires() RequiresBitmap -``` - -Fields returns requireness bitmap in the struct. By default, only Requred and Default fields are marked. - -## type TMessageType - -TMessageType is the type of message - -```go -type TMessageType int32 -``` - -```go -const ( - INVALID_TMESSAGE_TYPE TMessageType = 0 - CALL TMessageType = 1 - REPLY TMessageType = 2 - EXCEPTION TMessageType = 3 - ONEWAY TMessageType = 4 -) -``` - -## type Type - -Type constants in the Thrift protocol - -```go -type Type byte -``` - -built\-in Types - -```go -const ( - STOP Type = 0 - VOID Type = 1 - BOOL Type = 2 - BYTE Type = 3 - I08 Type = 3 - DOUBLE Type = 4 - I16 Type = 6 - I32 Type = 8 - I64 Type = 10 - STRING Type = 11 - // UTF7 Type = 11 - STRUCT Type = 12 - MAP Type = 13 - SET Type = 14 - LIST Type = 15 - UTF8 Type = 16 - UTF16 Type = 17 - - ERROR Type = 255 -) -``` - -### func FromThriftTType - -```go -func FromThriftTType(t thrift.TType) Type -``` - -FromThriftTType converts apache/thrift.TType to Type - -### func \(Type\) IsComplex - -```go -func (p Type) IsComplex() bool -``` - -IsComplex tells if the type is one of STRUCT, MAP, SET, LIST - -### func \(Type\) IsInt - -```go -func (p Type) IsInt() bool -``` - -IsInt tells if the type is one of I08, I16, I32, I64 - -### func \(Type\) String - -```go -func (p Type) String() string -``` - -String for format and print - -### func \(Type\) ToThriftTType - -```go -func (p Type) ToThriftTType() thrift.TType -``` - -ToThriftTType converts Type to apache/thrift.TType - -### func \(Type\) Valid - -```go -func (p Type) Valid() bool -``` - -## type TypeDescriptor - -TypeDescriptor is the runtime descriptor of a thrift type - -```go -type TypeDescriptor struct { - // contains filtered or unexported fields -} -``` - -### func FnRequest - -```go -func FnRequest(fn *FunctionDescriptor) *TypeDescriptor -``` - -FnRequest We assume the request only have one argument and the only argument it the type we want. - -### func FnResponse - -```go -func FnResponse(fn *FunctionDescriptor) *TypeDescriptor -``` - -FnResponse get the normal response type - -### func \(TypeDescriptor\) Elem - -```go -func (d TypeDescriptor) Elem() *TypeDescriptor -``` - -Elem returns the element type descriptor of a LIST, SET or MAP type - -### func \(TypeDescriptor\) IsBinary - -```go -func (d TypeDescriptor) IsBinary() bool -``` - -IsBinary tells if the type is binary type \(\[\]byte\) - -### func \(TypeDescriptor\) Key - -```go -func (d TypeDescriptor) Key() *TypeDescriptor -``` - -Key returns the key type descriptor of a MAP type - -### func \(TypeDescriptor\) Name - -```go -func (d TypeDescriptor) Name() string -``` - -Name returns the name of the descriptor for struct, it is the struct name; for build\-in type, it is the type name. - -### func \(TypeDescriptor\) Struct - -```go -func (d TypeDescriptor) Struct() *StructDescriptor -``` - -Struct returns the struct type descriptor of a STRUCT type - -### func \(TypeDescriptor\) Type - -```go -func (d TypeDescriptor) Type() Type -``` - -Type returns the build\-in type of the descriptor - -## type ValueMapping - -ValueMapping is used to convert thrift value while running convertion. See also: thrift/annotation/value\_mapping.go - -```go -type ValueMapping interface { - // Read thrift value from p and convert it into out - Read(ctx context.Context, p *BinaryProtocol, field *FieldDescriptor, out *[]byte) error - - // Write thrift value into p, which is converted from in - Write(ctx context.Context, p *BinaryProtocol, field *FieldDescriptor, in []byte) error -} -``` - - - -Generated by [gomarkdoc]() diff --git a/thrift/generic/README.md b/thrift/generic/README.md deleted file mode 100644 index 3c97d502..00000000 --- a/thrift/generic/README.md +++ /dev/null @@ -1,1217 +0,0 @@ - - -# generic - -```go -import "github.com/cloudwego/dynamicgo/thrift/generic" -``` - -## Index - -- [Variables](<#variables>) -- [func DescriptorToPathNode(desc *thrift.TypeDescriptor, root *PathNode, opts *Options) error](<#func-descriptortopathnode>) -- [func FreePathNode(p *PathNode)](<#func-freepathnode>) -- [func GetDescByPath(desc *thrift.TypeDescriptor, path ...Path) (ret *thrift.TypeDescriptor, err error)](<#func-getdescbypath>) -- [type Node](<#type-node>) - - [func NewNode(t thrift.Type, src []byte) Node](<#func-newnode>) - - [func NewNodeBinary(val []byte) Node](<#func-newnodebinary>) - - [func NewNodeBool(val bool) Node](<#func-newnodebool>) - - [func NewNodeByte(val byte) Node](<#func-newnodebyte>) - - [func NewNodeDouble(val float64) Node](<#func-newnodedouble>) - - [func NewNodeInt16(val int16) Node](<#func-newnodeint16>) - - [func NewNodeInt32(val int32) Node](<#func-newnodeint32>) - - [func NewNodeInt64(val int64) Node](<#func-newnodeint64>) - - [func NewNodeString(val string) Node](<#func-newnodestring>) - - [func NewTypedNode(typ thrift.Type, et thrift.Type, kt thrift.Type) (ret Node)](<#func-newtypednode>) - - [func (self Node) Binary() ([]byte, error)](<#func-node-binary>) - - [func (self Node) Bool() (bool, error)](<#func-node-bool>) - - [func (self Node) Byte() (byte, error)](<#func-node-byte>) - - [func (self *Node) Check() error](<#func-node-check>) - - [func (self Node) Children(out *[]PathNode, recurse bool, opts *Options) (err error)](<#func-node-children>) - - [func (self Node) ElemType() thrift.Type](<#func-node-elemtype>) - - [func (self Node) ErrCode() meta.ErrCode](<#func-node-errcode>) - - [func (self Node) Error() string](<#func-node-error>) - - [func (self Node) Field(id thrift.FieldID) (v Node)](<#func-node-field>) - - [func (self Node) Fields(ids []PathNode, opts *Options) (err error)](<#func-node-fields>) - - [func (self Node) Float64() (float64, error)](<#func-node-float64>) - - [func (self Node) Foreach(handler func(path Path, node Node) bool, opts *Options) error](<#func-node-foreach>) - - [func (self Node) ForeachKV(handler func(key Node, val Node) bool, opts *Options) error](<#func-node-foreachkv>) - - [func (self Node) Fork() Node](<#func-node-fork>) - - [func (self Node) GetByInt(key int) (v Node)](<#func-node-getbyint>) - - [func (self Node) GetByPath(pathes ...Path) Node](<#func-node-getbypath>) - - [func (self Node) GetByRaw(key []byte) (v Node)](<#func-node-getbyraw>) - - [func (self Node) GetByStr(key string) (v Node)](<#func-node-getbystr>) - - [func (self Node) GetMany(pathes []PathNode, opts *Options) error](<#func-node-getmany>) - - [func (self Node) GetTree(tree *PathNode, opts *Options) error](<#func-node-gettree>) - - [func (self Node) Gets(keys []PathNode, opts *Options) (err error)](<#func-node-gets>) - - [func (self Node) Index(i int) (v Node)](<#func-node-index>) - - [func (self Node) Indexes(ins []PathNode, opts *Options) (err error)](<#func-node-indexes>) - - [func (self Node) Int() (int, error)](<#func-node-int>) - - [func (self Node) IntMap(opts *Options) (map[int]interface{}, error)](<#func-node-intmap>) - - [func (self Node) Interface(opts *Options) (interface{}, error)](<#func-node-interface>) - - [func (self Node) InterfaceMap(opts *Options) (map[interface{}]interface{}, error)](<#func-node-interfacemap>) - - [func (self Node) IsEmpty() bool](<#func-node-isempty>) - - [func (self Node) IsErrNotFound() bool](<#func-node-iserrnotfound>) - - [func (self Node) IsError() bool](<#func-node-iserror>) - - [func (self Node) KeyType() thrift.Type](<#func-node-keytype>) - - [func (self Node) Len() (int, error)](<#func-node-len>) - - [func (self Node) List(opts *Options) ([]interface{}, error)](<#func-node-list>) - - [func (self Node) Raw() []byte](<#func-node-raw>) - - [func (self *Node) SetByPath(sub Node, path ...Path) (exist bool, err error)](<#func-node-setbypath>) - - [func (self *Node) SetMany(pathes []PathNode, opts *Options) (err error)](<#func-node-setmany>) - - [func (self Node) StrMap(opts *Options) (map[string]interface{}, error)](<#func-node-strmap>) - - [func (self Node) String() (string, error)](<#func-node-string>) - - [func (self Node) Type() thrift.Type](<#func-node-type>) - - [func (self *Node) UnsetByPath(path ...Path) error](<#func-node-unsetbypath>) -- [type Options](<#type-options>) -- [type Path](<#type-path>) - - [func NewPathBinKey(key []byte) Path](<#func-newpathbinkey>) - - [func NewPathFieldId(id thrift.FieldID) Path](<#func-newpathfieldid>) - - [func NewPathFieldName(name string) Path](<#func-newpathfieldname>) - - [func NewPathIndex(index int) Path](<#func-newpathindex>) - - [func NewPathIntKey(key int) Path](<#func-newpathintkey>) - - [func NewPathStrKey(key string) Path](<#func-newpathstrkey>) - - [func (self Path) Bin() []byte](<#func-path-bin>) - - [func (self Path) Id() thrift.FieldID](<#func-path-id>) - - [func (self Path) Int() int](<#func-path-int>) - - [func (self Path) Str() string](<#func-path-str>) - - [func (self Path) String() string](<#func-path-string>) - - [func (self Path) ToRaw(t thrift.Type) []byte](<#func-path-toraw>) - - [func (self Path) Type() PathType](<#func-path-type>) - - [func (self Path) Value() interface{}](<#func-path-value>) -- [type PathNode](<#type-pathnode>) - - [func NewPathNode() *PathNode](<#func-newpathnode>) - - [func (self *PathNode) Assgin(recurse bool, opts *Options) error](<#func-pathnode-assgin>) - - [func (self *PathNode) Check() error](<#func-pathnode-check>) - - [func (self PathNode) CopyTo(to *PathNode)](<#func-pathnode-copyto>) - - [func (self PathNode) Error() string](<#func-pathnode-error>) - - [func (self *PathNode) Field(id thrift.FieldID, opts *Options) *PathNode](<#func-pathnode-field>) - - [func (self PathNode) Fork() PathNode](<#func-pathnode-fork>) - - [func (self *PathNode) GetByInt(key int, opts *Options) *PathNode](<#func-pathnode-getbyint>) - - [func (self *PathNode) GetByStr(key string, opts *Options) *PathNode](<#func-pathnode-getbystr>) - - [func (self *PathNode) Load(recurse bool, opts *Options) error](<#func-pathnode-load>) - - [func (self PathNode) Marshal(opt *Options) (out []byte, err error)](<#func-pathnode-marshal>) - - [func (self PathNode) MarshalIntoBuffer(out *[]byte, opt *Options) error](<#func-pathnode-marshalintobuffer>) - - [func (self *PathNode) ResetAll()](<#func-pathnode-resetall>) - - [func (self *PathNode) ResetValue()](<#func-pathnode-resetvalue>) - - [func (self *PathNode) SetByInt(key int, val Node, opts *Options) (bool, error)](<#func-pathnode-setbyint>) - - [func (self *PathNode) SetByStr(key string, val Node, opts *Options) (bool, error)](<#func-pathnode-setbystr>) - - [func (self *PathNode) SetField(id thrift.FieldID, val Node, opts *Options) (bool, error)](<#func-pathnode-setfield>) -- [type PathType](<#type-pathtype>) -- [type Value](<#type-value>) - - [func NewValue(desc *thrift.TypeDescriptor, src []byte) Value](<#func-newvalue>) - - [func (self Value) Field(id thrift.FieldID) (v Value)](<#func-value-field>) - - [func (self Value) FieldByName(name string) (v Value)](<#func-value-fieldbyname>) - - [func (self Value) Fork() Value](<#func-value-fork>) - - [func (self Value) GetByInt(key int) (v Value)](<#func-value-getbyint>) - - [func (self Value) GetByPath(pathes ...Path) Value](<#func-value-getbypath>) - - [func (self Value) GetByStr(key string) (v Value)](<#func-value-getbystr>) - - [func (self Value) Index(i int) (v Value)](<#func-value-index>) - - [func (self Value) MarshalTo(to *thrift.TypeDescriptor, opts *Options) ([]byte, error)](<#func-value-marshalto>) - - [func (self *Value) SetByPath(sub Value, path ...Path) (exist bool, err error)](<#func-value-setbypath>) - - [func (self *Value) UnsetByPath(path ...Path) error](<#func-value-unsetbypath>) - - -## Variables - -```go -var ( - // UseNativeSkipForGet indicates to use native.Skip (instead of go.Skip) method to skip thrift value - // This only works for single-value searching API like GetByInt()/GetByRaw()/GetByStr()/Field()/Index()/GetByPath() methods. - // WARN: this will promote performance when thrift value to be skipped is large, but may decrease preformance when thrift value is small. - UseNativeSkipForGet = false - - // DefaultNodeSliceCap is the default capacity of a Node or NodePath slice - // Usually, a Node or NodePath slice is used to store intermediate or consequential elements of a generic API like Children()|Interface()|SetMany() - DefaultNodeSliceCap = 16 -) -``` - -```go -var ( - // StoreChildrenByIdShreshold is the maximum id to store children node by id. - StoreChildrenByIdShreshold = 256 - - // StoreChildrenByIdShreshold is the minimum id to store children node by hash. - StoreChildrenByIntHashShreshold = DefaultNodeSliceCap -) -``` - -## func DescriptorToPathNode - -```go -func DescriptorToPathNode(desc *thrift.TypeDescriptor, root *PathNode, opts *Options) error -``` - -DescriptorToPathNode converts a thrift type descriptor to a DOM, assgining path to root NOTICE: it only recursively converts STRUCT type - -## func FreePathNode - -```go -func FreePathNode(p *PathNode) -``` - -FreePathNode put a PathNode back to memory pool - -## func GetDescByPath - -```go -func GetDescByPath(desc *thrift.TypeDescriptor, path ...Path) (ret *thrift.TypeDescriptor, err error) -``` - -GetDescByPath searches longitudinally and returns the sub descriptor of the desc specified by path - -## type Node - -Node is a generic wrap of raw thrift data - -```go -type Node struct { - // contains filtered or unexported fields -} -``` - -### func NewNode - -```go -func NewNode(t thrift.Type, src []byte) Node -``` - -NewNodeBool create a new node with given type and data WARN: it WON'T check the correctness of the data - -### func NewNodeBinary - -```go -func NewNodeBinary(val []byte) Node -``` - -NewNodeBinary converts a \[\]byte value to a STRING node - -### func NewNodeBool - -```go -func NewNodeBool(val bool) Node -``` - -NewNodeBool converts a bool value to a BOOL node - -### func NewNodeByte - -```go -func NewNodeByte(val byte) Node -``` - -NewNodeInt8 converts a byte value to a BYTE node - -### func NewNodeDouble - -```go -func NewNodeDouble(val float64) Node -``` - -NewNodeDouble converts a float64 value to a DOUBLE node - -### func NewNodeInt16 - -```go -func NewNodeInt16(val int16) Node -``` - -NewNodeInt16 converts a int16 value to a I16 node - -### func NewNodeInt32 - -```go -func NewNodeInt32(val int32) Node -``` - -NewNodeInt32 converts a int32 value to a I32 node - -### func NewNodeInt64 - -```go -func NewNodeInt64(val int64) Node -``` - -NewNodeInt64 converts a int64 value to a I64 node - -### func NewNodeString - -```go -func NewNodeString(val string) Node -``` - -NewNodeString converts a string value to a STRING node - -### func NewTypedNode - -```go -func NewTypedNode(typ thrift.Type, et thrift.Type, kt thrift.Type) (ret Node) -``` - -NewTypedNode creates a new Node with the given typ, including element type \(for LIST/SET/MAP\) and key type \(for MAP\) - -### func \(Node\) Binary - -```go -func (self Node) Binary() ([]byte, error) -``` - -Binary returns the bytes value contained by a BINARY node - -### func \(Node\) Bool - -```go -func (self Node) Bool() (bool, error) -``` - -Bool returns the bool value contained by a BOOL node - -### func \(Node\) Byte - -```go -func (self Node) Byte() (byte, error) -``` - -Byte returns the byte value contained by a I8/BYTE node - -### func \(\*Node\) Check - -```go -func (self *Node) Check() error -``` - -Check checks if it is a ERROR node and returns corresponding error - -### func \(Node\) Children - -```go -func (self Node) Children(out *[]PathNode, recurse bool, opts *Options) (err error) -``` - -Children loads all its children and children's children recursively \(if recurse is true\). out is used for store children, and it is always reset to zero length before use. - -NOTICE: if opts.NotScanParentNode is true, the parent nodes \(PathNode.Node\) of complex \(LIST/SET/MAP/STRUCT\) type won't be assgined data - -### func \(Node\) ElemType - -```go -func (self Node) ElemType() thrift.Type -``` - -ElemType returns the thrift type of a LIST/SET/MAP node's element - -### func \(Node\) ErrCode - -```go -func (self Node) ErrCode() meta.ErrCode -``` - -ErrCode return the meta.ErrCode of a ERROR node - -### func \(Node\) Error - -```go -func (self Node) Error() string -``` - -Error return error message if it is a ERROR node - -### func \(Node\) Field - -```go -func (self Node) Field(id thrift.FieldID) (v Node) -``` - -Field returns a sub node at the given field id from a STRUCT node. - -### func \(Node\) Fields - -```go -func (self Node) Fields(ids []PathNode, opts *Options) (err error) -``` - -Fields returns all sub nodes ids along with the given int path from a STRUCT node. - -### func \(Node\) Float64 - -```go -func (self Node) Float64() (float64, error) -``` - -Float64 returns the float64 value contained by a DOUBLE node - -### func \(Node\) Foreach - -```go -func (self Node) Foreach(handler func(path Path, node Node) bool, opts *Options) error -``` - -Foreach scan each element of a complex type \(LIST/SET/MAP/STRUCT\), and call handler sequentially with corresponding path and node - -### func \(Node\) ForeachKV - -```go -func (self Node) ForeachKV(handler func(key Node, val Node) bool, opts *Options) error -``` - -ForeachKV scan each element of a MAP type, and call handler sequentially with corresponding key and value - -### func \(Node\) Fork - -```go -func (self Node) Fork() Node -``` - -Fork forks the node to a new node, copy underlying data as well - -### func \(Node\) GetByInt - -```go -func (self Node) GetByInt(key int) (v Node) -``` - -GetByInt returns a sub node at the given int key from a MAP\ node. - -### func \(Node\) GetByPath - -```go -func (self Node) GetByPath(pathes ...Path) Node -``` - -GetByPath searches longitudinally and return a sub node at the given path from the node. - -The path is a list of PathFieldId, PathIndex, PathStrKey, PathBinKey, PathIntKey, Each path MUST be a valid path type for current layer \(e.g. PathFieldId is only valid for STRUCT\). Empty path will return the current node. - -### func \(Node\) GetByRaw - -```go -func (self Node) GetByRaw(key []byte) (v Node) -``` - -GetByInt returns a sub node at the given bytes key from a MAP node. The key must be deep equal \(bytes.Equal\) to the key in the map. - -### func \(Node\) GetByStr - -```go -func (self Node) GetByStr(key string) (v Node) -``` - -GetByInt returns a sub node at the given int key from a MAP\ node. - -### func \(Node\) GetMany - -```go -func (self Node) GetMany(pathes []PathNode, opts *Options) error -``` - -GetMany searches transversely and returns all the sub nodes along with the given pathes. - -### func \(Node\) GetTree - -```go -func (self Node) GetTree(tree *PathNode, opts *Options) error -``` - -GetTree returns a tree of all sub nodes along with the given path on the tree. It supports longitudinally search \(like GetByPath\) and transversely search \(like GetMany\) both. - -### func \(Node\) Gets - -```go -func (self Node) Gets(keys []PathNode, opts *Options) (err error) -``` - -Gets returns all sub nodes along with the given key \(PathStrKey|PathIntKey|PathBinKey\) path from a MAP node. - -### func \(Node\) Index - -```go -func (self Node) Index(i int) (v Node) -``` - -Index returns a sub node at the given index from a LIST/SET node. - -### func \(Node\) Indexes - -```go -func (self Node) Indexes(ins []PathNode, opts *Options) (err error) -``` - -Indexes returns all sub nodes along with the given int path from a LIST/SET node. - -### func \(Node\) Int - -```go -func (self Node) Int() (int, error) -``` - -Int returns the int value contaned by a I8/I16/I32/I64 node - -### func \(Node\) IntMap - -```go -func (self Node) IntMap(opts *Options) (map[int]interface{}, error) -``` - -StrMap returns the integer keys and interface elements contained by a MAP\ node - -### func \(Node\) Interface - -```go -func (self Node) Interface(opts *Options) (interface{}, error) -``` - -Interface returns the go interface value contained by a node. If the node is a STRUCT, it will return a map\[thrift.FieldID\]interface\{\} If it is a map, it will return map\[int|string|interface\{\}\]interface\{\}, which depends on the key type - -### func \(Node\) InterfaceMap - -```go -func (self Node) InterfaceMap(opts *Options) (map[interface{}]interface{}, error) -``` - -InterfaceMap returns the interface keys and interface elements contained by a MAP node. If the key type is complex \(LIST/SET/MAP/STRUCT\), it will be stored using its pointer since its value are not supported by Go - -### func \(Node\) IsEmpty - -```go -func (self Node) IsEmpty() bool -``` - -IsEmpty tells if the node is thrift.STOP - -### func \(Node\) IsErrNotFound - -```go -func (self Node) IsErrNotFound() bool -``` - -IsErrorNotFound tells if the node is not\-found\-data error - -### func \(Node\) IsError - -```go -func (self Node) IsError() bool -``` - -IsEmtpy tells if the node is thrift.ERROR - -### func \(Node\) KeyType - -```go -func (self Node) KeyType() thrift.Type -``` - -KeyType returns the thrift type of a MAP node's key - -### func \(Node\) Len - -```go -func (self Node) Len() (int, error) -``` - -Len returns the element count of container\-kind type \(LIST/SET/MAP\) - -### func \(Node\) List - -```go -func (self Node) List(opts *Options) ([]interface{}, error) -``` - -List returns interface elements contained by a LIST/SET node - -### func \(Node\) Raw - -```go -func (self Node) Raw() []byte -``` - -Return its underlying raw data - -### func \(\*Node\) SetByPath - -```go -func (self *Node) SetByPath(sub Node, path ...Path) (exist bool, err error) -``` - -SetByPath sets the sub node at the given path. - -### func \(\*Node\) SetMany - -```go -func (self *Node) SetMany(pathes []PathNode, opts *Options) (err error) -``` - -SetMany searches longitudinally and sets the sub nodes at the given pathes. - -### func \(Node\) StrMap - -```go -func (self Node) StrMap(opts *Options) (map[string]interface{}, error) -``` - -StrMap returns the string keys and interface elements contained by a MAP\ node - -### func \(Node\) String - -```go -func (self Node) String() (string, error) -``` - -String returns the string value contianed by a STRING node - -### func \(Node\) Type - -```go -func (self Node) Type() thrift.Type -``` - -Type returns the thrift type of the node - -### func \(\*Node\) UnsetByPath - -```go -func (self *Node) UnsetByPath(path ...Path) error -``` - -## type Options - -Opions for generic.Node - -```go -type Options struct { - // DisallowUnknow indicates to report error when read unknown fields. - DisallowUnknow bool - - // WriteDefault indicates to write value if a DEFAULT requireness field is not set. - WriteDefault bool - - // NoCheckRequireNess indicates not to check requiredness when writing. - NotCheckRequireNess bool - - // UseNativeSkip indicates to use native.Skip (instead of go.Skip) method to skip thrift value - // WARN: this will promote performance when thrift value to be skipped is large, but may decrease preformance when thrift value is small. - UseNativeSkip bool - - // MapStructById indicates to use field id as map key instead of when call Node.Interface() on STRUCT type. - MapStructById bool - - // CastStringAsBinary indicates to cast STRING type to []byte when call Node.Interface()/Map(). - CastStringAsBinary bool - - // NotScanParentNode indicates to only assign children node when PathNode.Load()/Node.Children. - // Thies will promote performance but may be misued when handle PathNode. - NotScanParentNode bool - - // ClearDirtyValues indicates one multi-query (includeing - // Fields()/GetMany()/Gets()/Indexies()) to clear out all nodes - // in passed []PathNode first - ClearDirtyValues bool - - // StoreChildrenById indicates to store children node by id when call Node.Children() or PathNode.Load(). - // When field id exceeds StoreChildrenByIdShreshold, children node will be stored sequentially after the threshold. - StoreChildrenById bool - - // StoreChildrenByHash indicates to store children node by str hash (mod parent's size) when call Node.Children() or PathNode.Load(). - StoreChildrenByHash bool -} -``` - -## type Path - -Path represents the relative position of a sub node in a complex parent node - -```go -type Path struct { - // contains filtered or unexported fields -} -``` - -### func NewPathBinKey - -```go -func NewPathBinKey(key []byte) Path -``` - -NewPathBinKey creates a PathBinKey path - -### func NewPathFieldId - -```go -func NewPathFieldId(id thrift.FieldID) Path -``` - -NewPathFieldId creates a PathFieldId path - -### func NewPathFieldName - -```go -func NewPathFieldName(name string) Path -``` - -NewPathFieldName creates a PathFieldName path - -### func NewPathIndex - -```go -func NewPathIndex(index int) Path -``` - -NewPathIndex creates a PathIndex path - -### func NewPathIntKey - -```go -func NewPathIntKey(key int) Path -``` - -NewPathIntKey creates a PathIntKey path - -### func NewPathStrKey - -```go -func NewPathStrKey(key string) Path -``` - -NewPathStrKey creates a PathStrKey path - -### func \(Path\) Bin - -```go -func (self Path) Bin() []byte -``` - -Bin returns the raw bytes value of a PathBinKey path - -### func \(Path\) Id - -```go -func (self Path) Id() thrift.FieldID -``` - -Id returns the field id of a PathFieldId path - -### func \(Path\) Int - -```go -func (self Path) Int() int -``` - -Int returns the int value of a PathIndex\\PathIntKey path - -### func \(Path\) Str - -```go -func (self Path) Str() string -``` - -Str returns the string value of a PathFieldName\\PathStrKey path - -### func \(Path\) String - -```go -func (self Path) String() string -``` - -String returns the string representation of a Path - -### func \(Path\) ToRaw - -```go -func (self Path) ToRaw(t thrift.Type) []byte -``` - -ToRaw converts underlying value to thrift\-encoded bytes - -### func \(Path\) Type - -```go -func (self Path) Type() PathType -``` - -Type returns the type of a Path - -### func \(Path\) Value - -```go -func (self Path) Value() interface{} -``` - -Value returns the equivalent go interface of a Path - -## type PathNode - -PathNode is a three node of DOM tree - -```go -type PathNode struct { - Path - Node - Next []PathNode -} -``` - -### func NewPathNode - -```go -func NewPathNode() *PathNode -``` - -NewPathNode get a new PathNode from memory pool - -### func \(\*PathNode\) Assgin - -```go -func (self *PathNode) Assgin(recurse bool, opts *Options) error -``` - -Assgin assigns self's raw Value according to its Next Path, which must be set before calling this method. - -### func \(\*PathNode\) Check - -```go -func (self *PathNode) Check() error -``` - -Check returns non\-nil error if the PathNode has error - -### func \(PathNode\) CopyTo - -```go -func (self PathNode) CopyTo(to *PathNode) -``` - -CopyTo deeply copy self and its children to a PathNode - -### func \(PathNode\) Error - -```go -func (self PathNode) Error() string -``` - -Error returns non\-empty string if the PathNode has error - -### func \(\*PathNode\) Field - -```go -func (self *PathNode) Field(id thrift.FieldID, opts *Options) *PathNode -``` - -Field get the child node by field id. Only support STRUCT. - -If opts.StoreChildrenById is true, it will try to use id \(O\(1\)\) as index to search the key. However, if the struct fields have changed, it may fallback to O\(n\) search. - -### func \(PathNode\) Fork - -```go -func (self PathNode) Fork() PathNode -``` - -Fork deeply copy self and its children to a new PathNode - -### func \(\*PathNode\) GetByInt - -```go -func (self *PathNode) GetByInt(key int, opts *Options) *PathNode -``` - -GetByInt get the child node by integer. Only support MAP with integer\-type key. - -If opts.StoreChildrenByHash is true, it will try to use hash \(O\(1\)\) to search the key. However, if the map size has changed, it may fallback to O\(n\) search. - -### func \(\*PathNode\) GetByStr - -```go -func (self *PathNode) GetByStr(key string, opts *Options) *PathNode -``` - -GetByInt get the child node by string. Only support MAP with string\-type key. - -If opts.StoreChildrenByHash is true, it will try to use hash \(O\(1\)\) to search the key. However, if the map size has changed, it may fallback to O\(n\) search. - -### func \(\*PathNode\) Load - -```go -func (self *PathNode) Load(recurse bool, opts *Options) error -``` - -Load loads self's all children \( and children's children if recurse is true\) into self.Next, no matter whether self.Next is empty or set before \(will be reset\). NOTICE: if opts.NotScanParentNode is true, the parent nodes \(PathNode.Node\) of complex \(map/list/struct\) type won't be assgined data - -
Example -

- -```go -{ - - data := getExampleData() - root := PathNode{ - Node: NewNode(thrift.STRUCT, data), - } - - err := root.Load(false, opts) - if err != nil { - panic(err) - } - fmt.Printf("%#v", root) - - err = root.Load(true, opts) - if err != nil { - panic(err) - } - fmt.Printf("%#v", root) - - reuse := pathNodePool.Get().(*PathNode) - root.Node = NewNode(thrift.STRUCT, data) - err = root.Load(true, opts) - if err != nil { - panic(err) - } - fmt.Printf("%#v", root) - reuse.ResetValue() - pathNodePool.Put(reuse) - -} -``` - -

-
- -### func \(PathNode\) Marshal - -```go -func (self PathNode) Marshal(opt *Options) (out []byte, err error) -``` - -Marshal marshals self to thrift bytes - -### func \(PathNode\) MarshalIntoBuffer - -```go -func (self PathNode) MarshalIntoBuffer(out *[]byte, opt *Options) error -``` - -MarshalIntoBuffer marshals self to thrift bytes into a buffer - -### func \(\*PathNode\) ResetAll - -```go -func (self *PathNode) ResetAll() -``` - -ResetAll resets self and its children, including path and node both - -### func \(\*PathNode\) ResetValue - -```go -func (self *PathNode) ResetValue() -``` - -ResetValue resets self's node and its children's node - -### func \(\*PathNode\) SetByInt - -```go -func (self *PathNode) SetByInt(key int, val Node, opts *Options) (bool, error) -``` - -SetByInt set the child node by integer. Only support MAP with integer\-type key. If the key already exists, it will be overwritten and return true. - -If opts.StoreChildrenByHash is true, it will try to use hash \(O\(1\)\) to search the key. However, if the map hash size has changed, it may fallback to O\(n\) search. - -### func \(\*PathNode\) SetByStr - -```go -func (self *PathNode) SetByStr(key string, val Node, opts *Options) (bool, error) -``` - -SetByStr set the child node by string. Only support MAP with string\-type key. If the key already exists, it will be overwritten and return true. - -If opts.StoreChildrenByHash is true, it will try to use hash \(O\(1\)\) to search the key. However, if the map hash size has changed, it may fallback to O\(n\) search. - -### func \(\*PathNode\) SetField - -```go -func (self *PathNode) SetField(id thrift.FieldID, val Node, opts *Options) (bool, error) -``` - -SetField set the child node by field id. Only support STRUCT. If the key already exists, it will be overwritten and return true. - -If opts.StoreChildrenById is true, it will try to use id \(O\(1\)\) as index to search the key. However, if the struct fields have changed, it may fallback to O\(n\) search. - -## type PathType - -PathType is the type of path - -```go -type PathType uint8 -``` - -```go -const ( - // PathFieldId represents a field id of STRUCT type - PathFieldId PathType = 1 + iota - - // PathFieldName represents a field name of STRUCT type - // NOTICE: it is only supported by Value - PathFieldName - - // PathIndex represents a index of LIST\SET type - PathIndex - - // Path represents a string key of MAP type - PathStrKey - - // Path represents a int key of MAP type - PathIntKey - - // Path represents a raw-bytes key of MAP type - // It is usually used for neither-string-nor-integer type key - PathBinKey -) -``` - -## type Value - -Value is a generic API wrapper for operations on thrift data. Node is the underlying raw bytes. Desc is the corresponding type descriptor. - -```go -type Value struct { - Node - Desc *thrift.TypeDescriptor -} -``` - -
Example (3et Many) -

- -```go -{ - - desc := getExampleDesc() - data := getExampleData() - d1 := desc.Struct().FieldByKey("Msg").Type() - d2 := desc.Struct().FieldByKey("Subfix").Type() - v := NewValue(desc, data) - - p := thrift.NewBinaryProtocol([]byte{}) - e1 := "test1" - p.WriteString(e1) - v1 := NewValue(d1, p.RawBuf()) - p = thrift.NewBinaryProtocol([]byte{}) - e2 := float64(-255.0001) - p.WriteDouble(e2) - v2 := NewValue(d2, p.RawBuf()) - v3 := v.GetByPath(NewPathFieldName("Base")) - - ps := []PathNode{ - { - Path: NewPathFieldId(1), - Node: v1.Node, - }, - { - Path: NewPathFieldId(32767), - Node: v2.Node, - }, - { - Path: NewPathFieldId(255), - Node: v3.Node, - }, - } - - err := v.SetMany(ps, opts) - if err != nil { - panic(err) - } - any, err := v.Interface(opts) - if err != nil { - panic(err) - } - fmt.Printf("%#v", any) - - ps2 := []PathNode{ - { - Path: NewPathFieldId(1), - }, - { - Path: NewPathFieldId(32767), - }, - { - Path: NewPathFieldId(255), - }, - } - if err := v.GetMany(ps2, opts); err != nil { - panic(err) - } - any0, err := ps2[2].Node.Interface(opts) - if err != nil { - panic(err) - } - fmt.Printf("%#v", any0) -} -``` - -

-
- -### func NewValue - -```go -func NewValue(desc *thrift.TypeDescriptor, src []byte) Value -``` - -NewValue creates a new Value from a raw byte slice. - -### func \(Value\) Field - -```go -func (self Value) Field(id thrift.FieldID) (v Value) -``` - -Field returns a sub node at the given field id from a STRUCT value. - -### func \(Value\) FieldByName - -```go -func (self Value) FieldByName(name string) (v Value) -``` - -FieldByName returns a sub node at the given field name from a STRUCT value. - -### func \(Value\) Fork - -```go -func (self Value) Fork() Value -``` - -NewValueFromNode copy both Node and TypeDescriptor from another Value. - -### func \(Value\) GetByInt - -```go -func (self Value) GetByInt(key int) (v Value) -``` - -GetByInt returns a sub node at the given int key from a MAP value. - -### func \(Value\) GetByPath - -```go -func (self Value) GetByPath(pathes ...Path) Value -``` - -GetByPath searches longitudinally and return a sub value at the given path from the value. - -The path is a list of PathFieldId, PathFieldName, PathIndex, PathStrKey, PathBinKey, PathIntKey, Each path MUST be a valid path type for current layer \(e.g. PathFieldId is only valid for STRUCT\). Empty path will return the current value. - -### func \(Value\) GetByStr - -```go -func (self Value) GetByStr(key string) (v Value) -``` - -GetByStr returns a sub node at the given string key from a MAP value. - -### func \(Value\) Index - -```go -func (self Value) Index(i int) (v Value) -``` - -Index returns a sub node at the given index from a LIST value. - -### func \(Value\) MarshalTo - -```go -func (self Value) MarshalTo(to *thrift.TypeDescriptor, opts *Options) ([]byte, error) -``` - -MarshalTo marshals self value into a sub value descripted by the to descriptor, alse called as "Cutting". Usually, the to descriptor is a subset of self descriptor. - -
Example -

- -```go -{ - - desc := getExampleDesc() - data := getExampleData() - v := NewValue(desc, data) - - full, err := NewNode(thrift.STRUCT, data).Interface(opts) - if err != nil { - panic(err) - } - fmt.Printf("%#v", full) - - pdesc := getExamplePartialDesc() - - out, err := v.MarshalTo(pdesc, opts) - if err != nil { - panic(err) - } - - partial, err := NewNode(thrift.STRUCT, out).Interface(opts) - if err != nil { - panic(err) - } - fmt.Printf("%#v", partial) -} -``` - -

-
- -### func \(\*Value\) SetByPath - -```go -func (self *Value) SetByPath(sub Value, path ...Path) (exist bool, err error) -``` - -SetByPath searches longitudinally and sets a sub value at the given path from the value. exist tells whether the node is already exists. - -
Example -

- -```go -{ - - desc := getExampleDesc() - data := getExampleData() - v := NewValue(desc, data) - - d := desc.Struct().FieldByKey("Base").Type().Struct().FieldByKey("Extra").Type().Elem() - p := thrift.NewBinaryProtocol([]byte{}) - exp := "中文" - p.WriteString(exp) - buf := p.RawBuf() - vv := NewValue(d, buf) - - ps := []Path{NewPathFieldName("Base"), NewPathFieldName("Extra"), NewPathStrKey("b")} - exist, err2 := v.SetByPath(vv, ps...) - if err2 != nil { - panic(err2) - } - println(exist) - - s2 := v.GetByPath(ps...) - if s2.Error() != "" { - panic(s2.Error()) - } - f2, _ := s2.String() - println(f2) -} -``` - -

-
- -### func \(\*Value\) UnsetByPath - -```go -func (self *Value) UnsetByPath(path ...Path) error -``` - -UnsetByPath searches longitudinally and unsets a sub value at the given path from the value. - - - -Generated by [gomarkdoc]()