github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/put_item-param-conf-livetest.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/smugmug/godynamo/conf" 6 "github.com/smugmug/godynamo/conf_file" 7 conf_iam "github.com/smugmug/godynamo/conf_iam" 8 put "github.com/smugmug/godynamo/endpoints/put_item" 9 keepalive "github.com/smugmug/godynamo/keepalive" 10 "github.com/smugmug/godynamo/types/attributevalue" 11 "log" 12 "net/http" 13 "os" 14 "time" 15 ) 16 17 func main() { 18 19 // this is the same as put-item-livetest except here we demonstrate using a parameterized conf 20 home := os.Getenv("HOME") 21 home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME 22 home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file) 23 if home_conf_err != nil { 24 panic("cannot read conf from " + home_conf_file) 25 } 26 home_conf.ConfLock.RLock() 27 if home_conf.Initialized == false { 28 panic("conf struct has not been initialized") 29 } 30 31 // launch a background poller to keep conns to aws alive 32 if home_conf.Network.DynamoDB.KeepAlive { 33 log.Printf("launching background keepalive") 34 go keepalive.KeepAlive([]string{}) 35 } 36 37 // deal with iam, or not 38 if home_conf.UseIAM { 39 iam_ready_chan := make(chan bool) 40 go conf_iam.GoIAM(iam_ready_chan) 41 _ = <-iam_ready_chan 42 } 43 home_conf.ConfLock.RUnlock() 44 45 put1 := put.NewPutItem() 46 put1.TableName = "test-godynamo-livetest" 47 k := fmt.Sprintf("hk1") 48 v := fmt.Sprintf("%v", time.Now().Unix()) 49 // In simple cases you don't need to call NewAttributeValue 50 put1.Item["TheHashKey"] = &attributevalue.AttributeValue{S: k} 51 put1.Item["TheRangeKey"] = &attributevalue.AttributeValue{N: v} 52 n := fmt.Sprintf("%v", time.Now().Unix()) 53 put1.Item["Mtime"] = &attributevalue.AttributeValue{N: n} 54 put1.Item["SomeJunk"] = &attributevalue.AttributeValue{S: "some junk"} 55 // for complex attributevalue instances, call the constructor first 56 av := attributevalue.NewAttributeValue() 57 av.InsertSS("some junk1") 58 av.InsertSS("some junk2") 59 put1.Item["SomeJunks"] = av 60 av2 := attributevalue.NewAttributeValue() 61 av2.InsertL(&attributevalue.AttributeValue{S: "some junk1"}) 62 av2.InsertL(&attributevalue.AttributeValue{S: "some junk2"}) 63 put1.Item["JunkL"] = av2 64 av3 := attributevalue.NewAttributeValue() 65 av3.InsertM("somejunkkey", &attributevalue.AttributeValue{S: "some junk1"}) 66 67 body, code, err := put1.EndpointReqWithConf(home_conf) 68 if err != nil || code != http.StatusOK { 69 fmt.Printf("put failed %d %v %s\n", code, err, body) 70 } 71 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 72 }