go17/skills/protobuf/hello.pb_test.go

53 lines
1000 B
Go
Raw Permalink Normal View History

2025-02-16 16:14:23 +08:00
package protobuf_test
import (
"testing"
"gitlab.com/go-course-project/go17/skills/protobuf"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
func TestHelloRequest(t *testing.T) {
req := &protobuf.HelloRequest{MyName: "bob", Age: 64}
data, err := proto.Marshal(req)
if err != nil {
t.Fatal(err)
}
t.Log(string(data))
req2 := &protobuf.HelloRequest{}
err = proto.Unmarshal(data, req2)
if err != nil {
t.Fatal(err)
}
t.Log(req2)
t.Log(protobuf.DESCRIBE_BY_ID)
}
func TestAny(t *testing.T) {
// 1. Pack
req := &protobuf.Event{
Type: protobuf.EVENT_TYPE_ECS,
Message: "test for any",
Detail: []*anypb.Any{},
}
ecsAny, err := anypb.New(&protobuf.EVENT_ECS{Message: "1"})
if err != nil {
t.Fatal(err)
}
req.Detail = append(req.Detail, ecsAny)
t.Log(req)
// 2. Unpack
t.Log(req.Detail[0])
ecsEvent := &protobuf.EVENT_ECS{}
if err := req.Detail[0].UnmarshalTo(ecsEvent); err != nil {
t.Fatal(err)
}
t.Log(ecsEvent)
}