summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/codeberg.org/gruf/go-kv/v2/format/methods.go')
-rw-r--r--vendor/codeberg.org/gruf/go-kv/v2/format/methods.go115
1 files changed, 87 insertions, 28 deletions
diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go b/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go
index fcbffd3a4..8febd4a1d 100644
--- a/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go
+++ b/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go
@@ -3,31 +3,45 @@ package format
import (
"reflect"
"unsafe"
+
+ "codeberg.org/gruf/go-xunsafe"
)
type Stringer interface{ String() string }
+type Formattable interface{ Format(*State) }
+
var (
// stringer type for implement checks.
stringerType = typeof[Stringer]()
+ // formattableType type for implement checks.
+ formattableType = typeof[Formattable]()
+
// error type for implement checks.
errorType = typeof[error]()
)
// getMethodType returns a *possible* FormatFunc to handle case
// of a type that implements any known interface{} types, else nil.
-func getMethodType(t typenode) FormatFunc {
+func getMethodType(t xunsafe.TypeIter) FormatFunc {
switch {
- case t.rtype.Implements(stringerType):
- switch t.rtype.Kind() {
+ case t.Type.Implements(stringerType):
+ switch t.Type.Kind() {
case reflect.Interface:
return getInterfaceStringerType(t)
default:
return getConcreteStringerType(t)
}
- case t.rtype.Implements(errorType):
- switch t.rtype.Kind() {
+ case t.Type.Implements(formattableType):
+ switch t.Type.Kind() {
+ case reflect.Interface:
+ return getInterfaceFormattableType(t)
+ default:
+ return getConcreteFormattableType(t)
+ }
+ case t.Type.Implements(errorType):
+ switch t.Type.Kind() {
case reflect.Interface:
return getInterfaceErrorType(t)
default:
@@ -40,12 +54,12 @@ func getMethodType(t typenode) FormatFunc {
// getInterfaceStringerType returns a FormatFunc to handle case of an interface{}
// type that implements Stringer{}, i.e. Stringer{} itself and any superset of.
-func getInterfaceStringerType(t typenode) FormatFunc {
- switch t.indirect() && !t.iface_indir() {
+func getInterfaceStringerType(t xunsafe.TypeIter) FormatFunc {
+ switch t.Indirect() && !t.IfaceIndir() {
case true:
return with_typestr_ptrs(t, func(s *State) {
s.P = *(*unsafe.Pointer)(s.P)
- if s.P == nil || (*abi_NonEmptyInterface)(s.P).Data == nil {
+ if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
appendNil(s)
return
}
@@ -54,7 +68,7 @@ func getInterfaceStringerType(t typenode) FormatFunc {
})
case false:
return with_typestr_ptrs(t, func(s *State) {
- if s.P == nil || (*abi_NonEmptyInterface)(s.P).Data == nil {
+ if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
appendNil(s)
return
}
@@ -68,9 +82,9 @@ func getInterfaceStringerType(t typenode) FormatFunc {
// getConcreteStringerType returns a FormatFunc to handle case of concrete
// (i.e. non-interface{}) type that has a Stringer{} method receiver.
-func getConcreteStringerType(t typenode) FormatFunc {
- itab := get_iface_ITab[Stringer](t.rtype)
- switch t.indirect() && !t.iface_indir() {
+func getConcreteStringerType(t xunsafe.TypeIter) FormatFunc {
+ itab := xunsafe.GetIfaceITab[Stringer](t.Type)
+ switch t.Indirect() && !t.IfaceIndir() {
case true:
return with_typestr_ptrs(t, func(s *State) {
s.P = *(*unsafe.Pointer)(s.P)
@@ -78,17 +92,66 @@ func getConcreteStringerType(t typenode) FormatFunc {
appendNil(s)
return
}
- v := *(*Stringer)(pack_iface(itab, s.P))
+ v := *(*Stringer)(xunsafe.PackIface(itab, s.P))
appendString(s, v.String())
})
case false:
return with_typestr_ptrs(t, func(s *State) {
+ v := *(*Stringer)(xunsafe.PackIface(itab, s.P))
+ appendString(s, v.String())
+ })
+ default:
+ panic("unreachable")
+ }
+}
+
+// getInterfaceFormattableType returns a FormatFunc to handle case of an interface{}
+// type that implements Formattable{}, i.e. Formattable{} itself and any superset of.
+func getInterfaceFormattableType(t xunsafe.TypeIter) FormatFunc {
+ switch t.Indirect() && !t.IfaceIndir() {
+ case true:
+ return with_typestr_ptrs(t, func(s *State) {
+ s.P = *(*unsafe.Pointer)(s.P)
+ if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
+ appendNil(s)
+ return
+ }
+ v := *(*Formattable)(s.P)
+ v.Format(s)
+ })
+ case false:
+ return with_typestr_ptrs(t, func(s *State) {
+ if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
+ appendNil(s)
+ return
+ }
+ v := *(*Formattable)(s.P)
+ v.Format(s)
+ })
+ default:
+ panic("unreachable")
+ }
+}
+
+// getConcreteFormattableType returns a FormatFunc to handle case of concrete
+// (i.e. non-interface{}) type that has a Formattable{} method receiver.
+func getConcreteFormattableType(t xunsafe.TypeIter) FormatFunc {
+ itab := xunsafe.GetIfaceITab[Formattable](t.Type)
+ switch t.Indirect() && !t.IfaceIndir() {
+ case true:
+ return with_typestr_ptrs(t, func(s *State) {
+ s.P = *(*unsafe.Pointer)(s.P)
if s.P == nil {
appendNil(s)
return
}
- v := *(*Stringer)(pack_iface(itab, s.P))
- appendString(s, v.String())
+ v := *(*Formattable)(xunsafe.PackIface(itab, s.P))
+ v.Format(s)
+ })
+ case false:
+ return with_typestr_ptrs(t, func(s *State) {
+ v := *(*Formattable)(xunsafe.PackIface(itab, s.P))
+ v.Format(s)
})
default:
panic("unreachable")
@@ -97,12 +160,12 @@ func getConcreteStringerType(t typenode) FormatFunc {
// getInterfaceErrorType returns a FormatFunc to handle case of an interface{}
// type that implements error{}, i.e. error{} itself and any superset of.
-func getInterfaceErrorType(t typenode) FormatFunc {
- switch t.indirect() && !t.iface_indir() {
+func getInterfaceErrorType(t xunsafe.TypeIter) FormatFunc {
+ switch t.Indirect() && !t.IfaceIndir() {
case true:
return with_typestr_ptrs(t, func(s *State) {
s.P = *(*unsafe.Pointer)(s.P)
- if s.P == nil || (*abi_NonEmptyInterface)(s.P).Data == nil {
+ if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
appendNil(s)
return
}
@@ -111,7 +174,7 @@ func getInterfaceErrorType(t typenode) FormatFunc {
})
case false:
return with_typestr_ptrs(t, func(s *State) {
- if s.P == nil || (*abi_NonEmptyInterface)(s.P).Data == nil {
+ if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
appendNil(s)
return
}
@@ -125,9 +188,9 @@ func getInterfaceErrorType(t typenode) FormatFunc {
// getConcreteErrorType returns a FormatFunc to handle case of concrete
// (i.e. non-interface{}) type that has an error{} method receiver.
-func getConcreteErrorType(t typenode) FormatFunc {
- itab := get_iface_ITab[error](t.rtype)
- switch t.indirect() && !t.iface_indir() {
+func getConcreteErrorType(t xunsafe.TypeIter) FormatFunc {
+ itab := xunsafe.GetIfaceITab[error](t.Type)
+ switch t.Indirect() && !t.IfaceIndir() {
case true:
return with_typestr_ptrs(t, func(s *State) {
s.P = *(*unsafe.Pointer)(s.P)
@@ -135,16 +198,12 @@ func getConcreteErrorType(t typenode) FormatFunc {
appendNil(s)
return
}
- v := *(*error)(pack_iface(itab, s.P))
+ v := *(*error)(xunsafe.PackIface(itab, s.P))
appendString(s, v.Error())
})
case false:
return with_typestr_ptrs(t, func(s *State) {
- if s.P == nil {
- appendNil(s)
- return
- }
- v := *(*error)(pack_iface(itab, s.P))
+ v := *(*error)(xunsafe.PackIface(itab, s.P))
appendString(s, v.Error())
})
default: