package comm

import (
	"fmt"
	"go_dreamfactory/pb"

	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
)

// 对protobuf格式的数据进行反序列化
func ProtoUnmarshal(msg *pb.UserMessage, req proto.Message) (ok bool) {
	err := msg.Data.UnmarshalTo(req)
	if err != nil {
		fmt.Printf("UnmarshalTo %s.%s %v\n", msg.MainType, msg.SubType, err)
		return
	}
	return true
}

// 对protobuf格式的数据进行序列化
func ProtoMarshal(rsp proto.Message, msg *pb.UserMessage) (ok bool) {
	any, err := anypb.New(rsp)
	if err != nil {
		fmt.Printf("Any New %s.%s %v", msg.MainType, msg.SubType, err)
		return
	}
	msg.Data = any
	return true
}
