This commit is contained in:
aozhiwei 2020-09-03 17:17:38 +08:00
parent 16f6ac3f01
commit 45cd22c75b

View File

@ -17,13 +17,14 @@ type MetaClass struct {
RawMeta interface{} RawMeta interface{}
WrapMeta interface{} WrapMeta interface{}
PrimKey string PrimKey string
SecKey string
} }
type MetaMgr struct { type MetaMgr struct {
metaClasses *[]MetaClass metaClasses *[]MetaClass
rawList []interface{} rawList []interface{}
wrapList []interface{} wrapList []interface{}
wrapIdHash []map[int32]interface{} wrapIdHash []map[int64]interface{}
wrapNameHash []map[string]interface{} wrapNameHash []map[string]interface{}
} }
@ -42,49 +43,88 @@ func (this *MetaMgr) RegisterMetaClasses(metaClasses *[]MetaClass) {
func (this *MetaMgr) Load() { func (this *MetaMgr) Load() {
this.rawList = make([]interface{}, len(*this.metaClasses)) this.rawList = make([]interface{}, len(*this.metaClasses))
this.wrapList = make([]interface{}, len(*this.metaClasses)) this.wrapList = make([]interface{}, len(*this.metaClasses))
for key, val := range *this.metaClasses { this.wrapIdHash = make([]map[int64]interface{}, len(*this.metaClasses))
f, _ := os.Open(val.FileName) this.wrapNameHash = make([]map[string]interface{}, len(*this.metaClasses))
data, _ := bufio.NewReader(f).ReadString(0) this.loadRawMetaTable()
data = "{\"values\":" + data this.bindPrimKey()
data += "}" }
u := jsonpb.Unmarshaler{AllowUnknownFields: true} func (this *MetaMgr) loadRawMetaTable() {
msgType := reflect.TypeOf(val.RawMeta).Elem() for key, val := range *this.metaClasses {
msg := reflect.New(msgType) rawMeta, _ := this.loadJson(&val)
msgPb := msg.Interface().(proto.Message) values := rawMeta.Elem().FieldByName("Values")
err := u.Unmarshal(strings.NewReader(data), msgPb) wrapMetaType := reflect.TypeOf(val.WrapMeta)
if err != nil { wrapMetaList := reflect.MakeSlice(reflect.SliceOf(wrapMetaType), 0, values.Len())
fmt.Println(err) for i := 0; i < values.Len(); i++ {
val := values.Index(i)
wrapMeta := reflect.New(reflect.TypeOf(wrapMetaList.Interface()).Elem().Elem())
clsName := reflect.TypeOf(val.Elem().Interface()).Name()
clsField := wrapMeta.Elem().FieldByName(clsName)
clsField.Set(val)
wrapMetaList = reflect.Append(wrapMetaList, wrapMeta)
} }
wrapMsgType := reflect.TypeOf(val.WrapMeta) this.rawList[key] = rawMeta
wrapMsgList := reflect.MakeSlice(reflect.SliceOf(wrapMsgType), 0, 100) this.wrapList[key] = wrapMetaList.Interface()
this.wrapIdHash[key] = map[int64]interface{}{}
this.rawList[key] = msg this.wrapNameHash[key] = map[string]interface{}{}
this.wrapList[key] = wrapMsgList
this.wrapMeta(msg, wrapMsgList)
} }
} }
func (this *MetaMgr) wrapMeta(rawMeta reflect.Value, wrapMetaList reflect.Value) { func (this *MetaMgr) bindPrimKey() {
values := rawMeta.Elem().FieldByName("Values") for key, val := range *this.metaClasses {
for i := 0; i < values.Len(); i++ { wrapMetaList := reflect.ValueOf(this.wrapList[key])
val := values.Index(i) for i := 0; i < wrapMetaList.Len(); i++ {
clsName := reflect.TypeOf(val.Elem().Interface()).Name() wrapMeta := wrapMetaList.Index(i)
wrapMeta := reflect.New(reflect.TypeOf(wrapMetaList.Interface()).Elem().Elem()) if val.PrimKey == "" {
serverInfo := wrapMeta.Elem().FieldByName(clsName) this.wrapIdHash[key][int64(i + 1)] = wrapMeta.Interface()
serverInfo.Set(val) continue
wrapMetaList = reflect.Append(wrapMetaList, wrapMeta) }
primVal := wrapMeta.Elem().FieldByName(val.PrimKey)
secVal := wrapMeta.Elem().FieldByName(val.SecKey)
if fieldVal, ok := primVal.Interface().(*string); ok {
this.wrapNameHash[key][*fieldVal] = wrapMeta
} else if fieldVal, ok := primVal.Interface().(*int32); ok {
if val.SecKey == "" {
this.wrapIdHash[key][int64(*fieldVal)] = wrapMeta.Interface()
} else {
if subFieldVal, ok := secVal.Interface().(*int32); ok {
this.wrapIdHash[key][int64(*subFieldVal)] = wrapMeta.Interface()
} else {
}
}
}
}
} }
fmt.Println(wrapMetaList) fmt.Println("ok")
}
func (this *MetaMgr) loadJson(metaClass *MetaClass) (reflect.Value, error) {
f, _ := os.Open(metaClass.FileName)
data, _ := bufio.NewReader(f).ReadString(0)
data = "{\"values\":" + data
data += "}"
u := jsonpb.Unmarshaler{AllowUnknownFields: true}
msgType := reflect.TypeOf(metaClass.RawMeta).Elem()
msg := reflect.New(msgType)
msgPb := msg.Interface().(proto.Message)
err := u.Unmarshal(strings.NewReader(data), msgPb)
return msg, err
} }
func (this *MetaMgr) GetMetaById(idx int, id int32) interface{} { func (this *MetaMgr) GetMetaById(idx int, id int32) interface{} {
return this.GetMetaById64(idx, int64(id))
}
func (this *MetaMgr) GetMetaById64(idx int, id int64) interface{} {
if idx >=0 && idx < len(this.wrapIdHash) { if idx >=0 && idx < len(this.wrapIdHash) {
idHash := this.wrapIdHash[idx] idHash := this.wrapIdHash[idx]
if v, ok := idHash[id]; ok { if v, ok := idHash[id]; ok {
return v return reflect.ValueOf(v).Interface()
} else { } else {
return nil return nil
} }