-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Pulled out parseReq func into a generic package + tests #600
Merged
achew22
merged 2 commits into
grpc-ecosystem:master
from
f0rmiga:refactor-codegenerator
Apr 15, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
Package codegenerator contains reusable functions used by the code generators. | ||
*/ | ||
package codegenerator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package codegenerator | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
|
||
"github.com/golang/protobuf/proto" | ||
plugin "github.com/golang/protobuf/protoc-gen-go/plugin" | ||
) | ||
|
||
// ParseRequest parses a code generator request from a proto Message. | ||
func ParseRequest(r io.Reader) (*plugin.CodeGeneratorRequest, error) { | ||
input, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read code generator request: %v", err) | ||
} | ||
req := new(plugin.CodeGeneratorRequest) | ||
if err = proto.Unmarshal(input, req); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal code generator request: %v", err) | ||
} | ||
return req, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package codegenerator_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/golang/protobuf/proto" | ||
plugin "github.com/golang/protobuf/protoc-gen-go/plugin" | ||
"github.com/grpc-ecosystem/grpc-gateway/codegenerator" | ||
) | ||
|
||
var parseReqTests = []struct { | ||
name string | ||
in io.Reader | ||
out *plugin.CodeGeneratorRequest | ||
err error | ||
}{ | ||
{ | ||
"Empty input should produce empty output", | ||
mustGetReader(&plugin.CodeGeneratorRequest{}), | ||
&plugin.CodeGeneratorRequest{}, | ||
nil, | ||
}, | ||
{ | ||
"Invalid reader should produce error", | ||
&invalidReader{}, | ||
nil, | ||
fmt.Errorf("failed to read code generator request: invalid reader"), | ||
}, | ||
{ | ||
"Invalid proto message should produce error", | ||
strings.NewReader("{}"), | ||
nil, | ||
fmt.Errorf("failed to unmarshal code generator request: unexpected EOF"), | ||
}, | ||
} | ||
|
||
func TestFlagParser(t *testing.T) { | ||
for _, tt := range parseReqTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
out, err := codegenerator.ParseRequest(tt.in) | ||
if !reflect.DeepEqual(err, tt.err) { | ||
t.Errorf("got %v, want %v", err, tt.err) | ||
} | ||
if err == nil && !reflect.DeepEqual(*out, *tt.out) { | ||
t.Errorf("got %v, want %v", *out, *tt.out) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func mustGetReader(pb proto.Message) io.Reader { | ||
b, _ := proto.Marshal(pb) | ||
return bytes.NewBuffer(b) | ||
} | ||
|
||
type invalidReader struct { | ||
} | ||
|
||
func (*invalidReader) Read(p []byte) (int, error) { | ||
return 0, fmt.Errorf("invalid reader") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,14 +10,13 @@ package main | |
|
||
import ( | ||
"flag" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/golang/glog" | ||
"github.com/golang/protobuf/proto" | ||
plugin "github.com/golang/protobuf/protoc-gen-go/plugin" | ||
"github.com/grpc-ecosystem/grpc-gateway/codegenerator" | ||
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor" | ||
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/gengateway" | ||
) | ||
|
@@ -29,33 +28,19 @@ var ( | |
allowDeleteBody = flag.Bool("allow_delete_body", false, "unless set, HTTP DELETE methods may not have a body") | ||
) | ||
|
||
func parseReq(r io.Reader) (*plugin.CodeGeneratorRequest, error) { | ||
glog.V(1).Info("Parsing code generator request") | ||
input, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
glog.Errorf("Failed to read code generator request: %v", err) | ||
return nil, err | ||
} | ||
req := new(plugin.CodeGeneratorRequest) | ||
if err = proto.Unmarshal(input, req); err != nil { | ||
glog.Errorf("Failed to unmarshal code generator request: %v", err) | ||
return nil, err | ||
} | ||
glog.V(1).Info("Parsed code generator request") | ||
return req, nil | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
defer glog.Flush() | ||
|
||
reg := descriptor.NewRegistry() | ||
|
||
glog.V(1).Info("Processing code generator request") | ||
req, err := parseReq(os.Stdin) | ||
glog.V(1).Info("Parsing code generator request") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate logging There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove the one on line 37 |
||
req, err := codegenerator.ParseRequest(os.Stdin) | ||
if err != nil { | ||
glog.Fatal(err) | ||
} | ||
glog.V(1).Info("Parsed code generator request") | ||
if req.Parameter != nil { | ||
for _, p := range strings.Split(req.GetParameter(), ",") { | ||
spec := strings.SplitN(p, "=", 2) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to panic on error instead of silencing in a must function