Skip to content

Commit

Permalink
fix empty parameter fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Hochman committed Oct 14, 2020
1 parent 1aab03c commit f75cafd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
9 changes: 8 additions & 1 deletion protoc-gen-grpc-gateway/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@io_bazel_rules_go//proto:compiler.bzl", "go_proto_compiler")

package(default_visibility = ["//visibility:private"])
Expand Down Expand Up @@ -41,3 +41,10 @@ go_proto_compiler(
"@org_golang_google_protobuf//proto:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["main_test.go"],
embed = [":go_default_library"],
deps = ["//internal/descriptor:go_default_library"],
)
4 changes: 4 additions & 0 deletions protoc-gen-grpc-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func main() {
}

func parseFlags(reg *descriptor.Registry, parameter string) {
if parameter == "" {
return
}

for _, p := range strings.Split(parameter, ",") {
spec := strings.SplitN(p, "=", 2)
if len(spec) == 1 {
Expand Down
31 changes: 31 additions & 0 deletions protoc-gen-grpc-gateway/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"testing"

"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor"
)

func TestParseFlagsEmptyNoPanic(t *testing.T) {
reg := descriptor.NewRegistry()
parseFlags(reg, "")
}

func TestParseFlags(t *testing.T) {
reg := descriptor.NewRegistry()
parseFlags(reg, "allow_repeated_fields_in_body=true")
if *allowRepeatedFieldsInBody != true {
t.Errorf("flag allow_repeated_fields_in_body was not set correctly, wanted true got %v", *allowRepeatedFieldsInBody)
}
}

func TestParseFlagsMultiple(t *testing.T) {
reg := descriptor.NewRegistry()
parseFlags(reg, "allow_repeated_fields_in_body=true,import_prefix=foo")
if *allowRepeatedFieldsInBody != true {
t.Errorf("flag allow_repeated_fields_in_body was not set correctly, wanted 'true' got '%v'", *allowRepeatedFieldsInBody)
}
if *importPrefix != "foo" {
t.Errorf("flag importPrefix was not set correctly, wanted 'foo' got '%v'", *importPrefix)
}
}

0 comments on commit f75cafd

Please sign in to comment.