From 46ca97c6c055eff6933944505f52826979ce5bb6 Mon Sep 17 00:00:00 2001 From: Bryan Moffatt Date: Tue, 26 Jul 2022 14:43:20 -0700 Subject: [PATCH] dynamodb: add alternative to Integer conversion helper, Int64, that does not silently cast float values --- events/attributevalue.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/events/attributevalue.go b/events/attributevalue.go index d702e9b1..f582d30f 100644 --- a/events/attributevalue.go +++ b/events/attributevalue.go @@ -68,6 +68,15 @@ func (av DynamoDBAttributeValue) Number() string { return av.value.(string) } +// Int64 provides access to an attribute of type Number. +// DynamoDB sends the values as strings. For convenience this method +// provides conversion to int. +// Method panics if the attribute is not of type Number. +func (av DynamoDBAttributeValue) Int64() (int64, error) { + number := av.Number() + return strconv.ParseInt(number, 10, 64) +} + // Integer provides access to an attribute of type Number. // DynamoDB sends the values as strings. For convenience this method // provides conversion to int. If the value cannot be represented by @@ -76,7 +85,7 @@ func (av DynamoDBAttributeValue) Number() string { // Method panics if the attribute is not of type Number. func (av DynamoDBAttributeValue) Integer() (int64, error) { number := av.Number() - value, err := strconv.ParseInt(number, 10, 64) + value, err := av.Int64() if err == nil { return value, nil }