-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.go
140 lines (121 loc) · 2.42 KB
/
table.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
package engine
import (
"bytes"
"encoding/json"
"io"
"sort"
"strconv"
)
type Table struct {
Data []*Env
sortKey string
Chan chan *Env
}
func NewTable(sortKey string, sizeHint int) *Table {
return &Table{
make([]*Env, 0, sizeHint),
sortKey,
make(chan *Env),
}
}
func (t *Table) SetKey(sortKey string) {
t.sortKey = sortKey
}
func (t *Table) Add(env *Env) {
t.Data = append(t.Data, env)
}
func (t *Table) Len() int {
return len(t.Data)
}
func (t *Table) Less(a, b int) bool {
return t.lessBy(a, b, t.sortKey)
}
func (t *Table) lessBy(a, b int, by string) bool {
keyA := t.Data[a].Get(by)
keyB := t.Data[b].Get(by)
intA, errA := strconv.ParseInt(keyA, 10, 64)
intB, errB := strconv.ParseInt(keyB, 10, 64)
if errA == nil && errB == nil {
return intA < intB
}
return keyA < keyB
}
func (t *Table) Swap(a, b int) {
tmp := t.Data[a]
t.Data[a] = t.Data[b]
t.Data[b] = tmp
}
func (t *Table) Sort() {
sort.Sort(t)
}
func (t *Table) ReverseSort() {
sort.Sort(sort.Reverse(t))
}
func (t *Table) WriteListTo(dst io.Writer) (n int64, err error) {
if _, err := dst.Write([]byte{'['}); err != nil {
return -1, err
}
n = 1
for i, env := range t.Data {
bytes, err := env.WriteTo(dst)
if err != nil {
return -1, err
}
n += bytes
if i != len(t.Data)-1 {
if _, err := dst.Write([]byte{','}); err != nil {
return -1, err
}
n++
}
}
if _, err := dst.Write([]byte{']'}); err != nil {
return -1, err
}
return n + 1, nil
}
func (t *Table) ToListString() (string, error) {
buffer := bytes.NewBuffer(nil)
if _, err := t.WriteListTo(buffer); err != nil {
return "", err
}
return buffer.String(), nil
}
func (t *Table) WriteTo(dst io.Writer) (n int64, err error) {
for _, env := range t.Data {
bytes, err := env.WriteTo(dst)
if err != nil {
return -1, err
}
n += bytes
}
return n, nil
}
func (t *Table) ReadListFrom(src []byte) (n int64, err error) {
var array []interface{}
if err := json.Unmarshal(src, &array); err != nil {
return -1, err
}
for _, item := range array {
if m, ok := item.(map[string]interface{}); ok {
env := &Env{}
for key, value := range m {
env.SetAuto(key, value)
}
t.Add(env)
}
}
return int64(len(src)), nil
}
func (t *Table) ReadFrom(src io.Reader) (n int64, err error) {
decoder := NewDecoder(src)
for {
env, err := decoder.Decode()
if err == io.EOF {
return 0, nil
} else if err != nil {
return -1, err
}
t.Add(env)
}
}