Skip to content

Commit

Permalink
fix: grpc-transcode plugin: fix map data population (#8731)
Browse files Browse the repository at this point in the history
Fixes #8655
  • Loading branch information
kingluo authored Jan 30, 2023
1 parent c8d5afb commit 736c8f2
Show file tree
Hide file tree
Showing 9 changed files with 505 additions and 5 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ indent_style = unset
indent_style = unset
insert_final_newline = unset
trim_trailing_whitespace = unset
end_of_line = unset
30 changes: 25 additions & 5 deletions apisix/plugins/grpc-transcode/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ local ngx = ngx
local string = string
local table = table
local ipairs = ipairs
local pairs = pairs
local tonumber = tonumber
local type = type

Expand Down Expand Up @@ -131,7 +132,7 @@ local function get_from_request(request_table, name, kind)
end


function _M.map_message(field, default_values, request_table)
function _M.map_message(field, default_values, request_table, real_key)
if not pb.type(field) then
return nil, "Field " .. field .. " is not defined"
end
Expand Down Expand Up @@ -164,15 +165,34 @@ function _M.map_message(field, default_values, request_table)
end
sub = sub_array
else
sub, err = _M.map_message(field_type, default_values and default_values[name],
request_table[name])
if err then
return nil, err
if ty == "map" then
for k, v in pairs(request_table[name]) do
local tbl, err = _M.map_message(field_type,
default_values and default_values[name],
request_table[name], k)
if err then
return nil, err
end
if not sub then
sub = {}
end
sub[k] = tbl[k]
end
else
sub, err = _M.map_message(field_type,
default_values and default_values[name],
request_table[name])
if err then
return nil, err
end
end
end

request[name] = sub
else
if real_key then
name = real_key
end
request[name] = get_from_request(request_table, name, field_type)
or (default_values and default_values[name])
end
Expand Down
Binary file added t/grpc_server_example/echo.pb
Binary file not shown.
1 change: 1 addition & 0 deletions t/grpc_server_example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/api7/grpc_server_example
go 1.11

require (
github.com/golang/protobuf v1.5.0
golang.org/x/net v0.2.0
google.golang.org/grpc v1.32.0
google.golang.org/protobuf v1.27.1
Expand Down
12 changes: 12 additions & 0 deletions t/grpc_server_example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/helloworld.proto
//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/import.proto
//go:generate protoc --include_imports --descriptor_set_out=proto.pb --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/src.proto
//go:generate protoc --descriptor_set_out=echo.pb --include_imports --proto_path=$PWD/proto echo.proto
//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/echo.proto

// Package main implements a server for Greeter service.
package main
Expand Down Expand Up @@ -76,6 +78,7 @@ type server struct {
// Embed the unimplemented server
pb.UnimplementedGreeterServer
pb.UnimplementedTestImportServer
pb.UnimplementedEchoServer
}

// SayHello implements helloworld.GreeterServer
Expand Down Expand Up @@ -141,6 +144,14 @@ func (s *server) Plus(ctx context.Context, in *pb.PlusRequest) (*pb.PlusReply, e
return &pb.PlusReply{Result: in.A + in.B}, nil
}

func (s *server) EchoStruct(ctx context.Context, in *pb.StructRequest) (*pb.StructReply, error) {
log.Printf("Received: %+v", in)

return &pb.StructReply{
Data: in.Data,
}, nil
}

// SayHelloServerStream streams HelloReply back to the client.
func (s *server) SayHelloServerStream(req *pb.HelloRequest, stream pb.Greeter_SayHelloServerStreamServer) error {
log.Printf("Received server side stream req: %v\n", req)
Expand Down Expand Up @@ -251,6 +262,7 @@ func main() {
reflection.Register(s)
pb.RegisterGreeterServer(s, &server{})
pb.RegisterTestImportServer(s, &server{})
pb.RegisterEchoServer(s, &server{})

if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
Expand Down
236 changes: 236 additions & 0 deletions t/grpc_server_example/proto/echo.pb.go

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

35 changes: 35 additions & 0 deletions t/grpc_server_example/proto/echo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

syntax = "proto3";

package echo;
option go_package = "./proto";

import "google/protobuf/struct.proto";

service Echo {
rpc EchoStruct (StructRequest) returns (StructReply) {}
}

message StructRequest {
google.protobuf.Struct data = 1;
}

message StructReply {
google.protobuf.Struct data = 1;
}
Loading

0 comments on commit 736c8f2

Please sign in to comment.