Debian Codegraph @

api/v1/lib/extras/gen/httpsender.go

raw symbols SCIP
1 // +build ignore
2
3 package main
4
5 import (
6 "os"
7 "text/template"
8 )
9
10 func main() {
11 Run(srcTemplate, testTemplate, os.Args...)
12 }
13
14 var srcTemplate = template.Must(template.New("").Parse(`package {{.Package}}
15
16 // go generate {{.Args}}
17 // GENERATED CODE FOLLOWS; DO NOT EDIT.
18
19 import (
20 "context"
21
22 "github.com/mesos/mesos-go/api/v1/lib"
23 "github.com/mesos/mesos-go/api/v1/lib/client"
24 "github.com/mesos/mesos-go/api/v1/lib/httpcli"
25 {{range .Imports}}{{/* a "calls" package is assumed to be imported */}}
26 {{ printf "%q" . -}}
27 {{end}}
28 )
29
30 {{.RequireType "C" -}}{{/* C is assumed to be a struct type */ -}}
31 {{.RequirePrototype "C" -}}
32 // ResponseClassifier determines the appropriate response class for the given call.
33 type ResponseClassifier func(*{{.Type "C"}}) (client.ResponseClass, error)
34
35 // ClientFunc sends a Request to Mesos and returns the generated Response.
36 type ClientFunc func(client.Request, client.ResponseClass, ...httpcli.RequestOpt) (mesos.Response, error)
37
38 {{/* classifyResponse must be declared in the package wherein this generated module lives */ -}}
39 // DefaultResponseClassifier is a pluggable classifier.
40 var DefaultResponseClassifier = ResponseClassifier(classifyResponse)
41
42 // NewSender generates a sender that uses the Mesos v1 HTTP API for encoding/decoding requests/responses.
43 // The ResponseClass is inferred from the first object generated by the given Request.
44 func NewSender(cf ClientFunc, ro ...httpcli.RequestOpt) calls.Sender {
45 return calls.SenderFunc(func(ctx context.Context, r calls.Request) (mesos.Response, error) {
46 var (
47 obj = r.Call()
48 rc, err = DefaultResponseClassifier(obj)
49 )
50 if err != nil {
51 return nil, err
52 }
53
54 var req client.Request
55
56 switch r := r.(type) {
57 case calls.RequestStreaming:
58 req = calls.Push(r, obj)
59 default:
60 req = calls.NonStreaming(obj)
61 }
62
63 return cf(req, rc, append(ro, httpcli.Context(ctx))...)
64 })
65 }
66 `))
67 var testTemplate = template.Must(template.New("").Parse(`package {{.Package}}
68
69 // go generate {{.Args}}
70 // GENERATED CODE FOLLOWS; DO NOT EDIT.
71
72 import (
73 "context"
74 "testing"
75
76 "github.com/mesos/mesos-go/api/v1/lib"
77 "github.com/mesos/mesos-go/api/v1/lib/client"
78 "github.com/mesos/mesos-go/api/v1/lib/httpcli"
79 {{range .Imports}}{{/* a "calls" package is assumed to be imported */}}
80 {{ printf "%q" . -}}
81 {{end}}
82 )
83
84 func TestNewSender(t *testing.T) {
85 ch := make(chan client.Request, 1)
86 cf := ClientFunc(func(r client.Request, _ client.ResponseClass, _ ...httpcli.RequestOpt) (_ mesos.Response, _ error) {
87 ch <- r
88 return
89 })
90 check := func(_ mesos.Response, err error) {
91 if err != nil {
92 t.Fatal(err)
93 }
94 }
95 sent := func() client.Request {
96 select {
97 case r := <-ch:
98 return r
99 default:
100 t.Fatal("no request was sent")
101 }
102 return nil
103 }
104 sender := NewSender(cf)
105 c := &{{.Prototype "C"}}
106
107 check(sender.Send(context.Background(), calls.NonStreaming(c)))
108 r := sent()
109 if _, ok := r.(client.RequestStreaming); ok {
110 t.Fatalf("expected non-streaming request instead of %v", r)
111 }
112
113 check(sender.Send(context.Background(), calls.Empty().Push(c)))
114 r = sent()
115 if _, ok := r.(client.RequestStreaming); !ok {
116 t.Fatalf("expected streaming request instead of %v", r)
117 }
118
119 // expect this to fail because newly created call structs don't have a type
120 // that can be used for classifying an expected response type.
121 _, err := sender.Send(context.Background(), calls.Empty().Push(new({{.Type "C"}})))
122 if err == nil {
123 t.Fatal("expected send to fail w/ malformed call")
124 }
125 }
126 `))