-
Notifications
You must be signed in to change notification settings - Fork 5
/
webhook_generator.go
81 lines (68 loc) · 1.93 KB
/
webhook_generator.go
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
package generator
import (
"errors"
"go/types"
"os"
"path"
"text/template"
"github.com/schartey/dgraph-lambda-go/codegen/config"
"github.com/schartey/dgraph-lambda-go/codegen/parser"
"github.com/schartey/dgraph-lambda-go/codegen/rewriter"
)
func generateWebhook(c *config.Config, r *rewriter.Rewriter) error {
if c.ResolverFilename == "resolver" {
fileName := path.Join(c.Resolver.Dir, "webhook.resolver.go")
f, err := os.Create(fileName)
if err != nil {
return err
}
defer f.Close()
pkgs := make(map[string]*types.Package)
var models = make(map[string]*parser.Model)
for _, m := range c.ParsedTree.ModelTree.Models {
if len(m.LambdaOnMutate) > 0 {
models[m.Name] = m
//pkgs[m.TypeName.Pkg().Name()] = m.TypeName.Pkg()
}
}
if len(models) > 0 {
pkgs["context"] = types.NewPackage("context", "context")
pkgs["api"] = types.NewPackage("github.com/schartey/dgraph-lambda-go/api", "api")
}
err = webhookResolverTemplate.Execute(f, struct {
Models map[string]*parser.Model
Rewriter *rewriter.Rewriter
Packages map[string]*types.Package
PackageName string
}{
Models: models,
Rewriter: r,
Packages: pkgs,
PackageName: c.Resolver.Package,
})
if err != nil {
return err
}
return nil
}
return errors.New("Resolver file pattern invalid")
}
var webhookResolverTemplate = template.Must(template.New("webhook-resolver").Funcs(template.FuncMap{
"path": pkgPath,
"body": middlewareBody,
"typeName": typeName,
"is": is,
}).Parse(`
package {{.PackageName}}
import(
{{- range $pkg := .Packages }}
"{{ $pkg | path }}"{{- end}}
)
/** Put these into resolvers.go or similar **/
type WebhookResolver struct {
*Resolver
}
{{ range $model := .Models}}
func (w *WebhookResolver) Webhook_{{ $model.TypeName | typeName }}(ctx context.Context, event api.Event) error { {{ body (printf "Webhook_%s" ($model.TypeName | typeName)) $.Rewriter }}}
{{ end }}
`))