GoToSocial/vendor/codeberg.org/gruf/go-kv/field.go

106 lines
2.7 KiB
Go
Raw Normal View History

[chore] use our own logging implementation (#716) * first commit Signed-off-by: kim <grufwub@gmail.com> * replace logging with our own log library Signed-off-by: kim <grufwub@gmail.com> * fix imports Signed-off-by: kim <grufwub@gmail.com> * fix log imports Signed-off-by: kim <grufwub@gmail.com> * add license text Signed-off-by: kim <grufwub@gmail.com> * fix package import cycle between config and log package Signed-off-by: kim <grufwub@gmail.com> * fix empty kv.Fields{} being passed to WithFields() Signed-off-by: kim <grufwub@gmail.com> * fix uses of log.WithFields() with whitespace issues and empty slices Signed-off-by: kim <grufwub@gmail.com> * *linter related grumbling* Signed-off-by: kim <grufwub@gmail.com> * gofmt the codebase! also fix more log.WithFields() formatting issues Signed-off-by: kim <grufwub@gmail.com> * update testrig code to match new changes Signed-off-by: kim <grufwub@gmail.com> * fix error wrapping in non fmt.Errorf function Signed-off-by: kim <grufwub@gmail.com> * add benchmarking of log.Caller() vs non-cached Signed-off-by: kim <grufwub@gmail.com> * fix syslog tests, add standard build tags to test runner to ensure consistency Signed-off-by: kim <grufwub@gmail.com> * make syslog tests more robust Signed-off-by: kim <grufwub@gmail.com> * fix caller depth arithmatic (is that how you spell it?) Signed-off-by: kim <grufwub@gmail.com> * update to use unkeyed fields in kv.Field{} instances Signed-off-by: kim <grufwub@gmail.com> * update go-kv library Signed-off-by: kim <grufwub@gmail.com> * update libraries list Signed-off-by: kim <grufwub@gmail.com> * fuck you linter get nerfed Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
2022-07-19 10:47:55 +02:00
package kv
import (
"codeberg.org/gruf/go-byteutil"
)
// bufsize is the default buffer size per field to alloc
// when calling .AppendFormat() from within .String().
const bufsize = 64
// Fields is a typedef for a []Field slice to provide
// slightly more performant string formatting for multiples.
type Fields []Field
// Get will return the field with given 'key'.
func (f Fields) Get(key string) (*Field, bool) {
for i := 0; i < len(f); i++ {
if f[i].K == key {
return &f[i], true
}
}
return nil, false
}
// Set will set an existing field with 'key' to 'value', or append new.
func (f *Fields) Set(key string, value interface{}) {
for i := 0; i < len(*f); i++ {
// Update existing value
if (*f)[i].K == key {
(*f)[i].V = value
return
}
}
// Append new field
*f = append(*f, Field{
K: key,
V: value,
})
}
// AppendFormat appends a string representation of receiving Field(s) to 'b'.
func (f Fields) AppendFormat(buf *byteutil.Buffer, vbose bool) {
for i := 0; i < len(f); i++ {
f[i].AppendFormat(buf, vbose)
buf.WriteByte(' ')
}
if len(f) > 0 {
buf.Truncate(1)
}
}
// String returns a string representation of receiving Field(s).
func (f Fields) String() string {
b := make([]byte, 0, bufsize*len(f))
buf := byteutil.Buffer{B: b}
f.AppendFormat(&buf, false)
return buf.String()
}
// GoString performs .String() but with type prefix.
func (f Fields) GoString() string {
b := make([]byte, 0, bufsize*len(f))
buf := byteutil.Buffer{B: b}
f.AppendFormat(&buf, true)
return "kv.Fields{" + buf.String() + "}"
}
// Field represents an individual key-value field.
type Field struct {
K string // Field key
V interface{} // Field value
}
// Key returns the formatted key string of this Field.
func (f Field) Key() string {
buf := byteutil.Buffer{B: make([]byte, 0, bufsize/2)}
AppendQuoteKey(&buf, f.K)
[chore] use our own logging implementation (#716) * first commit Signed-off-by: kim <grufwub@gmail.com> * replace logging with our own log library Signed-off-by: kim <grufwub@gmail.com> * fix imports Signed-off-by: kim <grufwub@gmail.com> * fix log imports Signed-off-by: kim <grufwub@gmail.com> * add license text Signed-off-by: kim <grufwub@gmail.com> * fix package import cycle between config and log package Signed-off-by: kim <grufwub@gmail.com> * fix empty kv.Fields{} being passed to WithFields() Signed-off-by: kim <grufwub@gmail.com> * fix uses of log.WithFields() with whitespace issues and empty slices Signed-off-by: kim <grufwub@gmail.com> * *linter related grumbling* Signed-off-by: kim <grufwub@gmail.com> * gofmt the codebase! also fix more log.WithFields() formatting issues Signed-off-by: kim <grufwub@gmail.com> * update testrig code to match new changes Signed-off-by: kim <grufwub@gmail.com> * fix error wrapping in non fmt.Errorf function Signed-off-by: kim <grufwub@gmail.com> * add benchmarking of log.Caller() vs non-cached Signed-off-by: kim <grufwub@gmail.com> * fix syslog tests, add standard build tags to test runner to ensure consistency Signed-off-by: kim <grufwub@gmail.com> * make syslog tests more robust Signed-off-by: kim <grufwub@gmail.com> * fix caller depth arithmatic (is that how you spell it?) Signed-off-by: kim <grufwub@gmail.com> * update to use unkeyed fields in kv.Field{} instances Signed-off-by: kim <grufwub@gmail.com> * update go-kv library Signed-off-by: kim <grufwub@gmail.com> * update libraries list Signed-off-by: kim <grufwub@gmail.com> * fuck you linter get nerfed Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
2022-07-19 10:47:55 +02:00
return buf.String()
}
// String will return a string representation of this Field
// of the form `key=value` where `value` is formatted using
// fmt package's `%+v` directive. If the .X = true (verbose),
// then it uses '%#v'. Both key and value are escaped and
// quoted if necessary to fit on single line.
//
// If the `kvformat` build tag is provided, the formatting
// will be performed by the `kv/format` package. In this case
// the value will be formatted using the `{:v}` directive, or
// `{:?}` if .X = true (verbose).
func (f Field) String() string {
b := make([]byte, 0, bufsize)
buf := byteutil.Buffer{B: b}
f.AppendFormat(&buf, false)
return buf.String()
}
// GoString performs .String() but with verbose always enabled.
func (f Field) GoString() string {
b := make([]byte, 0, bufsize)
buf := byteutil.Buffer{B: b}
f.AppendFormat(&buf, true)
return buf.String()
}