diff --git a/app.go b/app.go index 5f6b20a..0b21b56 100644 --- a/app.go +++ b/app.go @@ -28,11 +28,11 @@ func (this *App_) Init() { func (context interface{}) int64 { return q5.GetTickCount() }, - func (context interface{}, fixedTimierExecuteTimes int32, milliSeconds int32, tick int64) int64 { + func (context interface{}, isFirstAdd bool, milliSeconds int32, tick int64) int64 { nowTime := time.Now().Unix() todayPassedSeconds := nowTime - q5.GetDaySeconds(nowTime) expires := (tick - todayPassedSeconds * 1000) + int64(milliSeconds) - if fixedTimierExecuteTimes > 0 { + if isFirstAdd { if expires <= tick { expires += 1000 * 3600 * 24 } diff --git a/metamgr.go b/metamgr.go index 54babfe..e9ee713 100644 --- a/metamgr.go +++ b/metamgr.go @@ -6,6 +6,7 @@ import ( "strings" "reflect" "fmt" + "q5" "github.com/golang/protobuf/proto" "github.com/golang/protobuf/jsonpb" @@ -18,7 +19,6 @@ type MetaClass struct { WrapMeta interface{} PrimKey string SecKey string - IsObject bool } type MetaMgr struct { @@ -75,51 +75,86 @@ func (this *MetaMgr) loadRawMetaTable() { } func (this *MetaMgr) bindPrimKey() { - for _, val := range *this.metaClasses { - wrapMetaList := reflect.ValueOf(this.wrapList[val.Idx]) + bindFunc := func (metaClass *MetaClass, wrapMeta reflect.Value) { + primVal := wrapMeta.Elem().FieldByName(metaClass.PrimKey) + secVal := wrapMeta.Elem().FieldByName(metaClass.SecKey) + nameHash := this.wrapNameHash[metaClass.Idx] + idHash := this.wrapIdHash[metaClass.Idx] + if fieldVal, ok := primVal.Interface().(*string); ok { + if _, ok := nameHash[*fieldVal]; ok { + panic(fmt.Sprintf( + "bindPrimKey duplicated key error %s %s", + metaClass.FileName, + *fieldVal)) + } + nameHash[*fieldVal] = wrapMeta + } else if fieldVal, ok := primVal.Interface().(*int32); ok { + if metaClass.SecKey == "" { + if _, ok := idHash[int64(*fieldVal)]; ok { + panic(fmt.Sprintf( + "bindPrimKey duplicated key error %s %s", + metaClass.FileName, + *fieldVal)) + } + idHash[int64(*fieldVal)] = wrapMeta.Interface() + } else { + if subFieldVal, ok := secVal.Interface().(*int32); ok { + if _, ok := idHash[q5.MkInt64(*fieldVal, *subFieldVal)]; ok { + panic(fmt.Sprintf( + "bindPrimKey duplicated key error %s %s", + metaClass.FileName, + q5.MkInt64(*fieldVal, *subFieldVal))) + } + idHash[q5.MkInt64(*fieldVal, *subFieldVal)] = wrapMeta.Interface() + } else { + panic(fmt.Sprintf("bindPrimKey subField error %s", metaClass.FileName)) + } + } + } else { + panic(fmt.Sprintf("bindPrimKey primKey error %s", metaClass.FileName)) + } + } + for _, metaClass := range *this.metaClasses { + wrapMetaList := reflect.ValueOf(this.wrapList[metaClass.Idx]) for i := 0; i < wrapMetaList.Len(); i++ { wrapMeta := wrapMetaList.Index(i) - if val.PrimKey == "" { - this.wrapIdHash[val.Idx][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[val.Idx][*fieldVal] = wrapMeta - } else if fieldVal, ok := primVal.Interface().(*int32); ok { - if val.SecKey == "" { - this.wrapIdHash[val.Idx][int64(*fieldVal)] = wrapMeta.Interface() - } else { - if subFieldVal, ok := secVal.Interface().(*int32); ok { - this.wrapIdHash[val.Idx][int64(*subFieldVal)] = wrapMeta.Interface() - } else { - - } - } + if metaClass.PrimKey == "" { + this.wrapIdHash[metaClass.Idx][int64(i + 1)] = wrapMeta.Interface() + } else { + bindFunc(&metaClass, wrapMeta) } } } - fmt.Println("ok") } func (this *MetaMgr) loadJson(metaClass *MetaClass) (reflect.Value, error) { - f, _ := os.Open(metaClass.FileName) - data, _ := bufio.NewReader(f).ReadString(0) - if metaClass.IsObject { - data = "{\"values\":[" + data - data += "]}" - } else { - data = "{\"values\":" + data - data += "}" - } + if f, err := os.Open(metaClass.FileName); err == nil { + data, _ := bufio.NewReader(f).ReadString(0) + switch q5.JsonStrType(data) { + case q5.JSON_ARRAY: + data = "{\"values\":" + data + "}" + case q5.JSON_OBJECT: + data = "{\"values\":[" + data + "]}" + default: + panic(fmt.Sprintf("error json format %s", metaClass.FileName)) + } - 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 + msgType := reflect.TypeOf(metaClass.RawMeta).Elem() + msg := reflect.New(msgType) + msgPb := msg.Interface().(proto.Message) + u := jsonpb.Unmarshaler{AllowUnknownFields: true} + err := u.Unmarshal(strings.NewReader(data), msgPb) + if err != nil { + panic(fmt.Sprintf( + "parse json error %s %s %s", + err, + metaClass.FileName, + msgType.Name())) + } + return msg, err + } else { + panic(fmt.Sprintf("open metafile error %s %s", metaClass.FileName, err)) + } } func (this *MetaMgr) GetMetaById(idx int, id int32) interface{} {