Debian Codegraph @

protoc-gen-go/main.go

raw
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // protoc-gen-go is a plugin for the Google protocol buffer compiler to generate
6 // Go code. Install it by building this program and making it accessible within
7 // your PATH with the name:
8 //
9 // protoc-gen-go
10 //
11 // The 'go' suffix becomes part of the argument for the protocol compiler,
12 // such that it can be invoked as:
13 //
14 // protoc --go_out=paths=source_relative:. path/to/file.proto
15 //
16 // This generates Go bindings for the protocol buffer defined by file.proto.
17 // With that input, the output will be written to:
18 //
19 // path/to/file.pb.go
20 //
21 // See the README and documentation for protocol buffers to learn more:
22 //
23 // https://developers.google.com/protocol-buffers/
24 package main
25
26 import (
27 "flag"
28 "fmt"
29 "strings"
30
31 "github.com/golang/protobuf/internal/gengogrpc"
32 gengo "google.golang.org/protobuf/cmd/protoc-gen-go/internal_gengo"
33 "google.golang.org/protobuf/compiler/protogen"
34 )
35
36 func main() {
37 var (
38 flags flag.FlagSet
39 plugins = flags.String("plugins", "", "list of plugins to enable (supported values: grpc)")
40 importPrefix = flags.String("import_prefix", "", "prefix to prepend to import paths")
41 )
42 importRewriteFunc := func(importPath protogen.GoImportPath) protogen.GoImportPath {
43 switch importPath {
44 case "context", "fmt", "math":
45 return importPath
46 }
47 if *importPrefix != "" {
48 return protogen.GoImportPath(*importPrefix) + importPath
49 }
50 return importPath
51 }
52 protogen.Options{
53 ParamFunc: flags.Set,
54 ImportRewriteFunc: importRewriteFunc,
55 }.Run(func(gen *protogen.Plugin) error {
56 grpc := false
57 for _, plugin := range strings.Split(*plugins, ",") {
58 switch plugin {
59 case "grpc":
60 grpc = true
61 case "":
62 default:
63 return fmt.Errorf("protoc-gen-go: unknown plugin %q", plugin)
64 }
65 }
66 for _, f := range gen.Files {
67 if !f.Generate {
68 continue
69 }
70 g := gengo.GenerateFile(gen, f)
71 if grpc {
72 gengogrpc.GenerateFileContent(gen, f, g)
73 }
74 }
75 gen.SupportedFeatures = gengo.SupportedFeatures
76 return nil
77 })
78 }