测试嵌套消息和import功能

This commit is contained in:
2023-01-19 16:02:36 +08:00
parent ad688a03dd
commit aed9362e36
7 changed files with 48 additions and 44 deletions
+13 -13
View File
@@ -7,36 +7,36 @@ import (
"time"
"google.golang.org/grpc"
pb "tldemo/services/Greeter"
Greeter "tldemo/services/Greeter"
)
type GreeterServer struct {
pb.UnimplementedGreeterServiceServer
Greeter.UnimplementedGreeterServiceServer
}
func (s *GreeterServer) Hello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloResponse, error) {
func (s *GreeterServer) Hello(ctx context.Context, in *Greeter.HelloRequest) (*Greeter.HelloResponse, error) {
currentTime := time.Now()
replyMessage := fmt.Sprintf("Hello %s, time is %s", in.Name, currentTime.Format("15:04:05"))
log.Printf("[Hello User] %s", in.Name)
result := &pb.HelloResponse{ReplyMessage: replyMessage}
result := &Greeter.HelloResponse{ReplyMessage: replyMessage}
return result, nil
}
type TimeServer struct {
pb.UnimplementedTimeServiceServer
Greeter.UnimplementedTimeServiceServer
}
func (s *TimeServer) Now(ctx context.Context, in *pb.Nothing) (*pb.NowResponse, error) {
func (s *TimeServer) Now(ctx context.Context, in *Greeter.Nothing) (*Greeter.NowResponse, error) {
currentTime := uint64(time.Now().UTC().UnixMilli())
log.Printf("[Now]send time: %d", currentTime)
result := &pb.NowResponse{Now: currentTime}
result := &Greeter.NowResponse{Now: currentTime}
return result, nil
}
func (s *TimeServer) Ticktok(in *pb.TicktokRequest, stream pb.TimeService_TicktokServer) error {
func (s *TimeServer) Ticktok(in *Greeter.TicktokRequest, stream Greeter.TimeService_TicktokServer) error {
ctx := stream.Context()
response := &pb.NowResponse{}
response := &Greeter.NowResponse{}
sendNow := func(counts int32) {
response.Now = uint64(time.Now().UTC().UnixMilli())
@@ -72,9 +72,9 @@ func (s *TimeServer) Ticktok(in *pb.TicktokRequest, stream pb.TimeService_Tickto
return nil
}
func (s *TimeServer) Watch(stream pb.TimeService_WatchServer) error {
func (s *TimeServer) Watch(stream Greeter.TimeService_WatchServer) error {
ctx := stream.Context()
response := &pb.NowResponse{}
response := &Greeter.NowResponse{}
sendNow := func() error {
response.Now = uint64(time.Now().UTC().UnixMilli())
@@ -100,10 +100,10 @@ func (s *TimeServer) Watch(stream pb.TimeService_WatchServer) error {
func RegisterGreeterServer(s *grpc.Server) {
greeterServer := &GreeterServer{}
pb.RegisterGreeterServiceServer(s, greeterServer)
Greeter.RegisterGreeterServiceServer(s, greeterServer)
timeServer := &TimeServer{}
pb.RegisterTimeServiceServer(s, timeServer)
Greeter.RegisterTimeServiceServer(s, timeServer)
}
+14 -14
View File
@@ -7,7 +7,8 @@ import (
"errors"
"google.golang.org/grpc"
pb "tldemo/services/User"
User "tldemo/services/User"
Common "tldemo/services/Common"
)
type UserAddress struct {
@@ -18,16 +19,16 @@ type UserAddress struct {
type UserData struct {
Id int
Name string
Gender pb.GENDER_ENUM
Gender Common.Gender
Addresses [] *UserAddress
}
type UserServer struct {
pb.UnimplementedUserServiceServer
User.UnimplementedUserServiceServer
UserArray []*UserData
}
func (s *UserServer) Register(ctx context.Context, in *pb.RegisterRequest) (*pb.RegisterResponse, error) {
func (s *UserServer) Register(ctx context.Context, in *User.RegisterRequest) (*User.RegisterResponse, error) {
userData := &UserData{Id:len(s.UserArray), Name:in.Name, Gender:in.Gender}
userData.Addresses = make([]*UserAddress, 0)
for i:=0; i<len(in.Address); i++ {
@@ -38,11 +39,11 @@ func (s *UserServer) Register(ctx context.Context, in *pb.RegisterRequest) (*pb.
replyMessage := fmt.Sprintf("Hello %s, Your Id is %v", userData.Name, userData.Id)
log.Printf("[Register User] %s", replyMessage)
result := &pb.RegisterResponse{Id: uint64(userData.Id), Msg: replyMessage}
result := &User.RegisterResponse{Id: uint64(userData.Id), Msg: replyMessage}
return result, nil
}
func (s *UserServer) Query(ctx context.Context, in *pb.QueryRequest) (*pb.QueryResponse, error) {
func (s *UserServer) Query(ctx context.Context, in *User.QueryRequest) (*User.QueryResponse, error) {
log.Printf("[Query User] %d", in.Id)
if(in.Id<0 || int(in.Id)>=len(s.UserArray)) {
@@ -51,16 +52,15 @@ func (s *UserServer) Query(ctx context.Context, in *pb.QueryRequest) (*pb.QueryR
var userData = s.UserArray[int(in.Id)]
address := make([]*pb.Address, 0)
for i:=0; i<len(userData.Addresses); i++ {
address = append(address, &pb.Address{ Address: userData.Addresses[i].Address, PostalCode: userData.Addresses[i].PostalCode})
}
//address[0] = &pb.Address{ Address: "xxxx", PostalCode: "1234"}
//address := make([]*User.RegisterRequest_Address, 0)
//for i:=0; i<len(userData.Addresses); i++ {
// address = append(address, &User.RegisterRequest_Address{ Address: userData.Addresses[i].Address, PostalCode: userData.Addresses[i].PostalCode})
//}
//address[0] = &User.Address{ Address: "xxxx", PostalCode: "1234"}
result := &pb.QueryResponse{
result := &User.QueryResponse{
Name: userData.Name,
Gender: userData.Gender,
Address : address,
}
return result, nil
}
@@ -69,6 +69,6 @@ func RegisterUserServer(s *grpc.Server) {
userServer := &UserServer{}
userServer.UserArray = make([]*UserData, 0)
pb.RegisterUserServiceServer(s, userServer)
User.RegisterUserServiceServer(s, userServer)
}