AWS SDK for Go (PILOT)
API Reference

PREVIEW DOCUMENTATION - This is a preview of a new format for the AWS SDK for Go API Reference documentation. For the current AWS SDK for Go API Reference, see https://docs.aws.amazon.com/sdk-for-go/api/.

We welcome your feedback on this new version of the documentation. Send your comments to aws-sdkdocs-feedback@amazon.com.

Builder

import "github.com/aws/aws-sdk-go/service/dynamodb/expression"

type Builder struct { }

Builder represents the struct that builds the Expression struct. Methods such as WithProjection() and WithCondition() can add different kinds of DynamoDB Expressions to the Builder. The method Build() creates an Expression struct with the specified types of DynamoDB Expressions.

Example:

keyCond := expression.Key("someKey").Equal(expression.Value("someValue")) proj := expression.NamesList(expression.Name("aName"), expression.Name("anotherName"), expression.Name("oneOtherName")) builder := expression.NewBuilder().WithKeyCondition(keyCond).WithProjection(proj) expr := builder.Build() queryInput := dynamodb.QueryInput{ KeyConditionExpression: expr.KeyCondition(), ProjectionExpression: expr.Projection(), ExpressionAttributeNames: expr.Names(), ExpressionAttributeValues: expr.Values(), TableName: aws.String("SomeTable"), }

Method

Build

func (b Builder) Build() (Expression, error)

Build builds an Expression struct representing multiple types of DynamoDB Expressions. Getter methods on the resulting Expression struct returns the DynamoDB Expression strings as well as the maps that correspond to ExpressionAttributeNames and ExpressionAttributeValues. Calling Build() on an empty Builder returns the typed error EmptyParameterError.

Example:

// keyCond represents the Key Condition Expression keyCond := expression.Key("someKey").Equal(expression.Value("someValue")) // proj represents the Projection Expression proj := expression.NamesList(expression.Name("aName"), expression.Name("anotherName"), expression.Name("oneOtherName")) // Add keyCond and proj to builder as a Key Condition and Projection // respectively builder := expression.NewBuilder().WithKeyCondition(keyCond).WithProjection(proj) expr := builder.Build() queryInput := dynamodb.QueryInput{ KeyConditionExpression: expr.KeyCondition(), ProjectionExpression: expr.Projection(), ExpressionAttributeNames: expr.Names(), ExpressionAttributeValues: expr.Values(), TableName: aws.String("SomeTable"), }

WithCondition

func (b Builder) WithCondition(conditionBuilder ConditionBuilder) Builder

WithCondition method adds the argument ConditionBuilder as a Condition Expression to the argument Builder. If the argument Builder already has a ConditionBuilder representing a Condition Expression, WithCondition() overwrites the existing ConditionBuilder.

Example:

// let builder be an existing Builder{} and cond be an existing // ConditionBuilder{} builder = builder.WithCondition(cond) // add other DynamoDB Expressions to the builder. let proj be an already // existing ProjectionBuilder builder = builder.WithProjection(proj) // create an Expression struct expr := builder.Build()

Example

Builder WithCondition

Using Condition Expression

This example deletes an item from the Music table if the rating is lower than 7.

{ svc := dynamodb.New(session.New()) cond := expression.Name("Rating").LessThan(expression.Value(7)) expr, err := expression.NewBuilder(). WithCondition(cond). Build() if err != nil { fmt.Println(err) } input := &dynamodb.DeleteItemInput{ Key: map[string]*dynamodb.AttributeValue{ "Artist": { S: aws.String("No One You Know"), }, "SongTitle": { S: aws.String("Scared of My Shadow"), }, }, ExpressionAttributeNames: expr.Names(), ExpressionAttributeValues: expr.Values(), ConditionExpression: expr.Condition(), TableName: aws.String("Music"), } result, err := svc.DeleteItem(input) if err != nil { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case dynamodb.ErrCodeConditionalCheckFailedException: fmt.Println(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error()) case dynamodb.ErrCodeProvisionedThroughputExceededException: fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) case dynamodb.ErrCodeResourceNotFoundException: fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) case dynamodb.ErrCodeItemCollectionSizeLimitExceededException: fmt.Println(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error()) case dynamodb.ErrCodeInternalServerError: fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error()) default: fmt.Println(aerr.Error()) } } else { fmt.Println(err.Error()) } return } fmt.Println(result) }

WithFilter

func (b Builder) WithFilter(filterBuilder ConditionBuilder) Builder

WithFilter method adds the argument ConditionBuilder as a Filter Expression to the argument Builder. If the argument Builder already has a ConditionBuilder representing a Filter Expression, WithFilter() overwrites the existing ConditionBuilder.

Example:

// let builder be an existing Builder{} and filt be an existing // ConditionBuilder{} builder = builder.WithFilter(filt) // add other DynamoDB Expressions to the builder. let cond be an already // existing ConditionBuilder builder = builder.WithCondition(cond) // create an Expression struct expr := builder.Build()

Example

Builder WithFilter

Using Filter Expression

This example scans the entire Music table, and then narrows the results to songs by the artist "No One You Know". For each item, only the album title and song title are returned.

{ svc := dynamodb.New(session.New()) filt := expression.Name("Artist").Equal(expression.Value("No One You Know")) proj := expression.NamesList( expression.Name("AlbumTitle"), expression.Name("SongTitle"), ) expr, err := expression.NewBuilder(). WithFilter(filt). WithProjection(proj). Build() if err != nil { fmt.Println(err) } input := &dynamodb.ScanInput{ ExpressionAttributeNames: expr.Names(), ExpressionAttributeValues: expr.Values(), FilterExpression: expr.Filter(), ProjectionExpression: expr.Projection(), TableName: aws.String("Music"), } result, err := svc.Scan(input) if err != nil { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case dynamodb.ErrCodeProvisionedThroughputExceededException: fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) case dynamodb.ErrCodeResourceNotFoundException: fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) case dynamodb.ErrCodeInternalServerError: fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error()) default: fmt.Println(aerr.Error()) } } else { fmt.Println(err.Error()) } return } fmt.Println(result) }

WithKeyCondition

func (b Builder) WithKeyCondition(keyConditionBuilder KeyConditionBuilder) Builder

WithKeyCondition method adds the argument KeyConditionBuilder as a Key Condition Expression to the argument Builder. If the argument Builder already has a KeyConditionBuilder representing a Key Condition Expression, WithKeyCondition() overwrites the existing KeyConditionBuilder.

Example:

// let builder be an existing Builder{} and keyCond be an existing // KeyConditionBuilder{} builder = builder.WithKeyCondition(keyCond) // add other DynamoDB Expressions to the builder. let cond be an already // existing ConditionBuilder builder = builder.WithCondition(cond) // create an Expression struct expr := builder.Build()

Example

Builder WithKeyCondition

Using Key Condition Expression

This example queries items in the Music table. The table has a partition key and sort key (Artist and SongTitle), but this query only specifies the partition key value. It returns song titles by the artist named "No One You Know".

{ svc := dynamodb.New(session.New()) keyCond := expression.Key("Artist").Equal(expression.Value("No One You Know")) proj := expression.NamesList(expression.Name("SongTitle")) expr, err := expression.NewBuilder(). WithKeyCondition(keyCond). WithProjection(proj). Build() if err != nil { fmt.Println(err) } input := &dynamodb.QueryInput{ ExpressionAttributeValues: expr.Values(), KeyConditionExpression: expr.KeyCondition(), ProjectionExpression: expr.Projection(), TableName: aws.String("Music"), } result, err := svc.Query(input) if err != nil { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case dynamodb.ErrCodeProvisionedThroughputExceededException: fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) case dynamodb.ErrCodeResourceNotFoundException: fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) case dynamodb.ErrCodeInternalServerError: fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error()) default: fmt.Println(aerr.Error()) } } else { fmt.Println(err.Error()) } return } fmt.Println(result) }

WithProjection

func (b Builder) WithProjection(projectionBuilder ProjectionBuilder) Builder

WithProjection method adds the argument ProjectionBuilder as a Projection Expression to the argument Builder. If the argument Builder already has a ProjectionBuilder representing a Projection Expression, WithProjection() overwrites the existing ProjectionBuilder.

Example:

// let builder be an existing Builder{} and proj be an existing // ProjectionBuilder{} builder = builder.WithProjection(proj) // add other DynamoDB Expressions to the builder. let cond be an already // existing ConditionBuilder builder = builder.WithCondition(cond) // create an Expression struct expr := builder.Build()

Example

Builder WithProjection

Using Projection Expression

This example queries items in the Music table. The table has a partition key and sort key (Artist and SongTitle), but this query only specifies the partition key value. It returns song titles by the artist named "No One You Know".

{ svc := dynamodb.New(session.New()) keyCond := expression.Key("Artist").Equal(expression.Value("No One You Know")) proj := expression.NamesList(expression.Name("SongTitle")) expr, err := expression.NewBuilder(). WithKeyCondition(keyCond). WithProjection(proj). Build() if err != nil { fmt.Println(err) } input := &dynamodb.QueryInput{ ExpressionAttributeValues: expr.Values(), KeyConditionExpression: expr.KeyCondition(), ProjectionExpression: expr.Projection(), TableName: aws.String("Music"), } result, err := svc.Query(input) if err != nil { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case dynamodb.ErrCodeProvisionedThroughputExceededException: fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) case dynamodb.ErrCodeResourceNotFoundException: fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) case dynamodb.ErrCodeInternalServerError: fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error()) default: fmt.Println(aerr.Error()) } } else { fmt.Println(err.Error()) } return } fmt.Println(result) }

WithUpdate

func (b Builder) WithUpdate(updateBuilder UpdateBuilder) Builder

WithUpdate method adds the argument UpdateBuilder as an Update Expression to the argument Builder. If the argument Builder already has a UpdateBuilder representing a Update Expression, WithUpdate() overwrites the existing UpdateBuilder.

Example:

// let builder be an existing Builder{} and update be an existing // UpdateBuilder{} builder = builder.WithUpdate(update) // add other DynamoDB Expressions to the builder. let cond be an already // existing ConditionBuilder builder = builder.WithCondition(cond) // create an Expression struct expr := builder.Build()

Example

Builder WithUpdate

Using Update Expression

This example updates an item in the Music table. It adds a new attribute (Year) and modifies the AlbumTitle attribute. All of the attributes in the item, as they appear after the update, are returned in the response.

{ svc := dynamodb.New(session.New()) update := expression.Set( expression.Name("Year"), expression.Value(2015), ).Set( expression.Name("AlbumTitle"), expression.Value("Louder Than Ever"), ) expr, err := expression.NewBuilder(). WithUpdate(update). Build() input := &dynamodb.UpdateItemInput{ ExpressionAttributeNames: expr.Names(), ExpressionAttributeValues: expr.Values(), Key: map[string]*dynamodb.AttributeValue{ "Artist": { S: aws.String("Acme Band"), }, "SongTitle": { S: aws.String("Happy Day"), }, }, ReturnValues: aws.String("ALL_NEW"), TableName: aws.String("Music"), UpdateExpression: expr.Update(), } result, err := svc.UpdateItem(input) if err != nil { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case dynamodb.ErrCodeConditionalCheckFailedException: fmt.Println(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error()) case dynamodb.ErrCodeProvisionedThroughputExceededException: fmt.Println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error()) case dynamodb.ErrCodeResourceNotFoundException: fmt.Println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error()) case dynamodb.ErrCodeItemCollectionSizeLimitExceededException: fmt.Println(dynamodb.ErrCodeItemCollectionSizeLimitExceededException, aerr.Error()) case dynamodb.ErrCodeInternalServerError: fmt.Println(dynamodb.ErrCodeInternalServerError, aerr.Error()) default: fmt.Println(aerr.Error()) } } else { fmt.Println(err.Error()) } return } fmt.Println(result) }

On this page: