-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.go
59 lines (55 loc) · 1.3 KB
/
slice.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
package pretty
import (
"reflect"
)
// SliceValueWriter is a [ValueWriter] that handles slice values.
//
// It should be created with [NewSliceValueWriter].
type SliceValueWriter struct {
ValueWriter
// ShowLen shows the len.
// Default: true.
ShowLen bool
// ShowCap shows the cap.
// Default: false.
ShowCap bool
// ShowAddr shows the address.
// Default: false.
ShowAddr bool
// ShowIndexes shows the indexes.
// Default: false.
ShowIndexes bool
// MaxLen is the maximum length of the slice.
// Default: 0 (no limit).
MaxLen int
}
// NewSliceValueWriter creates a new [SliceValueWriter] with default values.
func NewSliceValueWriter(vw ValueWriter) *SliceValueWriter {
return &SliceValueWriter{
ValueWriter: vw,
ShowLen: true,
ShowCap: false,
ShowAddr: false,
ShowIndexes: false,
MaxLen: 0,
}
}
// WriteValue implements [ValueWriter].
func (vw *SliceValueWriter) WriteValue(st *State, v reflect.Value) bool {
if v.Kind() != reflect.Slice {
return false
}
if checkNil(st.Writer, v) {
return true
}
infos{
showLen: vw.ShowLen,
len: v.Len(),
showCap: vw.ShowCap,
cap: v.Cap(),
showAddr: vw.ShowAddr,
addr: uintptr(v.UnsafePointer()),
}.writeWithTrailingSpace(st)
writeArray(st, v, vw.ShowIndexes, vw.MaxLen, vw.ValueWriter)
return true
}