2024-01-29 16:13:53 +01:00
|
|
|
package structr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
"unsafe"
|
|
|
|
|
2024-04-02 12:03:40 +02:00
|
|
|
"codeberg.org/gruf/go-mangler"
|
2024-01-29 16:13:53 +01:00
|
|
|
"github.com/modern-go/reflect2"
|
|
|
|
)
|
|
|
|
|
2024-04-02 12:03:40 +02:00
|
|
|
// struct_field contains pre-prepared type
|
|
|
|
// information about a struct's field member,
|
|
|
|
// including memory offset and hash function.
|
|
|
|
type struct_field struct {
|
|
|
|
|
2024-04-11 11:46:08 +02:00
|
|
|
// type2 contains the reflect2
|
|
|
|
// type information for this field,
|
|
|
|
// used in repacking it as eface.
|
2024-04-02 12:03:40 +02:00
|
|
|
type2 reflect2.Type
|
2024-01-29 16:13:53 +01:00
|
|
|
|
2024-04-11 11:46:08 +02:00
|
|
|
// offsets defines whereabouts in
|
|
|
|
// memory this field is located.
|
|
|
|
offsets []next_offset
|
2024-01-29 16:13:53 +01:00
|
|
|
|
2024-04-02 12:03:40 +02:00
|
|
|
// struct field type mangling
|
|
|
|
// (i.e. fast serializing) fn.
|
|
|
|
mangle mangler.Mangler
|
|
|
|
|
|
|
|
// mangled zero value string,
|
|
|
|
// if set this indicates zero
|
|
|
|
// values of field not allowed
|
|
|
|
zero string
|
2024-01-29 16:13:53 +01:00
|
|
|
}
|
|
|
|
|
2024-04-11 11:46:08 +02:00
|
|
|
// next_offset defines a next offset location
|
|
|
|
// in a struct_field, first by the number of
|
|
|
|
// derefences required, then by offset from
|
|
|
|
// that final memory location.
|
|
|
|
type next_offset struct {
|
|
|
|
derefs uint
|
|
|
|
offset uintptr
|
|
|
|
}
|
|
|
|
|
2024-01-29 16:13:53 +01:00
|
|
|
// find_field will search for a struct field with given set of names,
|
|
|
|
// where names is a len > 0 slice of names account for struct nesting.
|
2024-04-02 12:03:40 +02:00
|
|
|
func find_field(t reflect.Type, names []string) (sfield struct_field) {
|
2024-01-29 16:13:53 +01:00
|
|
|
var (
|
|
|
|
// is_exported returns whether name is exported
|
|
|
|
// from a package; can be func or struct field.
|
|
|
|
is_exported = func(name string) bool {
|
|
|
|
r, _ := utf8.DecodeRuneInString(name)
|
|
|
|
return unicode.IsUpper(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// pop_name pops the next name from
|
|
|
|
// the provided slice of field names.
|
|
|
|
pop_name = func() string {
|
|
|
|
name := names[0]
|
|
|
|
names = names[1:]
|
|
|
|
if !is_exported(name) {
|
|
|
|
panicf("field is not exported: %s", name)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// field is the iteratively searched
|
|
|
|
// struct field value in below loop.
|
|
|
|
field reflect.StructField
|
|
|
|
)
|
|
|
|
|
|
|
|
for len(names) > 0 {
|
|
|
|
// Pop next name.
|
|
|
|
name := pop_name()
|
|
|
|
|
2024-04-11 11:46:08 +02:00
|
|
|
var off next_offset
|
|
|
|
|
|
|
|
// Dereference any ptrs to struct.
|
|
|
|
for t.Kind() == reflect.Pointer {
|
|
|
|
t = t.Elem()
|
|
|
|
off.derefs++
|
|
|
|
}
|
|
|
|
|
2024-01-29 16:13:53 +01:00
|
|
|
// Check for valid struct type.
|
|
|
|
if t.Kind() != reflect.Struct {
|
2024-04-11 11:46:08 +02:00
|
|
|
panicf("field %s is not struct (or ptr-to): %s", t, name)
|
2024-01-29 16:13:53 +01:00
|
|
|
}
|
|
|
|
|
2024-04-11 11:46:08 +02:00
|
|
|
var ok bool
|
|
|
|
|
2024-01-29 16:13:53 +01:00
|
|
|
// Look for next field by name.
|
|
|
|
field, ok = t.FieldByName(name)
|
|
|
|
if !ok {
|
|
|
|
panicf("unknown field: %s", name)
|
|
|
|
}
|
|
|
|
|
2024-04-11 11:46:08 +02:00
|
|
|
// Set next offset value.
|
|
|
|
off.offset = field.Offset
|
|
|
|
sfield.offsets = append(sfield.offsets, off)
|
2024-01-29 16:13:53 +01:00
|
|
|
|
|
|
|
// Set the next type.
|
|
|
|
t = field.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get field type as reflect2.
|
2024-04-02 12:03:40 +02:00
|
|
|
sfield.type2 = reflect2.Type2(t)
|
2024-04-11 11:46:08 +02:00
|
|
|
i := sfield.type2.New()
|
2024-01-29 16:13:53 +01:00
|
|
|
|
2024-04-02 12:03:40 +02:00
|
|
|
// Find mangler for field type.
|
|
|
|
sfield.mangle = mangler.Get(t)
|
2024-01-29 16:13:53 +01:00
|
|
|
|
2024-04-11 11:46:08 +02:00
|
|
|
// Set possible mangled zero value.
|
|
|
|
sfield.zero = string(sfield.mangle(nil, i))
|
|
|
|
|
2024-01-29 16:13:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract_fields extracts given structfields from the provided value type,
|
|
|
|
// this is done using predetermined struct field memory offset locations.
|
2024-04-11 11:46:08 +02:00
|
|
|
func extract_fields(ptr unsafe.Pointer, fields []struct_field) []any {
|
2024-01-29 16:13:53 +01:00
|
|
|
// Prepare slice of field ifaces.
|
|
|
|
ifaces := make([]any, len(fields))
|
2024-04-11 11:46:08 +02:00
|
|
|
for i, field := range fields {
|
|
|
|
|
|
|
|
// loop scope.
|
|
|
|
fptr := ptr
|
|
|
|
|
|
|
|
for _, offset := range field.offsets {
|
|
|
|
// Dereference any ptrs to offset.
|
|
|
|
fptr = deref(fptr, offset.derefs)
|
|
|
|
if fptr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-29 16:13:53 +01:00
|
|
|
|
2024-04-11 11:46:08 +02:00
|
|
|
// Jump forward by offset to next ptr.
|
|
|
|
fptr = unsafe.Pointer(uintptr(fptr) +
|
|
|
|
offset.offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repack value data ptr as empty interface.
|
|
|
|
ifaces[i] = field.type2.UnsafeIndirect(fptr)
|
2024-01-29 16:13:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ifaces
|
|
|
|
}
|
|
|
|
|
2024-04-11 11:46:08 +02:00
|
|
|
// deref will dereference ptr 'n' times (or until nil).
|
|
|
|
func deref(p unsafe.Pointer, n uint) unsafe.Pointer {
|
|
|
|
for ; n > 0; n-- {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p = *(*unsafe.Pointer)(p)
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2024-01-29 16:13:53 +01:00
|
|
|
// panicf provides a panic with string formatting.
|
|
|
|
func panicf(format string, args ...any) {
|
|
|
|
panic(fmt.Sprintf(format, args...))
|
|
|
|
}
|