github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/types/item/item.go (about)

     1  // Item as described in docs for various endpoints.
     2  package item
     3  
     4  import (
     5  	"errors"
     6  	"fmt"
     7  	"github.com/smugmug/godynamo/types/attributevalue"
     8  )
     9  
    10  type Item attributevalue.AttributeValueMap
    11  
    12  type item Item
    13  
    14  // Item is already a reference type
    15  func NewItem() Item {
    16  	a := attributevalue.NewAttributeValueMap()
    17  	return Item(a)
    18  }
    19  
    20  // Copy an Item
    21  func (i Item) Copy(ic Item) error {
    22  	if i == nil {
    23  		return errors.New("Item.Copy: pointer receiver is nil")
    24  	}
    25  	if ic == nil {
    26  		return errors.New("Item.Copy: copy target Item instance is nil")
    27  	}
    28  
    29  	for k, av := range i {
    30  		ac := attributevalue.NewAttributeValue()
    31  		if ac == nil {
    32  			return errors.New("Item.Copy: copy target attributeValue is nil")
    33  		}
    34  		cp_err := av.Copy(ac)
    35  		if cp_err != nil {
    36  			e := fmt.Sprintf("Item.Copy:%s", cp_err.Error())
    37  			return errors.New(e)
    38  		}
    39  		ic[k] = ac
    40  	}
    41  	return nil
    42  }
    43  
    44  // ItemLike is an interface for those structs you wish to map back and forth to Items.
    45  // This is currently provided instead of the lossy translation advocated by the
    46  // JSON document mapping as described by AWS.
    47  type ItemLike interface {
    48  	ToItem(interface{}) (Item, error)
    49  	FromItem(Item) (interface{}, error)
    50  }
    51  
    52  // GetItem and UpdateItem share a Key type which is another alias to AttributeValueMap
    53  type Key attributevalue.AttributeValueMap
    54  
    55  func NewKey() Key {
    56  	a := attributevalue.NewAttributeValueMap()
    57  	return Key(a)
    58  }