diff --git a/common/utils.go b/common/utils.go index 070a714..3c44d6e 100644 --- a/common/utils.go +++ b/common/utils.go @@ -3,13 +3,12 @@ package common import ( "crypto/md5" "encoding/hex" - "google.golang.org/protobuf/reflect/protoreflect" "math/rand" "strings" "time" ) -func Md5V(str string) string { +func Md5V(str string) string { h := md5.New() h.Write([]byte(str)) return hex.EncodeToString(h.Sum(nil)) @@ -20,6 +19,7 @@ func Timex() int64 { } var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + func RandomString(n int, allowedChars ...[]rune) string { var letters []rune @@ -37,7 +37,6 @@ func RandomString(n int, allowedChars ...[]rune) string { return string(b) } - func RandomName(name [][]string) string { idx1 := rand.Intn(len(name[0])) idx2 := rand.Intn(len(name[1])) @@ -56,19 +55,3 @@ func FirstCharToUpper(key string) string { str.WriteString(key[1:]) return str.String() } - - -func ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32 { - ids := make([]int32,0) - for k, v := range properties { - name := protoreflect.Name(strings.ToLower(k)) - field := message.Descriptor().Fields().ByName(name) - if field == nil { - continue - } - - ids = append(ids, int32(field.Index())) - message.Set(field, protoreflect.ValueOf(v)) - } - return ids -} \ No newline at end of file diff --git a/models/role.go b/models/role.go index e927201..e1aeb78 100644 --- a/models/role.go +++ b/models/role.go @@ -115,7 +115,7 @@ func (m *RoleModel) LoadHero() { func (m *RoleModel) LoadTeams() { teams := make([]*pb.Team, 4) - err := mongoproxy.FindMany("hero", "role_id", m.Role.Id, &teams) + err := mongoproxy.FindMany("team", "role_id", m.Role.Id, &teams) if err != nil { logger.Error(err) return @@ -131,7 +131,7 @@ func (m *RoleModel) LoadAll() { } func (m *RoleModel) UpdateProperty(conn components.IConnection, key string, val interface{}, notify bool) { - m.UpdateProperties(conn, map[string]interface{}{key:val}, notify) + m.UpdateProperties(conn, map[string]interface{}{key: val}, notify) } func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[string]interface{}, notify bool) { @@ -139,7 +139,7 @@ func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[s return } - role := &pb.Role{} + role := &pb.Role{} ids := m.ParseFields(role.ProtoReflect(), property) if len(ids) == 0 { logger.Error("ParseFields err, len is 0") @@ -147,14 +147,16 @@ func (m *RoleModel) UpdateProperties(conn components.IConnection, property map[s } update := &pb.UpdateRolePropertyRsp{ - Id: ids, + Id: ids, Role: role, } if rsp, err := proto.Marshal(update); err != nil { logger.Error("id %s, err:", m.Role.Id, err) return - }else { - conn.Send(0, uint32(pb.ProtoCode_UpdateRolePropertyRsp), rsp) + } else { + if conn != nil { + conn.Send(0, uint32(pb.ProtoCode_UpdateRolePropertyRsp), rsp) + } } } diff --git a/models/role_test.go b/models/role_test.go index a71285f..e567eee 100644 --- a/models/role_test.go +++ b/models/role_test.go @@ -31,7 +31,7 @@ func TestNewRole(t *testing.T) { //}) role.SetProperty("Device", "1111") //role.Save() - }else { + } else { //uid不存在,创建角色 role = NewRole("1") role.Role.Uid = uid @@ -45,7 +45,7 @@ func TestNewRole(t *testing.T) { func TestRoleIndex(t *testing.T) { coll, keys := mongoproxy.FindIndex(pb.Role{}) - for _, index := range keys { + for _, index := range keys { logger.Debug("coll: %s, key: %s", coll, index) } } @@ -74,16 +74,11 @@ func TestRoleModel_ProtoReflect(t *testing.T) { return } - role := &pb.Role{} - sch := NewSchema("", role) - mp := map[string]interface{}{ - "Id": "1", - "Device": "12312312312", - } - ids := sch.ParseFields(role.ProtoReflect(), mp) - if len(ids) == 0 { - return - } - - fmt.Println(role) -} \ No newline at end of file + sch := NewRole("") + //mp := map[string]interface{}{ + // "Id": "1", + // "Device": "12312312312", + //} + sch.UpdateProperty(nil, "Device", "123123123", false) + fmt.Println(sch.Role) +} diff --git a/models/schema.go b/models/schema.go index e4103d6..d82c038 100644 --- a/models/schema.go +++ b/models/schema.go @@ -2,7 +2,6 @@ package models import ( "google.golang.org/protobuf/reflect/protoreflect" - "pro2d/common" "pro2d/common/components" "pro2d/common/db/mongoproxy" "pro2d/common/logger" @@ -23,6 +22,7 @@ type SchemaMap map[string]components.ISchema type Schema struct { db components.IDB reflectValue *reflect.Value + reflectIndex map[string]int cacheFields map[string]interface{} @@ -39,6 +39,11 @@ func NewSchema(key string, schema interface{}) *Schema { reflectValue: &s, cacheFields: make(map[string]interface{}), schema: schema, + reflectIndex: make(map[string]int), + } + + for i := 0; i < s.Type().NumField(); i++ { + sch.reflectIndex[strings.ToLower(s.Type().Field(i).Name)] = i } sch.db = mongoproxy.NewMongoColl(sch.GetSchemaName(), sch) @@ -111,22 +116,30 @@ func (s *Schema) Update() { } func (s *Schema) SetProperty(key string, val interface{}) { - s.reflectValue.FieldByName(common.FirstCharToUpper(key)).Set(reflect.ValueOf(val)) + idx, ok := s.reflectIndex[strings.ToLower(key)] + if !ok { + return + } + s.reflectValue.Field(idx).Set(reflect.ValueOf(val)) s.cacheFields[strings.ToLower(key)] = val } func (s *Schema) SetProperties(properties map[string]interface{}) { for key, val := range properties { - s.reflectValue.FieldByName(common.FirstCharToUpper(key)).Set(reflect.ValueOf(val)) + idx, ok := s.reflectIndex[strings.ToLower(key)] + if !ok { + continue + } + + s.reflectValue.Field(idx).Set(reflect.ValueOf(val)) s.cacheFields[strings.ToLower(key)] = val } } -func (s *Schema) ParseFields(message protoreflect.Message ,properties map[string]interface{}) []int32 { +func (s *Schema) ParseFields(message protoreflect.Message, properties map[string]interface{}) []int32 { ids := make([]int32, 0, len(properties)) for k, v := range properties { - name := protoreflect.Name(strings.ToLower(k)) - field := message.Descriptor().Fields().ByName(name) + field := message.Descriptor().Fields().ByName(protoreflect.Name(strings.ToLower(k))) if field == nil { continue } -- libgit2 0.21.2