File: handlers.go

package info (click to toggle)
golang-github-mesos-mesos-go 0.0.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 11,724 kB
  • sloc: makefile: 163
file content (104 lines) | stat: -rw-r--r-- 2,854 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// +build ignore

package main

import (
	"os"
	"text/template"
)

func main() {
	Run(handlersTemplate, nil, os.Args...)
}

var handlersTemplate = template.Must(template.New("").Parse(`package {{.Package}}

// go generate {{.Args}}
// GENERATED CODE FOLLOWS; DO NOT EDIT.

import (
	"context"
{{range .Imports}}
	{{ printf "%q" . -}}
{{end}}
)

{{.RequireType "E" -}}
{{.RequireType "ET" -}}
type (
	// Handler is invoked upon the occurrence of some scheduler event that is generated
	// by some other component in the Mesos ecosystem (e.g. master, agent, executor, etc.)
	Handler interface {
		HandleEvent(context.Context, {{.Type "E"}}) error
	}

	// HandlerFunc is a functional adaptation of the Handler interface
	HandlerFunc func(context.Context, {{.Type "E"}}) error

	// Handlers executes an event Handler according to the event's type
	Handlers map[{{.Type "ET"}}]Handler

	// HandlerFuncs executes an event HandlerFunc according to the event's type
	HandlerFuncs map[{{.Type "ET"}}]HandlerFunc
)

// HandleEvent implements Handler for HandlerFunc
func (f HandlerFunc) HandleEvent(ctx context.Context, e {{.Type "E"}}) error { return f(ctx, e) }

type noopHandler int

func (noopHandler) HandleEvent(_ context.Context, _ {{.Type "E"}}) error { return nil }

// NoopHandler is a Handler that does nothing and always returns nil
const NoopHandler = noopHandler(0)

// HandleEvent implements Handler for Handlers
func (hs Handlers) HandleEvent(ctx context.Context, e {{.Type "E"}}) (err error) {
	if h := hs[e.GetType()]; h != nil {
		return h.HandleEvent(ctx, e)
	}
	return nil
}

// HandleEvent implements Handler for HandlerFuncs
func (hs HandlerFuncs) HandleEvent(ctx context.Context, e {{.Type "E"}}) (err error) {
	if h := hs[e.GetType()]; h != nil {
		return h.HandleEvent(ctx, e)
	}
	return nil
}

// Otherwise returns a HandlerFunc that attempts to process an event with the Handlers map; unmatched event types are
// processed by the given HandlerFunc. A nil HandlerFunc parameter is effecitvely a noop.
func (hs Handlers) Otherwise(f HandlerFunc) HandlerFunc {
	if f == nil {
		return hs.HandleEvent
	}
	return func(ctx context.Context, e {{.Type "E"}}) error {
		if h := hs[e.GetType()]; h != nil {
			return h.HandleEvent(ctx, e)
		}
		return f(ctx, e)
	}
}

// Otherwise returns a HandlerFunc that attempts to process an event with the HandlerFuncs map; unmatched event types
// are processed by the given HandlerFunc. A nil HandlerFunc parameter is effecitvely a noop.
func (hs HandlerFuncs) Otherwise(f HandlerFunc) HandlerFunc {
	if f == nil {
		return hs.HandleEvent
	}
	return func(ctx context.Context, e {{.Type "E"}}) error {
		if h := hs[e.GetType()]; h != nil {
			return h.HandleEvent(ctx, e)
		}
		return f(ctx, e)
	}
}

var (
	_ = Handler(Handlers(nil))
	_ = Handler(HandlerFunc(nil))
	_ = Handler(HandlerFuncs(nil))
)
`))