Exemplo n.º 1
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")
	}
}
Exemplo n.º 2
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())
}
Exemplo n.º 3
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")
}
Exemplo n.º 4
0
func convertFile(file *descriptor.FileDescriptorProto) ([]*plugin.CodeGeneratorResponse_File, error) {
	name := path.Base(file.GetName())
	pkg, ok := globalPkg.relativelyLookupPackage(file.GetPackage())
	if !ok {
		return nil, fmt.Errorf("no such package found: %s", file.GetPackage())
	}

	response := []*plugin.CodeGeneratorResponse_File{}
	for _, msg := range file.GetMessageType() {
		options := msg.GetOptions()
		if options == nil {
			continue
		}
		if !proto.HasExtension(options, E_TableName) {
			continue
		}
		optionValue, err := proto.GetExtension(options, E_TableName)
		if err != nil {
			return nil, err
		}
		tableName := *optionValue.(*string)
		if len(tableName) == 0 {
			return nil, fmt.Errorf("table name of %s cannot be empty", msg.GetName())
		}

		glog.V(2).Info("Generating schema for a message type ", msg.GetName())
		schema, err := convertMessageType(pkg, msg)
		if err != nil {
			glog.Errorf("Failed to convert %s: %v", name, err)
			return nil, err
		}

		jsonSchema, err := json.Marshal(schema)
		if err != nil {
			glog.Error("Failed to encode schema", err)
			return nil, err
		}

		resFile := &plugin.CodeGeneratorResponse_File{
			Name:    proto.String(fmt.Sprintf("%s/%s.schema", strings.Replace(file.GetPackage(), ".", "/", -1), tableName)),
			Content: proto.String(string(jsonSchema)),
		}
		response = append(response, resFile)
	}

	return response, nil
}