Update Go AWS SDK to the latest version

This commit is contained in:
Andrey Smirnov
2019-07-13 00:03:55 +03:00
committed by Andrey Smirnov
parent d08be990ef
commit 94a72b23ff
2183 changed files with 885887 additions and 228114 deletions
@@ -1,6 +1,7 @@
package dynamodbattribute
import (
"encoding/base64"
"fmt"
"reflect"
"strconv"
@@ -16,7 +17,7 @@ import (
// Value int
// }
//
// func (u *exampleUnmarshaler) UnmarshalDynamoDBAttributeValue(av *dynamodb.AttributeValue) error {
// func (u *ExampleUnmarshaler) UnmarshalDynamoDBAttributeValue(av *dynamodb.AttributeValue) error {
// if av.N == nil {
// return nil
// }
@@ -26,7 +27,7 @@ import (
// return err
// }
//
// u.Value = n
// u.Value = int(n)
// return nil
// }
type Unmarshaler interface {
@@ -538,6 +539,16 @@ func (d *Decoder) decodeString(s *string, v reflect.Value, fieldTag tag) error {
switch v.Kind() {
case reflect.String:
v.SetString(*s)
case reflect.Slice:
// To maintain backwards compatibility with the ConvertFrom family of methods
// which converted []byte into base64-encoded strings if the input was typed
if v.Type() == byteSliceType {
decoded, err := base64.StdEncoding.DecodeString(*s)
if err != nil {
return &UnmarshalError{Err: err, Value: "string", Type: v.Type()}
}
v.SetBytes(decoded)
}
case reflect.Interface:
// Ensure type aliasing is handled properly
v.Set(reflect.ValueOf(*s).Convert(v.Type()))
@@ -727,9 +738,9 @@ func (e *InvalidUnmarshalError) Message() string {
return "cannot unmarshal to nil value, " + e.Type.String()
}
// An UnmarshalError wraps an error that occured while unmarshaling a DynamoDB
// An UnmarshalError wraps an error that occurred while unmarshaling a DynamoDB
// AttributeValue element into a Go type. This is different from UnmarshalTypeError
// in that it wraps the underlying error that occured.
// in that it wraps the underlying error that occurred.
type UnmarshalError struct {
Err error
Value string
@@ -234,6 +234,30 @@ func TestUnmarshalListError(t *testing.T) {
}
}
func TestUnmarshalConvertToData(t *testing.T) {
type T struct {
Int int
Str string
ByteSlice []byte
StrSlice []string
}
exp := T{
Int: 42,
Str: "foo",
ByteSlice: []byte{42, 97, 83},
StrSlice: []string{"cat", "dog"},
}
av, err := ConvertToMap(exp)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
var act T
err = UnmarshalMap(av, &act)
assertConvertTest(t, 0, act, exp, err, nil)
}
func TestUnmarshalMapShared(t *testing.T) {
for i, c := range sharedMapTestCases {
err := UnmarshalMap(c.in, c.actual)
@@ -81,7 +81,7 @@
// The ConvertTo, ConvertToList, ConvertToMap, ConvertFrom, ConvertFromMap
// and ConvertFromList methods have been deprecated. The Marshal and Unmarshal
// functions should be used instead. The ConvertTo|From marshallers do not
// support BinarySet, NumberSet, nor StringSets, and will incorrect marshal
// support BinarySet, NumberSet, nor StringSets, and will incorrectly marshal
// binary data fields in structs as base64 strings.
//
// The Marshal and Unmarshal functions correct this behavior, and removes
@@ -91,5 +91,11 @@
// replaced with have been replaced with dynamodbattribute.Marshaler and
// dynamodbattribute.Unmarshaler interfaces.
//
// The Unmarshal functions are backwards compatible with data marshalled by
// ConvertTo*, but the reverse is not true: objects marshalled using Marshal
// are not necessarily usable by ConvertFrom*. This backward compatibility is
// intended to assist with incremental upgrading of data following a switch
// away from the Convert* family of functions.
//
// `time.Time` is marshaled as RFC3339 format.
package dynamodbattribute
@@ -189,6 +189,11 @@ type MarshalOptions struct {
//
// Enabled by default.
SupportJSONTags bool
// Support other custom struct tag keys, such as `yaml` or `toml`.
// Note that values provided with a custom TagKey must also be supported
// by the (un)marshalers in this package.
TagKey string
}
// An Encoder provides marshaling Go value types to AttributeValues.
@@ -99,8 +99,12 @@ func enumFields(t reflect.Type, opts MarshalOptions) []field {
fieldTag := tag{}
fieldTag.parseAVTag(sf.Tag)
if opts.SupportJSONTags && fieldTag == (tag{}) {
fieldTag.parseJSONTag(sf.Tag)
// Because MarshalOptions.TagKey must be explicitly set, use it
// over JSON, which is enabled by default.
if opts.TagKey != "" && fieldTag == (tag{}) {
fieldTag.parseStructTag(opts.TagKey, sf.Tag)
} else if opts.SupportJSONTags && fieldTag == (tag{}) {
fieldTag.parseStructTag("json", sf.Tag)
}
if fieldTag.Ignore {
@@ -571,3 +571,65 @@ func BenchmarkMarshal(b *testing.B) {
}
}
}
func Test_Encode_YAML_TagKey(t *testing.T) {
input := struct {
String string `yaml:"string"`
EmptyString string `yaml:"empty"`
OmitString string `yaml:"omitted,omitempty"`
Ignored string `yaml:"-"`
Byte []byte `yaml:"byte"`
Float32 float32 `yaml:"float32"`
Float64 float64 `yaml:"float64"`
Int int `yaml:"int"`
Uint uint `yaml:"uint"`
Slice []string `yaml:"slice"`
Map map[string]int `yaml:"map"`
NoTag string
}{
String: "String",
Ignored: "Ignored",
Slice: []string{"one", "two"},
Map: map[string]int{
"one": 1,
"two": 2,
},
NoTag: "NoTag",
}
expected := &dynamodb.AttributeValue{
M: map[string]*dynamodb.AttributeValue{
"string": {S: aws.String("String")},
"empty": {NULL: &trueValue},
"byte": {NULL: &trueValue},
"float32": {N: aws.String("0")},
"float64": {N: aws.String("0")},
"int": {N: aws.String("0")},
"uint": {N: aws.String("0")},
"slice": {
L: []*dynamodb.AttributeValue{
{S: aws.String("one")},
{S: aws.String("two")},
},
},
"map": {
M: map[string]*dynamodb.AttributeValue{
"one": {N: aws.String("1")},
"two": {N: aws.String("2")},
},
},
"NoTag": {S: aws.String("NoTag")},
},
}
enc := NewEncoder(func(e *Encoder) {
e.TagKey = "yaml"
})
actual, err := enc.Encode(input)
if err != nil {
t.Errorf("Encode with input %#v retured error `%s`, expected nil", input, err)
}
compareObjects(t, expected, actual)
}
@@ -24,8 +24,8 @@ func (t *tag) parseAVTag(structTag reflect.StructTag) {
t.parseTagStr(tagStr)
}
func (t *tag) parseJSONTag(structTag reflect.StructTag) {
tagStr := structTag.Get("json")
func (t *tag) parseStructTag(tag string, structTag reflect.StructTag) {
tagStr := structTag.Get(tag)
if len(tagStr) == 0 {
return
}
@@ -35,7 +35,7 @@ func TestTagParse(t *testing.T) {
for i, c := range cases {
actual := tag{}
if c.json {
actual.parseJSONTag(c.in)
actual.parseStructTag("json", c.in)
}
if c.av {
actual.parseAVTag(c.in)