From 4beb96e1be0689394f0c320ee619c459c046053f Mon Sep 17 00:00:00 2001 From: Yorhel Date: Sun, 8 Apr 2012 20:59:05 +0200 Subject: El(): Allow Element{} and bool + panic on uint64->int64 overflow --- tuples.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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)} -- cgit v1.2.3