Skip to content

Commit

Permalink
feature/dynamodb/attributevalue: Fix string alias unmarshal (#1388)
Browse files Browse the repository at this point in the history
Fixes unmarshaler's decoding of AttributeValueMemberN into a type that is a string alias.
  • Loading branch information
jasdel committed Aug 27, 2021
1 parent 3dde893 commit 2c11b4f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .changelog/1d09c67fd2aa4bb1adfbe42f8218f7a2.json
@@ -0,0 +1,8 @@
{
"id": "1d09c67f-d2aa-4bb1-adfb-e42f8218f7a2",
"type": "bugfix",
"description": "Fix unmarshaler's decoding of AttributeValueMemberN into a type that is a string alias.",
"modules": [
"feature/dynamodb/attributevalue"
]
}
2 changes: 1 addition & 1 deletion feature/dynamodb/attributevalue/decode.go
Expand Up @@ -327,7 +327,7 @@ func (d *Decoder) decodeNumber(n string, v reflect.Value, fieldTag tag) error {
v.SetString(n)
return nil
}
v.Set(reflect.ValueOf(n))
v.SetString(n)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(n, 10, 64)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions feature/dynamodb/attributevalue/decode_test.go
Expand Up @@ -723,3 +723,45 @@ func TestDecoderFieldByIndex(t *testing.T) {
t.Error("expected f to be an int with value equal to outer.Inner")
}
}
func TestDecodeAliasType(t *testing.T) {
type Str string
type Int int
type Uint uint
type TT struct {
A Str
B Int
C Uint
S Str
}

expect := TT{
A: "12345",
B: 12345,
C: 12345,
S: "string",
}
m := map[string]types.AttributeValue{
"A": &types.AttributeValueMemberN{
Value: "12345",
},
"B": &types.AttributeValueMemberN{
Value: "12345",
},
"C": &types.AttributeValueMemberN{
Value: "12345",
},
"S": &types.AttributeValueMemberS{
Value: "string",
},
}

var actual TT
err := UnmarshalMap(m, &actual)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}

if !reflect.DeepEqual(expect, actual) {
t.Errorf("expect:\n%v\nactual:\n%v", expect, actual)
}
}

0 comments on commit 2c11b4f

Please sign in to comment.