diff options
-rw-r--r-- | tuples.go | 68 |
1 files changed, 46 insertions, 22 deletions
@@ -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 } } |