-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.go
38 lines (33 loc) · 992 Bytes
/
float.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"
)
// FloatValueWriter is a [ValueWriter] that handles float values.
//
// It should be created with [NewFloatValueWriter].
type FloatValueWriter struct {
// Format is the format used to format the float.
// Default: 'g'.
Format byte
// Precision is the precision used to format the float.
// Default: -1.
Precision int
}
// NewFloatValueWriter creates a new [FloatValueWriter] with default values.
func NewFloatValueWriter() *FloatValueWriter {
return &FloatValueWriter{
Format: 'g',
Precision: -1,
}
}
// WriteValue implements [ValueWriter].
func (vw *FloatValueWriter) WriteValue(st *State, v reflect.Value) bool {
switch v.Kind() { //nolint:exhaustive // Only handles float.
case reflect.Float32, reflect.Float64:
internal.MustWrite(strconvio.WriteFloat(st.Writer, v.Float(), vw.Format, vw.Precision, v.Type().Bits()))
return true
}
return false
}