github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/example_test.go (about)

     1  package thrift
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  
     8  	"github.com/cloudwego/dynamicgo/meta"
     9  )
    10  
    11  func ExampleNewDescritorFromPath() {
    12  	// default Options
    13  	p1, err := NewDescritorFromPath(context.Background(), "../testdata/idl/example.thrift")
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  	r1, _ := p1.LookupFunctionByMethod("ExampleMethod")
    18  	fmt.Printf("%#v\n", r1.Response())
    19  
    20  	// With Options.ParseFunctionMode = ParseRequestOnly
    21  	p2, err := Options{
    22  		ParseFunctionMode: meta.ParseRequestOnly,
    23  	}.NewDescritorFromPath(context.Background(), "../testdata/idl/example.thrift")
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	r2, _ := p2.LookupFunctionByMethod("ExampleMethod")
    28  	fmt.Printf("%#v\n", r2.Response())
    29  }
    30  
    31  func ExampleNewDescritorFromContent() {
    32  	path := "a/b/main.thrift"
    33  	content := `
    34  	include "base/base.thrift"
    35  	namespace go test.server
    36  
    37  	service InboxService {
    38  		base.BaseResp ExampleMethod(1: base.Base req)
    39  	}`
    40  	base := `
    41  	namespace py base
    42  	namespace go base
    43  	namespace java com.bytedance.thrift.base
    44  	
    45  	struct TrafficEnv {
    46  		1: bool Open = false,
    47  		2: string Env = "",
    48  	}
    49  	
    50  	struct Base {
    51  		1: string LogID = "",
    52  		2: string Caller = "",
    53  		3: string Addr = "",
    54  		4: string Client = "",
    55  		5: optional TrafficEnv TrafficEnv,
    56  		6: optional map<string, string> Extra,
    57  	}
    58  	
    59  	struct BaseResp {
    60  		1: string StatusMessage = "",
    61  		2: i32 StatusCode = 0,
    62  		3: optional map<string, string> Extra,
    63  	}
    64  	`
    65  
    66  	includes := map[string]string{
    67  		path:                   content,
    68  		"a/b/base/base.thrift": base,
    69  	}
    70  	// default Options
    71  	p1, err := NewDescritorFromContent(context.Background(), path, content, includes, true)
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  	r1, _ := p1.LookupFunctionByMethod("ExampleMethod")
    76  	fmt.Printf("%#v\n", r1.Response())
    77  
    78  	// use relative path here
    79  	delete(includes, "a/b/base/base.thrift")
    80  	includes["base/base.thrift"] = base
    81  	// With Options.ParseFunctionMode = ParseRequestOnly
    82  	p2, err := Options{
    83  		ParseFunctionMode: meta.ParseRequestOnly,
    84  	}.NewDescritorFromContent(context.Background(), path, content, includes, false)
    85  	if err != nil {
    86  		panic(err)
    87  	}
    88  	r2, _ := p2.LookupFunctionByMethod("ExampleMethod")
    89  	fmt.Printf("%#v\n", r2.Response())
    90  }
    91  
    92  func ExampleBinaryProtocol_ReadAnyWithDesc() {
    93  	p1, err := NewDescritorFromPath(context.Background(), "../testdata/idl/example2.thrift")
    94  	if err != nil {
    95  		panic(err)
    96  	}
    97  	example2ReqDesc := p1.Functions()["ExampleMethod"].Request().Struct().FieldById(1).Type()
    98  	data, err := ioutil.ReadFile("../testdata/data/example2.bin")
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  
   103  	p := NewBinaryProtocol(data)
   104  	v, err := p.ReadAnyWithDesc(example2ReqDesc, false, false, false, true)
   105  	if err != nil {
   106  		panic(err)
   107  	}
   108  	fmt.Printf("%#v", v)
   109  	p = NewBinaryProtocolBuffer()
   110  	err = p.WriteAnyWithDesc(example2ReqDesc, v, true, true, true)
   111  	if err != nil {
   112  		panic(err)
   113  	}
   114  	fmt.Printf("%x", p.RawBuf())
   115  }