-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (61 loc) · 3.05 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
// Copyright 2020 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"flag"
"path/filepath"
"strings"
"github.com/digits/protoc-gen-openapi/generator"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
)
var flags flag.FlagSet
func main() {
conf := generator.Configuration{
Version: flags.String("version", "0.0.1", "version number text, e.g. 1.2.3"),
Title: flags.String("title", "", "name of the API"),
Description: flags.String("description", "", "description of the API"),
Naming: flags.String("naming", "json", `naming convention. Use "proto" for passing names directly from the proto files`),
FQSchemaNaming: flags.Bool("fq_schema_naming", false, `schema naming convention. If "true", generates fully-qualified schema names by prefixing them with the proto message package name`),
EnumType: flags.String("enum_type", "integer", `type for enum serialization. Use "string" for string-based serialization`),
CircularDepth: flags.Int("depth", 2, "depth of recursion for circular messages"),
DefaultResponse: flags.Bool("default_response", true, `add default response. If "true", automatically adds a default response to operations which use the google.rpc.Status message. Useful if you use envoy or grpc-gateway to transcode as they use this type for their default error responses.`),
OutputMode: flags.String("output_mode", "merged", `output generation mode. By default, a single openapi.yaml is generated at the out folder. Use "source_relative' to generate a separate '[inputfile].openapi.yaml' next to each '[inputfile].proto'.`),
}
opts := protogen.Options{
ParamFunc: flags.Set,
}
opts.Run(func(plugin *protogen.Plugin) error {
// Enable "optional" keyword in front of type (e.g. optional string label = 1;)
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
if *conf.OutputMode == "source_relative" {
for _, file := range plugin.Files {
if !file.Generate {
continue
}
outfileName := strings.TrimSuffix(file.Desc.Path(), filepath.Ext(file.Desc.Path())) + ".openapi.yaml"
outputFile := plugin.NewGeneratedFile(outfileName, "")
gen := generator.NewOpenAPIv3Generator(plugin, conf, []*protogen.File{file})
if err := gen.Run(outputFile); err != nil {
return err
}
}
} else {
outputFile := plugin.NewGeneratedFile("openapi.yaml", "")
return generator.NewOpenAPIv3Generator(plugin, conf, plugin.Files).Run(outputFile)
}
return nil
})
}