summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2012-04-08 20:59:05 +0200
committerYorhel <git@yorhel.nl>2012-04-08 20:59:05 +0200
commit4beb96e1be0689394f0c320ee619c459c046053f (patch)
treedb4e76250763606c626f84035474a849619657af
parent2418c30f312d9bdce6392215006b9431d191cace (diff)
El(): Allow Element{} and bool + panic on uint64->int64 overflow
-rw-r--r--tuples.go14
1 files changed, 13 insertions, 1 deletions
diff --git a/tuples.go b/tuples.go
index cd53266..c312471 100644
--- a/tuples.go
+++ b/tuples.go
@@ -144,7 +144,7 @@ func (e *Element) UnmarshalJSON(b []byte) (err error) {
return
}
-// Mostly the same as Element{e}, but also converts any int or float type to an
+// Mostly the same as Element{e}, but also converts any int/bool or float type to an
// int64 or float64, respectively.
// Throws a runtime panic if an unsupported type is given.
// TODO: Also convert composite types into arrays/maps?
@@ -153,7 +153,16 @@ func El(e interface{}) Element {
// Core elements
case nil, string, int64, float64, []Element, map[string]Element:
return Element{v}
+ // Already an element
+ case Element:
+ return v
// Conversions
+ case bool:
+ if v {
+ return Element{int64(1)}
+ } else {
+ return Element{int64(0)}
+ }
case int:
return Element{int64(v)}
case int8:
@@ -171,6 +180,9 @@ func El(e interface{}) Element {
case uint32:
return Element{int64(v)}
case uint64:
+ if v > 1<<63-1 {
+ panic("Value out of range")
+ }
return Element{int64(v)}
case float32:
return Element{float64(v)}