blob: 9befc2544f49d1a903307f597a4ae10cb22bbb9a [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001package main
2
3import (
4 "flag"
5 "fmt"
6 "os"
7 "strings"
8
9 "github.com/golang/glog"
10 "github.com/golang/protobuf/proto"
11 plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
12 "github.com/grpc-ecosystem/grpc-gateway/codegenerator"
13 "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
14 "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/genswagger"
15)
16
17var (
18 importPrefix = flag.String("import_prefix", "", "prefix to be added to go package paths for imported proto files")
19 file = flag.String("file", "-", "where to load data from")
20 allowDeleteBody = flag.Bool("allow_delete_body", false, "unless set, HTTP DELETE methods may not have a body")
21 grpcAPIConfiguration = flag.String("grpc_api_configuration", "", "path to gRPC API Configuration in YAML format")
22 allowMerge = flag.Bool("allow_merge", false, "if set, generation one swagger file out of multiple protos")
23 mergeFileName = flag.String("merge_file_name", "apidocs", "target swagger file name prefix after merge")
24 useJSONNamesForFields = flag.Bool("json_names_for_fields", false, "if it sets Field.GetJsonName() will be used for generating swagger definitions, otherwise Field.GetName() will be used")
25 repeatedPathParamSeparator = flag.String("repeated_path_param_separator", "csv", "configures how repeated fields should be split. Allowed values are `csv`, `pipes`, `ssv` and `tsv`.")
26 versionFlag = flag.Bool("version", false, "print the current verison")
27 allowRepeatedFieldsInBody = flag.Bool("allow_repeated_fields_in_body", false, "allows to use repeated field in `body` and `response_body` field of `google.api.http` annotation option")
28 includePackageInTags = flag.Bool("include_package_in_tags", false, "if unset, the gRPC service name is added to the `Tags` field of each operation. if set and the `package` directive is shown in the proto file, the package name will be prepended to the service name")
29 useFQNForSwaggerName = flag.Bool("fqn_for_swagger_name", false, "if set, the object's swagger names will use the fully qualify name from the proto definition (ie my.package.MyMessage.MyInnerMessage")
Arjun E K57a7fcb2020-01-30 06:44:45 +000030 useGoTemplate = flag.Bool("use_go_templates", false, "if set, you can use Go templates in protofile comments")
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070031)
32
33// Variables set by goreleaser at build time
34var (
35 version = "dev"
36 commit = "unknown"
37 date = "unknown"
38)
39
40func main() {
41 flag.Parse()
42 defer glog.Flush()
43
44 if *versionFlag {
45 fmt.Printf("Version %v, commit %v, built at %v\n", version, commit, date)
46 os.Exit(0)
47 }
48
49 reg := descriptor.NewRegistry()
50
51 glog.V(1).Info("Processing code generator request")
52 f := os.Stdin
53 if *file != "-" {
54 var err error
55 f, err = os.Open(*file)
56 if err != nil {
57 glog.Fatal(err)
58 }
59 }
60 glog.V(1).Info("Parsing code generator request")
61 req, err := codegenerator.ParseRequest(f)
62 if err != nil {
63 glog.Fatal(err)
64 }
65 glog.V(1).Info("Parsed code generator request")
66 pkgMap := make(map[string]string)
67 if req.Parameter != nil {
68 err := parseReqParam(req.GetParameter(), flag.CommandLine, pkgMap)
69 if err != nil {
70 glog.Fatalf("Error parsing flags: %v", err)
71 }
72 }
73
74 reg.SetPrefix(*importPrefix)
75 reg.SetAllowDeleteBody(*allowDeleteBody)
76 reg.SetAllowMerge(*allowMerge)
77 reg.SetMergeFileName(*mergeFileName)
78 reg.SetUseJSONNamesForFields(*useJSONNamesForFields)
79 reg.SetAllowRepeatedFieldsInBody(*allowRepeatedFieldsInBody)
80 reg.SetIncludePackageInTags(*includePackageInTags)
81 reg.SetUseFQNForSwaggerName(*useFQNForSwaggerName)
Arjun E K57a7fcb2020-01-30 06:44:45 +000082 reg.SetUseGoTemplate(*useGoTemplate)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070083 if err := reg.SetRepeatedPathParamSeparator(*repeatedPathParamSeparator); err != nil {
84 emitError(err)
85 return
86 }
87 for k, v := range pkgMap {
88 reg.AddPkgMap(k, v)
89 }
90
91 if *grpcAPIConfiguration != "" {
92 if err := reg.LoadGrpcAPIServiceFromYAML(*grpcAPIConfiguration); err != nil {
93 emitError(err)
94 return
95 }
96 }
97
98 g := genswagger.New(reg)
99
100 if err := genswagger.AddStreamError(reg); err != nil {
101 emitError(err)
102 return
103 }
104
105 if err := reg.Load(req); err != nil {
106 emitError(err)
107 return
108 }
109
110 var targets []*descriptor.File
111 for _, target := range req.FileToGenerate {
112 f, err := reg.LookupFile(target)
113 if err != nil {
114 glog.Fatal(err)
115 }
116 targets = append(targets, f)
117 }
118
119 out, err := g.Generate(targets)
120 glog.V(1).Info("Processed code generator request")
121 if err != nil {
122 emitError(err)
123 return
124 }
125 emitFiles(out)
126}
127
128func emitFiles(out []*plugin.CodeGeneratorResponse_File) {
129 emitResp(&plugin.CodeGeneratorResponse{File: out})
130}
131
132func emitError(err error) {
133 emitResp(&plugin.CodeGeneratorResponse{Error: proto.String(err.Error())})
134}
135
136func emitResp(resp *plugin.CodeGeneratorResponse) {
137 buf, err := proto.Marshal(resp)
138 if err != nil {
139 glog.Fatal(err)
140 }
141 if _, err := os.Stdout.Write(buf); err != nil {
142 glog.Fatal(err)
143 }
144}
145
146// parseReqParam parses a CodeGeneratorRequest parameter and adds the
147// extracted values to the given FlagSet and pkgMap. Returns a non-nil
148// error if setting a flag failed.
149func parseReqParam(param string, f *flag.FlagSet, pkgMap map[string]string) error {
150 if param == "" {
151 return nil
152 }
153 for _, p := range strings.Split(param, ",") {
154 spec := strings.SplitN(p, "=", 2)
155 if len(spec) == 1 {
156 if spec[0] == "allow_delete_body" {
157 err := f.Set(spec[0], "true")
158 if err != nil {
159 return fmt.Errorf("Cannot set flag %s: %v", p, err)
160 }
161 continue
162 }
163 if spec[0] == "allow_merge" {
164 err := f.Set(spec[0], "true")
165 if err != nil {
166 return fmt.Errorf("Cannot set flag %s: %v", p, err)
167 }
168 continue
169 }
170 if spec[0] == "allow_repeated_fields_in_body" {
171 err := f.Set(spec[0], "true")
172 if err != nil {
173 return fmt.Errorf("Cannot set flag %s: %v", p, err)
174 }
175 continue
176 }
177 if spec[0] == "include_package_in_tags" {
178 err := f.Set(spec[0], "true")
179 if err != nil {
180 return fmt.Errorf("Cannot set flag %s: %v", p, err)
181 }
182 continue
183 }
184 err := f.Set(spec[0], "")
185 if err != nil {
186 return fmt.Errorf("Cannot set flag %s: %v", p, err)
187 }
188 continue
189 }
190 name, value := spec[0], spec[1]
191 if strings.HasPrefix(name, "M") {
192 pkgMap[name[1:]] = value
193 continue
194 }
195 if err := f.Set(name, value); err != nil {
196 return fmt.Errorf("Cannot set flag %s: %v", p, err)
197 }
198 }
199 return nil
200}