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