File tree 1 file changed +45
-1
lines changed
1 file changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -347,11 +347,55 @@ func typeof(v interface{}) (s string) {
347
347
}
348
348
}
349
349
350
+ type limitWriter struct {
351
+ buf []byte
352
+ off int
353
+ }
354
+
355
+ func (w * limitWriter ) Write (bs []byte ) (int , error ) {
356
+ n := copy (w .buf [w .off :], bs )
357
+ if w .off += n ; w .off == len (w .buf ) {
358
+ panic (true )
359
+ }
360
+ return n , nil
361
+ }
362
+
363
+ func (w * limitWriter ) WriteByte (b byte ) error {
364
+ w .buf [w .off ] = b
365
+ w .off ++
366
+ if w .off == len (w .buf ) {
367
+ panic (true )
368
+ }
369
+ return nil
370
+ }
371
+
372
+ func (w * limitWriter ) WriteString (s string ) (int , error ) {
373
+ n := copy (w .buf [w .off :], s )
374
+ if w .off += n ; w .off == len (w .buf ) {
375
+ panic (true )
376
+ }
377
+ return n , nil
378
+ }
379
+
380
+ func (w * limitWriter ) String () string {
381
+ return string (w .buf [:w .off ])
382
+ }
383
+
384
+ func jsonLimitedMarshal (v interface {}, n int ) (s string ) {
385
+ w := & limitWriter {buf : make ([]byte , n )}
386
+ defer func () {
387
+ recover ()
388
+ s = w .String ()
389
+ }()
390
+ (& encoder {w : w }).encode (v )
391
+ return
392
+ }
393
+
350
394
func preview (v interface {}) string {
351
395
if v == nil {
352
396
return ""
353
397
}
354
- s := jsonMarshal ( v )
398
+ s := jsonLimitedMarshal ( v , 32 )
355
399
if l := 30 ; len (s ) > l {
356
400
var trailing string
357
401
switch v .(type ) {
You can’t perform that action at this time.
0 commit comments