Example #1
0
// Deletes corresponding item in the given table.
func (s *Service) Delete(tableName string, item interface{}) error {
	keys, err := types.Marshal(item, true)
	if err != nil {
		return nil
	}
	body := struct {
		TableName string
		Key       types.AttributeValue
	}{
		TableName: tableName,
		Key:       keys,
	}
	return s.Do("DeleteItem", body, nil)
}
Example #2
0
// Create or replace the item in the given table.
func (s *Service) Put(tableName string, item interface{}) error {
	values, err := types.Marshal(item, false)
	if err != nil {
		return err
	}
	body := struct {
		TableName string
		Item      types.AttributeValue
	}{
		TableName: tableName,
		Item:      values,
	}
	return s.Do("PutItem", body, nil)
}
Example #3
0
// Get the corresponding item from the given table.
func (s *Service) Get(tableName string, item interface{}) error {
	keys, err := types.Marshal(item, true)
	if err != nil {
		return err
	}
	var resp struct {
		Item types.AttributeValue
	}
	body := struct {
		TableName string
		Key       types.AttributeValue
	}{
		TableName: tableName,
		Key:       keys,
	}
	err = s.Do("GetItem", body, &resp)
	if err != nil {
		return err
	}
	return types.Unmarshal(resp.Item, item)
}
Example #4
0
func (s *Service) BatchDelete(tableName string, items interface{}) error {
	rv := reflect.ValueOf(items)
	requests := make(types.WriteRequests)
	deletes := make([]*types.WriteRequest, 0)
	for i := 0; i < rv.Len(); i++ {
		value, err := types.Marshal(rv.Index(i).Interface(), true)
		if err != nil {
			return err
		}
		deletes = append(deletes, &types.WriteRequest{
			DeleteRequest: &types.DeleteRequest{
				Key: value,
			},
		})
	}
	requests[tableName] = deletes
	body := struct {
		RequestItems types.WriteRequests
	}{
		RequestItems: requests,
	}
	return s.Do("BatchWriteItem", body, nil)
}