We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Here is the code below.
Look at the TestOK(), I call MyGenerate() once, so the test passed.
Look at the TestFail(), I call MyGenerate() twice, so the test failed.
And I think the reason is that var plugins pluginSlice in generator.go is a global variable.
var plugins pluginSlice
So if I use gogo as a binary(such as .exe) through command line console, then gogo works well.
gogo
But if I import gogo and try to use gogo in my go code, then gogo could not work.
package adminProtoComponent_test import ( "encoding/json" "fmt" "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" "github.com/gogo/protobuf/protoc-gen-gogo/generator" "github.com/gogo/protobuf/vanity" "github.com/gogo/protobuf/vanity/command" "github.com/stretchr/testify/assert" "testing" gogoplugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin" ) /* // test.proto syntax = "proto3"; message Foo { int32 A = 1; int32 B = 2; } */ func GetFd() *descriptor.FileDescriptorProto { var fd descriptor.FileDescriptorProto jsonText := `{"name":"test.proto","message_type":[{"name":"Foo","field":[{"name":"A","number":1,"label":1,"type":5,"json_name":"A"},{"name":"B","number":2,"label":1,"type":5,"json_name":"B"}]}],"source_code_info":{"location":[{"span":[1,0,6,1]},{"path":[12],"span":[1,0,18]},{"path":[4,0],"span":[3,0,6,1]},{"path":[4,0,1],"span":[3,8,11]},{"path":[4,0,2,0],"span":[4,2,14]},{"path":[4,0,2,0,5],"span":[4,2,7]},{"path":[4,0,2,0,1],"span":[4,8,9]},{"path":[4,0,2,0,3],"span":[4,12,13]},{"path":[4,0,2,1],"span":[5,2,14]},{"path":[4,0,2,1,5],"span":[5,2,7]},{"path":[4,0,2,1,1],"span":[5,8,9]},{"path":[4,0,2,1,3],"span":[5,12,13]}]},"syntax":"proto3"}` fmt.Println(jsonText) err := json.Unmarshal([]byte(jsonText), &fd) if err != nil { panic(err) } return &fd } func MyGenerate(protoFileNames []string, fdProtos []*descriptor.FileDescriptorProto) *gogoplugin.CodeGeneratorResponse { g := generator.New() g.Request = &gogoplugin.CodeGeneratorRequest{ ProtoFile: fdProtos, } g.Request.FileToGenerate = protoFileNames files := g.Request.GetProtoFile() files = vanity.FilterFiles(files, vanity.NotGoogleProtobufDescriptorProto) vanity.ForEachFile(files, vanity.TurnOnMarshalerAll) vanity.ForEachFile(files, vanity.TurnOnSizerAll) vanity.ForEachFile(files, vanity.TurnOnUnmarshalerAll) vanity.ForEachFieldInFilesExcludingExtensions(vanity.OnlyProto2(files), vanity.TurnOffNullableForNativeTypesWithoutDefaultsOnly) vanity.ForEachFile(files, vanity.TurnOffGoUnrecognizedAll) vanity.ForEachFile(files, vanity.TurnOffGoUnkeyedAll) vanity.ForEachFile(files, vanity.TurnOffGoSizecacheAll) vanity.ForEachFile(files, vanity.TurnOffGoEnumPrefixAll) vanity.ForEachFile(files, vanity.TurnOffGoGettersAll) vanity.ForEachFile(files, vanity.TurnOnMessageNameAll) resp := command.Generate(g.Request) //command.Write(resp) return resp } // --- PASS: TestOK (0.00s) func TestOK(t *testing.T) { fd := GetFd() resp := MyGenerate([]string{*fd.Name}, []*descriptor.FileDescriptorProto{fd}) assert.Equal(t, 7276, len(*resp.File[0].Content)) } /* GogoProto_test.go:87: Error Trace: D:\proj\mine\gopath\src\gitee-server\web\admin\adminProto\adminProtoComponent\GogoProto_test.go:87 Error: Not equal: expected: 7276 actual : 2591 Test: TestFail --- FAIL: TestFail (0.01s) */ func TestFail(t *testing.T) { for i := 1; i <= 2; i++ { fd := GetFd() resp := MyGenerate([]string{*fd.Name}, []*descriptor.FileDescriptorProto{fd}) assert.Equal(t, 7276, len(*resp.File[0].Content)) } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Here is the code below.
Look at the TestOK(), I call MyGenerate() once, so the test passed.
Look at the TestFail(), I call MyGenerate() twice, so the test failed.
And I think the reason is that
var plugins pluginSlice
in generator.go is a global variable.So if I use
gogo
as a binary(such as .exe) through command line console, thengogo
works well.But if I import
gogo
and try to usegogo
in my go code, thengogo
could not work.The text was updated successfully, but these errors were encountered: