func getServices(containerID string, svc *dynamodb.DynamoDB) ([]*Service, error) { context := Context{} params := &dynamodb.QueryInput{ TableName: aws.String("Services"), KeyConditions: map[string]*dynamodb.Condition{ "ContainerID": { ComparisonOperator: aws.String("EQ"), // Required AttributeValueList: []*dynamodb.AttributeValue{ { S: aws.String(containerID), }, }, }, }, } resp, err := svc.Query(params) if err != nil { return context, err } // loop through the items and convert back to struct for _, item := range resp.Items { service := Service{} dynamodbattribute.ConvertFromMap(item, &service) context = append(context, &service) } return context, err }
// List is used to list all the keys under a given // prefix, up to the next prefix. func (d *DynamoDBBackend) List(prefix string) ([]string, error) { defer metrics.MeasureSince([]string{"dynamodb", "list"}, time.Now()) prefix = strings.TrimSuffix(prefix, "/") keys := []string{} prefix = escapeEmptyPath(prefix) queryInput := &dynamodb.QueryInput{ TableName: aws.String(d.table), ConsistentRead: aws.Bool(true), KeyConditions: map[string]*dynamodb.Condition{ "Path": { ComparisonOperator: aws.String("EQ"), AttributeValueList: []*dynamodb.AttributeValue{{ S: aws.String(prefix), }}, }, }, } err := d.client.QueryPages(queryInput, func(out *dynamodb.QueryOutput, lastPage bool) bool { var record DynamoDBRecord for _, item := range out.Items { dynamodbattribute.ConvertFromMap(item, &record) if !strings.HasPrefix(record.Key, DynamoDBLockPrefix) { keys = append(keys, record.Key) } } return !lastPage }) if err != nil { return nil, err } return keys, nil }
// Get is used to fetch an entry func (d *DynamoDBBackend) Get(key string) (*Entry, error) { defer metrics.MeasureSince([]string{"dynamodb", "get"}, time.Now()) resp, err := d.client.GetItem(&dynamodb.GetItemInput{ TableName: aws.String(d.table), ConsistentRead: aws.Bool(true), Key: map[string]*dynamodb.AttributeValue{ "Path": {S: aws.String(recordPathForVaultKey(key))}, "Key": {S: aws.String(recordKeyForVaultKey(key))}, }, }) if err != nil { return nil, err } if resp.Item == nil { return nil, nil } record := &DynamoDBRecord{} if err := dynamodbattribute.ConvertFromMap(resp.Item, record); err != nil { return nil, err } return &Entry{ Key: vaultKey(record), Value: record.Value, }, nil }
// GetItem get the item from the table and convert it to v. func (t *Table) GetItem(hashKeyValue, rangeKeyValue *dynamodb.AttributeValue, v interface{}, opts ...option.GetItemInput) error { req := &dynamodb.GetItemInput{ TableName: t.Name, } key := make(map[string]*dynamodb.AttributeValue) key[*t.hashKey.AttributeDefinition.AttributeName] = hashKeyValue if t.rangeKey != nil { key[*t.rangeKey.AttributeDefinition.AttributeName] = rangeKeyValue } req.Key = key for _, f := range opts { f(req) } resp, err := t.DynamoDB.GetItem(req) if err != nil { return err } if len(resp.Item) == 0 { return ErrItemNotFound } // Use ItemUnmarshaler if available if unmarshaller, ok := v.(item.Unmarshaler); ok { return unmarshaller.UnmarshalItem(resp.Item) } return dynamodbattribute.ConvertFromMap(resp.Item, v) }
func main() { svc := dynamodb.New(session.New(), &aws.Config{Region: aws.String("eu-central-1")}) k := Key{ Email: "*****@*****.**", } item, err := dynamodbattribute.ConvertToMap(k) if err != nil { panic(err) } result, err := svc.GetItem(&dynamodb.GetItemInput{ TableName: aws.String("Users"), Key: item, }) if err != nil { panic(err) } r := Record{} err = dynamodbattribute.ConvertFromMap(result.Item, &r) fmt.Println(r) }
func (a *AccountT) UnmarshalDynamoDB(item map[string]*dynamodb.AttributeValue) error { if v, ok := item["UnmarshalFailPlease"]; ok { if *v.BOOL == true { return fmt.Errorf("could not grob the frob") } } return dynamodbattribute.ConvertFromMap(item, a) }
func (dynamoDBSource *DynamoDBSource) Get() (map[string]interface{}, error) { config := defaults.Config() if dynamoDBSource.AccessKey != "" { config = config.WithCredentials(credentials.NewCredentials(&credentials.StaticProvider{ Value: credentials.Value{ AccessKeyID: dynamoDBSource.AccessKey, SecretAccessKey: dynamoDBSource.SecretKey, }, })) } if dynamoDBSource.Endpoint != "" { config = config.WithEndpoint(dynamoDBSource.Endpoint) } if dynamoDBSource.Region != "" { config = config.WithRegion(dynamoDBSource.Region) } else { config = config.WithRegion("us-west-1") } client := dynamodb.New(session.New(config)) tableName := aws.String(dynamoDBSource.Table) describeTableInput := &dynamodb.DescribeTableInput{TableName: tableName} if _, err := client.DescribeTable(describeTableInput); err != nil { return nil, err } if err := client.WaitUntilTableExists(describeTableInput); err != nil { return nil, err } key := dynamoDBSource.Key response, err := client.GetItem(&dynamodb.GetItemInput{ Key: map[string]*dynamodb.AttributeValue{ "key": {S: aws.String(key)}, }, TableName: tableName, ConsistentRead: aws.Bool(true), }) if err != nil { return nil, err } result := make(map[string]interface{}) err = dynamodbattribute.ConvertFromMap(response.Item, &result) delete(result, key) return result, err }
// Query the registry for named service func (c *Client) Discover(name string) (*Service, error) { // Make sure registry is active if active, _ := c.Registry.IsActive(); active == true { expressionAttributeValues := map[string]interface{}{ ":NameVal": name, ":ExpiryVal": time.Now().Unix(), } ean := map[string]*string{ "#N": aws.String("Name"), } eav, err := dynamodbattribute.ConvertToMap(expressionAttributeValues) if err != nil { return nil, err } resp, err := c.svc.Query(&dynamodb.QueryInput{ TableName: c.config.GetRegistryName(), KeyConditionExpression: aws.String("#N = :NameVal"), FilterExpression: aws.String("Expiry > :ExpiryVal"), ExpressionAttributeValues: eav, ExpressionAttributeNames: ean, }) if err != nil { return nil, err } if len(resp.Items) > 0 { // Randomly select one of the available endpoints (in effect load balancing between available endpoints) service := Service{} err = dynamodbattribute.ConvertFromMap(resp.Items[rand.Intn(len(resp.Items))], &service) if err != nil { return nil, err } return &service, nil } else { // No service found return nil, ErrServiceNotFound } } else { return nil, ErrRegistryNotActive } }
// Query queries items to the table and convert it to v. v must be a slice of struct. // If the Query operation does not return the last page, LastEvaluatedKey will be returned. func (t *Table) Query(slice interface{}, opts ...option.QueryInput) (map[string]*dynamodb.AttributeValue, error) { req := &dynamodb.QueryInput{ TableName: t.Name, } for _, f := range opts { if err := f(req); err != nil { return nil, err } } resp, err := t.DynamoDB.Query(req) if err != nil { return nil, err } v := reflect.ValueOf(slice) typ := v.Type() if !(typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Slice) { return nil, fmt.Errorf("dynamodb: slice must be a pointer to slice but %s", typ) } items := reflect.MakeSlice(typ.Elem(), 0, len(resp.Items)) for _, i := range resp.Items { p := reflect.New(typ.Elem().Elem()) // Use ItemUnmarshaler if available var err error if v, ok := p.Interface().(item.Unmarshaler); ok { err = v.UnmarshalItem(i) } else { err = dynamodbattribute.ConvertFromMap(i, p.Interface()) } if err != nil { return nil, err } items = reflect.Append(items, p.Elem()) } reflect.Indirect(v).Set(items) return resp.LastEvaluatedKey, nil }
func TestNewMockCtrl(t *testing.T) { dynmodbservicemock := NewMockService(t) dynmodbservicemock.EXPECT().GetItem(&dynamodb.GetItemInput{ TableName: aws.String("testtable"), Key: map[string]*dynamodb.AttributeValue{ "IdKey": &dynamodb.AttributeValue{ S: aws.String("mykey"), }, }, }).Return(&dynamodb.GetItemOutput{ Item: map[string]*dynamodb.AttributeValue{ "IdKey": &dynamodb.AttributeValue{ S: aws.String("mykey"), }, "TestValue": &dynamodb.AttributeValue{ S: aws.String("test value"), }, }, }, nil) params := &dynamodb.GetItemInput{ TableName: aws.String("testtable"), Key: map[string]*dynamodb.AttributeValue{ "IdKey": &dynamodb.AttributeValue{ S: aws.String("mykey"), }, }, } resp, err := dynmodbservicemock.GetItem(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } testObj := &TestStruct{} err = dynamodbattribute.ConvertFromMap(resp.Item, testObj) log.Printf("%+v\n", testObj) }
// UnmarshalItem implements ItemUnmarshaler interface. func (i *TestItem) UnmarshalItem(item map[string]*dynamodb.AttributeValue) error { role := item["role"] // dynamodbattribute.ConvertFromMap does not support StringSet so unset it delete(item, "role") if err := dynamodbattribute.ConvertFromMap(item, i); err != nil { return err } // restore role by hand if role == nil || role.SS == nil { // empty role is still legal return nil } for _, s := range role.SS { i.Role = append(i.Role, *s) } return nil }
// UnmarshalDynamoDB implements the dynamotree.Storable interface func (l *Link) UnmarshalDynamoDB(item map[string]*dynamodb.AttributeValue) error { return dynamodbattribute.ConvertFromMap(item, l) }
// UnmarshalDynamoDB implements the dynamotree.Storable interface func (a *Account) UnmarshalDynamoDB(item map[string]*dynamodb.AttributeValue) error { return dynamodbattribute.ConvertFromMap(item, a) }