2024-01-29 18:13:53 +03:00
|
|
|
package structr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
"unsafe"
|
|
|
|
|
2024-04-02 13:03:40 +03:00
|
|
|
"codeberg.org/gruf/go-mangler"
|
2024-01-29 18:13:53 +03:00
|
|
|
)
|
|
|
|
|
2024-04-02 13:03:40 +03: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-06-21 18:43:17 +03:00
|
|
|
rtype reflect.Type
|
2024-01-29 18:13:53 +03:00
|
|
|
|
2024-04-02 13:03:40 +03:00
|
|
|
// struct field type mangling
|
|
|
|
// (i.e. fast serializing) fn.
|
|
|
|
mangle mangler.Mangler
|
|
|
|
|
2024-05-13 11:05:46 +03:00
|
|
|
// zero value data, used when
|
|
|
|
// nil encountered during ptr
|
|
|
|
// offset following.
|
|
|
|
zero unsafe.Pointer
|
|
|
|
|
2024-04-02 13:03:40 +03:00
|
|
|
// mangled zero value string,
|
|
|
|
// if set this indicates zero
|
|
|
|
// values of field not allowed
|
2024-05-13 11:05:46 +03:00
|
|
|
zerostr string
|
2024-08-14 15:08:24 +03:00
|
|
|
|
|
|
|
// offsets defines whereabouts in
|
|
|
|
// memory this field is located.
|
|
|
|
offsets []next_offset
|
2024-01-29 18:13:53 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 12:46:08 +03: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 18:13:53 +03: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 13:03:40 +03:00
|
|
|
func find_field(t reflect.Type, names []string) (sfield struct_field) {
|
2024-01-29 18:13:53 +03: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 12:46:08 +03:00
|
|
|
var off next_offset
|
|
|
|
|
|
|
|
// Dereference any ptrs to struct.
|
|
|
|
for t.Kind() == reflect.Pointer {
|
|
|
|
t = t.Elem()
|
|
|
|
off.derefs++
|
|
|
|
}
|
|
|
|
|
2024-01-29 18:13:53 +03:00
|
|
|
// Check for valid struct type.
|
|
|
|
if t.Kind() != reflect.Struct {
|
2024-04-11 12:46:08 +03:00
|
|
|
panicf("field %s is not struct (or ptr-to): %s", t, name)
|
2024-01-29 18:13:53 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 12:46:08 +03:00
|
|
|
var ok bool
|
|
|
|
|
2024-01-29 18:13:53 +03:00
|
|
|
// Look for next field by name.
|
|
|
|
field, ok = t.FieldByName(name)
|
|
|
|
if !ok {
|
|
|
|
panicf("unknown field: %s", name)
|
|
|
|
}
|
|
|
|
|
2024-04-11 12:46:08 +03:00
|
|
|
// Set next offset value.
|
|
|
|
off.offset = field.Offset
|
|
|
|
sfield.offsets = append(sfield.offsets, off)
|
2024-01-29 18:13:53 +03:00
|
|
|
|
|
|
|
// Set the next type.
|
|
|
|
t = field.Type
|
|
|
|
}
|
|
|
|
|
2024-06-21 18:43:17 +03:00
|
|
|
// Set final type.
|
|
|
|
sfield.rtype = t
|
2024-01-29 18:13:53 +03:00
|
|
|
|
2024-04-02 13:03:40 +03:00
|
|
|
// Find mangler for field type.
|
|
|
|
sfield.mangle = mangler.Get(t)
|
2024-01-29 18:13:53 +03:00
|
|
|
|
2024-06-21 18:43:17 +03:00
|
|
|
// Get new zero value data ptr.
|
|
|
|
v := reflect.New(t).Elem()
|
|
|
|
zptr := eface_data(v.Interface())
|
|
|
|
zstr := sfield.mangle(nil, zptr)
|
|
|
|
sfield.zerostr = string(zstr)
|
|
|
|
sfield.zero = zptr
|
2024-04-11 12:46:08 +03:00
|
|
|
|
2024-01-29 18:13:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract_fields extracts given structfields from the provided value type,
|
|
|
|
// this is done using predetermined struct field memory offset locations.
|
2024-06-21 18:43:17 +03:00
|
|
|
func extract_fields(ptr unsafe.Pointer, fields []struct_field) []unsafe.Pointer {
|
|
|
|
// Prepare slice of field value pointers.
|
|
|
|
ptrs := make([]unsafe.Pointer, len(fields))
|
2024-04-11 12:46:08 +03: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 {
|
2024-05-13 11:05:46 +03:00
|
|
|
break
|
2024-04-11 12:46:08 +03:00
|
|
|
}
|
2024-01-29 18:13:53 +03:00
|
|
|
|
2024-04-11 12:46:08 +03:00
|
|
|
// Jump forward by offset to next ptr.
|
|
|
|
fptr = unsafe.Pointer(uintptr(fptr) +
|
|
|
|
offset.offset)
|
|
|
|
}
|
|
|
|
|
2024-06-21 18:43:17 +03:00
|
|
|
if like_ptr(field.rtype) && fptr != nil {
|
|
|
|
// Further dereference value ptr.
|
|
|
|
fptr = *(*unsafe.Pointer)(fptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fptr == nil {
|
|
|
|
// Use zero value.
|
|
|
|
fptr = field.zero
|
|
|
|
}
|
|
|
|
|
2024-09-16 19:46:09 +03:00
|
|
|
// Set field ptr.
|
2024-06-21 18:43:17 +03:00
|
|
|
ptrs[i] = fptr
|
2024-01-29 18:13:53 +03:00
|
|
|
}
|
2024-09-16 19:46:09 +03:00
|
|
|
|
2024-06-21 18:43:17 +03:00
|
|
|
return ptrs
|
|
|
|
}
|
2024-01-29 18:13:53 +03:00
|
|
|
|
2024-06-21 18:43:17 +03:00
|
|
|
// like_ptr returns whether type's kind is ptr-like.
|
|
|
|
func like_ptr(t reflect.Type) bool {
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Pointer,
|
|
|
|
reflect.Map,
|
|
|
|
reflect.Chan,
|
|
|
|
reflect.Func:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2024-01-29 18:13:53 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 12:46:08 +03: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 18:13:53 +03:00
|
|
|
// panicf provides a panic with string formatting.
|
|
|
|
func panicf(format string, args ...any) {
|
|
|
|
panic(fmt.Sprintf(format, args...))
|
|
|
|
}
|