diff options
Diffstat (limited to 'vendor/codeberg.org/gruf/go-kv/v2/format')
| -rw-r--r-- | vendor/codeberg.org/gruf/go-kv/v2/format/README.md | 32 | ||||
| -rw-r--r-- | vendor/codeberg.org/gruf/go-kv/v2/format/methods.go | 54 |
2 files changed, 74 insertions, 12 deletions
diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/README.md b/vendor/codeberg.org/gruf/go-kv/v2/format/README.md new file mode 100644 index 000000000..5d11cc6b9 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-kv/v2/format/README.md @@ -0,0 +1,32 @@ +# format + +a low-level string formatting library that takes arbitrary input types as interfaces, and arguments as a struct. this does not contain any printf-like argument parsing, only log-friendly serialization of arbitrary input arguments. (noting that our output is noticably more log-friendly for struct / map types than stdlib "fmt"). + +benchmarks: +```shell +goos: linux +goarch: amd64 +pkg: codeberg.org/gruf/go-kv/v2/format +cpu: AMD Ryzen 7 7840U w/ Radeon 780M Graphics + +# go-kv/v2/format (i.e. latest) +BenchmarkFormatV2Append +BenchmarkFormatV2Append-16 590422 1977 ns/op 488 B/op 23 allocs/op +BenchmarkFormatV2AppendVerbose +BenchmarkFormatV2AppendVerbose-16 375628 2981 ns/op 1704 B/op 45 allocs/op + +# go-kv/format (i.e. v1) +BenchmarkFormatAppend +BenchmarkFormatAppend-16 208357 5883 ns/op 2624 B/op 169 allocs/op +BenchmarkFormatAppendVerbose +BenchmarkFormatAppendVerbose-16 35916 33563 ns/op 3734 B/op 208 allocs/op + +# fmt (i.e. stdlib) +BenchmarkFmtAppend +BenchmarkFmtAppend-16 147722 8418 ns/op 4747 B/op 191 allocs/op +BenchmarkFmtAppendVerbose +BenchmarkFmtAppendVerbose-16 167112 7238 ns/op 4401 B/op 178 allocs/op + +PASS +ok codeberg.org/gruf/go-kv/v2/format +``` 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 8febd4a1d..4c0c1dbda 100644 --- a/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go +++ b/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go @@ -84,8 +84,8 @@ func getInterfaceStringerType(t xunsafe.TypeIter) FormatFunc { // (i.e. non-interface{}) type that has a Stringer{} method receiver. func getConcreteStringerType(t xunsafe.TypeIter) FormatFunc { itab := xunsafe.GetIfaceITab[Stringer](t.Type) - switch t.Indirect() && !t.IfaceIndir() { - case true: + switch { + case t.Indirect() && !t.IfaceIndir(): return with_typestr_ptrs(t, func(s *State) { s.P = *(*unsafe.Pointer)(s.P) if s.P == nil { @@ -95,13 +95,23 @@ func getConcreteStringerType(t xunsafe.TypeIter) FormatFunc { v := *(*Stringer)(xunsafe.PackIface(itab, s.P)) appendString(s, v.String()) }) - case false: + case t.Type.Kind() == reflect.Pointer && t.Type.Implements(stringerType): + // if the interface implementation is received by + // value type, the pointer type will also support + // it but it requires an extra dereference check. return with_typestr_ptrs(t, func(s *State) { + if s.P == nil { + appendNil(s) + return + } v := *(*Stringer)(xunsafe.PackIface(itab, s.P)) appendString(s, v.String()) }) default: - panic("unreachable") + return with_typestr_ptrs(t, func(s *State) { + v := *(*Stringer)(xunsafe.PackIface(itab, s.P)) + appendString(s, v.String()) + }) } } @@ -137,8 +147,8 @@ func getInterfaceFormattableType(t xunsafe.TypeIter) FormatFunc { // (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: + switch { + case t.Indirect() && !t.IfaceIndir(): return with_typestr_ptrs(t, func(s *State) { s.P = *(*unsafe.Pointer)(s.P) if s.P == nil { @@ -148,13 +158,23 @@ func getConcreteFormattableType(t xunsafe.TypeIter) FormatFunc { v := *(*Formattable)(xunsafe.PackIface(itab, s.P)) v.Format(s) }) - case false: + case t.Type.Kind() == reflect.Pointer && t.Type.Implements(formattableType): + // if the interface implementation is received by + // value type, the pointer type will also support + // it but it requires an extra dereference check. return with_typestr_ptrs(t, func(s *State) { + if s.P == nil { + appendNil(s) + return + } v := *(*Formattable)(xunsafe.PackIface(itab, s.P)) v.Format(s) }) default: - panic("unreachable") + return with_typestr_ptrs(t, func(s *State) { + v := *(*Formattable)(xunsafe.PackIface(itab, s.P)) + v.Format(s) + }) } } @@ -190,8 +210,8 @@ func getInterfaceErrorType(t xunsafe.TypeIter) FormatFunc { // (i.e. non-interface{}) type that has an error{} method receiver. func getConcreteErrorType(t xunsafe.TypeIter) FormatFunc { itab := xunsafe.GetIfaceITab[error](t.Type) - switch t.Indirect() && !t.IfaceIndir() { - case true: + switch { + case t.Indirect() && !t.IfaceIndir(): return with_typestr_ptrs(t, func(s *State) { s.P = *(*unsafe.Pointer)(s.P) if s.P == nil { @@ -201,12 +221,22 @@ func getConcreteErrorType(t xunsafe.TypeIter) FormatFunc { v := *(*error)(xunsafe.PackIface(itab, s.P)) appendString(s, v.Error()) }) - case false: + case t.Type.Kind() == reflect.Pointer && t.Type.Implements(errorType): + // if the interface implementation is received by + // value type, the pointer type will also support + // it but it requires an extra dereference check. return with_typestr_ptrs(t, func(s *State) { + if s.P == nil { + appendNil(s) + return + } v := *(*error)(xunsafe.PackIface(itab, s.P)) appendString(s, v.Error()) }) default: - panic("unreachable") + return with_typestr_ptrs(t, func(s *State) { + v := *(*error)(xunsafe.PackIface(itab, s.P)) + appendString(s, v.Error()) + }) } } |
