| 1 |
|
| 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/encoding" |
| 24 |
{{range .Imports}} |
| 25 |
{{ printf "%q" . -}} |
| 26 |
{{end}} |
| 27 |
) |
| 28 |
|
| 29 |
{{.RequireType "C" -}}{{/* C is assumed to be a struct or primitive type */ -}} |
| 30 |
type ( |
| 31 |
// Request generates a Call that's sent to a Mesos agent. Subsequent invocations are expected to |
| 32 |
// yield equivalent calls. Intended for use w/ non-streaming requests to an agent. |
| 33 |
Request interface { |
| 34 |
Call() *{{.Type "C"}} |
| 35 |
} |
| 36 |
|
| 37 |
// RequestFunc is the functional adaptation of Request. |
| 38 |
RequestFunc func() *{{.Type "C"}} |
| 39 |
|
| 40 |
// RequestStreaming generates a Call that's send to a Mesos agent. Subsequent invocations MAY generate |
| 41 |
// different Call objects. No more Call objects are expected once a nil is returned to signal the end of |
| 42 |
// of the request stream. |
| 43 |
RequestStreaming interface { |
| 44 |
Request |
| 45 |
IsStreaming() |
| 46 |
} |
| 47 |
|
| 48 |
// RequestStreamingFunc is the functional adaptation of RequestStreaming. |
| 49 |
RequestStreamingFunc func() *{{.Type "C"}} |
| 50 |
|
| 51 |
// Send issues a Request to a Mesos agent and properly manages Call-specific mechanics. |
| 52 |
Sender interface { |
| 53 |
Send(context.Context, Request) (mesos.Response, error) |
| 54 |
} |
| 55 |
|
| 56 |
// SenderFunc is the functional adaptation of the Sender interface |
| 57 |
SenderFunc func(context.Context, Request) (mesos.Response, error) |
| 58 |
) |
| 59 |
|
| 60 |
func (f RequestFunc) Call() *{{.Type "C"}} { return f() } |
| 61 |
|
| 62 |
func (f RequestFunc) Marshaler() encoding.Marshaler { |
| 63 |
// avoid returning (*{{.Type "C"}})(nil) for interface type |
| 64 |
if call := f(); call != nil { |
| 65 |
return call |
| 66 |
} |
| 67 |
return nil |
| 68 |
} |
| 69 |
|
| 70 |
func (f RequestStreamingFunc) Push(c ...*{{.Type "C"}}) RequestStreamingFunc { return Push(f, c...) } |
| 71 |
|
| 72 |
func (f RequestStreamingFunc) Marshaler() encoding.Marshaler { |
| 73 |
// avoid returning (*{{.Type "C"}})(nil) for interface type |
| 74 |
if call := f(); call != nil { |
| 75 |
return call |
| 76 |
} |
| 77 |
return nil |
| 78 |
} |
| 79 |
|
| 80 |
func (f RequestStreamingFunc) IsStreaming() {} |
| 81 |
|
| 82 |
func (f RequestStreamingFunc) Call() *{{.Type "C"}} { return f() } |
| 83 |
|
| 84 |
// Push prepends one or more calls onto a request stream. If no calls are given then the original stream is returned. |
| 85 |
func Push(r RequestStreaming, c ...*{{.Type "C"}}) RequestStreamingFunc { |
| 86 |
return func() *{{.Type "C"}} { |
| 87 |
if len(c) == 0 { |
| 88 |
return r.Call() |
| 89 |
} |
| 90 |
head := c[0] |
| 91 |
c = c[1:] |
| 92 |
return head |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// Empty generates a stream that always returns nil. |
| 97 |
func Empty() RequestStreamingFunc { return func() *{{.Type "C"}} { return nil } } |
| 98 |
|
| 99 |
var ( |
| 100 |
_ = Request(RequestFunc(nil)) |
| 101 |
_ = RequestStreaming(RequestStreamingFunc(nil)) |
| 102 |
_ = Sender(SenderFunc(nil)) |
| 103 |
) |
| 104 |
|
| 105 |
// NonStreaming returns a RequestFunc that always generates the same Call. |
| 106 |
func NonStreaming(c *{{.Type "C"}}) RequestFunc { return func() *{{.Type "C"}} { return c } } |
| 107 |
|
| 108 |
// FromChan returns a streaming request that fetches calls from the given channel until it closes. |
| 109 |
// If a nil chan is specified then the returned func will always generate nil. |
| 110 |
func FromChan(ch <-chan *{{.Type "C"}}) RequestStreamingFunc { |
| 111 |
if ch == nil { |
| 112 |
// avoid blocking forever if we're handed a nil chan |
| 113 |
return func() *{{.Type "C"}} { return nil } |
| 114 |
} |
| 115 |
return func() *{{.Type "C"}} { |
| 116 |
if m, ok := <-ch; ok { |
| 117 |
return m |
| 118 |
} |
| 119 |
return nil |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// Send implements the Sender interface for SenderFunc |
| 124 |
func (f SenderFunc) Send(ctx context.Context, r Request) (mesos.Response, error) { |
| 125 |
return f(ctx, r) |
| 126 |
} |
| 127 |
|
| 128 |
// IgnoreResponse generates a sender that closes any non-nil response received by Mesos. |
| 129 |
func IgnoreResponse(s Sender) SenderFunc { |
| 130 |
return func(ctx context.Context, r Request) (mesos.Response, error) { |
| 131 |
resp, err := s.Send(ctx, r) |
| 132 |
if resp != nil { |
| 133 |
resp.Close() |
| 134 |
} |
| 135 |
return nil, err |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
// SendNoData is a convenience func that executes the given Call using the provided Sender |
| 140 |
// and always drops the response data. |
| 141 |
func SendNoData(ctx context.Context, sender Sender, r Request) (err error) { |
| 142 |
_, err = IgnoreResponse(sender).Send(ctx, r) |
| 143 |
return |
| 144 |
} |
| 145 |
{{if .Type "O"}}{{/* O is a functional call option, C.With(...O) must be defined elsewhere */}} |
| 146 |
// SendWith injects the given options for all calls. |
| 147 |
func SenderWith(s Sender, opts ...{{.Type "O"}}) SenderFunc { |
| 148 |
if len(opts) == 0 { |
| 149 |
return s.Send |
| 150 |
} |
| 151 |
return func(ctx context.Context, r Request) (mesos.Response, error) { |
| 152 |
f := func() (c *{{.Type "C"}}) { |
| 153 |
if c = r.Call(); c != nil { |
| 154 |
c = c.With(opts...) |
| 155 |
} |
| 156 |
return |
| 157 |
} |
| 158 |
switch r.(type) { |
| 159 |
case RequestStreaming: |
| 160 |
return s.Send(ctx, RequestStreamingFunc(f)) |
| 161 |
default: |
| 162 |
return s.Send(ctx, RequestFunc(f)) |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
{{end -}} |
| 167 |
`)) |
| 168 |
|
| 169 |
var testTemplate = template.Must(template.New("").Parse(`package {{.Package}} |
| 170 |
|
| 171 |
// go generate {{.Args}} |
| 172 |
// GENERATED CODE FOLLOWS; DO NOT EDIT. |
| 173 |
|
| 174 |
import ( |
| 175 |
"context" |
| 176 |
"testing" |
| 177 |
|
| 178 |
"github.com/mesos/mesos-go/api/v1/lib" |
| 179 |
{{range .Imports}} |
| 180 |
{{ printf "%q" . -}} |
| 181 |
{{end}} |
| 182 |
) |
| 183 |
|
| 184 |
func TestNonStreaming(t *testing.T) { |
| 185 |
c := new({{.Type "C"}}) |
| 186 |
f := NonStreaming(c) |
| 187 |
if x := f.Call(); x != c { |
| 188 |
t.Fatalf("expected %#v instead of %#v", c, x) |
| 189 |
} |
| 190 |
if x := f.Marshaler(); x == nil { |
| 191 |
t.Fatal("expected non-nil Marshaler") |
| 192 |
} |
| 193 |
f = NonStreaming(nil) |
| 194 |
if x := f.Marshaler(); x != nil { |
| 195 |
t.Fatalf("expected nil Marshaler instead of %#v", x) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func TestStreaming(t *testing.T) { |
| 200 |
f := Empty() |
| 201 |
|
| 202 |
f.IsStreaming() |
| 203 |
|
| 204 |
if x := f.Call(); x != nil { |
| 205 |
t.Fatalf("expected nil Call instead of %#v", x) |
| 206 |
} |
| 207 |
if x := f.Marshaler(); x != nil { |
| 208 |
t.Fatalf("expected nil Call instead of %#v", x) |
| 209 |
} |
| 210 |
|
| 211 |
c := new({{.Type "C"}}) |
| 212 |
|
| 213 |
f = f.Push(c) |
| 214 |
if x := f.Marshaler(); x == nil { |
| 215 |
t.Fatal("expected non-nil Marshaler") |
| 216 |
} |
| 217 |
if x := f.Marshaler(); x != nil { |
| 218 |
t.Fatalf("expected nil Marshaler instead of %#v", x) |
| 219 |
} |
| 220 |
|
| 221 |
c2 := new({{.Type "C"}}) |
| 222 |
|
| 223 |
f = Empty().Push(c, c2) |
| 224 |
if x := f.Call(); x != c { |
| 225 |
t.Fatalf("expected %#v instead of %#v", c, x) |
| 226 |
} |
| 227 |
if x := f.Call(); x != c2 { |
| 228 |
t.Fatalf("expected %#v instead of %#v", c2, x) |
| 229 |
} |
| 230 |
if x := f.Call(); x != nil { |
| 231 |
t.Fatalf("expected nil Call instead of %#v", x) |
| 232 |
} |
| 233 |
|
| 234 |
ch := make(chan *{{.Type "C"}}, 2) |
| 235 |
ch <- c |
| 236 |
ch <- c2 |
| 237 |
close(ch) |
| 238 |
f = FromChan(ch) |
| 239 |
if x := f.Call(); x != c { |
| 240 |
t.Fatalf("expected %#v instead of %#v", c, x) |
| 241 |
} |
| 242 |
if x := f.Call(); x != c2 { |
| 243 |
t.Fatalf("expected %#v instead of %#v", c2, x) |
| 244 |
} |
| 245 |
if x := f.Call(); x != nil { |
| 246 |
t.Fatalf("expected nil Call instead of %#v", x) |
| 247 |
} |
| 248 |
|
| 249 |
f = FromChan(nil) |
| 250 |
if x := f.Call(); x != nil { |
| 251 |
t.Fatalf("expected nil Call instead of %#v", x) |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
func TestIgnoreResponse(t *testing.T) { |
| 256 |
var closed bool |
| 257 |
|
| 258 |
IgnoreResponse(SenderFunc(func(_ context.Context, _ Request) (mesos.Response, error) { |
| 259 |
return &mesos.ResponseWrapper{Closer: mesos.CloseFunc(func() error { |
| 260 |
closed = true |
| 261 |
return nil |
| 262 |
})}, nil |
| 263 |
})).Send(nil, nil) |
| 264 |
|
| 265 |
if !closed { |
| 266 |
t.Fatal("expected response to be closed") |
| 267 |
} |
| 268 |
} |
| 269 |
{{if .Type "O"}}{{/* O is a functional call option, C.With(...O) must be defined elsewhere */}} |
| 270 |
func TestSenderWith(t *testing.T) { |
| 271 |
var ( |
| 272 |
s = SenderFunc(func(_ context.Context, r Request) (mesos.Response, error) { |
| 273 |
_ = r.Call() // need to invoke this to invoke SenderWith call decoration |
| 274 |
return nil, nil |
| 275 |
}) |
| 276 |
ignore = func(_ mesos.Response, _ error) {} |
| 277 |
c = new({{.Type "C"}}) |
| 278 |
) |
| 279 |
|
| 280 |
for ti, tc := range []Request{NonStreaming(c), Empty().Push(c, c)} { |
| 281 |
var ( |
| 282 |
invoked bool |
| 283 |
opt = func(c *{{.Type "C"}}) { invoked = true } |
| 284 |
) |
| 285 |
|
| 286 |
// sanity check (w/o any options) |
| 287 |
ignore(SenderWith(s).Send(context.Background(), tc)) |
| 288 |
if invoked { |
| 289 |
t.Fatalf("test case %d failed: unexpected option invocation", ti) |
| 290 |
} |
| 291 |
|
| 292 |
ignore(SenderWith(s, opt).Send(context.Background(), tc)) |
| 293 |
if !invoked { |
| 294 |
t.Fatalf("test case %d failed: expected option invocation", ti) |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
{{end -}} |
| 299 |
`)) |