Skip to content

DynamoDBAttributeValue.String() accessor method inconsistent with documented behavior #510

Description

@TylerHendrickson

Problem

The documented behavior for the DynamoDBAttributeValue.String() method, 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:

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.:

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

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