本文整理匯總了Golang中github.com/gogo/protobuf/protoc-gen-gogo/generator.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Generate
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
}
示例2: Generate
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
}
示例3: Write
func Write(resp *plugin.CodeGeneratorResponse) {
g := generator.New()
// Send back the results.
data, err := proto.Marshal(resp)
if err != nil {
g.Error(err, "failed to marshal output proto")
}
_, err = os.Stdout.Write(data)
if err != nil {
g.Error(err, "failed to write output proto")
}
}
示例4: Read
func Read() *plugin.CodeGeneratorRequest {
g := generator.New()
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
g.Error(err, "reading input")
}
if err := proto.Unmarshal(data, g.Request); err != nil {
g.Error(err, "parsing input proto")
}
if len(g.Request.FileToGenerate) == 0 {
g.Fail("no files to generate")
}
return g.Request
}
示例5: TestCreateCustom
func TestCreateCustom(t *testing.T) {
desc, err := parser.ParseFile("form.proto", ".")
if err != nil {
t.Fatal(err)
}
g := generator.New()
g.Request = &plugin.CodeGeneratorRequest{ProtoFile: desc.File}
g.Request.FileToGenerate = []string{"form.proto"}
g.Request.Parameter = proto.String("plugins=grpc")
g.CommandLineParameters(g.Request.GetParameter())
g.WrapTypes()
g.SetPackageNames()
g.BuildTypeNameMap()
g.Reset()
g.SetFile(desc.File[0])
formStr = CreateCustom("WeirdMethod", "form", "Weird", g, CustomBuildField)
server := http.Server{
Addr: "localhost:8080",
Handler: http.HandlerFunc(handle),
}
lis, err := net.Listen("tcp", "localhost:8080")
if err != nil {
t.Fatalf("Failed to listen: %v", err)
}
defer lis.Close()
go server.Serve(lis)
resp, err := http.Get("http://localhost:8080/WeirdMethod?json={%22Name%22:%22%22,%22WeirdName%22:%22another%20string%22,%22Number%22:null}")
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
t.Logf("%s", string(body))
if strings.Contains(string(body), "404") {
t.Fatal("404")
}
if !strings.Contains(string(body), `"WeirdName":"another string"`) {
t.Fatal("could not find json value")
}
}
示例6: main
func main() {
gen := generator.New()
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
gen.Error(err, "reading input")
}
if err := proto.Unmarshal(data, gen.Request); err != nil {
gen.Error(err, "parsing input proto")
}
if len(gen.Request.FileToGenerate) == 0 {
gen.Fail("no files to generate")
}
gen.CommandLineParameters(gen.Request.GetParameter())
gen.WrapTypes()
gen.SetPackageNames()
gen.BuildTypeNameMap()
gen.GeneratePlugin(html.New())
for i := 0; i < len(gen.Response.File); i++ {
gen.Response.File[i].Name = proto.String(strings.Replace(*gen.Response.File[i].Name, ".pb.go", ".letmegrpc.go", -1))
}
// Send back the results.
data, err = proto.Marshal(gen.Response)
if err != nil {
gen.Error(err, "failed to marshal output proto")
}
_, err = os.Stdout.Write(data)
if err != nil {
gen.Error(err, "failed to write output proto")
}
}
示例7: GeneratePlugin
// filenameSuffix replaces the .pb.go at the end of each filename.
func GeneratePlugin(req *plugin.CodeGeneratorRequest, p generator.Plugin, filenameSuffix string) *plugin.CodeGeneratorResponse {
g := generator.New()
g.Request = req
if len(g.Request.FileToGenerate) == 0 {
g.Fail("no files to generate")
}
g.CommandLineParameters(g.Request.GetParameter())
g.WrapTypes()
g.SetPackageNames()
g.BuildTypeNameMap()
g.GeneratePlugin(p)
for i := 0; i < len(g.Response.File); i++ {
g.Response.File[i].Name = proto.String(
strings.Replace(*g.Response.File[i].Name, ".pb.go", filenameSuffix, -1),
)
}
if err := goformat(g.Response); err != nil {
g.Error(err)
}
return g.Response
}