github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/endpoints/get_item/get_item.go (about) 1 // Support for the DynamoDB GetItem endpoint. 2 // 3 // example use: 4 // 5 // tests/get_item-livestest.go 6 // 7 package get_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/attributestoget" 16 "github.com/smugmug/godynamo/types/attributevalue" 17 "github.com/smugmug/godynamo/types/capacity" 18 "github.com/smugmug/godynamo/types/expressionattributenames" 19 "github.com/smugmug/godynamo/types/item" 20 ) 21 22 const ( 23 ENDPOINT_NAME = "GetItem" 24 JSON_ENDPOINT_NAME = ENDPOINT_NAME + "JSON" 25 GETITEM_ENDPOINT = aws_const.ENDPOINT_PREFIX + ENDPOINT_NAME 26 ) 27 28 type GetItem struct { 29 AttributesToGet attributestoget.AttributesToGet `json:",omitempty"` 30 ConsistentRead bool // false is sane default 31 ExpressionAttributeNames expressionattributenames.ExpressionAttributeNames `json:",omitempty"` 32 Key item.Key 33 ProjectionExpression string `json:",omitempty"` 34 ReturnConsumedCapacity string `json:",omitempty"` 35 TableName string 36 } 37 38 func NewGetItem() *GetItem { 39 g := new(GetItem) 40 g.Key = item.NewKey() 41 g.ExpressionAttributeNames = expressionattributenames.NewExpressionAttributeNames() 42 g.AttributesToGet = make(attributestoget.AttributesToGet, 0) 43 return g 44 } 45 46 // Get is an alias for backwards compatibility 47 type Get GetItem 48 49 func NewGet() *Get { 50 get_item := NewGetItem() 51 get := Get(*get_item) 52 return &get 53 } 54 55 type Request GetItem 56 57 type Response struct { 58 Item item.Item 59 ConsumedCapacity *capacity.ConsumedCapacity `json:",omitempty"` 60 } 61 62 type response Response 63 64 type response_no_capacity struct { 65 Item item.Item 66 } 67 68 func NewResponse() *Response { 69 r := new(Response) 70 r.Item = item.NewItem() 71 r.ConsumedCapacity = capacity.NewConsumedCapacity() 72 return r 73 } 74 75 func (r Response) MarshalJSON() ([]byte, error) { 76 if r.ConsumedCapacity.Empty() { 77 var ri response_no_capacity 78 ri.Item = r.Item 79 return json.Marshal(ri) 80 } 81 ri := response(r) 82 return json.Marshal(ri) 83 } 84 85 // ResponseItemJSON can be formed from a Response when the caller wishes 86 // to receive the Item as basic JSON. 87 type ResponseItemJSON struct { 88 Item interface{} 89 ConsumedCapacity *capacity.ConsumedCapacity `json:",omitempty"` 90 } 91 92 type responseItemJSON ResponseItemJSON 93 94 type responseItemJSON_no_capacity struct { 95 Item interface{} 96 } 97 98 func NewResponseItemJSON() *ResponseItemJSON { 99 r := new(ResponseItemJSON) 100 r.ConsumedCapacity = capacity.NewConsumedCapacity() 101 return r 102 } 103 104 func (r ResponseItemJSON) MarshalJSON() ([]byte, error) { 105 if r.ConsumedCapacity.Empty() { 106 var ri responseItemJSON_no_capacity 107 ri.Item = r.Item 108 return json.Marshal(ri) 109 } 110 ri := responseItemJSON(r) 111 return json.Marshal(ri) 112 } 113 114 // ToResponseItemJSON will try to convert the Response to a ResponseItemJSON, 115 // where the interface value for Item represents a structure that can be 116 // marshaled into basic JSON. 117 func (resp *Response) ToResponseItemJSON() (*ResponseItemJSON, error) { 118 if resp == nil { 119 return nil, errors.New("receiver is nil") 120 } 121 a := attributevalue.AttributeValueMap(resp.Item) 122 c, cerr := a.ToInterface() 123 if cerr != nil { 124 return nil, cerr 125 } 126 resp_json := NewResponseItemJSON() 127 resp_json.ConsumedCapacity = resp.ConsumedCapacity 128 resp_json.Item = c 129 return resp_json, nil 130 } 131 132 // These implementations of EndpointReq use a parameterized conf. 133 134 func (get_item *GetItem) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) { 135 if get_item == nil { 136 return nil, 0, errors.New("get_item.(GetItem)EndpointReqWithConf: receiver is nil") 137 } 138 if !conf.IsValid(c) { 139 return nil, 0, errors.New("get_item.EndpointReqWithConf: c is not valid") 140 } 141 // returns resp_body,code,err 142 reqJSON, json_err := json.Marshal(get_item) 143 if json_err != nil { 144 return nil, 0, json_err 145 } 146 return authreq.RetryReqJSON_V4WithConf(reqJSON, GETITEM_ENDPOINT, c) 147 } 148 149 func (get *Get) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) { 150 if get == nil { 151 return nil, 0, errors.New("get_item.(Get)EndpointReqWithConf: receiver is nil") 152 } 153 get_item := GetItem(*get) 154 return get_item.EndpointReqWithConf(c) 155 } 156 157 func (req *Request) EndpointReqWithConf(c *conf.AWS_Conf) ([]byte, int, error) { 158 if req == nil { 159 return nil, 0, errors.New("get_item.(Request)EndpointReqWithConf: receiver is nil") 160 } 161 get_item := GetItem(*req) 162 return get_item.EndpointReqWithConf(c) 163 } 164 165 // These implementations of EndpointReq use the global conf. 166 167 func (get_item *GetItem) EndpointReq() ([]byte, int, error) { 168 if get_item == nil { 169 return nil, 0, errors.New("get_item.(GetItem)EndpointReq: receiver is nil") 170 } 171 return get_item.EndpointReqWithConf(&conf.Vals) 172 } 173 174 func (get *Get) EndpointReq() ([]byte, int, error) { 175 if get == nil { 176 return nil, 0, errors.New("get_item.(Get)EndpointReq: receiver is nil") 177 } 178 get_item := GetItem(*get) 179 return get_item.EndpointReqWithConf(&conf.Vals) 180 } 181 182 func (req *Request) EndpointReq() ([]byte, int, error) { 183 if req == nil { 184 return nil, 0, errors.New("get_item.(GetItem)EndpointReq: receiver is nil") 185 } 186 get_item := GetItem(*req) 187 return get_item.EndpointReqWithConf(&conf.Vals) 188 }