summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-structr/runtime.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-04-02 11:03:40 +0100
committerLibravatar GitHub <noreply@github.com>2024-04-02 12:03:40 +0200
commitadf345f1ec0cb76a0df94a4505143d891659cba9 (patch)
treee0cca289c0a50f30191d4b65a2c336704570e470 /vendor/codeberg.org/gruf/go-structr/runtime.go
parent[feature] Option to hide followers/following (#2788) (diff)
downloadgotosocial-adf345f1ec0cb76a0df94a4505143d891659cba9.tar.xz
[chore] bump go structr cache version -> v0.6.0 (#2773)
* update go-structr library -> v0.6.0, add necessary wrapping types + code changes to support these changes * update readme with go-structr package changes * improved wrapping of the SliceCache type * add code comments for the cache wrapper types * remove test.out :innocent: --------- Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-structr/runtime.go')
-rw-r--r--vendor/codeberg.org/gruf/go-structr/runtime.go53
1 files changed, 22 insertions, 31 deletions
diff --git a/vendor/codeberg.org/gruf/go-structr/runtime.go b/vendor/codeberg.org/gruf/go-structr/runtime.go
index cc1bcd86d..7db1d7e7a 100644
--- a/vendor/codeberg.org/gruf/go-structr/runtime.go
+++ b/vendor/codeberg.org/gruf/go-structr/runtime.go
@@ -7,31 +7,38 @@ import (
"unicode/utf8"
"unsafe"
+ "codeberg.org/gruf/go-mangler"
"github.com/modern-go/reflect2"
- "github.com/zeebo/xxh3"
)
-type structfield struct {
- // _type is the runtime type pointer
+// struct_field contains pre-prepared type
+// information about a struct's field member,
+// including memory offset and hash function.
+type struct_field struct {
+
+ // type2 is the runtime type pointer
// underlying the struct field type.
// used for repacking our own erfaces.
- _type reflect2.Type
+ type2 reflect2.Type
// offset is the offset in memory
// of this struct field from the
// outer-most value ptr location.
offset uintptr
- // hasher is the relevant function
- // for hashing value of structfield
- // into the supplied hashbuf, where
- // return value indicates if zero.
- hasher func(*xxh3.Hasher, any) bool
+ // 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
}
// 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.
-func find_field(t reflect.Type, names []string) (sfield structfield) {
+func find_field(t reflect.Type, names []string) (sfield struct_field) {
var (
// is_exported returns whether name is exported
// from a package; can be func or struct field.
@@ -56,17 +63,6 @@ func find_field(t reflect.Type, names []string) (sfield structfield) {
field reflect.StructField
)
- switch {
- // The only 2 types we support are
- // structs, and ptrs to a struct.
- case t.Kind() == reflect.Struct:
- case t.Kind() == reflect.Pointer &&
- t.Elem().Kind() == reflect.Struct:
- t = t.Elem()
- default:
- panic("index only support struct{} and *struct{}")
- }
-
for len(names) > 0 {
var ok bool
@@ -92,17 +88,17 @@ func find_field(t reflect.Type, names []string) (sfield structfield) {
}
// Get field type as reflect2.
- sfield._type = reflect2.Type2(t)
+ sfield.type2 = reflect2.Type2(t)
- // Find hasher for type.
- sfield.hasher = hasher(t)
+ // Find mangler for field type.
+ sfield.mangle = mangler.Get(t)
return
}
// extract_fields extracts given structfields from the provided value type,
// this is done using predetermined struct field memory offset locations.
-func extract_fields[T any](value T, fields []structfield) []any {
+func extract_fields[T any](value T, fields []struct_field) []any {
// Get ptr to raw value data.
ptr := unsafe.Pointer(&value)
@@ -117,17 +113,12 @@ func extract_fields[T any](value T, fields []structfield) []any {
for i := 0; i < len(fields); i++ {
// Manually access field at memory offset and pack eface.
ptr := unsafe.Pointer(uintptr(ptr) + fields[i].offset)
- ifaces[i] = fields[i]._type.UnsafeIndirect(ptr)
+ ifaces[i] = fields[i].type2.UnsafeIndirect(ptr)
}
return ifaces
}
-// data_ptr returns the runtime data ptr associated with value.
-func data_ptr(a any) unsafe.Pointer {
- return (*struct{ t, v unsafe.Pointer })(unsafe.Pointer(&a)).v
-}
-
// panicf provides a panic with string formatting.
func panicf(format string, args ...any) {
panic(fmt.Sprintf(format, args...))