dynamodbattribute

package
v0.0.0-...-29ce5d4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2024 License: Apache-2.0, Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package dynamodbattribute provides marshaling and unmarshaling utilities to convert between Go types and dynamodb.AttributeValues.

These utilities allow you to marshal slices, maps, structs, and scalar values to and from dynamodb.AttributeValue. These are useful when marshaling Go value tyes to dynamodb.AttributeValue for DynamoDB requests, or unmarshaling the dynamodb.AttributeValue back into a Go value type.

AttributeValue Marshaling

To marshal a Go type to a dynamodbAttributeValue you can use the Marshal functions in the dynamodbattribute package. There are specialized versions of these functions for collections of Attributevalue, such as maps and lists.

The following example uses MarshalMap to convert the Record Go type to a dynamodb.AttributeValue type and use the value to make a PutItem API request.

type Record struct {
    ID     string
    URLs   []string
}

//...

r := Record{
    ID:   "ABC123",
    URLs: []string{
        "https://example.com/first/link",
        "https://example.com/second/url",
    },
}
av, err := dynamodbattribute.MarshalMap(r)
if err != nil {
    panic(fmt.Sprintf("failed to DynamoDB marshal Record, %v", err))
}

_, err = svc.PutItem(&dynamodb.PutItemInput{
    TableName: aws.String(myTableName),
    Item:      av,
})
if err != nil {
    panic(fmt.Sprintf("failed to put Record to DynamoDB, %v", err))
}

AttributeValue Unmarshaling

To unmarshal a dynamodb.AttributeValue to a Go type you can use the Unmarshal functions in the dynamodbattribute package. There are specialized versions of these functions for collections of Attributevalue, such as maps and lists.

The following example will unmarshal the DynamoDB's Scan API operation. The Items returned by the operation will be unmarshaled into the slice of Records Go type.

type Record struct {
    ID     string
    URLs   []string
}

//...

var records []Record

// Use the ScanPages method to perform the scan with pagination. Use
// just Scan method to make the API call without pagination.
err := svc.ScanPages(&dynamodb.ScanInput{
    TableName: aws.String(myTableName),
}, func(page *dynamodb.ScanOutput, last bool) bool {
    recs := []Record{}

    err := dynamodbattribute.UnmarshalListOfMaps(page.Items, &recs)
    if err != nil {
         panic(fmt.Sprintf("failed to unmarshal Dynamodb Scan Items, %v", err))
    }

    records = append(records, recs...)

    return true // keep paging
})

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 incorrectly marshal binary data fields in structs as base64 strings.

The Marshal and Unmarshal functions correct this behavior, and removes the reliance on encoding.json. `json` struct tags are still supported. In addition support for a new struct tag `dynamodbav` was added. Support for the json.Marshaler and json.Unmarshaler interfaces have been removed and 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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertFrom deprecated

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

func ConvertFromList deprecated

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 deprecated

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 deprecated

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

func ConvertToList deprecated

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 deprecated

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.

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

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

Types

type Decoder

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
}

A Decoder provides unmarshaling AttributeValues to Go value types.

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

type Encoder struct {
	MarshalOptions

	// Empty strings, "", will be marked as NULL AttributeValue types.
	// 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

	// Empty byte slices, len([]byte{}) == 0, will be marked as NULL AttributeValue types.
	// 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.
	NullEmptyByteSlice bool
}

An Encoder provides marshaling Go value types to AttributeValues.

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

type InvalidMarshalError struct {
	// contains filtered or unexported fields
}

An InvalidMarshalError is an error type representing an error occurring when marshaling a Go value type to an AttributeValue.

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

type InvalidUnmarshalError struct {
	Type reflect.Type
	// contains filtered or unexported fields
}

An InvalidUnmarshalError is an error type representing an invalid type encountered while unmarshaling a AttributeValue to a Go value type.

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

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

	// 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

	// EnableEmptyCollections modifies how structures, maps, and slices are (un)marshalled.
	// When set to true empty collection values will be preserved as their respective
	// empty DynamoDB AttributeValue type when set to true.
	//
	// Disabled by default.
	EnableEmptyCollections bool
}

A MarshalOptions is a collection of options shared between marshaling and unmarshaling

type Marshaler

type Marshaler interface {
	MarshalDynamoDBAttributeValue(*dynamodb.AttributeValue) error
}

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 Number

type Number string

A Number represents a Attributevalue number literal.

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

type UnixTime time.Time

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.

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) String

func (e UnixTime) String() string

String calls the underlying time.Time.String to return a human readable representation

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

type UnmarshalError struct {
	Err   error
	Value string
	Type  reflect.Type
}

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

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

type UnmarshalTypeError struct {
	Value string
	Type  reflect.Type
	// contains filtered or unexported fields
}

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.

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

type Unmarshaler interface {
	UnmarshalDynamoDBAttributeValue(*dynamodb.AttributeValue) error
}

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 = int(n)
	return nil
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL