forked from itchyny/gojq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preview.go
77 lines (69 loc) · 1.74 KB
/
preview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package gojq
import "unicode/utf8"
// Preview returns the preview string of v. The preview string is basically the
// same as the jq-flavored JSON encoding returned by [Marshal], but is truncated
// by 30 bytes, and more efficient than truncating the result of [Marshal].
//
// This method is used by error messages of built-in operators and functions,
// and accepts only limited types (nil, bool, int, float64, *big.Int, string,
// []any, and map[string]any). Note that the maximum width and trailing strings
// on truncation may be changed in the future.
func Preview(v any) string {
bs := jsonLimitedMarshal(v, 32)
if l := 30; len(bs) > l {
var trailing string
switch v.(type) {
case string:
trailing = ` ..."`
case []any:
trailing = " ...]"
case map[string]any:
trailing = " ...}"
default:
trailing = " ..."
}
for len(bs) > l-len(trailing) {
_, size := utf8.DecodeLastRune(bs)
bs = bs[:len(bs)-size]
}
bs = append(bs, trailing...)
}
return string(bs)
}
func jsonLimitedMarshal(v any, n int) (bs []byte) {
w := &limitedWriter{buf: make([]byte, n)}
defer func() {
_ = recover()
bs = w.Bytes()
}()
(&encoder{w: w}).encode(v)
return
}
type limitedWriter struct {
buf []byte
off int
}
func (w *limitedWriter) Write(bs []byte) (int, error) {
n := copy(w.buf[w.off:], bs)
if w.off += n; w.off == len(w.buf) {
panic(struct{}{})
}
return n, nil
}
func (w *limitedWriter) WriteByte(b byte) error {
w.buf[w.off] = b
if w.off++; w.off == len(w.buf) {
panic(struct{}{})
}
return nil
}
func (w *limitedWriter) WriteString(s string) (int, error) {
n := copy(w.buf[w.off:], s)
if w.off += n; w.off == len(w.buf) {
panic(struct{}{})
}
return n, nil
}
func (w *limitedWriter) Bytes() []byte {
return w.buf[:w.off]
}