This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (128 loc) · 4.5 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"flag"
"fmt"
"strings"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/grpc-ecosystem/grpc-gateway/codegenerator"
"github.com/thesoulless/protoc-gen-gokitmux/descriptor"
"github.com/golang/glog"
"github.com/thesoulless/protoc-gen-gokitmux/internal/generator"
"github.com/thesoulless/protoc-gen-gokitmux/internal/gengateway"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"os"
"google.golang.org/protobuf/types/pluginpb"
)
var (
importPrefix = flag.String("import_prefix", "", "prefix to be added to go package paths for imported proto files")
importPath = flag.String("import_path", "", "used as the package if no input files declare go_package. If it contains slashes, everything up to the rightmost slash is ignored.")
registerFuncSuffix = flag.String("register_func_suffix", "Handler", "used to construct names of generated Register*<Suffix> methods.")
grpcAPIConfiguration = flag.String("grpc_configuration", "", "path to gRPC API Configuration in YAML format")
modulePath = flag.String("module", "", "specifies a module prefix that will be stripped from the go package to determine the output directory")
repeatedPathParamSeparator = flag.String("repeated_path_param_separator", "csv", "configures how repeated fields should be split. Allowed values are `csv`, `pipes`, `ssv` and `tsv`.")
metricsPackage = flag.String("metrics", "", "path to metrics package")
generateService = flag.Bool("gen_service", false, "should a service interface be generated")
errorEncoder = flag.String("error_encoder", "", "sets error encoder name")
)
// Variables set by goreleaser at build time
var (
version = "dev"
commit = "unknown"
date = "unknown"
)
func main() {
flag.Parse()
defer glog.Flush()
reg := descriptor.NewRegistry()
glog.V(1).Info("Parsing code generator request")
req, err := codegenerator.ParseRequest(os.Stdin)
if err != nil {
glog.Fatal(err)
}
glog.V(1).Info("Parsed code generator request")
protogen.Options{}.Run(func(gen *protogen.Plugin) error {
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
if req.Parameter != nil {
for _, p := range strings.Split(req.GetParameter(), ",") {
spec := strings.SplitN(p, "=", 2)
if len(spec) == 1 {
if err := flag.CommandLine.Set(spec[0], ""); err != nil {
glog.Fatalf("Cannot set flag %s", p)
}
continue
}
name, value := spec[0], spec[1]
if strings.HasPrefix(name, "M") {
reg.AddPkgMap(name[1:], value)
continue
}
if err := flag.CommandLine.Set(name, value); err != nil {
glog.Fatalf("Cannot set flag %s", p)
}
}
}
if *grpcAPIConfiguration != "" {
if err = reg.LoadGrpcAPIServiceFromYAML(*grpcAPIConfiguration); err != nil {
emitError(err)
panic(1)
}
}
reg.SetPrefix(*importPrefix)
reg.SetImportPath(*importPath)
if err = reg.SetRepeatedPathParamSeparator(*repeatedPathParamSeparator); err != nil {
emitError(err)
panic(1)
}
if err = reg.Load(req); err != nil {
emitError(err)
panic(1)
}
unboundHTTPRules := reg.UnboundExternalHTTPRules()
if len(unboundHTTPRules) != 0 {
emitError(fmt.Errorf("HTTP rules without a matching selector: %s", strings.Join(unboundHTTPRules, ", ")))
panic(1)
}
var targets []*descriptor.File
for _, target := range req.FileToGenerate {
f, _err := reg.LookupFile(target)
if _err != nil {
glog.Fatal(err)
}
targets = append(targets, f)
}
packageName := strings.Split(*modulePath, "/")
PackageName := packageName[len(packageName)-1]
ps := generator.Params{
GenerateService: *generateService,
MetricsPackage: *metricsPackage,
ErrorEncoder: *errorEncoder,
PackageName: PackageName,
RegisterFuncSuffix: *registerFuncSuffix,
}
gwGen := gengateway.New(*modulePath)
out, err := gwGen.Generate(targets, ps)
glog.V(1).Info("Processed code generator request")
if err != nil {
emitError(err)
return err
}
emitFiles(out)
return nil
})
}
func emitFiles(out []*plugin.CodeGeneratorResponse_File) {
emitResp(&plugin.CodeGeneratorResponse{File: out})
}
func emitError(err error) {
emitResp(&plugin.CodeGeneratorResponse{Error: proto.String(err.Error())})
}
func emitResp(resp *plugin.CodeGeneratorResponse) {
buf, err := proto.Marshal(resp)
if err != nil {
glog.Fatal(err)
}
if _, err := os.Stdout.Write(buf); err != nil {
glog.Fatal(err)
}
}