Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 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 | |
| 17 | var ( |
| 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") |
| 30 | ) |
| 31 | |
| 32 | // Variables set by goreleaser at build time |
| 33 | var ( |
| 34 | version = "dev" |
| 35 | commit = "unknown" |
| 36 | date = "unknown" |
| 37 | ) |
| 38 | |
| 39 | func main() { |
| 40 | flag.Parse() |
| 41 | defer glog.Flush() |
| 42 | |
| 43 | if *versionFlag { |
| 44 | fmt.Printf("Version %v, commit %v, built at %v\n", version, commit, date) |
| 45 | os.Exit(0) |
| 46 | } |
| 47 | |
| 48 | reg := descriptor.NewRegistry() |
| 49 | |
| 50 | glog.V(1).Info("Processing code generator request") |
| 51 | f := os.Stdin |
| 52 | if *file != "-" { |
| 53 | var err error |
| 54 | f, err = os.Open(*file) |
| 55 | if err != nil { |
| 56 | glog.Fatal(err) |
| 57 | } |
| 58 | } |
| 59 | glog.V(1).Info("Parsing code generator request") |
| 60 | req, err := codegenerator.ParseRequest(f) |
| 61 | if err != nil { |
| 62 | glog.Fatal(err) |
| 63 | } |
| 64 | glog.V(1).Info("Parsed code generator request") |
| 65 | pkgMap := make(map[string]string) |
| 66 | if req.Parameter != nil { |
| 67 | err := parseReqParam(req.GetParameter(), flag.CommandLine, pkgMap) |
| 68 | if err != nil { |
| 69 | glog.Fatalf("Error parsing flags: %v", err) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | reg.SetPrefix(*importPrefix) |
| 74 | reg.SetAllowDeleteBody(*allowDeleteBody) |
| 75 | reg.SetAllowMerge(*allowMerge) |
| 76 | reg.SetMergeFileName(*mergeFileName) |
| 77 | reg.SetUseJSONNamesForFields(*useJSONNamesForFields) |
| 78 | reg.SetAllowRepeatedFieldsInBody(*allowRepeatedFieldsInBody) |
| 79 | reg.SetIncludePackageInTags(*includePackageInTags) |
| 80 | reg.SetUseFQNForSwaggerName(*useFQNForSwaggerName) |
| 81 | if err := reg.SetRepeatedPathParamSeparator(*repeatedPathParamSeparator); err != nil { |
| 82 | emitError(err) |
| 83 | return |
| 84 | } |
| 85 | for k, v := range pkgMap { |
| 86 | reg.AddPkgMap(k, v) |
| 87 | } |
| 88 | |
| 89 | if *grpcAPIConfiguration != "" { |
| 90 | if err := reg.LoadGrpcAPIServiceFromYAML(*grpcAPIConfiguration); err != nil { |
| 91 | emitError(err) |
| 92 | return |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | g := genswagger.New(reg) |
| 97 | |
| 98 | if err := genswagger.AddStreamError(reg); err != nil { |
| 99 | emitError(err) |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | if err := reg.Load(req); err != nil { |
| 104 | emitError(err) |
| 105 | return |
| 106 | } |
| 107 | |
| 108 | var targets []*descriptor.File |
| 109 | for _, target := range req.FileToGenerate { |
| 110 | f, err := reg.LookupFile(target) |
| 111 | if err != nil { |
| 112 | glog.Fatal(err) |
| 113 | } |
| 114 | targets = append(targets, f) |
| 115 | } |
| 116 | |
| 117 | out, err := g.Generate(targets) |
| 118 | glog.V(1).Info("Processed code generator request") |
| 119 | if err != nil { |
| 120 | emitError(err) |
| 121 | return |
| 122 | } |
| 123 | emitFiles(out) |
| 124 | } |
| 125 | |
| 126 | func emitFiles(out []*plugin.CodeGeneratorResponse_File) { |
| 127 | emitResp(&plugin.CodeGeneratorResponse{File: out}) |
| 128 | } |
| 129 | |
| 130 | func emitError(err error) { |
| 131 | emitResp(&plugin.CodeGeneratorResponse{Error: proto.String(err.Error())}) |
| 132 | } |
| 133 | |
| 134 | func emitResp(resp *plugin.CodeGeneratorResponse) { |
| 135 | buf, err := proto.Marshal(resp) |
| 136 | if err != nil { |
| 137 | glog.Fatal(err) |
| 138 | } |
| 139 | if _, err := os.Stdout.Write(buf); err != nil { |
| 140 | glog.Fatal(err) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // parseReqParam parses a CodeGeneratorRequest parameter and adds the |
| 145 | // extracted values to the given FlagSet and pkgMap. Returns a non-nil |
| 146 | // error if setting a flag failed. |
| 147 | func parseReqParam(param string, f *flag.FlagSet, pkgMap map[string]string) error { |
| 148 | if param == "" { |
| 149 | return nil |
| 150 | } |
| 151 | for _, p := range strings.Split(param, ",") { |
| 152 | spec := strings.SplitN(p, "=", 2) |
| 153 | if len(spec) == 1 { |
| 154 | if spec[0] == "allow_delete_body" { |
| 155 | err := f.Set(spec[0], "true") |
| 156 | if err != nil { |
| 157 | return fmt.Errorf("Cannot set flag %s: %v", p, err) |
| 158 | } |
| 159 | continue |
| 160 | } |
| 161 | if spec[0] == "allow_merge" { |
| 162 | err := f.Set(spec[0], "true") |
| 163 | if err != nil { |
| 164 | return fmt.Errorf("Cannot set flag %s: %v", p, err) |
| 165 | } |
| 166 | continue |
| 167 | } |
| 168 | if spec[0] == "allow_repeated_fields_in_body" { |
| 169 | err := f.Set(spec[0], "true") |
| 170 | if err != nil { |
| 171 | return fmt.Errorf("Cannot set flag %s: %v", p, err) |
| 172 | } |
| 173 | continue |
| 174 | } |
| 175 | if spec[0] == "include_package_in_tags" { |
| 176 | err := f.Set(spec[0], "true") |
| 177 | if err != nil { |
| 178 | return fmt.Errorf("Cannot set flag %s: %v", p, err) |
| 179 | } |
| 180 | continue |
| 181 | } |
| 182 | err := f.Set(spec[0], "") |
| 183 | if err != nil { |
| 184 | return fmt.Errorf("Cannot set flag %s: %v", p, err) |
| 185 | } |
| 186 | continue |
| 187 | } |
| 188 | name, value := spec[0], spec[1] |
| 189 | if strings.HasPrefix(name, "M") { |
| 190 | pkgMap[name[1:]] = value |
| 191 | continue |
| 192 | } |
| 193 | if err := f.Set(name, value); err != nil { |
| 194 | return fmt.Errorf("Cannot set flag %s: %v", p, err) |
| 195 | } |
| 196 | } |
| 197 | return nil |
| 198 | } |