go-micro.dev/v5@v5.12.0/cmd/protoc-gen-micro/main.go (about) 1 // Go support for Protocol Buffers - Google's data interchange format 2 // 3 // Copyright 2010 The Go Authors. All rights reserved. 4 // https://github.com/golang/protobuf 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32 // protoc-gen-micro is a plugin for the Google protocol buffer compiler to generate 33 // Go code. Run it by building this program and putting it in your path with 34 // the name 35 // 36 // protoc-gen-micro 37 // 38 // That word 'micro' at the end becomes part of the option string set for the 39 // protocol compiler, so once the protocol compiler (protoc) is installed 40 // you can run 41 // 42 // protoc --micro_out=output_directory --go_out=output_directory input_directory/file.proto 43 // 44 // to generate go-micro code for the protocol defined by file.proto. 45 // With that input, the output will be written to 46 // 47 // output_directory/file.micro.go 48 // 49 // The generated code is documented in the package comment for 50 // the library. 51 // 52 // See the README and documentation for protocol buffers to learn more: 53 // 54 // https://developers.google.com/protocol-buffers/ 55 package main 56 57 import ( 58 "io" 59 "os" 60 61 "go-micro.dev/v5/cmd/protoc-gen-micro/generator" 62 _ "go-micro.dev/v5/cmd/protoc-gen-micro/plugin/micro" 63 "google.golang.org/protobuf/proto" 64 ) 65 66 func main() { 67 // Begin by allocating a generator. The request and response structures are stored there 68 // so we can do error handling easily - the response structure contains the field to 69 // report failure. 70 g := generator.New() 71 72 data, err := io.ReadAll(os.Stdin) 73 if err != nil { 74 g.Error(err, "reading input") 75 } 76 77 if err := proto.Unmarshal(data, g.Request); err != nil { 78 g.Error(err, "parsing input proto") 79 } 80 81 if len(g.Request.FileToGenerate) == 0 { 82 g.Fail("no files to generate") 83 } 84 85 g.CommandLineParameters(g.Request.GetParameter()) 86 87 // Create a wrapped version of the Descriptors and EnumDescriptors that 88 // point to the file that defines them. 89 g.WrapTypes() 90 91 g.SetPackageNames() 92 g.BuildTypeNameMap() 93 94 g.GenerateAllFiles() 95 96 // Send back the results. 97 data, err = proto.Marshal(g.Response) 98 if err != nil { 99 g.Error(err, "failed to marshal output proto") 100 } 101 _, err = os.Stdout.Write(data) 102 if err != nil { 103 g.Error(err, "failed to write output proto") 104 } 105 }