-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
65 lines (54 loc) · 1.5 KB
/
handler.go
File metadata and controls
65 lines (54 loc) · 1.5 KB
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
package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"text/template"
"github.com/protobuf-orm/protobuf-orm/graph"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
)
type Handler struct {
Schema SchemaOpts
Ent EntOpts
Server ServerOpts
Store StoreOpts
}
func (h *Handler) Run(p *protogen.Plugin) error {
p.SupportedEditionsMinimum = descriptorpb.Edition_EDITION_PROTO2
p.SupportedEditionsMaximum = descriptorpb.Edition_EDITION_MAX
p.SupportedFeatures = uint64(0 |
pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL |
pluginpb.CodeGeneratorResponse_FEATURE_SUPPORTS_EDITIONS,
)
ctx := context.Background()
// TODO: set logger
if v, err := template.New("namer").Parse(h.Ent.Namer); err != nil {
return fmt.Errorf("opt.ent.namer: %w", err)
} else {
b := strings.Builder{}
if err := v.Execute(&b, struct{ Name string }{Name: "_"}); err != nil {
return fmt.Errorf("opt.ent.namer: %w", err)
}
path := b.String() // path/to/package/_.g.go
pkg_user_ent := protogen.GoImportPath(filepath.Dir(path))
h.Server.ent = pkg_user_ent
h.Store.ent = pkg_user_ent
}
g := graph.NewGraph()
for _, f := range p.Files {
if !f.Generate {
continue
}
if err := graph.Parse(ctx, g, f.Desc); err != nil {
return fmt.Errorf("parse entity at %s: %w", *f.Proto.Name, err)
}
}
h.Schema.Run(ctx, p, g)
h.Ent.Run(ctx, p, g)
h.Server.Run(ctx, p, g)
h.Store.Run(ctx, p, g)
return nil
}