This commit is contained in:
aozhiwei 2020-09-02 18:45:35 +08:00
parent 9f7943ca94
commit a10ae56c4f
2 changed files with 68 additions and 63 deletions

66
metamgr.go Normal file
View File

@ -0,0 +1,66 @@
package f5
type MetaClass struct {
FileName string
Idx int
RawMeta interface{}
WrapMeta interface{}
PrimKey string
}
type MetaMgr struct {
metaClasses *[]MetaClass
rawData []interface{}
wrapList []interface{}
wrapIdHash []map[int32]interface{}
wrapNameHash []map[string]interface{}
}
func (this *MetaMgr) Init() {
}
func (this *MetaMgr) UnInit() {
}
func (this *MetaMgr) RegisterMetaClasses(metaClasses *[]MetaClass) {
this.metaClasses = metaClasses
}
func (this *MetaMgr) Load() {
for k, _ := range this.rawData {
_ = this.wrapList[k]
}
}
func (this *MetaMgr) GetMetaById(idx int, id int32) interface{} {
if idx >=0 && idx < len(this.wrapIdHash) {
idHash := this.wrapIdHash[idx]
if v, ok := idHash[id]; ok {
return v
} else {
return nil
}
}
return nil
}
func (this *MetaMgr) GetMetaByName(idx int, name string) interface{} {
if idx >=0 && idx < len(this.wrapNameHash) {
nameHash := this.wrapNameHash[idx]
if v, ok := nameHash[name]; ok {
return v
} else {
return nil
}
}
return nil
}
func (this *MetaMgr) GetMetaList(idx int) interface{} {
if idx >=0 && idx < len(this.wrapList) {
return this.wrapList[idx]
}
return nil
}

View File

@ -1,7 +1,6 @@
package f5
import (
"q5"
"os"
"bufio"
"reflect"
@ -9,34 +8,9 @@ import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/jsonpb"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
func ReadCsvMetaFile(fileName string, list interface{}) {
msgType := reflect.TypeOf(list).Elem().Elem().Elem()
csvReader := &q5.CsvReader{}
csvReader.Load(fileName)
for csvReader.NextLine() {
msg := reflect.New(msgType)
msgPb := msg.Interface().(proto.Message)
msgReflect := proto.MessageReflect(msgPb)
msgDesc := msgReflect.Descriptor()
fields := msgDesc.Fields()
for i := 0; i < fields.Len(); i++ {
field := fields.Get(i)
value := csvReader.GetValue(string(field.Name()))
xvalueToPBSampleField(value, field, msgReflect)
}
newList := reflect.Append(reflect.ValueOf(list), msg)
reflect.ValueOf(list).Elem().Set(newList)
}
}
func ReadJsonMetaFile(fileName string, list interface{}) {
msgType := reflect.TypeOf(list).Elem().Elem().Elem()
msg := reflect.New(msgType)
@ -50,10 +24,11 @@ func ReadJsonMetaFile(fileName string, list interface{}) {
data, _ := bufio.NewReader(f).ReadString(0)
data = "{\"values\":" + data
data += "}"
err := jsonpb.Unmarshal(f, proto.MessageV1(metaList))
err := jsonpb.UnmarshalString(data, proto.MessageV1(metaList))
if err == nil {
fmt.Println("json decode ok", data)
}
fmt.Println(err)
/* msgReflect = proto.MessageReflect(proto.MessageV1(metaList))
msgDesc = msgReflect.Descriptor()
@ -64,39 +39,3 @@ func ReadJsonMetaFile(fileName string, list interface{}) {
fmt.Println(valuesField, values)*/
}
func xvalueToPBSampleField(
value *q5.XValue,
field protoreflect.FieldDescriptor,
msgReflect protoreflect.Message) {
switch field.Kind() {
case protoreflect.BoolKind:
msgReflect.Set(field, protoreflect.ValueOfBool(value.GetInt64() != 0))
break
case protoreflect.EnumKind:
break
case
protoreflect.Int32Kind, protoreflect.Sint32Kind,
protoreflect.Sfixed32Kind, protoreflect.Fixed32Kind:
break
case protoreflect.Uint32Kind:
break
case
protoreflect.Int64Kind, protoreflect.Sint64Kind,
protoreflect.Sfixed64Kind, protoreflect.Fixed64Kind:
break
case protoreflect.Uint64Kind:
break
case protoreflect.FloatKind:
break
case protoreflect.DoubleKind:
break
case protoreflect.StringKind:
break
case protoreflect.BytesKind:
break
case protoreflect.MessageKind:
break
case protoreflect.GroupKind:
break
}
}