Created
March 28, 2016 05:30
-
-
Save sinmetal/d975e7b13bccbf6bf23e to your computer and use it in GitHub Desktop.
This file contains 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 goonchk | |
import ( | |
"fmt" | |
"net/http" | |
"github.com/mjibson/goon" | |
"google.golang.org/appengine/datastore" | |
) | |
type User struct { | |
Id string `datastore:"-" goon:"id"` | |
Name string | |
} | |
func init() { | |
http.HandleFunc("/", handler) | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
oldid := "old" | |
g := goon.NewGoon(r) | |
olduser := &User{Id: oldid} | |
oldkey, err := g.Put(olduser) | |
if err != nil { | |
http.Error(w, fmt.Sprintf("err g.Put for olduser, %s", err.Error()), 500) | |
return | |
} | |
var kl []*datastore.Key | |
kl = append(kl, oldkey) | |
err = g.DeleteMulti(kl) | |
if err != nil { | |
http.Error(w, fmt.Sprintf("err g.DeleteMulti, %s", err.Error()), 500) | |
return | |
} | |
newid := "new" | |
newuser := &User{Id: newid} | |
_, err = g.Put(newuser) | |
if err != nil { | |
http.Error(w, fmt.Sprintf("err g.Put for newuser, %s", err.Error()), 500) | |
return | |
} | |
err = g.Get(olduser) | |
if err == datastore.ErrNoSuchEntity { | |
// nop | |
} else if err != nil { | |
http.Error(w, fmt.Sprintf("err g.Get for olduser, %s", err.Error()), 500) | |
return | |
} | |
w.WriteHeader(http.StatusOK) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment