Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Mar 15, 2024
1 parent d5dde90 commit 9901f49
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions thrift/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,10 @@ func GoType2ThriftType(val interface{}) Type {
if ok {
return STRUCT
}
_, ok = val.([]byte)
if ok {
return STRING
}
switch reflect.TypeOf(val).Kind() {
case reflect.Bool:
return BOOL
Expand Down
39 changes: 39 additions & 0 deletions thrift/generic/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generic

import (
"fmt"
"reflect"

"github.com/cloudwego/dynamicgo/thrift"
)
Expand All @@ -10,6 +11,44 @@ var opts = &Options{
UseNativeSkip: true,
}

func ExampleNewTypedNode() {
// make a map<string,list<i32>> node
ret := NewTypedNode(thrift.MAP, thrift.LIST, thrift.STRING, PathNode{
Path: NewPathStrKey("1"),
Node: NewNodeList([]interface{}{int32(1), int32(2)}),
})

// print raw data
fmt.Printf("buf:%+v\n", ret.Raw())

// print interface
val, err := ret.Interface(opts)
fmt.Printf("val:%#v\n", val)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(val, map[string]interface{}{"1": []interface{}{int(1), int(2)}}) {
panic("not equal")
}

// make a struct{1:map<string, binary>} node
ret = NewTypedNode(thrift.STRUCT, 0, 0, PathNode{
Path: NewPathFieldId(1),
Node: NewNodeMap(map[interface{}]interface{}{"1": []byte{1}}),
})
// print interface
opts.CastStringAsBinary = true
opts.MapStructById = true
val, err = ret.Interface(opts)
fmt.Printf("val:%#v\n", val)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(val, map[thrift.FieldID]interface{}{thrift.FieldID(1): map[string]interface{}{"1": []byte{1}}}) {
panic("not equal")
}
}

func ExampleValue_SetByPath() {
// pack root value
desc := getExampleDesc()
Expand Down

0 comments on commit 9901f49

Please sign in to comment.