()
| 51 | } |
| 52 | |
| 53 | func ExampleHTTPConv_DoInto() { |
| 54 | // get function descriptor |
| 55 | svc, err := thrift.NewDescritorFromPath(context.Background(), exampleIDLPath) |
| 56 | if err != nil { |
| 57 | panic(err) |
| 58 | } |
| 59 | fn := svc.Functions()["ExampleMethod"] |
| 60 | |
| 61 | // make HTTPConv |
| 62 | cv := NewHTTPConv(meta.EncodingThriftBinary, fn) |
| 63 | |
| 64 | // make std http request |
| 65 | jdata := `{"msg":"hello","InnerBase":{}}` |
| 66 | stdreq, err := stdhttp.NewRequest("POST", |
| 67 | "http://localhost:8080/example?query=1,2,3&inner_query=中文", |
| 68 | strings.NewReader(jdata)) |
| 69 | if err != nil { |
| 70 | panic(err) |
| 71 | } |
| 72 | stdreq.Header.Set("Content-Type", "application/json") |
| 73 | stdreq.Header.Set("heeader", "true") |
| 74 | stdreq.Header.Set("inner_string", "igorned") |
| 75 | |
| 76 | // wrap std http request as http.RequestGetter |
| 77 | req, err := http.NewHTTPRequestFromStdReq( |
| 78 | stdreq, |
| 79 | http.Param{Key: "path", Value: "OK"}, |
| 80 | http.Param{Key: "inner_string", Value: "priority"}, |
| 81 | ) |
| 82 | |
| 83 | // allocate output buffer |
| 84 | // TIPS: json data size is usually 1.5x times of thrift data size |
| 85 | buf := make([]byte, 0, len(jdata)*2/3) |
| 86 | |
| 87 | // do conversion |
| 88 | err = cv.DoInto(context.Background(), req, &buf, opts) |
| 89 | if err != nil { |
| 90 | panic(err) |
| 91 | } |
| 92 | |
| 93 | // unwrap thrift message |
| 94 | p := thrift.NewBinaryProtocol(buf) |
| 95 | method, mType, seqID, reqID, stru, err := p.UnwrapBody() |
| 96 | println(method, mType, seqID, reqID) // ExampleMethod 1 0 1 |
| 97 | |
| 98 | // validate result |
| 99 | act := example3.NewExampleReq() |
| 100 | _, err = act.FastRead(stru) |
| 101 | if err != nil { |
| 102 | panic(err) |
| 103 | } |
| 104 | spew.Dump(act) |
| 105 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…