@@ -10,7 +10,9 @@ import (
10
10
"bytes"
11
11
"context"
12
12
"encoding/json"
13
+ "fmt"
13
14
"io"
15
+ "log/slog/internal/buffer"
14
16
"os"
15
17
"path/filepath"
16
18
"slices"
@@ -529,6 +531,20 @@ func TestJSONAndTextHandlers(t *testing.T) {
529
531
wantText : "name.first=Perry name.last=Platypus" ,
530
532
wantJSON : `{"name":{"first":"Perry","last":"Platypus"}}` ,
531
533
},
534
+ {
535
+ name : "group and key (or both) needs quoting" ,
536
+ replace : removeKeys (TimeKey , LevelKey ),
537
+ attrs : []Attr {
538
+ Group ("prefix" ,
539
+ String (" needs quoting " , "v" ), String ("NotNeedsQuoting" , "v" ),
540
+ ),
541
+ Group ("prefix needs quoting" ,
542
+ String (" needs quoting " , "v" ), String ("NotNeedsQuoting" , "v" ),
543
+ ),
544
+ },
545
+ wantText : `msg=message "prefix. needs quoting "=v prefix.NotNeedsQuoting=v "prefix needs quoting. needs quoting "=v "prefix needs quoting.NotNeedsQuoting"=v` ,
546
+ wantJSON : `{"msg":"message","prefix":{" needs quoting ":"v","NotNeedsQuoting":"v"},"prefix needs quoting":{" needs quoting ":"v","NotNeedsQuoting":"v"}}` ,
547
+ },
532
548
} {
533
549
r := NewRecord (testTime , LevelInfo , "message" , callerPC (2 ))
534
550
line := strconv .Itoa (r .source ().Line )
@@ -732,3 +748,31 @@ func TestDiscardHandler(t *testing.T) {
732
748
l .Info ("info" , "a" , []Attr {Int ("i" , 1 )})
733
749
l .Info ("info" , "a" , GroupValue (Int ("i" , 1 )))
734
750
}
751
+
752
+ func BenchmarkAppendKey (b * testing.B ) {
753
+ for _ , size := range []int {5 , 10 , 30 , 50 , 100 } {
754
+ for _ , quoting := range []string {"no_quoting" , "pre_quoting" , "key_quoting" , "both_quoting" } {
755
+ b .Run (fmt .Sprintf ("%s_prefix_size_%d" , quoting , size ), func (b * testing.B ) {
756
+ var (
757
+ hs = NewJSONHandler (io .Discard , nil ).newHandleState (buffer .New (), false , "" )
758
+ prefix = bytes .Repeat ([]byte ("x" ), size )
759
+ key = "key"
760
+ )
761
+
762
+ if quoting == "pre_quoting" || quoting == "both_quoting" {
763
+ prefix [0 ] = '"'
764
+ }
765
+ if quoting == "key_quoting" || quoting == "both_quoting" {
766
+ key = "ke\" "
767
+ }
768
+
769
+ hs .prefix = (* buffer .Buffer )(& prefix )
770
+
771
+ for b .Loop () {
772
+ hs .appendKey (key )
773
+ hs .buf .Reset ()
774
+ }
775
+ })
776
+ }
777
+ }
778
+ }
0 commit comments