-
Notifications
You must be signed in to change notification settings - Fork 0
/
lengths.go
58 lines (48 loc) · 1.3 KB
/
lengths.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
package tablify
import (
"reflect"
"strconv"
)
func lengths(values []interface{}) reflect.Value {
// Convert to struct
s := reflect.ValueOf(values[0])
fieldCount := s.NumField()
fs := make([]reflect.StructField, fieldCount)
for i := 0; i < fieldCount; i++ {
n := s.Type().Field(i).Name
fs[i] = reflect.StructField{Name: n, Type: reflect.TypeOf(1)}
}
typ := reflect.StructOf(fs)
fin := reflect.New(typ).Elem()
// Set the initial value to be the length of the field name + 2
for i := 0; i < fieldCount; i++ {
n, p := fieldTitle(s.Type().Field(i))
fin.FieldByName(n).SetInt(int64(len(p) + 2))
}
// Iterate through field values and set the length to be the length of it, if greater
// for i := 0; i < values.
for _, v := range values {
for i := 0; i < fieldCount; i++ {
val := reflect.ValueOf(v)
k := val.Type().Field(i).Name
var valLen int64
switch val.Field(i).Kind() {
case reflect.String:
valLen = int64(len(val.Field(i).String()) + 2)
case reflect.Int:
valLen = int64(len(strconv.Itoa(int(val.Field(i).Int()))) + 2)
case reflect.Bool:
f := val.Field(i).Bool()
if f {
valLen = 6 // ` True `
} else {
valLen = 7 // ` False `
}
}
if valLen > fin.FieldByName(k).Int() {
fin.FieldByName(k).SetInt(valLen)
}
}
}
return fin
}