aws / aws/aws-lambda-go

DynamoDBAttributeValue.String() accessor method inconsistent with documented behavior

Open
#510 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
3.8k
Forks
578
Avg merge
8h 18m
Merged PRs (30d)
1

Description

# Problem
The [documented behavior](https://github.com/aws/aws-lambda-go/blob/771b391678d3f54bfa38531774d656f5e0f2ab58/events/attributevalue.go#L116-L117) for the [`DynamoDBAttributeValue.String()` method](https://github.com/aws/aws-lambda-go/blob/771b391678d3f54bfa38531774d656f5e0f2ab58/events/attributevalue.go#L118-L126), similar to the other accessor methods, states:
> Method panics if the attribute is not of type String.

However, this does not appear to be the case.

## Example
Using this code:
```go
func IncorrectBehaviorExample() {
input := []byte(`{"MyField": {"BOOL": true}}`)
var item map[string]events.DynamoDBAttributeValue
if err := json.Unmarshal(input, &item); err != nil {
panic(err)
}
fmt.Println("Result:", item["MyField"].String())
}
```
does not panic, but outputs `Result: {true 1}`.

This is unlike the other accessor methods, such as `.Number()`, e.g.:
```go
func CorrectBehaviorExample() {
input := []byte(`{"MyField": {"BOOL": true}}`)
var item map[string]events.DynamoDBAttributeValue
if err := json.Unmarshal(input, &item); err != nil {
panic(err)
}
fmt.Println("Result:", item["MyField"].Number())
}
```
which panics with `panic: accessor called for incompatible type, requested type 5 but actual type was 1`.

## Suggestions
Either of:
- Preferred: The method should panic if the attribute is not of type String, which is consistent with the documented behavior as well as the behavior of the other accessor methods.
- Update the documentation so that it correctly reflects the behavior of the method.

## Additional information
- Observed when using `github.com/aws/aws-lambda-go v1.41.0`
- Go `1.20`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.