func Generate(req *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse { // Begin by allocating a generator. The request and response structures are stored there // so we can do error handling easily - the response structure contains the field to // report failure. g := generator.New() g.Request = req g.CommandLineParameters(g.Request.GetParameter()) // Create a wrapped version of the Descriptors and EnumDescriptors that // point to the file that defines them. g.WrapTypes() g.SetPackageNames() g.BuildTypeNameMap() g.GenerateAllFiles() gtest := generator.New() data, err := proto.Marshal(req) if err != nil { g.Error(err, "failed to marshal modified proto") } if err := proto.Unmarshal(data, gtest.Request); err != nil { g.Error(err, "parsing modified proto") } if len(gtest.Request.FileToGenerate) == 0 { gtest.Fail("no files to generate") } gtest.CommandLineParameters(gtest.Request.GetParameter()) // Create a wrapped version of the Descriptors and EnumDescriptors that // point to the file that defines them. gtest.WrapTypes() gtest.SetPackageNames() gtest.BuildTypeNameMap() gtest.GeneratePlugin(testgen.NewPlugin()) for i := 0; i < len(gtest.Response.File); i++ { if strings.Contains(*gtest.Response.File[i].Content, `//These tests are generated by github.com/gogo/protobuf/plugin/testgen`) { gtest.Response.File[i].Name = proto.String(strings.Replace(*gtest.Response.File[i].Name, ".pb.go", "pb_test.go", -1)) g.Response.File = append(g.Response.File, gtest.Response.File[i]) } } for i := 0; i < len(g.Response.File); i++ { formatted, err := format.Source([]byte(g.Response.File[i].GetContent())) if err != nil { g.Error(err, "go format error") } fmts := string(formatted) g.Response.File[i].Content = &fmts } return g.Response }
func Generate(req *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse { // Begin by allocating a generator. The request and response structures are stored there // so we can do error handling easily - the response structure contains the field to // report failure. g := generator.New() g.Request = req g.CommandLineParameters(g.Request.GetParameter()) // Create a wrapped version of the Descriptors and EnumDescriptors that // point to the file that defines them. g.WrapTypes() g.SetPackageNames() g.BuildTypeNameMap() g.GenerateAllFiles() if err := goformat(g.Response); err != nil { g.Error(err) } testReq := proto.Clone(req).(*plugin.CodeGeneratorRequest) testResp := GeneratePlugin(testReq, testgen.NewPlugin(), "pb_test.go") for i := 0; i < len(testResp.File); i++ { if strings.Contains(*testResp.File[i].Content, `//These tests are generated by github.com/gogo/protobuf/plugin/testgen`) { g.Response.File = append(g.Response.File, testResp.File[i]) } } return g.Response }