|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "google.golang.org/protobuf/compiler/protogen" |
| 9 | + "google.golang.org/protobuf/types/pluginpb" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + var flags flag.FlagSet |
| 14 | + old := flags.String("old", "", "old path") |
| 15 | + new := flags.String("new", "", "new path") |
| 16 | + |
| 17 | + protogen.Options{ |
| 18 | + ParamFunc: flags.Set, |
| 19 | + }.Run(func(gen *protogen.Plugin) error { |
| 20 | + for _, f := range gen.Files { |
| 21 | + if f.Generate { |
| 22 | + if *old == "" || *new == "" { |
| 23 | + return fmt.Errorf("must specify old and new") |
| 24 | + } |
| 25 | + genShim(gen, f, *old, *new) |
| 26 | + } |
| 27 | + } |
| 28 | + gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL | pluginpb.CodeGeneratorResponse_FEATURE_SUPPORTS_EDITIONS) |
| 29 | + return nil |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func genShim(gen *protogen.Plugin, file *protogen.File, old string, new string) { |
| 34 | + importPath := strings.ReplaceAll(string(file.GoImportPath), old, new) |
| 35 | + |
| 36 | + filename := file.GeneratedFilenamePrefix + ".shim.pb.go" |
| 37 | + g := gen.NewGeneratedFile(filename, file.GoImportPath) |
| 38 | + g.P("// Code generated by protoc-gen-goshim. DO NOT EDIT.") |
| 39 | + g.P() |
| 40 | + g.P("package ", file.GoPackageName) |
| 41 | + g.P(`import slim "`, importPath, `"`) |
| 42 | + |
| 43 | + g.P("type (") |
| 44 | + for _, m := range file.Messages { |
| 45 | + g.P(fmt.Sprintf("%[1]s = slim.%[1]s", m.GoIdent.GoName)) |
| 46 | + } |
| 47 | + for _, m := range file.Enums { |
| 48 | + g.P(fmt.Sprintf("%[1]s = slim.%[1]s", m.GoIdent.GoName)) |
| 49 | + } |
| 50 | + g.P(")") |
| 51 | +} |
0 commit comments