| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"os" |
| 7 |
"text/template" |
| 8 |
) |
| 9 |
|
| 10 |
func main() { |
| 11 |
Run(handlersTemplate, nil, os.Args...) |
| 12 |
} |
| 13 |
|
| 14 |
var handlersTemplate = 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 |
{{range .Imports}} |
| 22 |
{{ printf "%q" . -}} |
| 23 |
{{end}} |
| 24 |
) |
| 25 |
|
| 26 |
{{.RequireType "E" -}} |
| 27 |
{{.RequireType "H" -}} |
| 28 |
{{.RequireType "HF" -}} |
| 29 |
// Handle generates a rule that executes the given {{.Type "H"}}. |
| 30 |
func Handle(h {{.Type "H"}}) Rule { |
| 31 |
if h == nil { |
| 32 |
return nil |
| 33 |
} |
| 34 |
return func(ctx context.Context, e {{.Type "E"}}, err error, chain Chain) (context.Context, {{.Type "E"}}, error) { |
| 35 |
newErr := h.HandleEvent(ctx, e) |
| 36 |
return chain(ctx, e, Error2(err, newErr)) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// HandleF is the functional equivalent of Handle |
| 41 |
func HandleF(h {{.Type "HF"}}) Rule { |
| 42 |
return Handle({{.Type "H"}}(h)) |
| 43 |
} |
| 44 |
|
| 45 |
// Handle returns a Rule that invokes the receiver, then the given {{.Type "H"}} |
| 46 |
func (r Rule) Handle(h {{.Type "H"}}) Rule { |
| 47 |
return Rules{r, Handle(h)}.Eval |
| 48 |
} |
| 49 |
|
| 50 |
// HandleF is the functional equivalent of Handle |
| 51 |
func (r Rule) HandleF(h {{.Type "HF"}}) Rule { |
| 52 |
return r.Handle({{.Type "H"}}(h)) |
| 53 |
} |
| 54 |
|
| 55 |
// HandleEvent implements {{.Type "H"}} for Rule |
| 56 |
func (r Rule) HandleEvent(ctx context.Context, e {{.Type "E"}}) (err error) { |
| 57 |
if r == nil { |
| 58 |
return nil |
| 59 |
} |
| 60 |
_, _, err = r(ctx, e, nil, ChainIdentity) |
| 61 |
return |
| 62 |
} |
| 63 |
|
| 64 |
// HandleEvent implements {{.Type "H"}} for Rules |
| 65 |
func (rs Rules) HandleEvent(ctx context.Context, e {{.Type "E"}}) error { |
| 66 |
return Rule(rs.Eval).HandleEvent(ctx, e) |
| 67 |
} |
| 68 |
`)) |