-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.go
38 lines (33 loc) · 1 KB
/
complex.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
package pretty
import (
"reflect"
"github.com/pierrre/go-libs/strconvio"
"github.com/pierrre/pretty/internal/write"
)
// ComplexValueWriter is a [ValueWriter] that handles complex values.
//
// It should be created with [NewComplexValueWriter].
type ComplexValueWriter struct {
// Format is the format used to format the complex.
// Default: 'g'.
Format byte
// Precision is the precision used to format the complex.
// Default: -1.
Precision int
}
// NewComplexValueWriter creates a new [ComplexValueWriter] with default values.
func NewComplexValueWriter() *ComplexValueWriter {
return &ComplexValueWriter{
Format: 'g',
Precision: -1,
}
}
// WriteValue implements [ValueWriter].
func (vw *ComplexValueWriter) WriteValue(st *State, v reflect.Value) bool {
switch v.Kind() { //nolint:exhaustive // Only handles complex.
case reflect.Complex64, reflect.Complex128:
write.Must(strconvio.WriteComplex(st.Writer, v.Complex(), vw.Format, vw.Precision, v.Type().Bits()))
return true
}
return false
}