本文整理汇总了Golang中google/golang.org/appengine/datastore.NewIncompleteKey函数的典型用法代码示例。如果您正苦于以下问题:Golang NewIncompleteKey函数的具体用法?Golang NewIncompleteKey怎么用?Golang NewIncompleteKey使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewIncompleteKey函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: put
func put(rw http.ResponseWriter, req *http.Request) {
c := appengine.NewContext(req)
u := user.Current(c)
m := req.FormValue("message")
s := req.FormValue("encoded_key")
// fmt.Fprintf(rw, "Key 1: %v", s)
p := req.FormValue("parent_key")
var t, ut string
var op bool
var k *datastore.Key
// make/decode keys
if s == "" {
if p == "" {
k = datastore.NewIncompleteKey(c, "post", nil)
op = true
} else {
pk, err := datastore.DecodeKey(p)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
k = datastore.NewIncompleteKey(c, "post", pk)
op = false
}
t = time.Now().Format("Jan 2, 2006 3:04 PM")
ut = ""
} else {
k, err := datastore.DecodeKey(s)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
mypost := Post{}
err = datastore.Get(c, k, &mypost)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
ut = time.Now().Format("Jan 2, 2006 3:04 PM")
t = mypost.PostDate
op = mypost.OP
}
// data := url.Values{}
// data.Set("encoded_key", k.Encode())
// r, _ := http.NewRequest("POST", "/view", bytes.NewBufferString(data.Encode()))
newpost := Post{Author: u.String(), Message: m, UpdateDate: ut, PostDate: t, OP: op}
_, err := datastore.Put(c, k, &newpost)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
// http.Redirect(rw, r, "/view", http.StatusOK)
http.Redirect(rw, req, "/", http.StatusTemporaryRedirect)
}
示例2: SaveBoardState
// SaveBoardState commits the state to the datastore
func SaveBoardState(c context.Context, state *BoardState) (*BoardState, error) {
sessionKey, err := datastore.DecodeKey(state.SessionID)
if err != nil {
return nil, err
}
stateModel := &boardStateModel{}
stateModel.parentKey = sessionKey
stateModel.lastModified = time.Now()
stateModel.key, err = datastore.Put(c, datastore.NewIncompleteKey(c, "BoardState", stateModel.parentKey), stateModel)
if err != nil {
return nil, err
}
// Initialize the result
result := &BoardState{
ID: stateModel.key.Encode(),
SessionID: stateModel.parentKey.Encode(),
LastModified: time.Now(),
Players: make(map[string]*Player),
}
// Save the players
for k, v := range state.Players {
p := &playerModel{
Name: k,
Location: v.Location,
}
p.parentKey = stateModel.key
p.key, err = datastore.Put(c, datastore.NewIncompleteKey(c, "PlayerState", p.parentKey), p)
if err != nil {
return nil, err
}
for _, card := range v.Hand {
cardModel := &cardModel{
Name: card.Name,
Color: card.Color,
}
cardModel.parentKey = p.key
cardModel.key, err = datastore.Put(c, datastore.NewIncompleteKey(c, "PlayerCard", p.parentKey), cardModel)
if err != nil {
return nil, err
}
}
// Added player to result
result.Players[k] = &Player{
Location: p.Location,
Hand: v.Hand,
}
}
return result, nil
}
示例3: example6
func example6() {
// [START parent]
// Create Employee entity
employee := &Employee{ /* ... */ }
employeeKey, err := datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "Employee", nil), employee)
// Use Employee as Address entity's parent
// and save Address entity to datastore
address := &Address{ /* ... */ }
addressKey := datastore.NewIncompleteKey(ctx, "Address", employeeKey)
_, err = datastore.Put(ctx, addressKey, address)
// [END parent]
_ = err
}
示例4: insert
// POST http://localhost:8080/profiles
// {"first_name": "Ivan", "nick_name": "Socks", "last_name": "Hawkes"}
//
func (u *ProfileApi) insert(r *restful.Request, w *restful.Response) {
c := appengine.NewContext(r.Request)
// Marshall the entity from the request into a struct.
p := new(Profile)
err := r.ReadEntity(&p)
if err != nil {
w.WriteError(http.StatusNotAcceptable, err)
return
}
// Ensure we start with a sensible value for this field.
p.LastModified = time.Now()
// The profile belongs to this user.
p.Email = user.Current(c).String()
k, err := datastore.Put(c, datastore.NewIncompleteKey(c, "profiles", nil), p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Let them know the location of the newly created resource.
// TODO: Use a safe Url path append function.
w.AddHeader("Location", u.Path+"/"+k.Encode())
// Return the resultant entity.
w.WriteHeader(http.StatusCreated)
w.WriteEntity(p)
}
示例5: writeLinkTweet
//WriteLinkTweet writes a given Tweet to the datastore
func (mb MapBuilder) writeLinkTweet(tweets <-chan anaconda.Tweet, wg *sync.WaitGroup) {
defer wg.Done()
var keys []*datastore.Key
var values []*int64
for tweet := range tweets {
key := datastore.NewIncompleteKey(mb.c, linkTweetKind, getTweetKey(mb.c))
keys = append(keys, key)
values = append(values, &tweet.Id)
}
err := datastore.RunInTransaction(mb.c, func(c context.Context) error {
_, err := datastore.PutMulti(c, keys, values)
if err != nil {
log.Errorf(c, "Failed to write LinkTweet to datastore. %v", err.Error())
return err
}
return nil
}, nil)
if err != nil {
log.Errorf(mb.c, "Failed to write LinkTweet to datastore. %v", err.Error())
}
}
示例6: APIKeyAddHandler
func APIKeyAddHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
loginUrl, _ := user.LoginURL(c, r.URL.RequestURI())
http.Redirect(w, r, loginUrl, http.StatusFound)
return
} else {
if !u.Admin {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("You're not an admin. Go away."))
} else {
key := randomString(26)
owner := r.FormValue("owner")
if owner == "" {
w.Write([]byte("You forgot a parameter."))
} else {
apiKey := APIKey{
APIKey: key,
OwnerEmail: owner,
}
dkey := datastore.NewIncompleteKey(c, "APIKey", nil)
_, err := datastore.Put(c, dkey, &apiKey)
if err != nil {
w.Write([]byte(fmt.Sprintf("error! %s", err.Error())))
} else {
w.Write([]byte(key))
}
}
}
}
}
示例7: warmupHandler
func warmupHandler(c *echo.Context) error {
if appengine.IsDevAppServer() {
photographers := []Photographer{
{1, "Mr Canon"},
{2, "Miss Nikon"},
{3, "Mrs Pentax"},
{4, "Ms Sony"},
}
// create some dummy data
for m := 1; m <= 12; m++ {
for d := 1; d < 28; d++ {
taken := time.Date(2015, time.Month(m), d, 12, 0, 0, 0, time.UTC)
id := rand.Int31n(4)
photographer := photographers[id]
p := Photo{
Photographer: photographer,
Uploaded: time.Now().UTC(),
Width: 8000,
Height: 6000,
Taken: taken,
}
k := datastore.NewIncompleteKey(c, "photo", nil)
nds.Put(c, k, &p)
}
}
}
return c.NoContent(http.StatusOK)
}
示例8: getOrCreateChat
func getOrCreateChat(c context.Context, fbChatID int64, keyBuf **datastore.Key) (*Chat, error) {
results := make([]Chat, 0, 1)
keys, err := datastore.NewQuery("Chat").
Filter("FacebookChatID =", fbChatID).Limit(1).GetAll(c, &results)
if err != nil {
return nil, err
}
var resultChat *Chat
var resultKey *datastore.Key
if len(keys) == 0 {
incKey := datastore.NewIncompleteKey(c, "Chat", nil)
resultChat = &Chat{
FacebookChatID: fbChatID,
ChatName: "",
}
resultKey, err = datastore.Put(c, incKey, resultChat)
if err != nil {
return nil, err
}
} else {
resultKey = keys[0]
resultChat = &results[0]
}
if keyBuf != nil {
*keyBuf = resultKey
}
return resultChat, nil
}
示例9: CreateVenueHandler
// CreateVenueHandler makes a new venue and saves it to the datastore
func CreateVenueHandler(w util.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
var venueData Venue
if err := json.NewDecoder(r.Body).Decode(&venueData); err != nil {
log.Println("Unable to decode create venue data:", err.Error())
http.Error(w, "Unable to decode JSON request body", http.StatusBadRequest)
return
}
if venueData.Name == "" {
http.Error(w, `must provide "name" field when creating a venue`, http.StatusBadRequest)
return
}
key := datastore.NewIncompleteKey(c, "Venues", nil)
newKey, err := datastore.Put(c, key, &venueData)
if err != nil {
w.WriteJSON(nil, err)
return
}
venueData.ID = newKey.IntID()
w.WriteJSON(&venueData, nil)
}
示例10: generateRandom
// this will generate some random data for the given day
// 24 * 12 on appengine (288)
// 24 only on development
func generateRandom(c context.Context, day time.Time) error {
var x int
if appengine.IsDevAppServer() {
x = 1
} else {
x = 12
}
keys := make([]*datastore.Key, 24*x)
photos := make([]*Photo, 24*x)
id := 0
for h := 0; h < 24; h++ {
taken := day.Add(time.Duration(h) * time.Hour)
for i := 0; i < x; i++ {
photographer := photographers[rand.Int31n(4)]
photos[id] = &Photo{
Photographer: photographer,
Uploaded: time.Now().UTC(),
Width: 8000,
Height: 6000,
Taken: taken,
TakenDay: day,
}
keys[id] = datastore.NewIncompleteKey(c, "photo", nil)
id++
}
}
nds.PutMulti(c, keys, photos)
return nil
}
示例11:
// Put will put this Submission into Datastore using the given key.
func (ƨ Submission) Put(ctx context.Context, key *datastore.Key) (*datastore.Key, error) {
if key != nil {
return datastore.Put(ctx, key, &ƨ)
}
return datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "Submission", nil), &ƨ)
}
示例12: adminNewTask
func adminNewTask(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
asInt := func(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}
task := Task{
Name: r.FormValue("name"),
Description: r.FormValue("description"),
Assignee: r.FormValue("assignee"),
RType: r.FormValue("rtype"),
Automatic: mightParseBool(r.FormValue("automatic")),
Period: asInt(r.FormValue("period")),
Value: asInt(r.FormValue("value")),
Next: time.Now().UTC(),
}
k, err := datastore.Put(c,
datastore.NewIncompleteKey(c, "Task", nil), &task)
if err != nil {
log.Warningf(c, "Error storing task: %v", err)
panic(err)
}
log.Infof(c, "Stored new thing with key %v", k)
http.Redirect(w, r, "/admin/tasks/", 307)
}
示例13: putTweet
// insert tweet
func putTweet(req *http.Request, tweet *Tweet) error {
ctx := appengine.NewContext(req)
key := datastore.NewIncompleteKey(ctx, "Tweet", nil)
_, err := datastore.Put(ctx, key, tweet)
return err
// you can use memcache also to improve your consistency
}
示例14: create2Handler
func create2Handler(w http.ResponseWriter, r *http.Request) {
strTitle := r.FormValue("title")
strLink := r.FormValue("link")
// fmt.Fprint(w, "go title: ", strTitle)
// fmt.Fprint(w, "link: ", strLink)
//TODO error checking for the title and link
c := appengine.NewContext(r)
u := user.Current(c)
//TODO error checking for user?
//TODO check random thread against an already existing one
rand.Seed(time.Now().UnixNano())
newThreadId := rand.Int63()
newSubmission := WebSubmission{
Title: strTitle,
Link: strLink,
SubmitBy: u.String(),
Thread: newThreadId, //TODO create random thread id
SubmitDateTime: time.Now(),
SubmissionDesc: "",
Score: 0,
}
_, err := datastore.Put(c, datastore.NewIncompleteKey(c, WebSubmissionEntityName, nil), &newSubmission)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
示例15:
// Put will put this JunitTestResult into Datastore using the given key.
func (ƨ JunitTestResult) Put(ctx context.Context, key *datastore.Key) (*datastore.Key, error) {
if key != nil {
return datastore.Put(ctx, key, &ƨ)
}
return datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "JunitTestResult", nil), &ƨ)
}