Package dynamodbattribute
Overview ▹
Index ▹
func ConvertFrom ¶
func ConvertFrom(item *dynamodb.AttributeValue, v interface{}) (err error)
ConvertFrom accepts a *dynamodb.AttributeValue and converts it to any interface{}.
If v contains any structs, the result is first converted it to a interface{}, then JSON encoded/decoded it to convert to a struct, so `json` struct tags are respected.
Deprecated: Use Unmarshal instead
▹ Example
func ConvertFromList ¶
func ConvertFromList(item []*dynamodb.AttributeValue, v interface{}) (err error)
ConvertFromList accepts a []*dynamodb.AttributeValue and converts it to an array or slice.
If v contains any structs, the result is first converted it to a []interface{}, then JSON encoded/decoded it to convert to a typed array or slice, so `json` struct tags are respected.
Deprecated: Use UnmarshalList instead
func ConvertFromMap ¶
func ConvertFromMap(item map[string]*dynamodb.AttributeValue, v interface{}) (err error)
ConvertFromMap accepts a map[string]*dynamodb.AttributeValue and converts it to a map[string]interface{} or struct.
If v points to a struct, the result is first converted it to a map[string]interface{}, then JSON encoded/decoded it to convert to a struct, so `json` struct tags are respected.
Deprecated: Use UnmarshalMap instead
func ConvertTo ¶
func ConvertTo(in interface{}) (item *dynamodb.AttributeValue, err error)
ConvertTo accepts any interface{} and converts it to a *dynamodb.AttributeValue.
If in contains any structs, it is first JSON encoded/decoded it to convert it to a interface{}, so `json` struct tags are respected.
Deprecated: Use Marshal instead
▹ Example
func ConvertToList ¶
func ConvertToList(in interface{}) (item []*dynamodb.AttributeValue, err error)
ConvertToList accepts an array or slice and converts it to a []*dynamodb.AttributeValue.
Converting []byte fields to dynamodb.AttributeValue are only currently supported if the input is a map[string]interface{} type. []byte within typed structs are not converted correctly and are converted into base64 strings. This is a known bug, and will be fixed in a later release.
If in contains any structs, it is first JSON encoded/decoded it to convert it to a []interface{}, so `json` struct tags are respected.
Deprecated: Use MarshalList instead
func ConvertToMap ¶
func ConvertToMap(in interface{}) (item map[string]*dynamodb.AttributeValue, err error)
ConvertToMap accepts a map[string]interface{} or struct and converts it to a map[string]*dynamodb.AttributeValue.
If in contains any structs, it is first JSON encoded/decoded it to convert it to a map[string]interface{}, so `json` struct tags are respected.
Deprecated: Use MarshalMap instead
func Marshal ¶
func Marshal(in interface{}) (*dynamodb.AttributeValue, error)
Marshal will serialize the passed in Go value type into a DynamoDB AttributeValue type. This value can be used in DynamoDB API operations to simplify marshaling your Go value types into AttributeValues.
Marshal will recursively transverse the passed in value marshaling its contents into a AttributeValue. Marshal supports basic scalars (int,uint,float,bool,string), maps, slices, and structs. Anonymous nested types are flattened based on Go anonymous type visibility.
Marshaling slices to AttributeValue will default to a List for all types except for []byte and [][]byte. []byte will be marshaled as Binary data (B), and [][]byte will be marshaled as binary data set (BS).
`dynamodbav` struct tag can be used to control how the value will be marshaled into a AttributeValue.
// Field is ignored Field int `dynamodbav:"-"` // Field AttributeValue map key "myName" Field int `dynamodbav:"myName"` // Field AttributeValue map key "myName", and // Field is omitted if it is empty Field int `dynamodbav:"myName,omitempty"` // Field AttributeValue map key "Field", and // Field is omitted if it is empty Field int `dynamodbav:",omitempty"` // Field's elems will be omitted if empty // only valid for slices, and maps. Field []string `dynamodbav:",omitemptyelem"` // Field will be marshaled as a AttributeValue string // only value for number types, (int,uint,float) Field int `dynamodbav:",string"` // Field will be marshaled as a binary set Field [][]byte `dynamodbav:",binaryset"` // Field will be marshaled as a number set Field []int `dynamodbav:",numberset"` // Field will be marshaled as a string set Field []string `dynamodbav:",stringset"` // Field will be marshaled as Unix time number in seconds. // This tag is only valid with time.Time typed struct fields. // Important to note that zero value time as unixtime is not 0 seconds // from January 1, 1970 UTC, but -62135596800. Which is seconds between // January 1, 0001 UTC, and January 1, 0001 UTC. Field time.Time `dynamodbav:",unixtime"`
The omitempty tag is only used during Marshaling and is ignored for Unmarshal. Any zero value or a value when marshaled results in a AttributeValue NULL will be added to AttributeValue Maps during struct marshal. The omitemptyelem tag works the same as omitempty except it applies to maps and slices instead of struct fields, and will not be included in the marshaled AttributeValue Map, List, or Set.
For convenience and backwards compatibility with ConvertTo functions json struct tags are supported by the Marshal and Unmarshal. If both json and dynamodbav struct tags are provided the json tag will be ignored in favor of dynamodbav.
All struct fields and with anonymous fields, are marshaled unless the any of the following conditions are meet.
- the field is not exported - json or dynamodbav field tag is "-" - json or dynamodbav field tag specifies "omitempty", and is empty.
Pointer and interfaces values encode as the value pointed to or contained in the interface. A nil value encodes as the AttributeValue NULL value.
Channel, complex, and function values are not encoded and will be skipped when walking the value to be marshaled.
When marshaling any error that occurs will halt the marshal and return the error.
Marshal cannot represent cyclic data structures and will not handle them. Passing cyclic structures to Marshal will result in an infinite recursion.
▹ Example
func MarshalList ¶
func MarshalList(in interface{}) ([]*dynamodb.AttributeValue, error)
MarshalList is an alias for Marshal func which marshals Go value type to a slice of AttributeValues.
func MarshalMap ¶
func MarshalMap(in interface{}) (map[string]*dynamodb.AttributeValue, error)
MarshalMap is an alias for Marshal func which marshals Go value type to a map of AttributeValues.
This is useful for DynamoDB APIs such as PutItem.
func Unmarshal ¶
func Unmarshal(av *dynamodb.AttributeValue, out interface{}) error
Unmarshal will unmarshal DynamoDB AttributeValues to Go value types. Both generic interface{} and concrete types are valid unmarshal destination types.
Unmarshal will allocate maps, slices, and pointers as needed to unmarshal the AttributeValue into the provided type value.
When unmarshaling AttributeValues into structs Unmarshal matches the field names of the struct to the AttributeValue Map keys. Initially it will look for exact field name matching, but will fall back to case insensitive if not exact match is found.
With the exception of omitempty, omitemptyelem, binaryset, numberset and stringset all struct tags used by Marshal are also used by Unmarshal.
When decoding AttributeValues to interfaces Unmarshal will use the following types.
[]byte, AV Binary (B) [][]byte, AV Binary Set (BS) bool, AV Boolean (BOOL) []interface{}, AV List (L) map[string]interface{}, AV Map (M) float64, AV Number (N) Number, AV Number (N) with UseNumber set []float64, AV Number Set (NS) []Number, AV Number Set (NS) with UseNumber set string, AV String (S) []string, AV String Set (SS)
If the Decoder option, UseNumber is set numbers will be unmarshaled as Number values instead of float64. Use this to maintain the original string formating of the number as it was represented in the AttributeValue. In addition provides additional opportunities to parse the number string based on individual use cases.
When unmarshaling any error that occurs will halt the unmarshal and return the error.
The output value provided must be a non-nil pointer
▹ Example
func UnmarshalList ¶
func UnmarshalList(l []*dynamodb.AttributeValue, out interface{}) error
UnmarshalList is an alias for Unmarshal func which unmarshals a slice of AttributeValues.
The output value provided must be a non-nil pointer
func UnmarshalListOfMaps ¶
func UnmarshalListOfMaps(l []map[string]*dynamodb.AttributeValue, out interface{}) error
UnmarshalListOfMaps is an alias for Unmarshal func which unmarshals a slice of maps of attribute values.
This is useful for when you need to unmarshal the Items from a DynamoDB Query API call.
The output value provided must be a non-nil pointer
func UnmarshalMap ¶
func UnmarshalMap(m map[string]*dynamodb.AttributeValue, out interface{}) error
UnmarshalMap is an alias for Unmarshal which unmarshals from a map of AttributeValues.
The output value provided must be a non-nil pointer
type Decoder ¶
A Decoder provides unmarshaling AttributeValues to Go value types.
type Decoder struct { MarshalOptions // Instructs the decoder to decode AttributeValue Numbers as // Number type instead of float64 when the destination type // is interface{}. Similar to encoding/json.Number UseNumber bool }
func NewDecoder ¶
func NewDecoder(opts ...func(*Decoder)) *Decoder
NewDecoder creates a new Decoder with default configuration. Use the `opts` functional options to override the default configuration.
func (*Decoder) Decode ¶
func (d *Decoder) Decode(av *dynamodb.AttributeValue, out interface{}, opts ...func(*Decoder)) error
Decode will unmarshal an AttributeValue into a Go value type. An error will be return if the decoder is unable to unmarshal the AttributeValue to the provide Go value type.
The output value provided must be a non-nil pointer
type Encoder ¶
An Encoder provides marshaling Go value types to AttributeValues.
type Encoder struct { MarshalOptions // Empty strings, "", will be marked as NULL AttributeValue types. // Empty strings are not valid values for DynamoDB. Will not apply // to lists, sets, or maps. Use the struct tag `omitemptyelem` // to skip empty (zero) values in lists, sets and maps. // // Enabled by default. NullEmptyString bool }
func NewEncoder ¶
func NewEncoder(opts ...func(*Encoder)) *Encoder
NewEncoder creates a new Encoder with default configuration. Use the `opts` functional options to override the default configuration.
func (*Encoder) Encode ¶
func (e *Encoder) Encode(in interface{}) (*dynamodb.AttributeValue, error)
Encode will marshal a Go value type to an AttributeValue. Returning the AttributeValue constructed or error.
type InvalidMarshalError ¶
An InvalidMarshalError is an error type representing an error occurring when marshaling a Go value type to an AttributeValue.
type InvalidMarshalError struct {
// contains filtered or unexported fields
}
func (*InvalidMarshalError) Code ¶
func (e *InvalidMarshalError) Code() string
Code returns the code of the error, satisfying the awserr.Error interface.
func (*InvalidMarshalError) Error ¶
func (e *InvalidMarshalError) Error() string
Error returns the string representation of the error. satisfying the error interface
func (*InvalidMarshalError) Message ¶
func (e *InvalidMarshalError) Message() string
Message returns the detailed message of the error, satisfying the awserr.Error interface.
func (InvalidMarshalError) OrigErr ¶
func (e InvalidMarshalError) OrigErr() error
type InvalidUnmarshalError ¶
An InvalidUnmarshalError is an error type representing an invalid type encountered while unmarshaling a AttributeValue to a Go value type.
type InvalidUnmarshalError struct {
Type reflect.Type
// contains filtered or unexported fields
}
func (*InvalidUnmarshalError) Code ¶
func (e *InvalidUnmarshalError) Code() string
Code returns the code of the error, satisfying the awserr.Error interface.
func (*InvalidUnmarshalError) Error ¶
func (e *InvalidUnmarshalError) Error() string
Error returns the string representation of the error. satisfying the error interface
func (*InvalidUnmarshalError) Message ¶
func (e *InvalidUnmarshalError) Message() string
Message returns the detailed message of the error, satisfying the awserr.Error interface.
func (InvalidUnmarshalError) OrigErr ¶
func (e InvalidUnmarshalError) OrigErr() error
type MarshalOptions ¶
A MarshalOptions is a collection of options shared between marshaling and unmarshaling
type MarshalOptions struct { // States that the encoding/json struct tags should be supported. // if a `dynamodbav` struct tag is also provided the encoding/json // tag will be ignored. // // Enabled by default. SupportJSONTags bool }
type Marshaler ¶
A Marshaler is an interface to provide custom marshaling of Go value types to AttributeValues. Use this to provide custom logic determining how a Go Value type should be marshaled.
type ExampleMarshaler struct { Value int } func (m *ExampleMarshaler) MarshalDynamoDBAttributeValue(av *dynamodb.AttributeValue) error { n := fmt.Sprintf("%v", m.Value) av.N = &n return nil }
type Marshaler interface { MarshalDynamoDBAttributeValue(*dynamodb.AttributeValue) error }
type Number ¶
A Number represents a Attributevalue number literal.
type Number string
func (Number) Float64 ¶
func (n Number) Float64() (float64, error)
Float64 attempts to cast the number ot a float64, returning the result of the case or error if the case failed.
func (Number) Int64 ¶
func (n Number) Int64() (int64, error)
Int64 attempts to cast the number ot a int64, returning the result of the case or error if the case failed.
func (Number) String ¶
func (n Number) String() string
String returns the raw number represented as a string
func (Number) Uint64 ¶
func (n Number) Uint64() (uint64, error)
Uint64 attempts to cast the number ot a uint64, returning the result of the case or error if the case failed.
type UnixTime ¶
An UnixTime provides aliasing of time.Time into a type that when marshaled and unmarshaled with DynamoDB AttributeValues it will be done so as number instead of string in seconds since January 1, 1970 UTC.
This type is useful as an alternative to the struct tag `unixtime` when you want to have your time value marshaled as Unix time in seconds intead of the default time.RFC3339.
Important to note that zero value time as unixtime is not 0 seconds from January 1, 1970 UTC, but -62135596800. Which is seconds between January 1, 0001 UTC, and January 1, 0001 UTC.
type UnixTime time.Time
func (UnixTime) MarshalDynamoDBAttributeValue ¶
func (e UnixTime) MarshalDynamoDBAttributeValue(av *dynamodb.AttributeValue) error
MarshalDynamoDBAttributeValue implements the Marshaler interface so that the UnixTime can be marshaled from to a DynamoDB AttributeValue number value encoded in the number of seconds since January 1, 1970 UTC.
func (*UnixTime) UnmarshalDynamoDBAttributeValue ¶
func (e *UnixTime) UnmarshalDynamoDBAttributeValue(av *dynamodb.AttributeValue) error
UnmarshalDynamoDBAttributeValue implements the Unmarshaler interface so that the UnixTime can be unmarshaled from a DynamoDB AttributeValue number representing the number of seconds since January 1, 1970 UTC.
If an error parsing the AttributeValue number occurs UnmarshalError will be returned.
type UnmarshalError ¶
An UnmarshalError wraps an error that occured while unmarshaling a DynamoDB AttributeValue element into a Go type. This is different from UnmarshalTypeError in that it wraps the underlying error that occured.
type UnmarshalError struct { Err error Value string Type reflect.Type }
func (*UnmarshalError) Code ¶
func (e *UnmarshalError) Code() string
Code returns the code of the error, satisfying the awserr.Error interface.
func (*UnmarshalError) Error ¶
func (e *UnmarshalError) Error() string
Error returns the string representation of the error. satisfying the error interface.
func (*UnmarshalError) Message ¶
func (e *UnmarshalError) Message() string
Message returns the detailed message of the error, satisfying the awserr.Error interface.
func (UnmarshalError) OrigErr ¶
func (e UnmarshalError) OrigErr() error
OrigErr returns the original error that caused this issue.
type UnmarshalTypeError ¶
An UnmarshalTypeError is an error type representing a error unmarshaling the AttributeValue's element to a Go value type. Includes details about the AttributeValue type and Go value type.
type UnmarshalTypeError struct {
Value string
Type reflect.Type
// contains filtered or unexported fields
}
func (*UnmarshalTypeError) Code ¶
func (e *UnmarshalTypeError) Code() string
Code returns the code of the error, satisfying the awserr.Error interface.
func (*UnmarshalTypeError) Error ¶
func (e *UnmarshalTypeError) Error() string
Error returns the string representation of the error. satisfying the error interface
func (*UnmarshalTypeError) Message ¶
func (e *UnmarshalTypeError) Message() string
Message returns the detailed message of the error, satisfying the awserr.Error interface.
func (UnmarshalTypeError) OrigErr ¶
func (e UnmarshalTypeError) OrigErr() error
type Unmarshaler ¶
An Unmarshaler is an interface to provide custom unmarshaling of AttributeValues. Use this to provide custom logic determining how AttributeValues should be unmarshaled.
type ExampleUnmarshaler struct { Value int } func (u *exampleUnmarshaler) UnmarshalDynamoDBAttributeValue(av *dynamodb.AttributeValue) error { if av.N == nil { return nil } n, err := strconv.ParseInt(*av.N, 10, 0) if err != nil { return err } u.Value = n return nil }
type Unmarshaler interface { UnmarshalDynamoDBAttributeValue(*dynamodb.AttributeValue) error }