github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/endpoints/update_item/update_item.go (about) 1 // Support for the DynamoDB UpdateItem endpoint. 2 // 3 // example use: 4 // 5 // tests/item_operations-livestest.go 6 // 7 package update_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 = "UpdateItem" 25 UPDATEITEM_ENDPOINT = aws_const.ENDPOINT_PREFIX + ENDPOINT_NAME 26 // the permitted Action flags for this op 27 ACTION_PUT = aws_strings.ACTION_PUT 28 ACTION_DEL = aws_strings.ACTION_DEL 29 ACTION_ADD = aws_strings.ACTION_ADD 30 // the permitted ReturnValues flags for this op 31 RETVAL_ALL_OLD = aws_strings.RETVAL_ALL_OLD 32 RETVAL_ALL_NEW = aws_strings.RETVAL_ALL_NEW 33 RETVAL_UPDATED_OLD = aws_strings.RETVAL_UPDATED_OLD 34 RETVAL_UPDATED_NEW = aws_strings.RETVAL_UPDATED_NEW 35 RETVAL_NONE = aws_strings.RETVAL_NONE 36 ) 37 38 type AttributeUpdates attributevalue.AttributeValueMap 39 40 type UpdateItem struct { 41 AttributeUpdates attributevalue.AttributeValueUpdateMap `json:",omitempty"` 42 ConditionExpression string `json:",omitempty"` 43 ConditionalOperator string `json:",omitempty"` 44 Expected expected.Expected `json:",omitempty"` 45 ExpressionAttributeNames expressionattributenames.ExpressionAttributeNames `json:",omitempty"` 46 ExpressionAttributeValues attributevalue.AttributeValueMap `json:",omitempty"` 47 Key item.Key 48 ReturnConsumedCapacity string `json:",omitempty"` 49 ReturnValues string `json:",omitempty"` 50 ReturnItemCollectionMetrics string `json:",omitempty"` 51 TableName string 52 UpdateExpression string `json:",omitempty"` 53 } 54 55 // NewUpdate returns a pointer to an instantiation of the Update struct. 56 func NewUpdateItem() *UpdateItem { 57 u := new(UpdateItem) 58 u.AttributeUpdates = attributevalue.NewAttributeValueUpdateMap() 59 u.Expected = expected.NewExpected() 60 u.ExpressionAttributeNames = expressionattributenames.NewExpressionAttributeNames() 61 u.ExpressionAttributeValues = attributevalue.NewAttributeValueMap() 62 u.Key = item.NewKey() 63 return u 64 } 65 66 // Update is an alias for backwards compatibility 67 type Update UpdateItem 68 69 func NewUpdate() *Update { 70 update_item := NewUpdateItem() 71 update := Update(*update_item) 72 return &update 73 } 74 75 type Request UpdateItem 76 77 type Response attributesresponse.AttributesResponse 78 79 func NewResponse() *Response { 80 a := attributesresponse.NewAttributesResponse() 81 r := Response(*a) 82 return &r 83 } 84 85 // These implementations of EndpointReq use a parameterized conf. 86 87 func (update_item *UpdateItem) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) { 88 if update_item == nil { 89 return nil, 0, errors.New("update_item.(UpdateItem)EndpointReqWithConf: receiver is nil") 90 } 91 if !conf.IsValid(c) { 92 return nil, 0, errors.New("update_item.EndpointReqWithConf: c is not valid") 93 } 94 // returns resp_body,code,err 95 reqJSON, json_err := json.Marshal(update_item) 96 if json_err != nil { 97 return nil, 0, json_err 98 } 99 return authreq.RetryReqJSON_V4WithConf(reqJSON, UPDATEITEM_ENDPOINT, c) 100 } 101 102 func (update *Update) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) { 103 if update == nil { 104 return nil, 0, errors.New("update_item.(Update)EndpointReqWithConf: receiver is nil") 105 } 106 update_item := UpdateItem(*update) 107 return update_item.EndpointReqWithConf(c) 108 } 109 110 func (req *Request) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) { 111 if req == nil { 112 return nil, 0, errors.New("update_item.(Request)EndpointReqWithConf: receiver is nil") 113 } 114 update_item := UpdateItem(*req) 115 return update_item.EndpointReqWithConf(c) 116 } 117 118 // These implementations of EndpointReq use the global conf. 119 120 func (update_item *UpdateItem) EndpointReq() ([]byte, int, error) { 121 if update_item == nil { 122 return nil, 0, errors.New("update_item.(UpdateItem)EndpointReq: receiver is nil") 123 } 124 return update_item.EndpointReqWithConf(&conf.Vals) 125 } 126 127 func (update *Update) EndpointReq() ([]byte, int, error) { 128 if update == nil { 129 return nil, 0, errors.New("update_item.(Update)EndpointReq: receiver is nil") 130 } 131 update_item := UpdateItem(*update) 132 return update_item.EndpointReqWithConf(&conf.Vals) 133 } 134 135 func (req *Request) EndpointReq() ([]byte, int, error) { 136 if req == nil { 137 return nil, 0, errors.New("update_item.(Request)EndpointReq: receiver is nil") 138 } 139 update_item := UpdateItem(*req) 140 return update_item.EndpointReqWithConf(&conf.Vals) 141 }