1
This commit is contained in:
parent
7c8ec429de
commit
6448c00807
110
metamgr.go
110
metamgr.go
@ -6,10 +6,13 @@ import (
|
||||
"strings"
|
||||
"reflect"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"encoding/json"
|
||||
"q5"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/golang/protobuf/jsonpb"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
)
|
||||
|
||||
type MetaClass struct {
|
||||
@ -19,6 +22,7 @@ type MetaClass struct {
|
||||
WrapMeta interface{}
|
||||
PrimKey string
|
||||
SecKey string
|
||||
eleMeta reflect.Type
|
||||
}
|
||||
|
||||
type MetaMgr struct {
|
||||
@ -38,6 +42,17 @@ func (this *MetaMgr) UnInit() {
|
||||
}
|
||||
|
||||
func (this *MetaMgr) RegisterMetaClasses(metaClasses *[]MetaClass) {
|
||||
for i := 0; i < len(*metaClasses); i++ {
|
||||
metaClass := &(*metaClasses)[i]
|
||||
msgType := reflect.TypeOf(metaClass.RawMeta).Elem()
|
||||
msg := reflect.New(msgType)
|
||||
metaClass.eleMeta = reflect.TypeOf(msg.Elem().FieldByName("Values").Interface()).Elem().Elem()
|
||||
if metaClass.eleMeta == nil {
|
||||
panic(fmt.Sprintf(
|
||||
"registerMetaClasses error not found Values field %s",
|
||||
metaClass.FileName))
|
||||
}
|
||||
}
|
||||
this.metaClasses = metaClasses
|
||||
}
|
||||
|
||||
@ -51,10 +66,10 @@ func (this *MetaMgr) Load() {
|
||||
}
|
||||
|
||||
func (this *MetaMgr) loadRawMetaTable() {
|
||||
for _, val := range *this.metaClasses {
|
||||
rawMeta, _ := this.loadJson(&val)
|
||||
for _, metaClass := range *this.metaClasses {
|
||||
rawMeta, _ := this.loadJson(&metaClass)
|
||||
values := rawMeta.Elem().FieldByName("Values")
|
||||
wrapMetaType := reflect.TypeOf(val.WrapMeta)
|
||||
wrapMetaType := reflect.TypeOf(metaClass.WrapMeta)
|
||||
wrapMetaList := reflect.MakeSlice(reflect.SliceOf(wrapMetaType), 0, values.Len())
|
||||
for i := 0; i < values.Len(); i++ {
|
||||
val := values.Index(i)
|
||||
@ -67,10 +82,10 @@ func (this *MetaMgr) loadRawMetaTable() {
|
||||
wrapMetaList = reflect.Append(wrapMetaList, wrapMeta)
|
||||
}
|
||||
|
||||
this.rawList[val.Idx] = rawMeta
|
||||
this.wrapList[val.Idx] = wrapMetaList.Interface()
|
||||
this.wrapIdHash[val.Idx] = map[int64]interface{}{}
|
||||
this.wrapNameHash[val.Idx] = map[string]interface{}{}
|
||||
this.rawList[metaClass.Idx] = rawMeta
|
||||
this.wrapList[metaClass.Idx] = wrapMetaList.Interface()
|
||||
this.wrapIdHash[metaClass.Idx] = map[int64]interface{}{}
|
||||
this.wrapNameHash[metaClass.Idx] = map[string]interface{}{}
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,12 +147,14 @@ func (this *MetaMgr) loadJson(metaClass *MetaClass) (reflect.Value, error) {
|
||||
data, _ := bufio.NewReader(f).ReadString(0)
|
||||
switch q5.JsonStrType(data) {
|
||||
case q5.JSON_ARRAY:
|
||||
data = "{\"values\":" + data + "}"
|
||||
break
|
||||
case q5.JSON_OBJECT:
|
||||
data = "{\"values\":[" + data + "]}"
|
||||
data = "[" + data + "]"
|
||||
default:
|
||||
panic(fmt.Sprintf("error json format %s", metaClass.FileName))
|
||||
}
|
||||
data = this.adjustJsonForamt(metaClass, data)
|
||||
data = "{\"values\":" + data + "}"
|
||||
|
||||
msgType := reflect.TypeOf(metaClass.RawMeta).Elem()
|
||||
msg := reflect.New(msgType)
|
||||
@ -191,3 +208,78 @@ func (this *MetaMgr) GetMetaList(idx int) interface{} {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *MetaMgr) adjustJsonForamt(metaClass *MetaClass, rawData string) string {
|
||||
adjustFieldFunc := func(msg map[string]interface{}, key string, val interface{}, field protoreflect.FieldDescriptor) {
|
||||
switch field.Kind() {
|
||||
case
|
||||
protoreflect.Int32Kind,
|
||||
protoreflect.Sint32Kind,
|
||||
protoreflect.Int64Kind,
|
||||
protoreflect.Sint64Kind,
|
||||
protoreflect.Uint64Kind,
|
||||
protoreflect.Sfixed32Kind,
|
||||
protoreflect.Fixed32Kind,
|
||||
protoreflect.FloatKind,
|
||||
protoreflect.Sfixed64Kind,
|
||||
protoreflect.Fixed64Kind,
|
||||
protoreflect.DoubleKind:
|
||||
if q5.IsNumberType(val) {
|
||||
} else if reflect.TypeOf(val).Kind() == reflect.String {
|
||||
strVal := val.(string)
|
||||
if strVal == "" {
|
||||
msg[key] = 0
|
||||
} else {
|
||||
intVal, err := strconv.ParseInt(strVal, 10, 64)
|
||||
if err == nil {
|
||||
msg[key] = intVal
|
||||
} else {
|
||||
floatVal, err := strconv.ParseFloat(strVal, 64)
|
||||
if err == nil {
|
||||
msg[key] = floatVal
|
||||
} else {
|
||||
panic(fmt.Sprintf(
|
||||
"adjustJsonFormat error %s %s %s",
|
||||
metaClass.FileName,
|
||||
key,
|
||||
val))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case
|
||||
protoreflect.BoolKind,
|
||||
protoreflect.EnumKind,
|
||||
protoreflect.BytesKind,
|
||||
//protoreflect.MessageKind,
|
||||
protoreflect.GroupKind:
|
||||
panic(fmt.Sprintf(
|
||||
"adjustJsonFormat error %s %s %s",
|
||||
metaClass.FileName,
|
||||
key,
|
||||
val))
|
||||
case protoreflect.MessageKind:
|
||||
break
|
||||
}
|
||||
}
|
||||
var rawJson []map[string]interface{}
|
||||
err := json.Unmarshal([]byte(rawData), &rawJson)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("adjustJsonFormat error %s %s", metaClass.FileName, err))
|
||||
}
|
||||
msgType := proto.MessageReflect(reflect.New(metaClass.eleMeta).Interface().(proto.Message))
|
||||
for i := 0; i < len(rawJson); i++ {
|
||||
msg := rawJson[i]
|
||||
for key, val := range msg {
|
||||
field := msgType.Descriptor().Fields().ByName(protoreflect.Name(key))
|
||||
if field != nil {
|
||||
adjustFieldFunc(msg, key, val, field)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newData, err := json.Marshal(rawJson)
|
||||
fmt.Println("rawData ", string(rawData))
|
||||
fmt.Println("newData ", string(newData))
|
||||
return string(newData)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user