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

     1  // Support for the DynamoDB PutItem endpoint.
     2  //
     3  // example use:
     4  //
     5  // tests/put_item-livestest.go
     6  //
     7  package put_item
     8  
     9  import (
    10  	"encoding/json"
    11  	"errors"
    12  	"github.com/smugmug/godynamo/authreq"
    13  	"github.com/smugmug/godynamo/aws_const"
    14  	"github.com/smugmug/godynamo/conf"
    15  	"github.com/smugmug/godynamo/types/attributesresponse"
    16  	"github.com/smugmug/godynamo/types/attributevalue"
    17  	"github.com/smugmug/godynamo/types/aws_strings"
    18  	"github.com/smugmug/godynamo/types/expected"
    19  	"github.com/smugmug/godynamo/types/expressionattributenames"
    20  	"github.com/smugmug/godynamo/types/item"
    21  )
    22  
    23  const (
    24  	ENDPOINT_NAME      = "PutItem"
    25  	JSON_ENDPOINT_NAME = ENDPOINT_NAME + "JSON"
    26  	PUTITEM_ENDPOINT   = aws_const.ENDPOINT_PREFIX + ENDPOINT_NAME
    27  	// the permitted ReturnValues flags for this op
    28  	RETVAL_ALL_OLD = aws_strings.RETVAL_ALL_OLD
    29  	RETVAL_NONE    = aws_strings.RETVAL_NONE
    30  )
    31  
    32  type PutItem struct {
    33  	ConditionExpression         string                                            `json:",omitempty"`
    34  	ConditionalOperator         string                                            `json:",omitempty"`
    35  	Expected                    expected.Expected                                 `json:",omitempty"`
    36  	ExpressionAttributeNames    expressionattributenames.ExpressionAttributeNames `json:",omitempty"`
    37  	ExpressionAttributeValues   attributevalue.AttributeValueMap                  `json:",omitempty"`
    38  	Item                        item.Item
    39  	ReturnConsumedCapacity      string `json:",omitempty"`
    40  	ReturnItemCollectionMetrics string `json:",omitempty"`
    41  	ReturnValues                string `json:",omitempty"`
    42  	TableName                   string
    43  }
    44  
    45  // NewPut will return a pointer to an initialized PutItem struct.
    46  func NewPutItem() *PutItem {
    47  	p := new(PutItem)
    48  	p.Expected = expected.NewExpected()
    49  	p.ExpressionAttributeNames = expressionattributenames.NewExpressionAttributeNames()
    50  	p.ExpressionAttributeValues = attributevalue.NewAttributeValueMap()
    51  	p.Item = item.NewItem()
    52  	return p
    53  }
    54  
    55  type Request PutItem
    56  
    57  // Put is an alias for backwards compatibility
    58  type Put PutItem
    59  
    60  func NewPut() *Put {
    61  	put_item := NewPutItem()
    62  	put := Put(*put_item)
    63  	return &put
    64  }
    65  
    66  // PutItemJSON differs from PutItem in that JSON is a string, which allows you to use a basic
    67  // JSON document as the Item
    68  type PutItemJSON struct {
    69  	ConditionExpression         string                                            `json:",omitempty"`
    70  	ConditionalOperator         string                                            `json:",omitempty"`
    71  	Expected                    expected.Expected                                 `json:",omitempty"`
    72  	ExpressionAttributeNames    expressionattributenames.ExpressionAttributeNames `json:",omitempty"`
    73  	ExpressionAttributeValues   attributevalue.AttributeValueMap                  `json:",omitempty"`
    74  	Item                        interface{}
    75  	ReturnConsumedCapacity      string `json:",omitempty"`
    76  	ReturnItemCollectionMetrics string `json:",omitempty"`
    77  	ReturnValues                string `json:",omitempty"`
    78  	TableName                   string
    79  }
    80  
    81  // NewPutJSON will return a pointer to an initialized PutItemJSON struct.
    82  func NewPutItemJSON() *PutItemJSON {
    83  	p := new(PutItemJSON)
    84  	p.Expected = expected.NewExpected()
    85  	p.ExpressionAttributeNames = expressionattributenames.NewExpressionAttributeNames()
    86  	p.ExpressionAttributeValues = attributevalue.NewAttributeValueMap()
    87  	return p
    88  }
    89  
    90  // ToPutItem will attempt to convert a PutItemJSON to PutItem
    91  func (put_item_json *PutItemJSON) ToPutItem() (*PutItem, error) {
    92  	if put_item_json == nil {
    93  		return nil, errors.New("receiver is nil")
    94  	}
    95  	a, cerr := attributevalue.InterfaceToAttributeValueMap(put_item_json.Item)
    96  	if cerr != nil {
    97  		return nil, cerr
    98  	}
    99  	p := NewPutItem()
   100  	p.ConditionExpression = put_item_json.ConditionExpression
   101  	p.ConditionalOperator = put_item_json.ConditionalOperator
   102  	p.Expected = put_item_json.Expected
   103  	p.ExpressionAttributeNames = put_item_json.ExpressionAttributeNames
   104  	p.ExpressionAttributeValues = put_item_json.ExpressionAttributeValues
   105  	p.Item = item.Item(a)
   106  	p.ReturnConsumedCapacity = put_item_json.ReturnConsumedCapacity
   107  	p.ReturnItemCollectionMetrics = put_item_json.ReturnItemCollectionMetrics
   108  	p.ReturnValues = put_item_json.ReturnValues
   109  	p.TableName = put_item_json.TableName
   110  	return p, nil
   111  }
   112  
   113  type Response attributesresponse.AttributesResponse
   114  
   115  func NewResponse() *Response {
   116  	a := attributesresponse.NewAttributesResponse()
   117  	r := Response(*a)
   118  	return &r
   119  }
   120  
   121  // These implementations of EndpointReq use a parameterized conf.
   122  
   123  func (put_item *PutItem) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
   124  	if put_item == nil {
   125  		return nil, 0, errors.New("put_item.(PutItem)EndpointReqWithConf: receiver is nil")
   126  	}
   127  	if !conf.IsValid(c) {
   128  		return nil, 0, errors.New("put_item.EndpointReqWithConf: c is not valid")
   129  	}
   130  	// returns resp_body,code,err
   131  	reqJSON, json_err := json.Marshal(put_item)
   132  	if json_err != nil {
   133  		return nil, 0, json_err
   134  	}
   135  	return authreq.RetryReqJSON_V4WithConf(reqJSON, PUTITEM_ENDPOINT, c)
   136  }
   137  
   138  func (put *Put) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
   139  	if put == nil {
   140  		return nil, 0, errors.New("put_item.(Put)EndpointReqWithConf: receiver is nil")
   141  	}
   142  	put_item := PutItem(*put)
   143  	return put_item.EndpointReqWithConf(c)
   144  }
   145  
   146  func (req *Request) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) {
   147  	if req == nil {
   148  		return nil, 0, errors.New("put_item.(Request)EndpointReqWithConf: receiver is nil")
   149  	}
   150  	put_item := PutItem(*req)
   151  	return put_item.EndpointReqWithConf(c)
   152  }
   153  
   154  // These implementations of EndpointReq use the global conf.
   155  
   156  func (put_item *PutItem) EndpointReq() ([]byte, int, error) {
   157  	if put_item == nil {
   158  		return nil, 0, errors.New("put_item.(PutItem)EndpointReq: receiver is nil")
   159  	}
   160  	return put_item.EndpointReqWithConf(&conf.Vals)
   161  }
   162  
   163  func (put *Put) EndpointReq() ([]byte, int, error) {
   164  	if put == nil {
   165  		return nil, 0, errors.New("put_item.(Put)EndpointReq: receiver is nil")
   166  	}
   167  	put_item := PutItem(*put)
   168  	return put_item.EndpointReqWithConf(&conf.Vals)
   169  }
   170  
   171  func (req *Request) EndpointReq() ([]byte, int, error) {
   172  	if req == nil {
   173  		return nil, 0, errors.New("put_item.(Request)EndpointReq: receiver is nil")
   174  	}
   175  	put_item := PutItem(*req)
   176  	return put_item.EndpointReqWithConf(&conf.Vals)
   177  }
   178  
   179  // ValidItem validates the size of a json serialization of an Item.
   180  // AWS says items can only be 400k bytes binary
   181  func ValidItem(i string) bool {
   182  	return !(len([]byte(i)) > 400000)
   183  }