diff --git a/metamgr.go b/metamgr.go index 2f83e50..0ace9fe 100644 --- a/metamgr.go +++ b/metamgr.go @@ -1,5 +1,16 @@ package f5 +import ( + "os" + "bufio" + "strings" + "reflect" + "fmt" + + "github.com/golang/protobuf/proto" + "github.com/golang/protobuf/jsonpb" +) + type MetaClass struct { FileName string Idx int @@ -10,7 +21,7 @@ type MetaClass struct { type MetaMgr struct { metaClasses *[]MetaClass - rawData []interface{} + rawList []interface{} wrapList []interface{} wrapIdHash []map[int32]interface{} wrapNameHash []map[string]interface{} @@ -29,11 +40,46 @@ func (this *MetaMgr) RegisterMetaClasses(metaClasses *[]MetaClass) { } func (this *MetaMgr) Load() { - for k, _ := range this.rawData { - _ = this.wrapList[k] + 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 += "}" + + 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) + } + + 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) } } +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) + } + fmt.Println(wrapMetaList) +} + func (this *MetaMgr) GetMetaById(idx int, id int32) interface{} { if idx >=0 && idx < len(this.wrapIdHash) { idHash := this.wrapIdHash[idx]