From 01814589b32681f5bc073270013986f11c177b89 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Fri, 20 Sep 2024 17:19:53 +0400 Subject: [PATCH] Use backquotes for strings --- wirebson/bson_test.go | 47 ++++++++++++++++++++-------------------- wirebson/logging.go | 18 +++++---------- wirebson/logging_test.go | 29 +++++++++++++------------ 3 files changed, 45 insertions(+), 49 deletions(-) diff --git a/wirebson/bson_test.go b/wirebson/bson_test.go index 10ac9a6..d8ccb19 100644 --- a/wirebson/bson_test.go +++ b/wirebson/bson_test.go @@ -15,6 +15,7 @@ package wirebson import ( + "strings" "testing" "time" @@ -31,7 +32,7 @@ type normalTestCase struct { name string raw RawDocument doc *Document - b string + mi string } // decodeTestCase represents a single test case for unsuccessful decoding. @@ -77,7 +78,7 @@ var normalTestCases = []normalTestCase{ "compression", MustArray("none"), "loadBalanced", false, ), - b: ` + mi: ` { "ismaster": true, "client": { @@ -126,7 +127,7 @@ var normalTestCases = []normalTestCase{ "compression", MustArray("none"), "loadBalanced", false, ), - b: ` + mi: ` { "ismaster": true, "client": { @@ -167,7 +168,7 @@ var normalTestCases = []normalTestCase{ ), "$db", "admin", ), - b: ` + mi: ` { "buildInfo": 1, "lsid": { @@ -220,7 +221,7 @@ var normalTestCases = []normalTestCase{ "storageEngines", MustArray("devnull", "ephemeralForTest", "wiredTiger"), "ok", float64(1), ), - b: ` + mi: ` { "version": "5.0.0", "gitVersion": "1184f004a99660de6f5e745573419bda8a28c0e9", @@ -290,7 +291,7 @@ var normalTestCases = []normalTestCase{ "timestamp", MustArray(Timestamp(42), Timestamp(0)), "decimal128", MustArray(Decimal128{L: 42, H: 13}), ), - b: ` + mi: ` { "array": [ [ @@ -353,7 +354,7 @@ var normalTestCases = []normalTestCase{ name: "nested", raw: testutil.MustParseDumpFile("testdata", "nested.hex"), doc: makeNested(false, 150).(*Document), - b: ` + mi: ` { "f": [ { @@ -407,7 +408,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", float64(3.141592653589793), ), - b: ` + mi: ` { "f": 3.141592653589793, }`, @@ -424,7 +425,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", "v", ), - b: ` + mi: ` { "f": "v", }`, @@ -442,7 +443,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", Binary{B: []byte("v"), Subtype: BinaryUser}, ), - b: ` + mi: ` { "f": Binary(user:dg==), }`, @@ -458,7 +459,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", ObjectID{0x62, 0x56, 0xc5, 0xba, 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}, ), - b: ` + mi: ` { "f": ObjectID(6256c5ba182d4454fb210940), }`, @@ -474,7 +475,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", true, ), - b: ` + mi: ` { "f": true, }`, @@ -490,7 +491,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", time.Date(2024, 1, 17, 17, 40, 42, 123000000, time.UTC), ), - b: ` + mi: ` { "f": 2024-01-17T17:40:42.123Z, }`, @@ -505,7 +506,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", Null, ), - b: ` + mi: ` { "f": null, }`, @@ -522,7 +523,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", Regex{Pattern: "p", Options: "o"}, ), - b: ` + mi: ` { "f": /p/o, }`, @@ -538,7 +539,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", int32(314159265), ), - b: ` + mi: ` { "f": 314159265, }`, @@ -554,7 +555,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", Timestamp(42), ), - b: ` + mi: ` { "f": Timestamp(42), }`, @@ -570,7 +571,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", int64(3141592653589793), ), - b: ` + mi: ` { "f": int64(3141592653589793), }`, @@ -586,7 +587,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "f", Decimal128{L: 42, H: 13}, ), - b: ` + mi: ` { "f": Decimal128(42,13), }`, @@ -602,7 +603,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "foo", MustDocument(), ), - b: ` + mi: ` { "foo": {}, }`, @@ -618,7 +619,7 @@ var normalTestCases = []normalTestCase{ doc: MustDocument( "foo", MustArray(), ), - b: ` + mi: ` { "foo": [], }`, @@ -635,7 +636,7 @@ var normalTestCases = []normalTestCase{ "", false, "", true, ), - b: ` + mi: ` { "": false, "": true, @@ -781,7 +782,7 @@ func TestNormal(t *testing.T) { assert.NotContains(t, ls, "called too many times") assert.NotEmpty(t, doc.LogMessage()) - assert.Equal(t, testutil.Unindent(tc.b), doc.LogMessageIndent()) + assert.Equal(t, strings.ReplaceAll(testutil.Unindent(tc.mi), `"`, "`"), doc.LogMessageIndent()) raw, err := doc.Encode() require.NoError(t, err) diff --git a/wirebson/logging.go b/wirebson/logging.go index 9257bc7..e35306a 100644 --- a/wirebson/logging.go +++ b/wirebson/logging.go @@ -187,8 +187,7 @@ func logMessage(v any, indent, depth int, b *strings.Builder) { b.WriteByte('{') for i, f := range v.fields { - b.WriteString(strconv.Quote(f.name)) - b.WriteString(": ") + fmt.Fprintf(b, "%#q: ", f.name) logMessage(f.value, -1, depth+1, b) @@ -206,8 +205,7 @@ func logMessage(v any, indent, depth int, b *strings.Builder) { for _, f := range v.fields { b.WriteString(strings.Repeat(" ", indent+1)) - b.WriteString(strconv.Quote(f.name)) - b.WriteString(": ") + fmt.Fprintf(b, "%#q: ", f.name) logMessage(f.value, indent+1, depth+1, b) @@ -218,9 +216,7 @@ func logMessage(v any, indent, depth int, b *strings.Builder) { b.WriteByte('}') case RawDocument: - b.WriteString("RawDocument<") - b.WriteString(strconv.FormatInt(int64(len(v)), 10)) - b.WriteByte('>') + fmt.Fprintf(b, "RawDocument<%d>", len(v)) case *Array: if v == nil { @@ -268,15 +264,13 @@ func logMessage(v any, indent, depth int, b *strings.Builder) { b.WriteByte(']') case RawArray: - b.WriteString("RawArray<") - b.WriteString(strconv.FormatInt(int64(len(v)), 10)) - b.WriteByte('>') + fmt.Fprintf(b, "RawArray<%d>", len(v)) case float64: switch { case math.IsNaN(v): if bits := math.Float64bits(v); bits != nanBits { - b.WriteString(fmt.Sprintf("NaN(%b)", bits)) + fmt.Fprintf(b, "NaN(%b)", bits) return } @@ -298,7 +292,7 @@ func logMessage(v any, indent, depth int, b *strings.Builder) { } case string: - b.WriteString(strconv.Quote(v)) + fmt.Fprintf(b, "%#q", v) case Binary: b.WriteString("Binary(") diff --git a/wirebson/logging_test.go b/wirebson/logging_test.go index 45cab47..d49ca4c 100644 --- a/wirebson/logging_test.go +++ b/wirebson/logging_test.go @@ -19,6 +19,7 @@ import ( "context" "log/slog" "math" + "strings" "testing" "time" @@ -89,7 +90,7 @@ func TestLogging(t *testing.T) { ), t: `v.f64=42 v.inf=+Inf v.neg_inf=-Inf v.zero=0 v.neg_zero=-0 v.nan=NaN v.nan_weird=NaN v.i32=42 v.i64=42`, j: `{"v":{"f64":42,"inf":"+Inf","neg_inf":"-Inf","zero":0,"neg_zero":-0,"nan":"NaN","nan_weird":"NaN","i32":42,"i64":42}}`, - mi: ` + mi: strings.ReplaceAll(` { "f64": 42.0, "inf": +Inf, @@ -100,10 +101,10 @@ func TestLogging(t *testing.T) { "nan_weird": NaN(111111111111000000000000000111100000000000011110000000000000001), "i32": 42, "i64": int64(42), - }`, - m: `{"f64": 42.0, "inf": +Inf, "neg_inf": -Inf, "zero": 0.0, "neg_zero": -0.0, "nan": NaN, ` + - `"nan_weird": NaN(111111111111000000000000000111100000000000011110000000000000001), ` + - `"i32": 42, "i64": int64(42)}`, + }`, `"`, "`"), + m: "{`f64`: 42.0, `inf`: +Inf, `neg_inf`: -Inf, `zero`: 0.0, `neg_zero`: -0.0, `nan`: NaN, " + + "`nan_weird`: NaN(111111111111000000000000000111100000000000011110000000000000001), " + + "`i32`: 42, `i64`: int64(42)}", }, { name: "Scalars", @@ -115,14 +116,14 @@ func TestLogging(t *testing.T) { ), t: `v.null= v.id=ObjectID(420000000000000000000000) v.bool=true v.time=2023-03-06T09:14:42.123Z`, j: `{"v":{"null":null,"id":"ObjectID(420000000000000000000000)","bool":true,"time":"2023-03-06T09:14:42.123Z"}}`, - mi: ` + mi: strings.ReplaceAll(` { "null": null, "id": ObjectID(420000000000000000000000), "bool": true, "time": 2023-03-06T09:14:42.123Z, - }`, - m: `{"null": null, "id": ObjectID(420000000000000000000000), "bool": true, "time": 2023-03-06T09:14:42.123Z}`, + }`, `"`, "`"), + m: "{`null`: null, `id`: ObjectID(420000000000000000000000), `bool`: true, `time`: 2023-03-06T09:14:42.123Z}", }, { name: "Composites", @@ -143,7 +144,7 @@ func TestLogging(t *testing.T) { `v.array.0=foo v.array.1=bar v.array.2.0=baz v.array.2.1=qux`, j: `{"v":{"doc":{"foo":"bar","baz":{"qux":"quux"}},"doc_raw":"RawDocument<1>",` + `"array":{"0":"foo","1":"bar","2":{"0":"baz","1":"qux"}}}}`, - mi: ` + mi: strings.ReplaceAll(` { "doc": { "foo": "bar", @@ -161,8 +162,8 @@ func TestLogging(t *testing.T) { "qux", ], ], - }`, - m: `{"doc": {"foo": "bar", "baz": {"qux": "quux"}}, "doc_raw": RawDocument<1>, "doc_empty": {}, "array": ["foo", "bar", ["baz", "qux"]]}`, + }`, `"`, "`"), + m: "{`doc`: {`foo`: `bar`, `baz`: {`qux`: `quux`}}, `doc_raw`: RawDocument<1>, `doc_empty`: {}, `array`: [`foo`, `bar`, [`baz`, `qux`]]}", }, { name: "Nested", @@ -170,7 +171,7 @@ func TestLogging(t *testing.T) { t: `v.f.0.f.0.f.0.f.0.f.0.f.0.f.0.f.0.f.0.f.0=`, j: `{"v":{"f":{"0":{"f":{"0":{"f":{"0":{"f":{"0":{"f":{"0":{"f":{"0":` + `{"f":{"0":{"f":{"0":{"f":{"0":{"f":{"0":null}}}}}}}}}}}}}}}}}}}}}`, - mi: ` + mi: strings.ReplaceAll(` { "f": [ { @@ -211,8 +212,8 @@ func TestLogging(t *testing.T) { ], }, ], - }`, - m: `{"f": [{"f": [{"f": [{"f": [{"f": [{"f": [{"f": [{"f": [{"f": [{"f": [null]}]}]}]}]}]}]}]}]}]}`, + }`, `"`, "`"), + m: "{`f`: [{`f`: [{`f`: [{`f`: [{`f`: [{`f`: [{`f`: [{`f`: [{`f`: [{`f`: [null]}]}]}]}]}]}]}]}]}]}", }, { name: "Raw",