-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.go
More file actions
112 lines (97 loc) · 2.53 KB
/
Copy pathcommon.go
File metadata and controls
112 lines (97 loc) · 2.53 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
package common
import (
"fmt"
"strconv"
)
// Devuelve los índices de las columnas seleccionadas en
// los argumentos, según el índice en las columnas.
func Filtrados(argumentos []string, columnas []string) ([]int, error) {
salida := make([]int, len(argumentos))
for k, v := range argumentos {
encontrado := false
for k2, v2 := range columnas {
if v == v2 {
encontrado = true
salida[k] = k2
break
}
}
// Como no se encontró puede ser que el argumento
// fuera el número de columna
if !encontrado {
num, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return nil, err
}
if num < 0 || int(num) > len(columnas) {
return nil, fmt.Errorf("Column number %d is out of range",
num)
}
salida[k] = int(num)
}
}
return salida, nil
}
// Devuelve las columnas seleccionadas
func Seleccionar(seleccionados []int, entradas []string) []string {
salida := make([]string, len(seleccionados))
for k, v := range seleccionados {
salida[k] = entradas[v]
}
return salida
}
func GetValue(col string, datos, columns []string) string {
for k, v := range columns {
if v == col {
return datos[k]
}
}
panic(fmt.Errorf("%v column not found", col))
}
type TableData struct {
Value string
Data []string
MultiValue []string
}
type ByValue []TableData
func (a ByValue) Len() int { return len(a) }
func (a ByValue) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByValue) Less(i, j int) bool { return a[i].Value < a[j].Value }
type ByMultiValue []TableData
func (a ByMultiValue) Len() int { return len(a) }
func (a ByMultiValue) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByMultiValue) Less(i, j int) bool {
for k, v := range a[i].MultiValue {
if v == a[j].MultiValue[k] {
continue
}
return v < a[j].MultiValue[k]
}
return false
}
func (a ByMultiValue) Equal(i, j int) bool {
for k, v := range a[i].MultiValue {
if v != a[j].MultiValue[k] {
return false
}
}
return true
}
func GenTable(header []string, sortBy string, data [][]string) []TableData {
salida := make([]TableData, len(data))
for k, v := range data {
salida[k] = TableData{GetValue(sortBy, v, header), v, nil}
}
return salida
}
func GenMultiValueTable(header []string, sortBy []string, data [][]string) []TableData {
salida := make([]TableData, len(data))
for k, v := range data {
claves := make([]string, len(sortBy))
for k2, v2 := range sortBy {
claves[k2] = GetValue(v2, v, header)
}
salida[k] = TableData{GetValue(sortBy[0], v, header), v, claves}
}
return salida
}