Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

google.protobuf.Value tests #56

Merged
merged 1 commit into from
Nov 6, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/known/wrapperspb"

"gopkg.in/guregu/null.v3"

"github.com/golang/protobuf/ptypes/any"
_struct "github.com/golang/protobuf/ptypes/struct"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -826,6 +828,63 @@ func TestClient(t *testing.T) {
`,
},
},
{
name: "ValueReflection",
setup: func(hb *httpmultibin.HTTPMultiBin) {
reflection.Register(hb.ServerGRPC)

srv := wrappers_testing.Register(hb.ServerGRPC)

srv.TestValueImplementation = func(_ context.Context, in *_struct.Value) (*_struct.Value, error) {
if in.GetNumberValue() == 12 {
return &_struct.Value{
Kind: &_struct.Value_NumberValue{
NumberValue: 42,
},
}, nil
}

if in.GetStringValue() != "" {
return &_struct.Value{
Kind: &_struct.Value_StringValue{
StringValue: "hey " + in.GetStringValue(),
},
}, nil
}

return &_struct.Value{
Kind: &_struct.Value_StringValue{
StringValue: "I don't know what to answer",
},
}, nil
}
},
initString: codeBlock{
code: `
const client = new grpc.Client();
`,
},
vuString: codeBlock{
code: `
client.connect("GRPCBIN_ADDR", {reflect: true});

let respString = client.invoke("grpc.wrappers.testing.Service/TestValue", "John")
if (respString.message !== "hey John") {
throw new Error("expected to get 'hey John', but got a " + respString.message)
}

let respNumber = client.invoke("grpc.wrappers.testing.Service/TestValue", 12)
if (respNumber.message !== 42) {
throw new Error("expected to get '42', but got a " + respNumber.message)
}

let respBool = client.invoke("grpc.wrappers.testing.Service/TestValue", false)
if (respBool.message !== "I don't know what to answer") {
throw new Error("expected to get 'I don't know what to answer', but got a " + respBool.message)
}
`,
},
},
}

for _, tt := range tests {
Expand Down
10 changes: 10 additions & 0 deletions grpc/testdata/wrappers_testing/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wrappers_testing
import (
context "context"

_struct "github.com/golang/protobuf/ptypes/struct"
wrappers "github.com/golang/protobuf/ptypes/wrappers"
grpc "google.golang.org/grpc"
)
Expand All @@ -25,6 +26,7 @@ type service struct {
TestIntegerImplementation func(context.Context, *wrappers.Int64Value) (*wrappers.Int64Value, error)
TestBooleanImplementation func(context.Context, *wrappers.BoolValue) (*wrappers.BoolValue, error)
TestDoubleImplementation func(context.Context, *wrappers.DoubleValue) (*wrappers.DoubleValue, error)
TestValueImplementation func(context.Context, *_struct.Value) (*_struct.Value, error)
TestStreamImplementation func(Service_TestStreamServer) error
}

Expand Down Expand Up @@ -60,6 +62,14 @@ func (s *service) TestDouble(ctx context.Context, in *wrappers.DoubleValue) (*wr
return s.UnimplementedServiceServer.TestDouble(ctx, in)
}

func (s *service) TestValue(ctx context.Context, in *_struct.Value) (*_struct.Value, error) {
if s.TestValueImplementation != nil {
return s.TestValueImplementation(ctx, in)
}

return s.UnimplementedServiceServer.TestValue(ctx, in)
}

func (s *service) TestStream(stream Service_TestStreamServer) error {
if s.TestStreamImplementation != nil {
return s.TestStreamImplementation(stream)
Expand Down
74 changes: 42 additions & 32 deletions grpc/testdata/wrappers_testing/test.pb.go

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

2 changes: 2 additions & 0 deletions grpc/testdata/wrappers_testing/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package grpc.wrappers.testing;
// https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/wrappers.proto

import "google/protobuf/wrappers.proto";
import "google/protobuf/struct.proto";

option go_package ="./wrappers_testing";

Expand All @@ -14,6 +15,7 @@ service Service {
rpc TestInteger(google.protobuf.Int64Value) returns (google.protobuf.Int64Value);
rpc TestBoolean(google.protobuf.BoolValue) returns (google.protobuf.BoolValue);
rpc TestDouble(google.protobuf.DoubleValue) returns (google.protobuf.DoubleValue);
rpc TestValue(google.protobuf.Value) returns (google.protobuf.Value);

rpc TestStream(stream google.protobuf.StringValue) returns (google.protobuf.StringValue);
}
37 changes: 37 additions & 0 deletions grpc/testdata/wrappers_testing/test_grpc.pb.go

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