-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pulled out parseReq func into a generic package + tests (#600)
- Loading branch information
Showing
5 changed files
with
104 additions
and
39 deletions.
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,69 @@ | ||
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 TestParseRequest(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, err := proto.Marshal(pb) | ||
if err != nil { | ||
panic(err) | ||
} | ||
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
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