Skip to content

Commit ff668fd

Browse files
committed
dataframe: support other integer types
1 parent 70365cb commit ff668fd

File tree

7 files changed

+75
-38
lines changed

7 files changed

+75
-38
lines changed

column.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ type Column interface {
8080
// SortByStringDescending sorts Column in string descending order.
8181
SortByStringDescending()
8282

83-
// SortByNumberAscending sorts Column in number(float) ascending order.
84-
SortByNumberAscending()
83+
// SortByFloat64Ascending sorts Column in number(float) ascending order.
84+
SortByFloat64Ascending()
8585

86-
// SortByNumberDescending sorts Column in number(float) descending order.
87-
SortByNumberDescending()
86+
// SortByFloat64Descending sorts Column in number(float) descending order.
87+
SortByFloat64Descending()
8888

8989
// SortByDurationAscending sorts Column in time.Duration ascending order.
9090
SortByDurationAscending()
@@ -391,7 +391,7 @@ func (c *column) Copy() Column {
391391

392392
func (c *column) SortByStringAscending() { sort.Sort(ByStringAscending(c.data)) }
393393
func (c *column) SortByStringDescending() { sort.Sort(ByStringDescending(c.data)) }
394-
func (c *column) SortByNumberAscending() { sort.Sort(ByNumberAscending(c.data)) }
395-
func (c *column) SortByNumberDescending() { sort.Sort(ByNumberDescending(c.data)) }
394+
func (c *column) SortByFloat64Ascending() { sort.Sort(ByFloat64Ascending(c.data)) }
395+
func (c *column) SortByFloat64Descending() { sort.Sort(ByFloat64Descending(c.data)) }
396396
func (c *column) SortByDurationAscending() { sort.Sort(ByDurationAscending(c.data)) }
397397
func (c *column) SortByDurationDescending() { sort.Sort(ByDurationDescending(c.data)) }

column_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,12 @@ func TestColumnSortByStringDescending(t *testing.T) {
268268
}
269269
}
270270

271-
func TestColumnSortByNumberAscending(t *testing.T) {
271+
func TestColumnSortByFloat64Ascending(t *testing.T) {
272272
c := NewColumn("column")
273273
for i := 0; i < 100; i++ {
274274
c.PushBack(NewStringValue(i))
275275
}
276-
c.SortByNumberAscending()
276+
c.SortByFloat64Ascending()
277277
fv, err := c.Value(0)
278278
if err != nil {
279279
t.Fatal(err)
@@ -283,13 +283,13 @@ func TestColumnSortByNumberAscending(t *testing.T) {
283283
}
284284
}
285285

286-
func TestColumnSortByNumberDescending(t *testing.T) {
286+
func TestColumnSortByFloat64Descending(t *testing.T) {
287287
c := NewColumn("column")
288288
for i := 0; i < 100; i++ {
289289
c.PushBack(NewStringValue(i))
290290
}
291291
c.PushBack(NewStringValue("199.9"))
292-
c.SortByNumberDescending()
292+
c.SortByFloat64Descending()
293293
fv, err := c.Value(0)
294294
if err != nil {
295295
t.Fatal(err)

dataframe.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,13 @@ func (f *frame) Sort(header string, st SortType, so SortOption) error {
469469
lesses = []LessFunc{StringDescendingFunc(idx)}
470470
}
471471

472-
case SortType_Number:
472+
case SortType_Float64:
473473
switch so {
474474
case SortOption_Ascending:
475-
lesses = []LessFunc{NumberAscendingFunc(idx)}
475+
lesses = []LessFunc{Float64AscendingFunc(idx)}
476476

477477
case SortOption_Descending:
478-
lesses = []LessFunc{NumberDescendingFunc(idx)}
478+
lesses = []LessFunc{Float64DescendingFunc(idx)}
479479
}
480480

481481
case SortType_Duration:

dataframe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func TestSort(t *testing.T) {
418418
if err != nil {
419419
t.Fatal(err)
420420
}
421-
if err := fr.Sort("second", SortType_Number, SortOption_Descending); err != nil {
421+
if err := fr.Sort("second", SortType_Float64, SortOption_Descending); err != nil {
422422
t.Fatal(err)
423423
}
424424
fpath := "test.csv"

sorter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type (
1313

1414
const (
1515
SortType_String SortType = iota
16-
SortType_Number
16+
SortType_Float64
1717
SortType_Duration
1818
)
1919

@@ -45,7 +45,7 @@ func StringDescendingFunc(idx int) func(row1, row2 *[]string) bool {
4545
}
4646
}
4747

48-
func NumberAscendingFunc(idx int) func(row1, row2 *[]string) bool {
48+
func Float64AscendingFunc(idx int) func(row1, row2 *[]string) bool {
4949
return func(row1, row2 *[]string) bool {
5050
v1s := (*row1)[idx]
5151
v1, _ := strconv.ParseFloat(v1s, 64)
@@ -55,7 +55,7 @@ func NumberAscendingFunc(idx int) func(row1, row2 *[]string) bool {
5555
}
5656
}
5757

58-
func NumberDescendingFunc(idx int) func(row1, row2 *[]string) bool {
58+
func Float64DescendingFunc(idx int) func(row1, row2 *[]string) bool {
5959
return func(row1, row2 *[]string) bool {
6060
v1s := (*row1)[idx]
6161
v1, _ := strconv.ParseFloat(v1s, 64)

value.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ type Value interface {
1111
// String parses Value to string. It returns false if not possible.
1212
String() (string, bool)
1313

14-
// Number parses Value to float64. It returns false if not possible.
15-
Number() (float64, bool)
14+
// Int64 parses Value to int64. It returns false if not possible.
15+
Int64() (int64, bool)
16+
17+
// Uint64 parses Value to uint64. It returns false if not possible.
18+
Uint64() (uint64, bool)
19+
20+
// Float64 parses Value to float64. It returns false if not possible.
21+
Float64() (float64, bool)
1622

1723
// Time parses Value to time.Time based on the layout. It returns false if not possible.
1824
Time(layout string) (time.Time, bool)
@@ -30,6 +36,7 @@ type Value interface {
3036
Copy() Value
3137
}
3238

39+
// NewStringValue takes any interface and returns Value.
3340
func NewStringValue(v interface{}) Value {
3441
switch t := v.(type) {
3542
case string:
@@ -54,17 +61,29 @@ func NewStringValue(v interface{}) Value {
5461
return nil
5562
}
5663

64+
// NewStringValueNil returns an empty value.
5765
func NewStringValueNil() Value {
5866
return String("")
5967
}
6068

69+
// String defines string data types.
6170
type String string
6271

6372
func (s String) String() (string, bool) {
6473
return string(s), true
6574
}
6675

67-
func (s String) Number() (float64, bool) {
76+
func (s String) Int64() (int64, bool) {
77+
iv, err := strconv.ParseInt(string(s), 10, 64)
78+
return iv, err == nil
79+
}
80+
81+
func (s String) Uint64() (uint64, bool) {
82+
iv, err := strconv.ParseUint(string(s), 10, 64)
83+
return iv, err == nil
84+
}
85+
86+
func (s String) Float64() (float64, bool) {
6887
f, err := strconv.ParseFloat(string(s), 64)
6988
return f, err == nil
7089
}
@@ -124,35 +143,35 @@ func (vs ByStringDescending) Less(i, j int) bool {
124143
return vs1 > vs2
125144
}
126145

127-
type ByNumberAscending []Value
146+
type ByFloat64Ascending []Value
128147

129-
func (vs ByNumberAscending) Len() int {
148+
func (vs ByFloat64Ascending) Len() int {
130149
return len(vs)
131150
}
132151

133-
func (vs ByNumberAscending) Swap(i, j int) {
152+
func (vs ByFloat64Ascending) Swap(i, j int) {
134153
vs[i], vs[j] = vs[j], vs[i]
135154
}
136155

137-
func (vs ByNumberAscending) Less(i, j int) bool {
138-
vs1, _ := vs[i].Number()
139-
vs2, _ := vs[j].Number()
156+
func (vs ByFloat64Ascending) Less(i, j int) bool {
157+
vs1, _ := vs[i].Float64()
158+
vs2, _ := vs[j].Float64()
140159
return vs1 < vs2
141160
}
142161

143-
type ByNumberDescending []Value
162+
type ByFloat64Descending []Value
144163

145-
func (vs ByNumberDescending) Len() int {
164+
func (vs ByFloat64Descending) Len() int {
146165
return len(vs)
147166
}
148167

149-
func (vs ByNumberDescending) Swap(i, j int) {
168+
func (vs ByFloat64Descending) Swap(i, j int) {
150169
vs[i], vs[j] = vs[j], vs[i]
151170
}
152171

153-
func (vs ByNumberDescending) Less(i, j int) bool {
154-
vs1, _ := vs[i].Number()
155-
vs2, _ := vs[j].Number()
172+
func (vs ByFloat64Descending) Less(i, j int) bool {
173+
vs1, _ := vs[i].Float64()
174+
vs2, _ := vs[j].Float64()
156175
return vs1 > vs2
157176
}
158177

value_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99

1010
func TestValue(t *testing.T) {
1111
v1 := NewStringValue("1")
12-
if v, ok := v1.Number(); !ok {
12+
if v, ok := v1.Float64(); !ok {
1313
t.Fatalf("expected number 1, got %v", v)
1414
}
1515

1616
v2 := NewStringValue("2.2")
17-
if v, ok := v2.Number(); !ok || v != 2.2 {
17+
if v, ok := v2.Float64(); !ok || v != 2.2 {
1818
t.Fatalf("expected number 2.2, got %v", v)
1919
}
2020

@@ -40,6 +40,24 @@ func TestValue(t *testing.T) {
4040
}
4141
}
4242

43+
func TestValueInt(t *testing.T) {
44+
v := NewStringValue("1")
45+
fv, ok := v.Float64()
46+
if !ok || fv != 1.0 {
47+
t.Fatalf("expected number 1, got %f(%v)", fv, v)
48+
}
49+
50+
iv, ok := v.Int64()
51+
if !ok || iv != 1 {
52+
t.Fatalf("expected number 1, got %d(%v)", iv, v)
53+
}
54+
55+
uv, ok := v.Uint64()
56+
if !ok || uv != 1 {
57+
t.Fatalf("expected number 1, got %d(%v)", uv, v)
58+
}
59+
}
60+
4361
func TestNewStringValueNil(t *testing.T) {
4462
v := NewStringValueNil()
4563
if !v.IsNil() {
@@ -69,24 +87,24 @@ func TestByStringDescending(t *testing.T) {
6987
}
7088
}
7189

72-
func TestByNumberAscending(t *testing.T) {
90+
func TestByFloat64Ascending(t *testing.T) {
7391
vs := []Value{}
7492
for i := 0; i < 100; i++ {
7593
vs = append(vs, NewStringValue(fmt.Sprintf("%d", i)))
7694
}
77-
sort.Sort(ByNumberAscending(vs))
95+
sort.Sort(ByFloat64Ascending(vs))
7896
if !vs[0].EqualTo(NewStringValue("0")) {
7997
t.Fatalf("expected '0', got %v", vs[0])
8098
}
8199
}
82100

83-
func TestByNumberDescending(t *testing.T) {
101+
func TestByFloat64Descending(t *testing.T) {
84102
vs := []Value{}
85103
for i := 0; i < 100; i++ {
86104
vs = append(vs, NewStringValue(fmt.Sprintf("%d", i)))
87105
}
88106
vs = append(vs, NewStringValue("199.9"))
89-
sort.Sort(ByNumberDescending(vs))
107+
sort.Sort(ByFloat64Descending(vs))
90108
if !vs[0].EqualTo(NewStringValue("199.9")) {
91109
t.Fatalf("expected '199.9', got %v", vs[0])
92110
}

0 commit comments

Comments
 (0)