Skip to content
New issue

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

Fix/lints #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions gripmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ func main() {
// for safety
output += "/"
if _, err := os.Stat(output); os.IsNotExist(err) {
os.Mkdir(output, os.ModePerm)
if err := os.Mkdir(output, os.ModePerm); err != nil {
log.Fatalf("mkdir: %v", err)
}
} else if err != nil {
log.Fatalf("os.stat: %v", err)
}

// run admin stub server
Expand Down Expand Up @@ -96,14 +100,14 @@ type protocParam struct {
}

func generateProtoc(param protocParam) {
param.protoPath = fixGoPackage(param.protoPath)
protodirs := strings.Split(param.protoPath[0], "/")
protodir := ""
if len(protodirs) > 0 {
protodir = strings.Join(protodirs[:len(protodirs)-1], "/") + "/"
param.protoPath = fixGoPackage(param.protoPath)
protoDirs := strings.Split(param.protoPath[0], "/")
protoDir := ""
if len(protoDirs) > 0 {
protoDir = strings.Join(protoDirs[:len(protoDirs)-1], "/") + "/"
}

args := []string{"-I", protodir}
args := []string{"-I", protoDir}
// include well-known-types
for _, i := range param.imports {
args = append(args, "-I", i)
Expand Down Expand Up @@ -150,9 +154,9 @@ func runGrpcServer(output string) (*exec.Cmd, <-chan error) {
log.Fatal(err)
}
fmt.Printf("grpc server pid: %d\n", run.Process.Pid)
runerr := make(chan error)
runErr := make(chan error)
go func() {
runerr <- run.Wait()
runErr <- run.Wait()
}()
return run, runerr
return run, runErr
}
34 changes: 19 additions & 15 deletions protoc-gen-gripmock/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func main() {
}

file := plugin.NewGeneratedFile("server.go", ".")
file.Write(buf.Bytes())
if _, err := file.Write(buf.Bytes()); err != nil {
log.Fatalf("wrtie generated file: %v", err)
}

// Generate a response from our plugin and marshall as protobuf
out, err := proto.Marshal(plugin.Response())
Expand All @@ -70,7 +72,9 @@ func main() {
}

// Write the response to stdout, to be picked up by protoc
os.Stdout.Write(out)
if _, err := os.Stdout.Write(out); err != nil {
log.Fatalf("write response: %v", err)
}
}

type generatorParam struct {
Expand Down Expand Up @@ -121,12 +125,12 @@ func init() {
log.Fatalf("error opening server.tmpl: %s", err)
}

bytes, err := ioutil.ReadAll(f)
serverTemplateBytes, err := ioutil.ReadAll(f)
if err != nil {
log.Fatalf("error reading server.tmpl: %s", err)
}

SERVER_TEMPLATE = string(bytes)
SERVER_TEMPLATE = string(serverTemplateBytes)
}

func generateServer(protos []*descriptor.FileDescriptorProto, opt *Options) error {
Expand Down Expand Up @@ -174,12 +178,12 @@ func generateServer(protos []*descriptor.FileDescriptorProto, opt *Options) erro
func resolveDependencies(protos []*descriptor.FileDescriptorProto) map[string]string {

deps := map[string]string{}
for _, proto := range protos {
alias, pkg := getGoPackage(proto)
for _, prt := range protos {
alias, pkg := getGoPackage(prt)

// fatal if go_package is not present
if pkg == "" {
log.Fatalf("option go_package is required. but %s doesn't have any", proto.GetName())
log.Fatalf("option go_package is required. but %s doesn't have any", prt.GetName())
}

if _, ok := deps[pkg]; ok {
Expand Down Expand Up @@ -240,12 +244,12 @@ func getGoPackage(proto *descriptor.FileDescriptorProto) (alias string, goPackag

// change the structure also translate method type
func extractServices(protos []*descriptor.FileDescriptorProto) []Service {
svcTmp := []Service{}
for _, proto := range protos {
for _, svc := range proto.GetService() {
var svcTmp []Service
for _, prt := range protos {
for _, svc := range prt.GetService() {
var s Service
s.Name = svc.GetName()
alias, _ := getGoPackage(proto)
alias, _ := getGoPackage(prt)
if alias != "" {
s.Package = alias + "."
}
Expand Down Expand Up @@ -280,14 +284,14 @@ func getMessageType(protos []*descriptor.FileDescriptorProto, tipe string) strin
split := strings.Split(tipe, ".")[1:]
targetPackage := strings.Join(split[:len(split)-1], ".")
targetType := split[len(split)-1]
for _, proto := range protos {
if proto.GetPackage() != targetPackage {
for _, prt := range protos {
if prt.GetPackage() != targetPackage {
continue
}

for _, msg := range proto.GetMessageType() {
for _, msg := range prt.GetMessageType() {
if msg.GetName() == targetType {
alias, _ := getGoPackage(proto)
alias, _ := getGoPackage(prt)
if alias != "" {
alias += "."
}
Expand Down