|
| 1 | +/* |
| 2 | +If this program is on the path of your machine you can invoke it in the following way: |
| 3 | +
|
| 4 | +protoc --plugin protoc-gen-gofake --goexample_out=package=cosi,packagePath=sigs.k8s.io/container-object-storage-interface-spec:fake cosi.proto |
| 5 | +
|
| 6 | +Requires package, and packagePath to be specified |
| 7 | +*/ |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "bytes" |
| 12 | + "fmt" |
| 13 | + "io" |
| 14 | + "io/ioutil" |
| 15 | + "os" |
| 16 | + "strings" |
| 17 | + "text/tabwriter" |
| 18 | + |
| 19 | + "github.com/golang/protobuf/proto" |
| 20 | + plugin "github.com/golang/protobuf/protoc-gen-go/plugin" |
| 21 | +) |
| 22 | + |
| 23 | +type GoFake struct { |
| 24 | + Request *plugin.CodeGeneratorRequest |
| 25 | + Response *plugin.CodeGeneratorResponse |
| 26 | + Parameters map[string]string |
| 27 | +} |
| 28 | + |
| 29 | +type FakeService struct { |
| 30 | + Name string |
| 31 | + Methods []Method |
| 32 | +} |
| 33 | + |
| 34 | +type Method struct { |
| 35 | + Name string |
| 36 | + Input string |
| 37 | + Output string |
| 38 | +} |
| 39 | + |
| 40 | +func (runner *GoFake) PrintParameters(w io.Writer) { |
| 41 | + const padding = 3 |
| 42 | + tw := tabwriter.NewWriter(w, 0, 0, padding, ' ', tabwriter.TabIndent) |
| 43 | + fmt.Fprintf(tw, "Parameters:\n") |
| 44 | + for k, v := range runner.Parameters { |
| 45 | + fmt.Fprintf(tw, "%s:\t%s\n", k, v) |
| 46 | + } |
| 47 | + fmt.Fprintln(tw, "") |
| 48 | + tw.Flush() |
| 49 | +} |
| 50 | + |
| 51 | +func cleanInput(val string) string { |
| 52 | + spl := strings.Split(val, ".") |
| 53 | + return spl[len(spl)-1] |
| 54 | +} |
| 55 | + |
| 56 | +func (runner *GoFake) getLocationMessage() map[string][]*FakeService { |
| 57 | + ret := make(map[string][]*FakeService) |
| 58 | + for _, protoFile := range runner.Request.ProtoFile { |
| 59 | + _, _ = fmt.Fprintf(os.Stderr, "%s\n", *protoFile.Name) |
| 60 | + fakeServices := make([]*FakeService, 0) |
| 61 | + _, _ = fmt.Fprintf(os.Stderr, "%+v\n", protoFile.GetService()) |
| 62 | + svcs := protoFile.GetService() |
| 63 | + for _, svc := range svcs { |
| 64 | + _, _ = fmt.Fprintf(os.Stderr, "service: %+v\n", svc) |
| 65 | + current := &FakeService{ |
| 66 | + Name: fmt.Sprintf("%sClient", *svc.Name), |
| 67 | + } |
| 68 | + methods := make([]Method, 0) |
| 69 | + for _, mtd := range svc.Method { |
| 70 | + method := Method{Name: *mtd.Name} |
| 71 | + if mtd.InputType != nil { |
| 72 | + method.Input = cleanInput(*mtd.InputType) |
| 73 | + } |
| 74 | + if mtd.OutputType != nil { |
| 75 | + method.Output = cleanInput(*mtd.OutputType) |
| 76 | + } |
| 77 | + methods = append(methods, method) |
| 78 | + } |
| 79 | + current.Methods = methods |
| 80 | + |
| 81 | + fakeServices = append(fakeServices, current) |
| 82 | + } |
| 83 | + ret[*protoFile.Name] = fakeServices |
| 84 | + } |
| 85 | + return ret |
| 86 | +} |
| 87 | + |
| 88 | +func (runner *GoFake) WriteImports(buf *bytes.Buffer, imports... string) { |
| 89 | + for _, i := range imports { |
| 90 | + buf.WriteString(fmt.Sprintf("\t\"%s\"\n", i)) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func (runner *GoFake) CreateFakeFile(filename string, fakeSVC []*FakeService) error { |
| 95 | + var outfileName string |
| 96 | + var content string |
| 97 | + outfileName = strings.Replace(filename, ".proto", ".pb.fake.go", -1) |
| 98 | + var mdFile plugin.CodeGeneratorResponse_File |
| 99 | + mdFile.Name = &outfileName |
| 100 | + var buf bytes.Buffer |
| 101 | + |
| 102 | + pkg := runner.Parameters["package"] |
| 103 | + pkgPath := runner.Parameters["packagePath"] |
| 104 | + |
| 105 | + buf.WriteString("package fake\n\n") |
| 106 | + buf.WriteString("import (\n") |
| 107 | + runner.WriteImports(&buf, "context", "google.golang.org/grpc") |
| 108 | + buf.WriteString(fmt.Sprintf("\t%s \"%s\"\n", pkg, pkgPath)) |
| 109 | + buf.WriteString(")\n\n") |
| 110 | + for _, fakeSVC := range fakeSVC { |
| 111 | + buf.WriteString(fmt.Sprintf("type Fake%s struct {\n", fakeSVC.Name)) |
| 112 | + for _, mtd := range fakeSVC.Methods { |
| 113 | + buf.WriteString(fmt.Sprintf("\tFake%s func(ctx context.Context, in *%s.%s, opts ...grpc.CallOption) (*%s.%s, error)\n", |
| 114 | + mtd.Name, pkg, mtd.Input, pkg, mtd.Output)) |
| 115 | + } |
| 116 | + buf.WriteString("}\n\n") |
| 117 | + for _, mtd := range fakeSVC.Methods { |
| 118 | + buf.WriteString(fmt.Sprintf("func (f *Fake%s) %s(ctx context.Context, in *%s.%s, opts ...grpc.CallOption) (*%s.%s, error) {\n", |
| 119 | + fakeSVC.Name, mtd.Name, pkg, mtd.Input, pkg, mtd.Output)) |
| 120 | + buf.WriteString(fmt.Sprintf("\treturn f.Fake%s(ctx, in, opts...)\n", |
| 121 | + mtd.Name)) |
| 122 | + buf.WriteString("}\n\n") |
| 123 | + } |
| 124 | + } |
| 125 | + content = buf.String() |
| 126 | + mdFile.Content = &content |
| 127 | + runner.Response.File = append(runner.Response.File, &mdFile) |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func (runner *GoFake) generateMessageMarkdown() error { |
| 132 | + // This convenience method will return a structure of some types that I use |
| 133 | + for filename, locationMessages := range runner.getLocationMessage() { |
| 134 | + runner.CreateFakeFile(filename, locationMessages) |
| 135 | + } |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func (runner *GoFake) generateCode() error { |
| 140 | + // Initialize the output file slice |
| 141 | + files := make([]*plugin.CodeGeneratorResponse_File, 0) |
| 142 | + runner.Response.File = files |
| 143 | + |
| 144 | + err := runner.generateMessageMarkdown() |
| 145 | + if err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func main() { |
| 152 | + // os.Stdin will contain data which will unmarshal into the following object: |
| 153 | + // https://godoc.org/github.com/golang/protobuf/protoc-gen-go/plugin#CodeGeneratorRequest |
| 154 | + req := &plugin.CodeGeneratorRequest{} |
| 155 | + resp := &plugin.CodeGeneratorResponse{} |
| 156 | + |
| 157 | + data, err := ioutil.ReadAll(os.Stdin) |
| 158 | + if err != nil { |
| 159 | + panic(err) |
| 160 | + } |
| 161 | + |
| 162 | + // You must use the requests unmarshal method to handle this type |
| 163 | + if err := proto.Unmarshal(data, req); err != nil { |
| 164 | + panic(err) |
| 165 | + } |
| 166 | + |
| 167 | + // You may require more data than what is in the proto files alone. There are a couple ways in which to do this. |
| 168 | + // The first is by parameters. Another may be using leading comments in the proto files which I will cover in generateCode. |
| 169 | + parameters := req.GetParameter() |
| 170 | + // =grpc,import_path=mypackage:. |
| 171 | + exampleRunner := &GoFake{ |
| 172 | + Request: req, |
| 173 | + Response: resp, |
| 174 | + Parameters: make(map[string]string), |
| 175 | + } |
| 176 | + groupkv := strings.Split(parameters, ",") |
| 177 | + for _, element := range groupkv { |
| 178 | + kv := strings.Split(element, "=") |
| 179 | + if len(kv) > 1 { |
| 180 | + exampleRunner.Parameters[kv[0]] = kv[1] |
| 181 | + } |
| 182 | + } |
| 183 | + // Print the parameters for example |
| 184 | + exampleRunner.PrintParameters(os.Stderr) |
| 185 | + |
| 186 | + err = exampleRunner.generateCode() |
| 187 | + if err != nil { |
| 188 | + panic(err) |
| 189 | + } |
| 190 | + |
| 191 | + marshalled, err := proto.Marshal(resp) |
| 192 | + if err != nil { |
| 193 | + panic(err) |
| 194 | + } |
| 195 | + os.Stdout.Write(marshalled) |
| 196 | +} |
0 commit comments