func main() { // this is the same as put-item-livetest except here we demonstrate using a parameterized conf home := os.Getenv("HOME") home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file) if home_conf_err != nil { panic("cannot read conf from " + home_conf_file) } home_conf.ConfLock.RLock() if home_conf.Initialized == false { panic("conf struct has not been initialized") } // launch a background poller to keep conns to aws alive if home_conf.Network.DynamoDB.KeepAlive { log.Printf("launching background keepalive") go keepalive.KeepAlive([]string{}) } // deal with iam, or not if home_conf.UseIAM { iam_ready_chan := make(chan bool) go conf_iam.GoIAM(iam_ready_chan) _ = <-iam_ready_chan } home_conf.ConfLock.RUnlock() put1 := put.NewPutItem() put1.TableName = "test-godynamo-livetest" k := fmt.Sprintf("hk1") v := fmt.Sprintf("%v", time.Now().Unix()) // In simple cases you don't need to call NewAttributeValue put1.Item["TheHashKey"] = &attributevalue.AttributeValue{S: k} put1.Item["TheRangeKey"] = &attributevalue.AttributeValue{N: v} n := fmt.Sprintf("%v", time.Now().Unix()) put1.Item["Mtime"] = &attributevalue.AttributeValue{N: n} put1.Item["SomeJunk"] = &attributevalue.AttributeValue{S: "some junk"} // for complex attributevalue instances, call the constructor first av := attributevalue.NewAttributeValue() av.InsertSS("some junk1") av.InsertSS("some junk2") put1.Item["SomeJunks"] = av av2 := attributevalue.NewAttributeValue() av2.InsertL(&attributevalue.AttributeValue{S: "some junk1"}) av2.InsertL(&attributevalue.AttributeValue{S: "some junk2"}) put1.Item["JunkL"] = av2 av3 := attributevalue.NewAttributeValue() av3.InsertM("somejunkkey", &attributevalue.AttributeValue{S: "some junk1"}) body, code, err := put1.EndpointReqWithConf(home_conf) if err != nil || code != http.StatusOK { fmt.Printf("put failed %d %v %s\n", code, err, body) } fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) }
func main() { conf_file.Read() conf.Vals.ConfLock.RLock() if conf.Vals.Initialized == false { panic("the conf.Vals global conf struct has not been initialized") } // launch a background poller to keep conns to aws alive if conf.Vals.Network.DynamoDB.KeepAlive { log.Printf("launching background keepalive") go keepalive.KeepAlive([]string{}) } // deal with iam, or not if conf.Vals.UseIAM { iam_ready_chan := make(chan bool) go conf_iam.GoIAM(iam_ready_chan) _ = <-iam_ready_chan } conf.Vals.ConfLock.RUnlock() put1 := put.NewPutItem() put1.TableName = "test-godynamo-livetest" k := fmt.Sprintf("hk1") v := fmt.Sprintf("%v", time.Now().Unix()) // In simple cases you don't need to call NewAttributeValue put1.Item["TheHashKey"] = &attributevalue.AttributeValue{S: k} put1.Item["TheRangeKey"] = &attributevalue.AttributeValue{N: v} n := fmt.Sprintf("%v", time.Now().Unix()) put1.Item["Mtime"] = &attributevalue.AttributeValue{N: n} put1.Item["SomeJunk"] = &attributevalue.AttributeValue{S: "some junk"} // for complex attributevalue instances, call the constructor first av := attributevalue.NewAttributeValue() av.InsertSS("some junk1") av.InsertSS("some junk2") put1.Item["SomeJunks"] = av av2 := attributevalue.NewAttributeValue() av2.InsertL(&attributevalue.AttributeValue{S: "some junk1"}) av2.InsertL(&attributevalue.AttributeValue{S: "some junk2"}) put1.Item["JunkL"] = av2 av3 := attributevalue.NewAttributeValue() av3.InsertM("somejunkkey", &attributevalue.AttributeValue{S: "some junk1"}) body, code, err := put1.EndpointReq() if err != nil || code != http.StatusOK { fmt.Printf("put failed %d %v %s\n", code, err, body) } fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) }
func NewConstraints() *Constraints { c := new(Constraints) c.Exists = new(bool) c.Value = attributevalue.NewAttributeValue() c.AttributeValueList = make([]*attributevalue.AttributeValue, 0) return c }
// unprocessedKeys2BatchGetItems will take a response from DynamoDB that indicates some Keys // require resubmitting, and turns these into a BatchGetItem struct instance. func unprocessedKeys2BatchGetItems(req *BatchGetItem, resp *Response) (*BatchGetItem, error) { if req == nil || resp == nil { return nil, errors.New("batch_get_item.unprocessedKeys2BatchGetItems: one of req or resp is nil") } b := NewBatchGetItem() b.ReturnConsumedCapacity = req.ReturnConsumedCapacity for tn := range resp.UnprocessedKeys { if _, tn_in_b := b.RequestItems[tn]; !tn_in_b { b.RequestItems[tn] = NewRequestInstance() b.RequestItems[tn].AttributesToGet = make( attributestoget.AttributesToGet, len(resp.UnprocessedKeys[tn].AttributesToGet)) copy(b.RequestItems[tn].AttributesToGet, resp.UnprocessedKeys[tn].AttributesToGet) b.RequestItems[tn].ConsistentRead = resp.UnprocessedKeys[tn].ConsistentRead for _, item_src := range resp.UnprocessedKeys[tn].Keys { item_cp := item.NewItem() for k, v := range item_src { v_cp := attributevalue.NewAttributeValue() cp_err := v.Copy(v_cp) if cp_err != nil { return nil, cp_err } item_cp[k] = v_cp } b.RequestItems[tn].Keys = append(b.RequestItems[tn].Keys, item_cp) } } } return b, nil }
func (c *Constraints) UnmarshalJSON(data []byte) error { if c == nil { return errors.New("Expected.UnmarshalJSON: pointer receiver for unmarshal is nil") } var ci constraints t_err := json.Unmarshal(data, &ci) if t_err != nil { return t_err } if c.Exists == nil { c.Exists = new(bool) } if ci.Exists == nil { c.Exists = nil } else { *c.Exists = *ci.Exists } if ci.ComparisonOperator != "" { c.ComparisonOperator = ci.ComparisonOperator } if ci.Value != nil { c.Value = attributevalue.NewAttributeValue() cp_err := ci.Value.Copy(c.Value) if cp_err != nil { return cp_err } } l_ci_avl := len(ci.AttributeValueList) if l_ci_avl != 0 { c.AttributeValueList = make([]*attributevalue.AttributeValue, l_ci_avl) for i := range ci.AttributeValueList { c.AttributeValueList[i] = attributevalue.NewAttributeValue() cp_err := ci.AttributeValueList[i].Copy(c.AttributeValueList[i]) if cp_err != nil { return cp_err } } } return nil }
func main() { // this is the same as put-item-livetest except here we demonstrate using a parameterized conf home := os.Getenv("HOME") home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file) if home_conf_err != nil { panic("cannot read conf from " + home_conf_file) } home_conf.ConfLock.RLock() if home_conf.Initialized == false { panic("conf struct has not been initialized") } // launch a background poller to keep conns to aws alive if home_conf.Network.DynamoDB.KeepAlive { log.Printf("launching background keepalive") go keepalive.KeepAlive([]string{}) } // deal with iam, or not if home_conf.UseIAM { iam_ready_chan := make(chan bool) go conf_iam.GoIAM(iam_ready_chan) _ = <-iam_ready_chan } home_conf.ConfLock.RUnlock() put1 := put.NewPutItem() put1.TableName = "test-godynamo-livetest" k := fmt.Sprintf("hk1000") v := fmt.Sprintf("%v", time.Now().Unix()) put1.Item["TheHashKey"] = &attributevalue.AttributeValue{S: k} put1.Item["TheRangeKey"] = &attributevalue.AttributeValue{N: v} i := fmt.Sprintf("%v", 1) t := fmt.Sprintf("%v", time.Now().Unix()) put1.Item["UserID"] = &attributevalue.AttributeValue{N: i} put1.Item["Timestamp"] = &attributevalue.AttributeValue{N: t} // the Token field is a simple string put1.Item["Token"] = &attributevalue.AttributeValue{S: "a token"} // the Location must be created as a "map" location := attributevalue.NewAttributeValue() location.InsertM("Latitude", &attributevalue.AttributeValue{N: "120.01"}) location.InsertM("Longitude", &attributevalue.AttributeValue{N: "50.99"}) put1.Item["Location"] = location body, code, err := put1.EndpointReqWithConf(home_conf) if err != nil || code != http.StatusOK { fmt.Printf("put failed %d %v %s\n", code, err, body) } fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) }
// unprocessedKeys2BatchWriteItems will take a response from DynamoDB that indicates some Keys // require resubmitting, and turns these into a BatchWriteItem struct instance. func unprocessedItems2BatchWriteItems(req *BatchWriteItem, resp *Response) (*BatchWriteItem, error) { if req == nil || resp == nil { return nil, errors.New("batch_write_item.unprocessedItems2BatchWriteItems: req or resp is nil") } b := NewBatchWriteItem() for tn := range resp.UnprocessedItems { for _, reqinst := range resp.UnprocessedItems[tn] { var reqinst_cp RequestInstance if reqinst.DeleteRequest != nil { reqinst_cp.DeleteRequest = new(DeleteRequest) reqinst_cp.DeleteRequest.Key = make(item.Item) for k, v := range reqinst.DeleteRequest.Key { v_cp := attributevalue.NewAttributeValue() cp_err := v.Copy(v_cp) if cp_err != nil { return nil, cp_err } reqinst_cp.DeleteRequest.Key[k] = v_cp } b.RequestItems[tn] = append(b.RequestItems[tn], reqinst_cp) } else if reqinst.PutRequest != nil { reqinst_cp.PutRequest = new(PutRequest) reqinst_cp.PutRequest.Item = make(item.Item) for k, v := range reqinst.PutRequest.Item { v_cp := attributevalue.NewAttributeValue() cp_err := v.Copy(v_cp) if cp_err != nil { return nil, cp_err } reqinst_cp.PutRequest.Item[k] = v_cp } b.RequestItems[tn] = append(b.RequestItems[tn], reqinst_cp) } } } b.ReturnConsumedCapacity = req.ReturnConsumedCapacity b.ReturnItemCollectionMetrics = req.ReturnItemCollectionMetrics return b, nil }
// Copy an Item func (i Item) Copy(ic Item) error { if i == nil { return errors.New("Item.Copy: pointer receiver is nil") } if ic == nil { return errors.New("Item.Copy: copy target Item instance is nil") } for k, av := range i { ac := attributevalue.NewAttributeValue() if ac == nil { return errors.New("Item.Copy: copy target attributeValue is nil") } cp_err := av.Copy(ac) if cp_err != nil { e := fmt.Sprintf("Item.Copy:%s", cp_err.Error()) return errors.New(e) } ic[k] = ac } return nil }
// Add actual response data from "this" Response to "all", the eventual stitched Response. func combineResponses(all, this *Response) error { if all == nil || this == nil { return errors.New("batch_get_item.combineResponses: all or this is nil") } for tn := range this.Responses { if _, tn_in_all := all.Responses[tn]; !tn_in_all { all.Responses[tn] = make([]item.Item, 0) } for _, item_src := range this.Responses[tn] { item_cp := item.NewItem() for k, v := range item_src { v_cp := attributevalue.NewAttributeValue() cp_err := v.Copy(v_cp) if cp_err != nil { return cp_err } item_cp[k] = v_cp } all.Responses[tn] = append(all.Responses[tn], item_cp) } } return nil }
func main() { // this is the same as item-operations-livetest except here we demonstrate using a parameterized conf home := os.Getenv("HOME") home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file) if home_conf_err != nil { panic("cannot read conf from " + home_conf_file) } home_conf.ConfLock.RLock() if home_conf.Initialized == false { panic("conf struct has not been initialized") } // launch a background poller to keep conns to aws alive if home_conf.Network.DynamoDB.KeepAlive { log.Printf("launching background keepalive") go keepalive.KeepAlive([]string{}) } // deal with iam, or not if home_conf.UseIAM { iam_ready_chan := make(chan bool) go conf_iam.GoIAM(iam_ready_chan) _ = <-iam_ready_chan } home_conf.ConfLock.RUnlock() tn := "test-godynamo-livetest" tablename1 := tn fmt.Printf("tablename1: %s\n", tablename1) var code int var err error var body []byte // INSERT SINGLE ITEM hk := "a-hash-key" rk := "1" put1 := put_item.NewPutItem() put1.TableName = tablename1 av1 := attributevalue.NewAttributeValue() av2 := attributevalue.NewAttributeValue() av3 := attributevalue.NewAttributeValue() av4 := attributevalue.NewAttributeValue() av5 := attributevalue.NewAttributeValue() av6 := attributevalue.NewAttributeValue() av7 := attributevalue.NewAttributeValue() av1.S = hk av2.N = rk av3.InsertSS("pk1_a") av3.InsertSS("pk1_c") av4.InsertNS("1") av4.InsertNS_float64(2) av4.InsertNS("3") av4.InsertNS("-7.2432342") av5.N = "1" av6.B = base64.StdEncoding.EncodeToString([]byte("hello")) av7.InsertBS(base64.StdEncoding.EncodeToString([]byte("hello"))) av7.InsertBS(base64.StdEncoding.EncodeToString([]byte("there"))) put1.Item["TheHashKey"] = av1 put1.Item["TheRangeKey"] = av2 put1.Item["stringlist"] = av3 put1.Item["numlist"] = av4 put1.Item["num"] = av5 put1.Item["byte"] = av6 put1.Item["bytelist"] = av7 body, code, err = put1.EndpointReqWithConf(home_conf) if err != nil || code != http.StatusOK { fmt.Printf("put1 failed %d %v %s\n", code, err, string(body)) os.Exit(1) } fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) // GET THAT ITEM get1 := get_item.NewGetItem() get1.TableName = tablename1 get1.Key["TheHashKey"] = av1 get1.Key["TheRangeKey"] = av2 get_json, get_json_err := json.Marshal(get1) if get_json_err != nil { fmt.Printf("%v\n", get_json_err) os.Exit(1) } fmt.Printf("%s\n", string(get_json)) body, code, err = get1.EndpointReqWithConf(home_conf) if err != nil || code != http.StatusOK { fmt.Printf("get failed %d %v %s\n", code, err, string(body)) os.Exit(1) } fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) var gr get_item.Response um_err := json.Unmarshal([]byte(body), &gr) if um_err != nil { fmt.Printf("get resp unmarshal failed %s\n", um_err.Error()) os.Exit(1) } fmt.Printf("%v\n", string(body)) // UPDATE THAT ITEM up1 := update_item.NewUpdateItem() new_attr_val := "new string here" up1.TableName = tablename1 up1.Key["TheHashKey"] = av1 up1.Key["TheRangeKey"] = av2 up1.AttributeUpdates = attributevalue.NewAttributeValueUpdateMap() up_avu := attributevalue.NewAttributeValueUpdate() up_avu.Action = update_item.ACTION_PUT up_avu.Value = &attributevalue.AttributeValue{S: new_attr_val} up1.AttributeUpdates["new_string"] = up_avu del_avu := attributevalue.NewAttributeValueUpdate() del_avu.Action = update_item.ACTION_DEL del_avu.Value = attributevalue.NewAttributeValue() del_avu.Value.InsertSS("pk1_a") up1.AttributeUpdates["stringlist"] = del_avu del2_avu := attributevalue.NewAttributeValueUpdate() del2_avu.Action = update_item.ACTION_DEL del2_avu.Value = &attributevalue.AttributeValue{} up1.AttributeUpdates["byte"] = del2_avu add_avu := attributevalue.NewAttributeValueUpdate() add_avu.Action = update_item.ACTION_ADD add_avu.Value = &attributevalue.AttributeValue{N: "4"} up1.AttributeUpdates["num"] = add_avu up1.ReturnValues = update_item.RETVAL_ALL_NEW update_item_json, update_item_json_err := json.Marshal(up1) if update_item_json_err != nil { fmt.Printf("%v\n", update_item_json_err) os.Exit(1) } fmt.Printf("%s\n", string(update_item_json)) body, code, err = up1.EndpointReqWithConf(home_conf) if err != nil || code != http.StatusOK { fmt.Printf("update item failed %d %v %s\n", code, err, string(body)) os.Exit(1) } fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) var ur update_item.Response um_err = json.Unmarshal([]byte(body), &ur) if um_err != nil { fmt.Printf("update resp unmarshal failed %s\n", um_err.Error()) os.Exit(1) } // GET IT AGAIN body, code, err = get1.EndpointReqWithConf(home_conf) if err != nil || code != http.StatusOK { fmt.Printf("get failed %d %v %s\n", code, err, string(body)) os.Exit(1) } fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) // DELETE THE ITEM del1 := delete_item.NewDeleteItem() del1.TableName = tablename1 del1.Key["TheHashKey"] = av1 del1.Key["TheRangeKey"] = av2 del1.ReturnValues = delete_item.RETVAL_ALL_OLD body, code, err = del1.EndpointReqWithConf(home_conf) if err != nil || code != http.StatusOK { fmt.Printf("delete item failed %d %v %s\n", code, err, string(body)) os.Exit(1) } fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) fmt.Printf("PASSED\n") }