Repository navigation
Expand file tree
/
Copy pathformat.go
More file actions
181 lines (148 loc) · 4.21 KB
/
Copy pathformat.go
File metadata and controls
181 lines (148 loc) · 4.21 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package render
import (
"strconv"
"strings"
"time"
)
// Sizes of the IEC units, and the boundaries of the duration units below.
const (
iecBase = 1024
secondsPerMin = 60
secondsPerHour = 3600
secondsPerDay = 86400
)
// iecUnits are the suffixes applied above one kibibyte. A controller counter is a
// 64-bit octet count, so the list stops where that stops mattering.
var iecUnits = [...]string{"Ki", "Mi", "Gi", "Ti", "Pi", "Ei"}
// Str renders a string cell, mapping the empty string to Absent. Almost every
// string leaf on this SDK's read paths sits in a non-pointer struct, so an omitted
// container and an omitted leaf both arrive as "" and neither is a value.
func Str(s string) string {
if s == "" {
return Absent
}
return s
}
func StrPtr(p *string) string {
if p == nil {
return Absent
}
return Str(*p)
}
func Int[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64](v T) string {
return strconv.FormatInt(int64(v), 10)
}
// IntPtr renders an optional integer, which is the shape to prefer: it separates
// "the controller said zero" from "the controller said nothing".
func IntPtr[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64](p *T) string {
if p == nil {
return Absent
}
return Int(*p)
}
// FloatPtr renders an optional decimal in the fewest digits that read back as the same value.
func FloatPtr(p *float64) string {
if p == nil {
return Absent
}
return strconv.FormatFloat(*p, 'f', -1, 64)
}
// UnitPtr renders an optional integer with its unit glued to the number, so the cell stays one
// whitespace-delimited field. An unreported value is Absent with no unit, because "-dBm" would
// read as a measurement rather than the lack of one.
func UnitPtr[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64](
p *T, unit string,
) string {
if p == nil {
return Absent
}
return Int(*p) + unit
}
// Bool renders an optional boolean as Yes or No. A nil is Absent rather than No:
// the controller omits some of these leaves, and reading that as No states a fact
// the controller did not.
func Bool(p *bool) string {
switch {
case p == nil:
return Absent
case *p:
return "Yes"
default:
return "No"
}
}
// IEC renders an octet count in binary units. The JSON output carries the raw
// count, so this affects the table alone.
func IEC(p *uint64) string {
if p == nil {
return Absent
}
n := float64(*p)
if n < iecBase {
return strconv.FormatUint(*p, 10) + "B"
}
unit := ""
for _, u := range iecUnits {
n /= iecBase
unit = u
if n < iecBase {
break
}
}
return strconv.FormatFloat(n, 'f', 1, 64) + unit + "B"
}
// Duration renders a second count as the two largest non-zero units, which
// makes an uptime of weeks and an association of minutes both readable in one
// column. The JSON output carries the raw seconds.
func Duration(p *int64) string {
if p == nil {
return Absent
}
s := *p
if s < 0 {
return Absent
}
var b strings.Builder
switch {
case s >= secondsPerDay:
writeUnit(&b, s/secondsPerDay, "d")
writeUnit(&b, (s%secondsPerDay)/secondsPerHour, "h")
case s >= secondsPerHour:
writeUnit(&b, s/secondsPerHour, "h")
writeUnit(&b, (s%secondsPerHour)/secondsPerMin, "m")
case s >= secondsPerMin:
writeUnit(&b, s/secondsPerMin, "m")
writeUnit(&b, s%secondsPerMin, "s")
default:
writeUnit(&b, s, "s")
}
return b.String()
}
func writeUnit(b *strings.Builder, v int64, unit string) {
if v == 0 && b.Len() > 0 {
return
}
b.WriteString(strconv.FormatInt(v, 10))
b.WriteString(unit)
}
// Join renders a list cell. An empty list is Absent rather than an empty cell.
func Join(items []string, sep string) string {
if len(items) == 0 {
return Absent
}
return strings.Join(items, sep)
}
// SecondsSince converts a controller timestamp into an age in seconds. A zero time
// and the Unix epoch both mean the controller reported no instant. A controller
// returns the epoch on several sibling timestamp leaves, and an age computed from
// it would read as fifty-six years.
func SecondsSince(now, t time.Time) *int64 {
if t.IsZero() || t.Unix() <= 0 {
return nil
}
s := int64(now.Sub(t).Seconds())
if s < 0 {
return nil
}
return &s
}