Ejemplo n.º 1
0
func newTestMessage() *pb.MyMessage {
	msg := &pb.MyMessage{
		Count: proto.Int32(42),
		Name:  proto.String("Dave"),
		Quote: proto.String(`"I didn't want to go."`),
		Pet:   []string{"bunny", "kitty", "horsey"},
		Inner: &pb.InnerMessage{
			Host:      proto.String("footrest.syd"),
			Port:      proto.Int32(7001),
			Connected: proto.Bool(true),
		},
		Others: []*pb.OtherMessage{
			{
				Key:   proto.Int64(0xdeadbeef),
				Value: []byte{1, 65, 7, 12},
			},
			{
				Weight: proto.Float32(6.022),
				Inner: &pb.InnerMessage{
					Host: proto.String("lesha.mtv"),
					Port: proto.Int32(8002),
				},
			},
		},
		Bikeshed: pb.MyMessage_BLUE.Enum(),
		Somegroup: &pb.MyMessage_SomeGroup{
			GroupField: proto.Int32(8),
		},
		// One normally wouldn't do this.
		// This is an undeclared tag 13, as a varint (wire type 0) with value 4.
		XXX_unrecognized: []byte{13<<3 | 0, 4},
	}
	ext := &pb.Ext{
		Data: proto.String("Big gobs for big rats"),
	}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
		panic(err)
	}
	greetings := []string{"adg", "easy", "cow"}
	if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil {
		panic(err)
	}

	// Add an unknown extension. We marshal a pb.Ext, and fake the ID.
	b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
	if err != nil {
		panic(err)
	}
	b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...)
	proto.SetRawExtension(msg, 201, b)

	// Extensions can be plain fields, too, so let's test that.
	b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19)
	proto.SetRawExtension(msg, 202, b)

	return msg
}
Ejemplo n.º 2
0
func TestGetExtensionStability(t *testing.T) {
	check := func(m *pb.MyMessage) bool {
		ext1, err := proto.GetExtension(m, pb.E_Ext_More)
		if err != nil {
			t.Fatalf("GetExtension() failed: %s", err)
		}
		ext2, err := proto.GetExtension(m, pb.E_Ext_More)
		if err != nil {
			t.Fatalf("GetExtension() failed: %s", err)
		}
		return ext1 == ext2
	}
	msg := &pb.MyMessage{Count: proto.Int32(4)}
	ext0 := &pb.Ext{}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext0); err != nil {
		t.Fatalf("Could not set ext1: %s", ext0)
	}
	if !check(msg) {
		t.Errorf("GetExtension() not stable before marshaling")
	}
	bb, err := proto.Marshal(msg)
	if err != nil {
		t.Fatalf("Marshal() failed: %s", err)
	}
	msg1 := &pb.MyMessage{}
	err = proto.Unmarshal(bb, msg1)
	if err != nil {
		t.Fatalf("Unmarshal() failed: %s", err)
	}
	if !check(msg1) {
		t.Errorf("GetExtension() not stable after unmarshaling")
	}
}
Ejemplo n.º 3
0
func init() {
	ext := &pb.Ext{
		Data: proto.String("extension"),
	}
	if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_More, ext); err != nil {
		panic("SetExtension: " + err.Error())
	}
}
Ejemplo n.º 4
0
func init() {
	ext := &pb.Ext{
		Data: proto.String("extension"),
	}
	if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_More, ext); err != nil {
		log.Fatalf("SetExtension: %v", err)
	}
}
Ejemplo n.º 5
0
func TestExtension(t *testing.T) {
	log.Println("test Extension...")

	request := &Request{}
	proto.SetDefaults(request)
	log.Println("request type:", request.GetType())
	log.Println("request command:", request.GetCommand())
	// has
	log.Println("has extension:",
		proto.HasExtension(request, E_Login_Account))
	// set
	account := &Account{}
	proto.SetDefaults(account)
	account.Name = proto.String("lee")
	account.Token = proto.String("lee")
	log.Println(account.Type, account.GetName(), account.GetToken())

	err := proto.SetExtension(request, E_Login_Account, account)
	if err != nil {
		t.Fatal(err.Error())
	}
	// has
	log.Println("has extension:",
		proto.HasExtension(request, E_Login_Account))
	// get
	var new_account interface{}
	new_account, err = proto.GetExtension(request, E_Login_Account)
	if err != nil {
		t.Fatal(err.Error())
	}
	{
		account := new_account.(*Account)
		log.Println("getname", account.GetName())
	}
	data_type := reflect.TypeOf(new_account)

	log.Println("type:", data_type)
	switch new_account.(type) {
	case *Account:
	default:
		t.Fatal("Bad Type")
	}
	value := reflect.ValueOf(new_account)

	log.Println("value:", value)
	GetName := value.MethodByName("GetName")
	log.Println("is reflect.Func", reflect.Func == GetName.Kind())
	name := GetName.Call(nil)

	name_str := name[0].String()
	log.Println("GetName:", reflect.TypeOf(name_str).Kind())
}
Ejemplo n.º 6
0
// register
func (this *ProtobufClient) register() {

	// has extension
	if proto.HasExtension(this.request, message.E_Register_User) {
		data, err := proto.GetExtension(this.request,
			message.E_Register_User)
		if err != nil {
			this.response.Error = proto.String(err.Error())
			return
		}
		user, ok := data.(*message.User)
		if ok != true {
			*this.response.Status = message.Response_kError
			this.response.Error =
				proto.String("Bad Register.User type")
			return
		}
		account := user.GetAccount()
		name := account.GetName()
		profile := user.GetProfile()
		log.Println("Register", name, account, profile)
		if false {
			user_byte, err := proto.Marshal(user)
			if err != nil {
				this.response.Error = proto.String(err.Error())
				return
			}
			//err = this.session.db.Set([]byte(name), user_byte)
			if err != nil {
				this.response.Error = proto.String(err.Error())
				return
			}
			log.Println(user_byte)
		}

		//this.session.user = user;
		//
		captcha := &message.Captcha{}
		captcha.Code = proto.String("captcha")
		captcha.Image = []byte("captcha")
		*this.response.Status = message.Response_kOk
		err = proto.SetExtension(&this.response,
			message.E_Register_Captcha,
			captcha)
		return
	}
	// not have extension
	this.response.Error = proto.String("Request need extentsion 9")
}
Ejemplo n.º 7
0
func TestGetExtensionsWithMissingExtensions(t *testing.T) {
	msg := &pb.MyMessage{}
	ext1 := &pb.Ext{}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext1); err != nil {
		t.Fatalf("Could not set ext1: %s", ext1)
	}
	exts, err := proto.GetExtensions(msg, []*proto.ExtensionDesc{
		pb.E_Ext_More,
		pb.E_Ext_Text,
	})
	if err != nil {
		t.Fatalf("GetExtensions() failed: %s", err)
	}
	if exts[0] != ext1 {
		t.Errorf("ext1 not in returned extensions: %T %v", exts[0], exts[0])
	}
	if exts[1] != nil {
		t.Errorf("ext2 in returned extensions: %T %v", exts[1], exts[1])
	}
}