Skip to content

Commit

Permalink
Use backquotes for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Sep 20, 2024
1 parent 21df9df commit 0181458
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 49 deletions.
47 changes: 24 additions & 23 deletions wirebson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package wirebson

import (
"strings"
"testing"
"time"

Expand All @@ -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.
Expand Down Expand Up @@ -77,7 +78,7 @@ var normalTestCases = []normalTestCase{
"compression", MustArray("none"),
"loadBalanced", false,
),
b: `
mi: `
{
"ismaster": true,
"client": {
Expand Down Expand Up @@ -126,7 +127,7 @@ var normalTestCases = []normalTestCase{
"compression", MustArray("none"),
"loadBalanced", false,
),
b: `
mi: `
{
"ismaster": true,
"client": {
Expand Down Expand Up @@ -167,7 +168,7 @@ var normalTestCases = []normalTestCase{
),
"$db", "admin",
),
b: `
mi: `
{
"buildInfo": 1,
"lsid": {
Expand Down Expand Up @@ -220,7 +221,7 @@ var normalTestCases = []normalTestCase{
"storageEngines", MustArray("devnull", "ephemeralForTest", "wiredTiger"),
"ok", float64(1),
),
b: `
mi: `
{
"version": "5.0.0",
"gitVersion": "1184f004a99660de6f5e745573419bda8a28c0e9",
Expand Down Expand Up @@ -290,7 +291,7 @@ var normalTestCases = []normalTestCase{
"timestamp", MustArray(Timestamp(42), Timestamp(0)),
"decimal128", MustArray(Decimal128{L: 42, H: 13}),
),
b: `
mi: `
{
"array": [
[
Expand Down Expand Up @@ -353,7 +354,7 @@ var normalTestCases = []normalTestCase{
name: "nested",
raw: testutil.MustParseDumpFile("testdata", "nested.hex"),
doc: makeNested(false, 150).(*Document),
b: `
mi: `
{
"f": [
{
Expand Down Expand Up @@ -407,7 +408,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"f", float64(3.141592653589793),
),
b: `
mi: `
{
"f": 3.141592653589793,
}`,
Expand All @@ -424,7 +425,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"f", "v",
),
b: `
mi: `
{
"f": "v",
}`,
Expand All @@ -442,7 +443,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"f", Binary{B: []byte("v"), Subtype: BinaryUser},
),
b: `
mi: `
{
"f": Binary(user:dg==),
}`,
Expand All @@ -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),
}`,
Expand All @@ -474,7 +475,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"f", true,
),
b: `
mi: `
{
"f": true,
}`,
Expand All @@ -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,
}`,
Expand All @@ -505,7 +506,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"f", Null,
),
b: `
mi: `
{
"f": null,
}`,
Expand All @@ -522,7 +523,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"f", Regex{Pattern: "p", Options: "o"},
),
b: `
mi: `
{
"f": /p/o,
}`,
Expand All @@ -538,7 +539,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"f", int32(314159265),
),
b: `
mi: `
{
"f": 314159265,
}`,
Expand All @@ -554,7 +555,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"f", Timestamp(42),
),
b: `
mi: `
{
"f": Timestamp(42),
}`,
Expand All @@ -570,7 +571,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"f", int64(3141592653589793),
),
b: `
mi: `
{
"f": int64(3141592653589793),
}`,
Expand All @@ -586,7 +587,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"f", Decimal128{L: 42, H: 13},
),
b: `
mi: `
{
"f": Decimal128(42,13),
}`,
Expand All @@ -602,7 +603,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"foo", MustDocument(),
),
b: `
mi: `
{
"foo": {},
}`,
Expand All @@ -618,7 +619,7 @@ var normalTestCases = []normalTestCase{
doc: MustDocument(
"foo", MustArray(),
),
b: `
mi: `
{
"foo": [],
}`,
Expand All @@ -635,7 +636,7 @@ var normalTestCases = []normalTestCase{
"", false,
"", true,
),
b: `
mi: `
{
"": false,
"": true,
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 6 additions & 12 deletions wirebson/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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(")
Expand Down
29 changes: 15 additions & 14 deletions wirebson/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"log/slog"
"math"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -115,14 +116,14 @@ func TestLogging(t *testing.T) {
),
t: `v.null=<nil> 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",
Expand All @@ -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",
Expand All @@ -161,16 +162,16 @@ 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",
doc: makeNested(false, 20).(*Document),
t: `v.f.0.f.0.f.0.f.0.f.0.f.0.f.0.f.0.f.0.f.0=<nil>`,
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": [
{
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 0181458

Please sign in to comment.