package protos

import (
	"testing"

	"github.com/golang/protobuf/proto"
	"github.com/golang/protobuf/ptypes"
	"github.com/golang/protobuf/ptypes/any"
)

func TestAny(t *testing.T) {
	v1 := &C_Login{
		Token: "xyz",
	}
	bytesv1, err := proto.Marshal(v1)
	if err != nil {
		t.Error("Marshal error")
		return
	}
	f := Request{
		MsgId:  1,
		UserId: 2,
		Req: &any.Any{
			TypeUrl: "kg/" + proto.MessageName(v1),
			Value:   bytesv1,
		},
	}

	sf, err := proto.Marshal(&f)
	if err != nil {
		t.Error("failed")
		return
	}

	var f2 Request
	if err := proto.Unmarshal(sf, &f2); err != nil {
		t.Error("failed2")
		return
	}
	t.Logf("t2.userId<%v>, t2.msgId<%v>, t2.type<%v>", f2.UserId, f2.MsgId, f2.Req.TypeUrl)
	var l2 C_Login
	if err := ptypes.UnmarshalAny(f2.Req, &l2); err != nil {
		t.Error("failed3", err)
		return
	}

	t.Logf("value<%v>", l2.Token)
}
