summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2012-04-08 20:59:58 +0200
committerYorhel <git@yorhel.nl>2012-04-08 21:02:56 +0200
commit03a7031997c45b966e0a302d13a9fae619c05c3a (patch)
treee0403cf786b44891bcea9d93f3720767102f9163
parent4beb96e1be0689394f0c320ee619c459c046053f (diff)
Element: Added Bool(), IsFloat(), IsString() + swapped IsInt() return value
-rw-r--r--tuples.go68
1 files changed, 46 insertions, 22 deletions
diff --git a/tuples.go b/tuples.go
index c312471..4478c5f 100644
--- a/tuples.go
+++ b/tuples.go
@@ -12,58 +12,82 @@ type Element struct {
type Tuple []Element
-func (e Element) IsInt() (bool, int64) {
+func (e Element) IsInt() (int64, bool) {
switch v := e.E.(type) {
case int64:
- return true, v
+ return v, true
case string:
n, r := ParseInt(v, 10, 64)
if r != nil {
- return false, 0
+ return 0, false
} else {
- return true, n
+ return n, true
}
case float64:
- return true, int64(v)
+ return int64(v), true
}
- return false, 0
+ return 0, false
}
// Just returns 0 on error
func (e Element) Int() int64 {
- _, i := e.IsInt()
+ i, _ := e.IsInt()
return i
}
-// Returns empty string on error
-func (e Element) String() string {
+func (e Element) IsString() (string, bool) {
switch v := e.E.(type) {
case int64:
- return FormatInt(v, 10)
+ return FormatInt(v, 10), true
case string:
- return v
+ return v, true
case float64:
- return FormatFloat(v, 'g', -1, 64)
+ return FormatFloat(v, 'g', -1, 64), true
}
- return ""
+ return "", false
}
-// Returns 0 on error
-func (e Element) Float() float64 {
+// Just returns empty string on error
+func (e Element) String() string {
+ s, _ := e.IsString()
+ return s
+}
+
+func (e Element) IsFloat() (float64, bool) {
switch v := e.E.(type) {
case int64:
- return float64(v)
+ return float64(v), true
case string:
n, r := ParseFloat(v, 64)
if r != nil {
- return 0
+ return 0, false
} else {
- return n
+ return n, true
}
case float64:
- return v
+ return v, true
+ }
+ return 0, false
+}
+
+// Just returns 0 on error
+func (e Element) Float() float64 {
+ f, _ := e.IsFloat()
+ return f
+}
+
+func (e Element) Bool() bool {
+ switch v := e.E.(type) {
+ case nil:
+ return false
+ case int64:
+ return v != 0
+ case float64:
+ return v != 0
+ case string:
+ return !(v == "" || v == "0")
}
- return 0
+ return true
}
// Whether this is a wildcard or not
@@ -93,8 +117,8 @@ func (a Element) Match(b Element) bool {
return true
}
// Fuzzy int matching
- if ca, ia := a.IsInt(); ca {
- if cb, ib := b.IsInt(); cb && ia == ib {
+ if ia, ca := a.IsInt(); ca {
+ if ib, cb := b.IsInt(); cb && ia == ib {
return true
}
}