-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
74 lines (63 loc) · 1.79 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/catalystcommunity/app-utils-go/env"
gorm "github.com/catalystcommunity/protoc-gen-go-gorm/options"
"github.com/catalystcommunity/protoc-gen-go-gorm/plugin"
"github.com/golang/glog"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
"reflect"
)
var logLevel = env.GetEnvOrDefault("LOG_LEVEL", "ERROR")
func main() {
flag.Set("stderrthreshold", logLevel)
flag.Parse()
defer glog.Flush()
protogen.Options{
ParamFunc: flag.CommandLine.Set,
}.Run(func(gp *protogen.Plugin) error {
gp.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
for _, name := range gp.Request.FileToGenerate {
f := gp.FilesByPath[name]
if len(f.Messages) == 0 {
glog.Infof("Skipping %s, no messages", name)
continue
}
glog.Infof("Processing %s", name)
glog.Infof("Generating %s\n", fmt.Sprintf("%s.pb.gorm.go", f.GeneratedFilenamePrefix))
if shouldGenerateFile(f) {
gf := gp.NewGeneratedFile(fmt.Sprintf("%s.pb.gorm.go", f.GeneratedFilenamePrefix), f.GoImportPath)
err := plugin.ApplyTemplate(gf, f)
if err != nil {
gf.Skip()
gp.Error(err)
continue
}
}
}
return nil
})
}
func shouldGenerateFile(file *protogen.File) bool {
options := getFileOptions(file)
return options != nil && options.Generate
}
func getFileOptions(file *protogen.File) *gorm.GormFileOptions {
options := file.Desc.Options().(*descriptorpb.FileOptions)
if options == nil {
return &gorm.GormFileOptions{}
}
v := proto.GetExtension(options, gorm.E_FileOpts)
if reflect.ValueOf(v).IsNil() {
return nil
}
opts, ok := v.(*gorm.GormFileOptions)
if !ok {
return nil
}
return opts
}