Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions events/attributevalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ func (av DynamoDBAttributeValue) NumberSet() []string {
// String provides access to an attribute of type String.
// Method panics if the attribute is not of type String.
func (av DynamoDBAttributeValue) String() string {
if av.dataType == DataTypeString {
return av.value.(string)
}
// If dataType is not DataTypeString during fmt.Sprintf("%#v", ...)
// compiler confuses with fmt.Stringer interface and panics
// instead of printing the struct.
return fmt.Sprintf("%v", dynamoDbAttributeValue(av))
av.ensureType(DataTypeString)
return av.value.(string)
}

// GoString formats the attribute value without calling the String accessor.
func (av DynamoDBAttributeValue) GoString() string {
return fmt.Sprintf("%#v", dynamoDbAttributeValue(av))
}

// StringSet provides access to an attribute of type String Set.
Expand Down
11 changes: 11 additions & 0 deletions events/attributevalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package events
import (
"encoding/base64"
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -206,6 +207,7 @@ func TestAccessWithWrongTypePanics(t *testing.T) {
{`{ "N": "123.45"}`, func(av DynamoDBAttributeValue) { av.Boolean() }, IncompatibleDynamoDBTypeError{Requested: DataTypeBoolean, Actual: DataTypeNumber}},
{`{ "NS": ["1234", "567.8"] }`, func(av DynamoDBAttributeValue) { av.Boolean() }, IncompatibleDynamoDBTypeError{Requested: DataTypeBoolean, Actual: DataTypeNumberSet}},
{`{ "NULL": true}`, func(av DynamoDBAttributeValue) { av.Number() }, IncompatibleDynamoDBTypeError{Requested: DataTypeNumber, Actual: DataTypeNull}},
{`{ "BOOL": true}`, func(av DynamoDBAttributeValue) { _ = av.String() }, IncompatibleDynamoDBTypeError{Requested: DataTypeString, Actual: DataTypeBoolean}},
{`{ "S": "Hello"}`, func(av DynamoDBAttributeValue) { av.Number() }, IncompatibleDynamoDBTypeError{Requested: DataTypeNumber, Actual: DataTypeString}},
{`{ "SS": [ "Giraffe", "Zebra" ] }`, func(av DynamoDBAttributeValue) { av.Number() }, IncompatibleDynamoDBTypeError{Requested: DataTypeNumber, Actual: DataTypeStringSet}},
}
Expand All @@ -219,6 +221,15 @@ func TestAccessWithWrongTypePanics(t *testing.T) {
}
}

func TestDynamoDBAttributeValueGoStringDoesNotCallStringAccessor(t *testing.T) {
var av DynamoDBAttributeValue
err := json.Unmarshal([]byte(`{ "BOOL": true}`), &av)
assert.Nil(t, err)
formatted := ""
assert.NotPanics(t, func() { formatted = fmt.Sprintf("%#v", av) })
assert.NotEmpty(t, formatted)
}

func TestMarshalAndUnmarshalString(t *testing.T) {
const inputString = "INPUT STRING"
inputValue := NewStringAttribute(inputString)
Expand Down