From e40f08e1076338ada8017bc08b619bc1bfffebac Mon Sep 17 00:00:00 2001 From: Nick K Date: Fri, 4 Mar 2016 11:25:28 +0300 Subject: [PATCH 1/3] Added uint encoder/decoder --- values.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/values.go b/values.go index 662499d0..1b37c200 100644 --- a/values.go +++ b/values.go @@ -686,6 +686,26 @@ func Decode(vr *ValueReader, d interface{}) error { *v = decodeInt2(vr) case *int32: *v = decodeInt4(vr) + case *uint32: + switch vr.Type().DataType { + case Int2Oid: + *v = uint32(decodeInt2(vr)) + case Int4Oid: + *v = uint32(decodeInt4(vr)) + default: + return fmt.Errorf("Can't convert OID %v to uint32", vr.Type().DataType) + } + case *uint64: + switch vr.Type().DataType { + case Int2Oid: + *v = uint64(decodeInt2(vr)) + case Int4Oid: + *v = uint64(decodeInt4(vr)) + case Int8Oid: + *v = uint64(decodeInt8(vr)) + default: + return fmt.Errorf("Can't convert OID %v to uint64", vr.Type().DataType) + } case *Oid: *v = decodeOid(vr) case *string: @@ -1009,6 +1029,7 @@ func encodeUInt64(w *WriteBuf, oid Oid, value uint64) error { return fmt.Errorf("%d is larger than max int32 %d", value, math.MaxInt32) } case Int8Oid: + if value <= math.MaxInt64 { w.WriteInt32(8) w.WriteInt64(int64(value)) From 0ea1a5245cdcbf4f309a50a1255e470ca4769bd1 Mon Sep 17 00:00:00 2001 From: Nick K Date: Fri, 11 Mar 2016 11:52:21 +0300 Subject: [PATCH 2/3] Now throwing errors on uint32/uint64 possible data loss --- values.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/values.go b/values.go index 1b37c200..143ed604 100644 --- a/values.go +++ b/values.go @@ -687,25 +687,35 @@ func Decode(vr *ValueReader, d interface{}) error { case *int32: *v = decodeInt4(vr) case *uint32: + var valInt int32 switch vr.Type().DataType { case Int2Oid: - *v = uint32(decodeInt2(vr)) + valInt = int32(decodeInt2(vr)) case Int4Oid: - *v = uint32(decodeInt4(vr)) + valInt = decodeInt4(vr) default: return fmt.Errorf("Can't convert OID %v to uint32", vr.Type().DataType) } + if valInt < 0 { + return fmt.Errorf("%d is less than zero for uint32", valInt) + } + *v = uint32(valInt) case *uint64: + var valInt int64 switch vr.Type().DataType { case Int2Oid: - *v = uint64(decodeInt2(vr)) + valInt = int64(decodeInt2(vr)) case Int4Oid: - *v = uint64(decodeInt4(vr)) + valInt = int64(decodeInt4(vr)) case Int8Oid: - *v = uint64(decodeInt8(vr)) + valInt = decodeInt8(vr) default: return fmt.Errorf("Can't convert OID %v to uint64", vr.Type().DataType) } + if valInt < 0 { + return fmt.Errorf("%d is less than zero for uint32", valInt) + } + *v = uint64(valInt) case *Oid: *v = decodeOid(vr) case *string: From 7f9f79656b26cd4b77c8b35993c7f4b0ca4f9f00 Mon Sep 17 00:00:00 2001 From: Nick K Date: Fri, 11 Mar 2016 11:54:07 +0300 Subject: [PATCH 3/3] Fixed typo for uint64 data loss checks' error --- values.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/values.go b/values.go index 143ed604..5a3a7d7a 100644 --- a/values.go +++ b/values.go @@ -713,7 +713,7 @@ func Decode(vr *ValueReader, d interface{}) error { return fmt.Errorf("Can't convert OID %v to uint64", vr.Type().DataType) } if valInt < 0 { - return fmt.Errorf("%d is less than zero for uint32", valInt) + return fmt.Errorf("%d is less than zero for uint64", valInt) } *v = uint64(valInt) case *Oid: