Created
February 26, 2015 23:42
-
-
Save bradclawsie/4e472dd83f4989f12fbe to your computer and use it in GitHub Desktop.
putjson.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"time" | |
"net/http" | |
"encoding/json" | |
"github.com/smugmug/godynamo/conf" | |
"github.com/smugmug/godynamo/conf_file" | |
"github.com/smugmug/godynamo/types/attributevalue" | |
put "github.com/smugmug/godynamo/endpoints/put_item" | |
) | |
func main() { | |
// read in the godynamo required conf - see https://github.com/smugmug/godynamo/ | |
conf_file.Read() | |
conf.Vals.ConfLock.RLock() | |
if conf.Vals.Initialized == false { | |
panic("the conf.Vals global conf struct has not been initialized") | |
} | |
conf.Vals.ConfLock.RUnlock() | |
// my test table - yours will be different! | |
tablename := "test-godynamo-livestest-2" | |
// ------------------------------------------------------ | |
// first lets do the put without using json....by building up a data structure. | |
// I think this is probably what you really want to do | |
put1 := put.NewPutItem() | |
put1.TableName = tablename | |
i := fmt.Sprintf("%v",1) | |
t := fmt.Sprintf("%v",time.Now().Unix()) | |
fmt.Printf("using timestamp: %v\n",t) | |
// the key | |
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 | |
// send the request | |
body,code,err := put1.EndpointReq() | |
if err != nil || code != http.StatusOK { | |
fmt.Printf("put1 failed %d %v %s\n",code,err,body) | |
} | |
fmt.Printf("%v\n%v\n,%v\n",string(body),code,err) | |
// ------------------------------------------------------ | |
// now lets try to do what you want - take a json string and | |
// create the struct from it | |
// the json string that we will try to convert | |
json_string := `{"TableName":"test-godynamo-livestest-2","Item":{"UserID":1,"Timestamp":2,"Token":"json","Location":{"Latitude":120.01,"Longitude":50.99}}}` | |
fmt.Printf("%s\n",json_string) | |
// read that json into our PutItemJSON struct | |
put_json2 := put.NewPutItemJSON() | |
um_err := json.Unmarshal([]byte(json_string),put_json2) | |
if um_err != nil { | |
fmt.Printf("cannot unmarshal:%v\n",um_err.Error()) | |
os.Exit(1) | |
} | |
// convert it to a PutItem | |
put2, put_convert_err := put_json2.ToPutItem() | |
if put_convert_err != nil { | |
fmt.Printf("cannot convert:%v\n",put_convert_err.Error()) | |
os.Exit(1) | |
} | |
// send the request | |
body,code,err = put2.EndpointReq() | |
if err != nil || code != http.StatusOK { | |
fmt.Printf("put2 json failed %d %v %s\n",code,err,body) | |
} | |
fmt.Printf("%v\n%v\n,%v\n",string(body),code,err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment