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

internal/descriptor: move to generated apiconfig test #1352

Merged
merged 1 commit into from May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions Makefile
Expand Up @@ -25,7 +25,6 @@ GATEWAY_PLUGIN_SRC= ./internal/utilities/doc.go \
./internal/descriptor/services.go \
./internal/descriptor/types.go \
./internal/descriptor/grpc_api_configuration.go \
./internal/descriptor/grpc_api_service.go \
./internal/generator \
./internal/generator/generator.go \
protoc-gen-grpc-gateway \
Expand Down Expand Up @@ -93,6 +92,9 @@ RUNTIME_TEST_PROTO=runtime/internal/examplepb/example.proto \
runtime/internal/examplepb/non_standard_names.proto
RUNTIME_TEST_SRCS=$(RUNTIME_TEST_PROTO:.proto=.pb.go)

APICONFIG_PROTO=internal/descriptor/apiconfig/apiconfig.proto
APICONFIG_SRCS=$(APICONFIG_PROTO:.proto=.pb.go)

EXAMPLE_CLIENT_DIR=examples/internal/clients
ECHO_EXAMPLE_SPEC=examples/internal/proto/examplepb/echo_service.swagger.json
ECHO_EXAMPLE_SRCS=$(EXAMPLE_CLIENT_DIR)/echo/client.go \
Expand Down Expand Up @@ -165,6 +167,9 @@ $(EXAMPLE_DEPSRCS): $(GO_PLUGIN) $(EXAMPLE_DEPS)
$(RUNTIME_TEST_SRCS): $(GO_PLUGIN) $(RUNTIME_TEST_PROTO)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=$(PKGMAP),plugins=grpc,paths=source_relative:. $(RUNTIME_TEST_PROTO)

$(APICONFIG_SRCS): $(GO_PLUGIN) $(APICONFIG_PROTO)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=$(PKGMAP),paths=source_relative:. $(APICONFIG_PROTO)

$(EXAMPLE_GWSRCS): ADDITIONAL_GW_FLAGS:=$(ADDITIONAL_GW_FLAGS),grpc_api_configuration=examples/internal/proto/examplepb/unannotated_echo_service.yaml
$(EXAMPLE_GWSRCS): ADDITIONAL_SA_FLAGS:=,standalone=true,grpc_api_configuration=examples/internal/proto/examplepb/standalone_echo_service.yaml
$(EXAMPLE_GWSRCS): $(GATEWAY_PLUGIN) $(EXAMPLES)
Expand Down Expand Up @@ -207,7 +212,7 @@ $(RESPONSE_BODY_EXAMPLE_SRCS): $(RESPONSE_BODY_EXAMPLE_SPEC)
$(EXAMPLE_CLIENT_DIR)/responsebody/git_push.sh

examples: $(EXAMPLE_DEPSRCS) $(EXAMPLE_SVCSRCS) $(EXAMPLE_GWSRCS) $(EXAMPLE_SWAGGERSRCS) $(EXAMPLE_CLIENT_SRCS) $(HELLOWORLD_SVCSRCS) $(HELLOWORLD_GWSRCS)
testproto: $(RUNTIME_TEST_SRCS)
testproto: $(RUNTIME_TEST_SRCS) $(APICONFIG_SRCS)
test: examples testproto
go test -short -race ./...
go test -race ./examples/internal/integration -args -network=unix -endpoint=test.sock
Expand Down
2 changes: 1 addition & 1 deletion internal/descriptor/BUILD.bazel
Expand Up @@ -6,14 +6,14 @@ go_library(
name = "go_default_library",
srcs = [
"grpc_api_configuration.go",
"grpc_api_service.go",
"registry.go",
"services.go",
"types.go",
],
importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor",
deps = [
"//internal/casing:go_default_library",
"//internal/descriptor/apiconfig:go_default_library",
"//internal/httprule:go_default_library",
"@com_github_ghodss_yaml//:go_default_library",
"@com_github_golang_glog//:go_default_library",
Expand Down
28 changes: 28 additions & 0 deletions internal/descriptor/apiconfig/BUILD.bazel
@@ -0,0 +1,28 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

package(default_visibility = ["//visibility:public"])

proto_library(
name = "apiconfig_proto",
srcs = [
"apiconfig.proto",
],
deps = [
"@go_googleapis//google/api:annotations_proto",
],
)

go_proto_library(
name = "apiconfig_go_proto",
importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor/apiconfig",
proto = ":apiconfig_proto",
deps = ["@go_googleapis//google/api:annotations_go_proto"],
)

go_library(
name = "go_default_library",
embed = [":apiconfig_go_proto"],
importpath = "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor/apiconfig",
)
170 changes: 170 additions & 0 deletions internal/descriptor/apiconfig/apiconfig.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions internal/descriptor/apiconfig/apiconfig.proto
@@ -0,0 +1,21 @@
syntax = "proto3";

package grpc.gateway.internal.descriptor.apiconfig;

option go_package = "github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor/apiconfig";

import "google/api/http.proto";

// GrpcAPIService represents a stripped down version of google.api.Service .
// Compare to https://github.com/googleapis/googleapis/blob/master/google/api/service.proto
// The original imports 23 other protobuf files we are not interested in. If a significant
// subset (>50%) of these start being reproduced in this file we should swap to using the
// full generated version instead.
//
// For the purposes of the gateway generator we only consider a small subset of all
// available features google supports in their service descriptions. Thanks to backwards
// compatibility guarantees by protobuf it is safe for us to remove the other fields.
message GrpcAPIService {
// Http Rule.
google.api.Http http = 1;
}
11 changes: 6 additions & 5 deletions internal/descriptor/grpc_api_configuration.go
Expand Up @@ -8,9 +8,10 @@ import (

"github.com/ghodss/yaml"
"github.com/golang/protobuf/jsonpb"
"github.com/grpc-ecosystem/grpc-gateway/v2/internal/descriptor/apiconfig"
)

func loadGrpcAPIServiceFromYAML(yamlFileContents []byte, yamlSourceLogName string) (*GrpcAPIService, error) {
func loadGrpcAPIServiceFromYAML(yamlFileContents []byte, yamlSourceLogName string) (*apiconfig.GrpcAPIService, error) {
jsonContents, err := yaml.YAMLToJSON(yamlFileContents)
if err != nil {
return nil, fmt.Errorf("Failed to convert gRPC API Configuration from YAML in '%v' to JSON: %v", yamlSourceLogName, err)
Expand All @@ -21,21 +22,21 @@ func loadGrpcAPIServiceFromYAML(yamlFileContents []byte, yamlSourceLogName strin
AllowUnknownFields: true,
}

serviceConfiguration := GrpcAPIService{}
serviceConfiguration := apiconfig.GrpcAPIService{}
if err := unmarshaler.Unmarshal(bytes.NewReader(jsonContents), &serviceConfiguration); err != nil {
return nil, fmt.Errorf("Failed to parse gRPC API Configuration from YAML in '%v': %v", yamlSourceLogName, err)
}

return &serviceConfiguration, nil
}

func registerHTTPRulesFromGrpcAPIService(registry *Registry, service *GrpcAPIService, sourceLogName string) error {
if service.HTTP == nil {
func registerHTTPRulesFromGrpcAPIService(registry *Registry, service *apiconfig.GrpcAPIService, sourceLogName string) error {
if service.Http == nil {
// Nothing to do
return nil
}

for _, rule := range service.HTTP.GetRules() {
for _, rule := range service.Http.GetRules() {
selector := "." + strings.Trim(rule.GetSelector(), " ")
if strings.ContainsAny(selector, "*, ") {
return fmt.Errorf("Selector '%v' in %v must specify a single service method without wildcards", rule.GetSelector(), sourceLogName)
Expand Down
20 changes: 10 additions & 10 deletions internal/descriptor/grpc_api_configuration_test.go
Expand Up @@ -15,7 +15,7 @@ func TestLoadGrpcAPIServiceFromYAMLEmpty(t *testing.T) {
t.Fatal("No service returned")
}

if service.HTTP != nil {
if service.Http != nil {
t.Fatal("HTTP not empty")
}
}
Expand Down Expand Up @@ -47,15 +47,15 @@ http:
t.Fatal(err)
}

if service.HTTP == nil {
if service.Http == nil {
t.Fatal("HTTP is empty")
}

if len(service.HTTP.GetRules()) != 1 {
t.Fatalf("Have %v rules instead of one. Got: %v", len(service.HTTP.GetRules()), service.HTTP.GetRules())
if len(service.Http.GetRules()) != 1 {
t.Fatalf("Have %v rules instead of one. Got: %v", len(service.Http.GetRules()), service.Http.GetRules())
}

rule := service.HTTP.GetRules()[0]
rule := service.Http.GetRules()[0]
if rule.GetSelector() != "grpctest.YourService.Echo" {
t.Errorf("Rule has unexpected selector '%v'", rule.GetSelector())
}
Expand Down Expand Up @@ -117,15 +117,15 @@ http:
t.Fatal("No service returned")
}

if service.HTTP == nil {
if service.Http == nil {
t.Fatal("HTTP is empty")
}

if len(service.HTTP.GetRules()) != 2 {
t.Fatalf("%v service(s) returned when two were expected. Got: %v", len(service.HTTP.GetRules()), service.HTTP)
if len(service.Http.GetRules()) != 2 {
t.Fatalf("%v service(s) returned when two were expected. Got: %v", len(service.Http.GetRules()), service.Http)
}

first := service.HTTP.GetRules()[0]
first := service.Http.GetRules()[0]
if first.GetSelector() != "first.selector" {
t.Errorf("first.selector has unexpected selector '%v'", first.GetSelector())
}
Expand All @@ -151,7 +151,7 @@ http:
t.Errorf("first.selector additional binding 3 has unexpected patch '%v'", first.GetAdditionalBindings()[0].GetPost())
}

second := service.HTTP.GetRules()[1]
second := service.Http.GetRules()[1]
if second.GetSelector() != "some.other.service" {
t.Errorf("some.other.service has unexpected selector '%v'", second.GetSelector())
}
Expand Down