[bugfix] bump go-kv version with logfmt quote fix (#2108)

This commit is contained in:
kim 2023-08-13 14:27:29 +01:00 committed by GitHub
parent 5e368d3089
commit 8ea7f551a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 49 additions and 26 deletions

2
go.mod
View file

@ -10,7 +10,7 @@ require (
codeberg.org/gruf/go-errors/v2 v2.2.0
codeberg.org/gruf/go-fastcopy v1.1.2
codeberg.org/gruf/go-iotools v0.0.0-20230601182242-d933b07dcbef
codeberg.org/gruf/go-kv v1.6.1
codeberg.org/gruf/go-kv v1.6.3
codeberg.org/gruf/go-logger/v2 v2.2.1
codeberg.org/gruf/go-mutexes v1.1.5
codeberg.org/gruf/go-runners v1.6.1

4
go.sum
View file

@ -66,8 +66,8 @@ codeberg.org/gruf/go-hashenc v1.0.2 h1:U3jH6zMXZiL96czD/qaJd8OR2h7LlBzGv/2WxnMHI
codeberg.org/gruf/go-hashenc v1.0.2/go.mod h1:eK+A8clLcEN/m1nftNsRId0kfYDQnETnuIfBGZ8Gvsg=
codeberg.org/gruf/go-iotools v0.0.0-20230601182242-d933b07dcbef h1:3Ydviw47TFEk27FRCOXkRxU3MfgyNzoicLzq8J3NbtI=
codeberg.org/gruf/go-iotools v0.0.0-20230601182242-d933b07dcbef/go.mod h1:B8uq4yHtIcKXhBZT9C/SYisz25lldLHMVpwZPz4ADLQ=
codeberg.org/gruf/go-kv v1.6.1 h1:HsCZEy0zfGq1oFGOEOO2qnpooiGefCZBbcUpa3KdXn8=
codeberg.org/gruf/go-kv v1.6.1/go.mod h1:O/YkSvKiS9XsRolM3rqCd9YJmND7dAXu9z+PrlYO4bc=
codeberg.org/gruf/go-kv v1.6.3 h1:RrAhVGzAq+kU3WPR0gl1gqKnYks+prpUpa8+xDzoQXA=
codeberg.org/gruf/go-kv v1.6.3/go.mod h1:O/YkSvKiS9XsRolM3rqCd9YJmND7dAXu9z+PrlYO4bc=
codeberg.org/gruf/go-logger/v2 v2.2.1 h1:RP2u059EQKTBFV3cN8X6xDxNk2RkzqdgXGKflKqB7Oc=
codeberg.org/gruf/go-logger/v2 v2.2.1/go.mod h1:m/vBfG5jNUmYXI8Hg9aVSk7Pn8YgEBITQB/B/CzdRss=
codeberg.org/gruf/go-mangler v1.2.3 h1:sj0dey2lF5GRQL7fXmCY0wPNaI5JrROiThb0VDbzF8A=

View file

@ -4,6 +4,33 @@ This library provides a key-value field structure `kv.Field{}` that plays well w
The formatting for these key-value fields is handled by the `"fmt"` package by default. If you set the `kvformat` build tag then it will use a custom formatting library found under `format/`. You can see the benchmarks for both below.
![benchmarks](https://codeberg.org/gruf/go-kv/raw/main/benchmark.png)
benchmarks:
```
grufwub @ ~/Projects/main/go-kv
--> go test -run=none -benchmem -bench=.*
goos: linux
goarch: amd64
pkg: codeberg.org/gruf/go-kv
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
BenchmarkFieldAppendMulti-8 125241 9389 ns/op 849 B/op 98 allocs/op
BenchmarkFieldStringMulti-8 113227 10444 ns/op 3029 B/op 120 allocs/op
BenchmarkFieldFprintfMulti-8 147915 7448 ns/op 1121 B/op 115 allocs/op
BenchmarkFieldsAppend-8 189126 6255 ns/op 849 B/op 98 allocs/op
BenchmarkFieldsString-8 166219 6517 ns/op 3798 B/op 100 allocs/op
PASS
ok codeberg.org/gruf/go-kv 6.169s
TODO: benchmarks comparing these to using `fmt.Sprintf("%q=%+v", ...)` yourself.
grufwub @ ~/Projects/main/go-kv
--> go test -run=none -benchmem -bench=.* -tags=kvformat
goos: linux
goarch: amd64
pkg: codeberg.org/gruf/go-kv
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
BenchmarkFieldAppendMulti-8 190161 5709 ns/op 592 B/op 56 allocs/op
BenchmarkFieldStringMulti-8 161763 7930 ns/op 3240 B/op 95 allocs/op
BenchmarkFieldFprintfMulti-8 181557 6207 ns/op 1120 B/op 115 allocs/op
BenchmarkFieldsAppend-8 247052 4580 ns/op 592 B/op 56 allocs/op
BenchmarkFieldsString-8 231235 5103 ns/op 1768 B/op 58 allocs/op
PASS
ok codeberg.org/gruf/go-kv 6.134s
```

Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

View file

@ -2,4 +2,3 @@ String formatting package using Rust-style formatting directives, geared specifc
Generally more visually friendly than `"fmt"` and performance is much improved.
![benchmarks](https://codeberg.org/gruf/go-kv/raw/main/format/benchmark.png)

View file

@ -316,7 +316,9 @@ func (f format) AppendStringSafe(s string) {
f.Buffer.B = strconv.AppendQuote(f.Buffer.B, s)
} else if ContainsDoubleQuote(s) {
// Contains double quotes, needs escaping
f.Buffer.B = append(f.Buffer.B, '"')
f.Buffer.B = AppendEscape(f.Buffer.B, s)
f.Buffer.B = append(f.Buffer.B, '"')
} else if len(s) == 0 || ContainsSpaceOrTab(s) {
// Contains space / empty, needs quotes
f.Buffer.B = append(f.Buffer.B, '"')

View file

@ -29,7 +29,7 @@ func IsSafeASCII(str string) bool {
return true
}
// ContainsSpaceOrTab checks if "s" contains space or tabs.
// ContainsSpaceOrTab checks if "s" contains space or tabs. EXPECTS ASCII.
func ContainsSpaceOrTab(s string) bool {
if i := strings.IndexByte(s, ' '); i >= 0 {
return true // note using indexbyte as it is ASM.
@ -39,31 +39,26 @@ func ContainsSpaceOrTab(s string) bool {
return false
}
// ContainsDoubleQuote checks if "s" contains a double quote.
// ContainsDoubleQuote checks if "s" contains a double quote. EXPECTS ASCII.
func ContainsDoubleQuote(s string) bool {
return (strings.IndexByte(s, '"') >= 0)
}
// AppendEscape will append 's' to 'dst' and escape any double quotes.
// AppendEscape will append 's' to 'dst' and escape any double quotes. EXPECTS ASCII.
func AppendEscape(dst []byte, str string) []byte {
var delim bool
for i := range str {
if str[i] == '\\' && !delim {
// Set delim flag
delim = true
continue
} else if str[i] == '"' && !delim {
// Append escaped double quote
dst = append(dst, `\"`...)
continue
} else if delim {
// Append skipped slash
dst = append(dst, `\`...)
delim = false
}
switch str[i] {
case '\\':
// Append delimited '\'
dst = append(dst, '\\', '\\')
// Append char as-is
dst = append(dst, str[i])
case '"':
// Append delimited '"'
dst = append(dst, '\\', '"')
default:
// Append char as-is
dst = append(dst, str[i])
}
}
return dst
}

2
vendor/modules.txt vendored
View file

@ -40,7 +40,7 @@ codeberg.org/gruf/go-hashenc
# codeberg.org/gruf/go-iotools v0.0.0-20230601182242-d933b07dcbef
## explicit; go 1.19
codeberg.org/gruf/go-iotools
# codeberg.org/gruf/go-kv v1.6.1
# codeberg.org/gruf/go-kv v1.6.3
## explicit; go 1.19
codeberg.org/gruf/go-kv
codeberg.org/gruf/go-kv/format