Skip to content

DynamoDBAttributeValue) Integer() hides float conversion and truncation #454

@amwill04

Description

@amwill04

When converting DynamoDBAttributeValue using Integer() method it currently hides the conversion to Float64 one is passed.

This hidden functionality hides its true nature and has led to errors downstream.

We are doing this as we are having to convert from a DynamoDBAttributeValue to json and therefore could be a float or an int

Example:

package main

import (
	"fmt"

	"github.com/aws/aws-lambda-go/events"
)

func main() {
	var val interface{}
	var err error
	// We may receive a float64 or an integer
	attr := events.NewNumberAttribute("0.123344")

	val, err = attr.Integer()
	if err != nil {
		// expect an error here but Integer() will return int64(float64) truncating the decimal
		// amd hiding the error.
		// we are therefore not able to correctly cast to float64
		val, err = attr.Float()
	}

	// To JSON - We are using a map as we are not sure what the key/values will be from the dynamodb stream
	output := map[string]interface{}{
		"key": val,
	}

	b, err := json.Marshal(output)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(b))
	// Output: {"key":0}
}

It would seem more predictable behaviour for Integer() to return an error if DynamoDBAttributeValue is not of integer type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions