Skip to content

Commit d2d4214

Browse files
committed
fixes
1 parent 7d77388 commit d2d4214

File tree

9 files changed

+36
-38
lines changed

9 files changed

+36
-38
lines changed

bson/raw_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ func BenchmarkRawString(b *testing.B) {
450450
b.ReportAllocs()
451451
b.ResetTimer()
452452
for i := 0; i < b.N; i++ {
453-
_ = bsoncore.Document(bs).StringN(1024) // Assuming you want to limit to 1024 bytes for this benchmark
453+
_, _ = bsoncore.Document(bs).StringN(1024) // Assuming you want to limit to 1024 bytes for this benchmark
454454
}
455455
})
456456
}
@@ -473,7 +473,7 @@ func TestComplexDocuments_StringN(t *testing.T) {
473473
bson, _ := Marshal(tc.doc)
474474
bsonDoc := bsoncore.Document(bson)
475475

476-
got := bsonDoc.StringN(tc.n)
476+
got, _ := bsonDoc.StringN(tc.n)
477477
assert.Equal(t, tc.n, len(got))
478478
})
479479
}

internal/logger/logger.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,11 @@ func FormatDocument(msg bson.Raw, width uint) string {
241241
return "{}"
242242
}
243243

244-
str := bsoncore.Document(msg).StringN(int(width))
244+
str, truncated := bsoncore.Document(msg).StringN(int(width))
245245

246-
fmt.Println("FormatDocument", len(str), width)
246+
fmt.Println("FormatDocument", len(str), width, truncated)
247247

248-
// If the last byte is not a closing bracket, then the document was truncated
249-
if len(str) > 0 && str[len(str)-1] != '}' {
248+
if truncated {
250249
str += TruncationSuffix
251250
}
252251

x/bsonx/bsoncore/array.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,29 @@ func (a Array) String() string {
8787
}
8888

8989
// StringN stringifies an array upto N bytes
90-
func (a Array) StringN(n int) string {
90+
func (a Array) StringN(n int) (string, bool) {
9191
if n <= 0 {
92-
return ""
92+
if len, _, ok := ReadLength(a); !ok || len < 5 {
93+
return "", false
94+
}
95+
return "", true
9396
}
94-
str, _ := a.stringN(n)
95-
return str
97+
return a.stringN(n)
9698
}
9799

98100
// stringN stringify an array. If N is larger than 0, it will truncate the string to N bytes.
99101
func (a Array) stringN(n int) (string, bool) {
100-
if lens, _, _ := ReadLength(a); lens < 5 {
102+
length, rem, ok := ReadLength(a) // We know we have enough bytes to read the length
103+
if !ok || length < 5 {
101104
return "", false
102105
}
106+
length -= (4 /* length bytes */ + 1 /* final null byte */)
103107

104108
var buf strings.Builder
105109
buf.WriteByte('[')
106110

107-
length, rem, _ := ReadLength(a) // We know we have enough bytes to read the length
108-
length -= (4 /* length bytes */ + 1 /* final null byte */)
109-
110111
var truncated bool
111112
var elem Element
112-
var ok bool
113113
var str string
114114
first := true
115115
for length > 0 && !truncated {
@@ -135,7 +135,7 @@ func (a Array) stringN(n int) (string, bool) {
135135
elem, rem, ok = ReadElement(rem)
136136
length -= int32(len(elem))
137137
if !ok || length < 0 {
138-
return "", true
138+
return "", false
139139
}
140140

141141
str, truncated = elem.Value().stringN(l)

x/bsonx/bsoncore/array_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ func TestArray_StringN(t *testing.T) {
573573

574574
for _, tc := range testCases {
575575
t.Run(tc.description, func(t *testing.T) {
576-
got := Array(BuildArray(nil, tc.values...)).StringN(tc.n)
576+
got, _ := Array(BuildArray(nil, tc.values...)).StringN(tc.n)
577577
assert.Equal(t, tc.want, got)
578578
if tc.n >= 0 {
579579
assert.LessOrEqual(t, len(got), tc.n, "got %v, want %v", got, tc.want)

x/bsonx/bsoncore/document.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,29 +266,29 @@ func (d Document) String() string {
266266
}
267267

268268
// StringN stringifies a document upto N bytes
269-
func (d Document) StringN(n int) string {
269+
func (d Document) StringN(n int) (string, bool) {
270270
if n <= 0 {
271-
return ""
271+
if len, _, ok := ReadLength(d); !ok || len < 5 {
272+
return "", false
273+
}
274+
return "", true
272275
}
273-
str, _ := d.stringN(n)
274-
return str
276+
return d.stringN(n)
275277
}
276278

277279
// stringN stringify a document. If N is larger than 0, it will truncate the string to N bytes.
278280
func (d Document) stringN(n int) (string, bool) {
279-
if len(d) < 5 {
281+
length, rem, ok := ReadLength(d)
282+
if !ok || length < 5 {
280283
return "", false
281284
}
285+
length -= (4 /* length bytes */ + 1 /* final null byte */)
282286

283287
var buf strings.Builder
284288
buf.WriteByte('{')
285289

286-
length, rem, _ := ReadLength(d)
287-
length -= (4 /* length bytes */ + 1 /* final null byte */)
288-
289290
var truncated bool
290291
var elem Element
291-
var ok bool
292292
var str string
293293
first := true
294294
for length > 0 && !truncated {
@@ -314,7 +314,7 @@ func (d Document) stringN(n int) (string, bool) {
314314
elem, rem, ok = ReadElement(rem)
315315
length -= int32(len(elem))
316316
if !ok || length < 0 {
317-
return "", true
317+
return "", false
318318
}
319319

320320
str, truncated = elem.stringN(l)

x/bsonx/bsoncore/document_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func TestDocument_StringN(t *testing.T) {
529529
for _, tc := range testCases {
530530
t.Run(tc.description, func(t *testing.T) {
531531
bs := tc.doc
532-
got := bs.StringN(tc.n)
532+
got, _ := bs.StringN(tc.n)
533533
assert.Equal(t, tc.want, got)
534534
if tc.n >= 0 {
535535
assert.LessOrEqual(t, len(got), tc.n, "got %v, want %v", got, tc.want)

x/bsonx/bsoncore/element.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,11 @@ func (e Element) String() string {
122122
}
123123

124124
// StringN implements the fmt.String interface for upto N bytes. The output will be in extended JSON format.
125-
func (e Element) StringN(n int) string {
125+
func (e Element) StringN(n int) (string, bool) {
126126
if n <= 0 {
127-
return ""
127+
return "", len(e) > 0
128128
}
129-
str, _ := e.stringN(n)
130-
return str
129+
return e.stringN(n)
131130
}
132131

133132
// stringN stringify an element. If N is larger than 0, it will truncate the string to N bytes.
@@ -155,7 +154,7 @@ func (e Element) stringN(n int) (string, bool) {
155154
buf.WriteString(postfix)
156155
case idx < n:
157156
buf.Write(key)
158-
buf.WriteString(postfix[:n-idx])
157+
buf.WriteString(postfix[:n-idx-1])
159158
default:
160159
buf.WriteString(bsoncoreutil.Truncate(string(key), n-1))
161160
}

x/bsonx/bsoncore/value.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,12 @@ func (v Value) String() string {
224224

225225
// StringN implements the fmt.String interface. This method will return values in extended JSON
226226
// format that will stringify a value upto N bytes. If the value is not valid, this returns an empty string
227-
func (v Value) StringN(n int) string {
227+
func (v Value) StringN(n int) (string, bool) {
228+
str, truncated := v.stringN(n)
228229
if n <= 0 {
229-
return ""
230+
return "", len(str) > 0
230231
}
231-
str, _ := v.stringN(n)
232-
return str
232+
return str, truncated
233233
}
234234

235235
// stringN stringify a value. If N is larger than 0, it will truncate the string to N bytes.

x/bsonx/bsoncore/value_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ func TestValue_StringN(t *testing.T) {
829829

830830
for _, tc := range testCases {
831831
t.Run(tc.description, func(t *testing.T) {
832-
got := tc.val.StringN(tc.n)
832+
got, _ := tc.val.StringN(tc.n)
833833
assert.Equal(t, tc.want, got)
834834
if tc.n >= 0 {
835835
assert.LessOrEqual(t, len(got), tc.n, "got %v, want %v", got, tc.want)

0 commit comments

Comments
 (0)