-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatters.go
213 lines (174 loc) · 4.6 KB
/
formatters.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2022, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package goform
import (
"encoding/json"
"fmt"
"strings"
"github.com/dustin/go-humanize"
"github.com/tidwall/gjson"
)
func (f *formatter) getDataItem(data json.RawMessage, item string) (gjson.Result, error) {
var sum bool
if strings.HasPrefix(item, "report.summary.") {
// we place items in with . so gjson needs us to escape that dot. So report.summary.x.y becomes report.summary.x\.y
item = "report.summary." + strings.ReplaceAll(strings.TrimPrefix(item, "report.summary."), ".", "\\.")
sum = true
}
val := gjson.GetBytes(data, item)
if (!val.Exists() || val.Type == gjson.Null) && sum {
val = gjson.Parse("0")
}
return val, nil
}
func formatDataItem(val gjson.Result, format string) (string, error) {
if !val.Exists() || val.Type == gjson.Null {
return "?" + strings.Repeat(" ", len(format)-1), nil
}
switch val.Type {
case gjson.String:
return formatString(val.String(), format)
case gjson.Number:
if strings.Contains(format, ".") {
return formatNumber(val.Float(), format)
} else {
return formatNumber(val.Int(), format)
}
case gjson.True, gjson.False:
return formatString(val.String(), format)
default:
return "", fmt.Errorf("invalid type %s", val.Type.String())
}
}
func formatString(v string, format string) (string, error) {
if len(format) == 0 {
return "", fmt.Errorf("invalid format: %s", format)
}
// only up to first new line
if strings.Contains(v, "\n") {
parts := strings.Split(v, "\n")
v = parts[0]
}
makeSuf := func(v string) string {
suf := string(format[len(format)-1])
if strings.ContainsAny(suf, "<>|") {
return v
}
if len(v) >= len(format) {
v = v[0 : len(format)-1]
}
return v + suf
}
lv := len(v)
lf := len(format)
// its already the full length no formatting needed
if lv == lf {
return makeSuf(v), nil
}
// if its longer we just take up to the length
if lv > lf {
return makeSuf(v[0:lf]), nil
}
switch format[1] {
case '|':
v = makeSuf(v)
lv = len(v)
leftPad := strings.Repeat(" ", (lf-lv)/2)
rightPad := strings.Repeat(" ", lf-len(leftPad)-lv)
return leftPad + v + rightPad, nil
case '>':
v = makeSuf(v)
lv = len(v)
return strings.Repeat(" ", lf-lv) + v, nil
case '<':
v = makeSuf(v)
lv = len(v)
return v + strings.Repeat(" ", lf-lv), nil
default:
return "", fmt.Errorf("invalid string format: %s", format)
}
}
func formatNumber(v any, format string) (string, error) {
var val string
var err error
switch {
case format[1] == '0' || format[1] == '#' || strings.Contains(format, "."):
val, err = formatFloat(v, format)
case format[1] == ',':
val, err = formatCommaNumber(v, format)
case format[1] == 'B':
val, err = formatBytesNumber(v, format)
default:
return "", fmt.Errorf("unknown number format: %v", format)
}
if err != nil {
return "", err
}
if len(val) == 0 {
return strings.Repeat(" ", len(format)), nil
}
if len(val) > len(format) {
return strings.Repeat("#", len(format)), nil
}
if len(val) < len(format) {
if format[1] == '0' {
val = strings.Repeat("0", len(format)-len(val)) + val
} else {
val = val + strings.Repeat(" ", len(format)-len(val))
}
}
return val, nil
}
func formatFloat(v any, format string) (string, error) {
parts := strings.Split(format, ".")
lp := len(parts)
var sfmt string
switch {
case lp == 1:
sfmt = "%d"
case lp == 2:
sfmt = fmt.Sprintf("%%%d.%df", len(parts[0]), len(parts[1]))
default:
return "", fmt.Errorf("invalid number format: %s", format)
}
return fmt.Sprintf(sfmt, v), nil
}
func formatCommaNumber(v any, format string) (string, error) {
switch nv := v.(type) {
case float64:
return humanize.Commaf(nv), nil
case float32:
return humanize.Commaf(float64(nv)), nil
case int:
return humanize.Comma(int64(nv)), nil
case int8:
return humanize.Comma(int64(nv)), nil
case int16:
return humanize.Comma(int64(nv)), nil
case int32:
return humanize.Comma(int64(nv)), nil
case int64:
return humanize.Comma(int64(nv)), nil
default:
return "", fmt.Errorf("do not know how to handle value %v", v)
}
}
func formatBytesNumber(v any, format string) (string, error) {
switch nv := v.(type) {
case float64:
return humanize.IBytes(uint64(nv)), nil
case int:
return humanize.IBytes(uint64(nv)), nil
case int8:
return humanize.IBytes(uint64(nv)), nil
case int16:
return humanize.IBytes(uint64(nv)), nil
case int32:
return humanize.IBytes(uint64(nv)), nil
case int64:
return humanize.IBytes(uint64(nv)), nil
default:
return "", fmt.Errorf("do not know how to handle value %v", v)
}
}