247 lines
6.2 KiB
Go
247 lines
6.2 KiB
Go
package nbt
|
|
|
|
import (
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
type structFields struct {
|
|
list []field
|
|
nameIndex map[string]int // index of the previous slice.
|
|
}
|
|
|
|
type field struct {
|
|
name string
|
|
|
|
tag bool
|
|
index []int
|
|
typ reflect.Type
|
|
omitEmpty bool
|
|
asDefault bool
|
|
asList bool
|
|
}
|
|
|
|
// byIndex sorts field by index sequence.
|
|
type byIndex []field
|
|
|
|
func (x byIndex) Len() int { return len(x) }
|
|
|
|
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
|
|
func (x byIndex) Less(i, j int) bool {
|
|
for k, xik := range x[i].index {
|
|
if k >= len(x[j].index) {
|
|
return false
|
|
}
|
|
if xik != x[j].index[k] {
|
|
return xik < x[j].index[k]
|
|
}
|
|
}
|
|
return len(x[i].index) < len(x[j].index)
|
|
}
|
|
|
|
func typeFields(t reflect.Type) (tInfo structFields) {
|
|
// Anonymous fields to explore at the current level and the next.
|
|
current := []field{}
|
|
next := []field{{typ: t}}
|
|
|
|
// Count of queued names for current level and the next.
|
|
var count, nextCount map[reflect.Type]int
|
|
|
|
// Types already visited at an earlier level.
|
|
visited := make(map[reflect.Type]struct{})
|
|
|
|
// Fields found.
|
|
var fields []field
|
|
|
|
for len(next) > 0 {
|
|
current, next = next, current[:0]
|
|
count, nextCount = nextCount, make(map[reflect.Type]int)
|
|
|
|
for _, f := range current {
|
|
if _, ok := visited[f.typ]; ok {
|
|
continue
|
|
}
|
|
visited[f.typ] = struct{}{}
|
|
|
|
// Scan f.typ for fields to include.
|
|
for i := 0; i < f.typ.NumField(); i++ {
|
|
sf := f.typ.Field(i)
|
|
if sf.Anonymous {
|
|
t := sf.Type
|
|
if t.Kind() == reflect.Pointer {
|
|
t = t.Elem()
|
|
}
|
|
if !sf.IsExported() && t.Kind() != reflect.Struct {
|
|
// Ignore embedded fields of unexported non-struct types.
|
|
continue
|
|
}
|
|
// Do not ignore embedded fields of unexported struct types
|
|
// since they may have exported fields.
|
|
} else if !sf.IsExported() {
|
|
// Ignore unexported non-embedded fields.
|
|
continue
|
|
}
|
|
|
|
tag := sf.Tag.Get("nbt")
|
|
if tag == "-" {
|
|
continue
|
|
}
|
|
|
|
// parse tags
|
|
name, opts, _ := strings.Cut(tag, ",")
|
|
index := make([]int, len(f.index)+1)
|
|
copy(index, f.index)
|
|
index[len(f.index)] = i
|
|
if keytag := sf.Tag.Get("nbtkey"); keytag != "" {
|
|
name = keytag
|
|
}
|
|
|
|
ft := sf.Type
|
|
if ft.Name() == "" && ft.Kind() == reflect.Pointer {
|
|
// Follow pointer.
|
|
ft = ft.Elem()
|
|
}
|
|
|
|
// parse options
|
|
var omitEmpty, asList, asDefault bool
|
|
for opts != "" {
|
|
var name string
|
|
name, opts, _ = strings.Cut(opts, ",")
|
|
switch name {
|
|
case "omitempty":
|
|
omitEmpty = true
|
|
case "list":
|
|
asList = true
|
|
case "default":
|
|
asDefault = true
|
|
}
|
|
}
|
|
// Deprecated: use `nbt:",list"` instead.
|
|
if sf.Tag.Get("nbt_type") == "list" {
|
|
asList = true
|
|
}
|
|
|
|
// Record found field and index sequence.
|
|
if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
|
|
tagged := name != ""
|
|
if name == "" {
|
|
name = sf.Name
|
|
}
|
|
field := field{
|
|
name: name,
|
|
tag: tagged,
|
|
index: index,
|
|
typ: ft,
|
|
omitEmpty: omitEmpty,
|
|
asDefault: asDefault,
|
|
asList: asList,
|
|
}
|
|
|
|
fields = append(fields, field)
|
|
if count[f.typ] > 1 {
|
|
// If there were multiple instances, add a second,
|
|
// so that the annihilation code will see a duplicate.
|
|
// It only cares about the distinction between 1 or 2,
|
|
// so don't bother generating any more copies.
|
|
fields = append(fields, fields[len(fields)-1])
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Record new anonymous struct to explore in next round.
|
|
nextCount[ft]++
|
|
if nextCount[ft] == 1 {
|
|
next = append(next, field{name: ft.Name(), index: index, typ: ft})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
sort.Slice(fields, func(i, j int) bool {
|
|
x := fields
|
|
// sort field by name, breaking ties with depth, then
|
|
// breaking ties with "name came from json tag", then
|
|
// breaking ties with index sequence.
|
|
if x[i].name != x[j].name {
|
|
return x[i].name < x[j].name
|
|
}
|
|
if len(x[i].index) != len(x[j].index) {
|
|
return len(x[i].index) < len(x[j].index)
|
|
}
|
|
if x[i].tag != x[j].tag {
|
|
return x[i].tag
|
|
}
|
|
return byIndex(x).Less(i, j)
|
|
})
|
|
|
|
// Delete all fields that are hidden by the Go rules for embedded fields,
|
|
// except that fields with JSON tags are promoted.
|
|
|
|
// The fields are sorted in primary order of name, secondary order
|
|
// of field index length. Loop over names; for each name, delete
|
|
// hidden fields by choosing the one dominant field that survives.
|
|
out := fields[:0]
|
|
for advance, i := 0, 0; i < len(fields); i += advance {
|
|
// One iteration per name.
|
|
// Find the sequence of fields with the name of this first field.
|
|
fi := fields[i]
|
|
name := fi.name
|
|
for advance = 1; i+advance < len(fields); advance++ {
|
|
fj := fields[i+advance]
|
|
if fj.name != name {
|
|
break
|
|
}
|
|
}
|
|
if advance == 1 { // Only one field with this name
|
|
out = append(out, fi)
|
|
continue
|
|
}
|
|
dominant, ok := dominantField(fields[i : i+advance])
|
|
if ok {
|
|
out = append(out, dominant)
|
|
}
|
|
}
|
|
|
|
fields = out
|
|
sort.Sort(byIndex(fields))
|
|
|
|
nameIndex := make(map[string]int, len(fields))
|
|
for i, field := range fields {
|
|
nameIndex[field.name] = i
|
|
}
|
|
return structFields{
|
|
list: fields,
|
|
nameIndex: nameIndex,
|
|
}
|
|
}
|
|
|
|
// dominantField looks through the fields, all of which are known to
|
|
// have the same name, to find the single field that dominates the
|
|
// others using Go's embedding rules, modified by the presence of
|
|
// NBT tags. If there are multiple top-level fields, the boolean
|
|
// will be false: This condition is an error in Go and we skip all
|
|
// the fields.
|
|
func dominantField(fields []field) (field, bool) {
|
|
// The fields are sorted in increasing index-length order, then by presence of tag.
|
|
// That means that the first field is the dominant one. We need only check
|
|
// for error cases: two fields at top level, either both tagged or neither tagged.
|
|
if len(fields) > 1 && len(fields[0].index) == len(fields[1].index) && fields[0].tag == fields[1].tag {
|
|
return field{}, false
|
|
}
|
|
return fields[0], true
|
|
}
|
|
|
|
var fieldCache sync.Map
|
|
|
|
func cachedTypeFields(t reflect.Type) structFields {
|
|
if ti, ok := fieldCache.Load(t); ok {
|
|
return ti.(structFields)
|
|
}
|
|
tInfo := typeFields(t)
|
|
ti, _ := fieldCache.LoadOrStore(t, tInfo)
|
|
return ti.(structFields)
|
|
}
|