dynamodbattribute

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2015 License: Apache-2.0 Imports: 8 Imported by: 2,097

Documentation

Overview

Package dynamodbattribute provides conversion utilities from dynamodb.AttributeValue to concrete Go types and structures. These conversion utilities allow you to convert a Struct, Slice, Map, or Scalar value to or from dynamodb.AttributeValue. These are most useful to serialize concrete types to dynamodb.AttributeValue for requests or unmarshalling the dynamodb.AttributeValue into a well known typed form.

Convert concrete type to dynamodb.AttributeValue: See (ExampleConvertTo)

type Record struct {
    MyField string
    Letters []string
    A2Num   map[string]int
}

...

r := Record{
    MyField: "dynamodbattribute.ConvertToX example",
    Letters: []string{"a", "b", "c", "d"},
    A2Num:   map[string]int{"a": 1, "b": 2, "c": 3},
}
av, err := dynamodbattribute.ConvertTo(r)
fmt.Println(av, err)

Convert dynamodb.AttributeValue to Concrete type: See (ExampleConvertFrom)

r2 := Record{}
err = dynamodbattribute.ConvertFrom(av, &r2)
fmt.Println(err, reflect.DeepEqual(r, r2))

Use Conversion utilities with DynamoDB.PutItem: See ()

svc := dynamodb.New(nil)
item, err := dynamodbattribute.ConvertToMap(r)
if err != nil {
    fmt.Println("Failed to convert", err)
    return
}
result, err := svc.PutItem(&dynamodb.PutItemInput{
    Item:      item,
    TableName: aws.String("exampleTable"),
})

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

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.

Example
package main

import (
	"fmt"
	"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
	"reflect"
)

func main() {
	type Record struct {
		MyField string
		Letters []string
		A2Num   map[string]int
	}

	r := Record{
		MyField: "MyFieldValue",
		Letters: []string{"a", "b", "c", "d"},
		A2Num:   map[string]int{"a": 1, "b": 2, "c": 3},
	}
	av, err := dynamodbattribute.ConvertTo(r)

	r2 := Record{}
	err = dynamodbattribute.ConvertFrom(av, &r2)
	fmt.Println(err, reflect.DeepEqual(r, r2))

}
Output:

<nil> true

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.

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.

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.

Example
package main

import (
	"fmt"
	"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)

func main() {
	type Record struct {
		MyField string
		Letters []string
		Numbers []int
	}

	r := Record{
		MyField: "MyFieldValue",
		Letters: []string{"a", "b", "c", "d"},
		Numbers: []int{1, 2, 3},
	}
	av, err := dynamodbattribute.ConvertTo(r)
	fmt.Println("err", err)
	fmt.Println("MyField", av.M["MyField"])
	fmt.Println("Letters", av.M["Letters"])
	fmt.Println("Numbers", av.M["Numbers"])

}
Output:

err <nil>
MyField {
  S: "MyFieldValue"
}
Letters {
  L: [
    {
      S: "a"
    },
    {
      S: "b"
    },
    {
      S: "c"
    },
    {
      S: "d"
    }
  ]
}
Numbers {
  L: [{
      N: "1"
    },{
      N: "2"
    },{
      N: "3"
    }]
}

func ConvertToList

func ConvertToList(in interface{}) (item []*dynamodb.AttributeValue, err error)

ConvertToList accepts an array or slice 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.

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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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