forked from go-gota/gota
/
type-string.go
179 lines (156 loc) · 2.94 KB
/
type-string.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
package series
import (
"fmt"
"math"
"strconv"
"strings"
"time"
)
type stringElement struct {
e *string
}
func (e stringElement) Set(value interface{}) Element {
var val string
switch value.(type) {
case string:
val = value.(string)
if val == "NaN" {
e.e = nil
return e
}
case int:
val = strconv.Itoa(value.(int))
case float64:
val = strconv.FormatFloat(value.(float64), 'f', 6, 64)
case bool:
b := value.(bool)
if b {
val = "true"
} else {
val = "false"
}
case Element:
val = value.(Element).String()
default:
e.e = nil
return e
}
e.e = &val
return e
}
func (e stringElement) Copy() Element {
if e.e == nil {
return stringElement{nil}
}
copyValue := *e.e
return stringElement{©Value}
}
func (e stringElement) IsNA() bool {
if e.e == nil {
return true
}
nanvalue := []string{"NA", "NaN", "<nil>"}
if findInStringSlice(*e.e, nanvalue) != -1 {
return true
}
return false
}
func (e stringElement) Type() Type {
return String
}
func (e stringElement) Val() ElementValue {
if e.IsNA() {
return nil
}
return *e.e
}
func (e stringElement) String() string {
if e.e == nil {
return "NaN"
}
return *e.e
}
func (e stringElement) Int() (int, error) {
if e.IsNA() {
return 0, fmt.Errorf("can't convert NaN to int")
}
return strconv.Atoi(*e.e)
}
func (e stringElement) Float() float64 {
if e.IsNA() {
return math.NaN()
}
f, err := strconv.ParseFloat(*e.e, 64)
if err != nil {
return math.NaN()
}
return f
}
func (e stringElement) Bool() (bool, error) {
if e.IsNA() {
return false, fmt.Errorf("can't convert NaN to bool")
}
switch strings.ToLower(*e.e) {
case "true", "t", "1":
return true, nil
case "false", "f", "0":
return false, nil
}
return false, fmt.Errorf("can't convert String \"%v\" to bool", *e.e)
}
func (e stringElement) Time() (time.Time, error) {
if e.IsNA() {
return time.Now(), fmt.Errorf("can't convert NaN to time")
}
return DefaultDatetimeParser(*e.e)
}
func (e stringElement) Addr() string {
return fmt.Sprint(e.e)
}
func (e stringElement) Eq(elem Element) bool {
if e.IsNA() && elem.IsNA() {
return true
}
if e.IsNA() && !elem.IsNA() {
return false
}
if !e.IsNA() && elem.IsNA() {
return false
}
return *e.e == elem.String()
}
func (e stringElement) Neq(elem Element) bool {
return !e.Eq(elem)
}
func (e stringElement) Less(elem Element) bool {
if e.IsNA() || elem.IsNA() {
return false
}
return *e.e < elem.String()
}
func (e stringElement) LessEq(elem Element) bool {
if e.IsNA() || elem.IsNA() {
return false
}
return *e.e <= elem.String()
}
func (e stringElement) Greater(elem Element) bool {
if e.IsNA() || elem.IsNA() {
return false
}
return *e.e > elem.String()
}
func (e stringElement) GreaterEq(elem Element) bool {
if e.IsNA() || elem.IsNA() {
return false
}
return *e.e >= elem.String()
}
func findInStringSlice(str string, s []string) int {
for i, e := range s {
if e == str {
return i
}
}
return -1
}