can parse TagCompound and TagString and TagFloat

This commit is contained in:
JunDao
2019-05-18 18:08:40 +08:00
parent 9cdab82ea3
commit b2164833f5
3 changed files with 291 additions and 50 deletions

View File

@ -5,18 +5,35 @@ import (
)
type typeInfo struct {
tagName string
nameToIndex map[string]int
}
func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
func getTypeInfo(typ reflect.Type) *typeInfo {
tinfo := new(typeInfo)
tinfo.nameToIndex = make(map[string]int)
if typ.Kind() == reflect.Struct {
n := typ.NumField()
for i := 0; i < n; i++ {
f := typ.Field(i)
if (f.PkgPath != "" && !f.Anonymous) || f.Tag.Get("nbt") == "-" {
tag := f.Tag.Get("nbt")
if (f.PkgPath != "" && !f.Anonymous) || tag == "-" {
continue // Private field
}
tinfo.nameToIndex[tag] = i
if _, ok := tinfo.nameToIndex[f.Name]; !ok {
tinfo.nameToIndex[f.Name] = i
}
}
}
return tinfo, nil
return tinfo
}
func (t *typeInfo) findIndexByName(name string) int {
i, ok := t.nameToIndex[name]
if !ok {
return -1
}
return i
}