Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(557)

Unified Diff: go/src/infra/gae/epservice/example/service_add.go

Issue 1153473008: A client/server helper wrapper for endpoints in Go. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: rm newline Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/src/infra/gae/epservice/example/service.go ('k') | go/src/infra/gae/epservice/example/service_cas.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/gae/epservice/example/service_add.go
diff --git a/go/src/infra/gae/epservice/example/service_add.go b/go/src/infra/gae/epservice/example/service_add.go
new file mode 100644
index 0000000000000000000000000000000000000000..da259b0d2b9840842aa40ca99c6b16322ac9a936
--- /dev/null
+++ b/go/src/infra/gae/epservice/example/service_add.go
@@ -0,0 +1,57 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package example
+
+import (
+ "golang.org/x/net/context"
+ "infra/gae/libs/wrapper"
+ "infra/gae/libs/wrapper/gae"
+ "infra/gae/libs/wrapper/gae/commonErrors"
+
+ "github.com/GoogleCloudPlatform/go-endpoints/endpoints"
+)
+
+// AddReq describes the input parameters to the 'Add' RPC. Name is required,
+// which makes it show up in the REST path, and Delta will be encoded in the
+// request body as JSON.
+type AddReq struct {
+ Name string `endpoints:"required"`
+
+ Delta int64 `json:",string"`
+}
+
+// AddRsp describes the return value from the 'Add' RPC. Prev is the previous
+// value, and Cur is the post-increment value.
+type AddRsp struct {
+ Prev int64 `json:",string"`
+ Cur int64 `json:",string"`
+}
+
+// Add adds a value to the current counter, and returns the old+new values. It
+// may cause a counter to come into existance.
+func (Example) Add(c endpoints.Context, r *AddReq) (rsp *AddRsp, err error) {
+ rsp = &AddRsp{}
+
+ ds := wrapper.GetDS(gae.Use(context.Background(), c))
+ err = ds.RunInTransaction(func(context.Context) error {
+ ctr := &Counter{ID: r.Name}
+ if err := ds.Get(ctr); err != nil && err != commonErrors.ErrNoSuchEntityDS {
+ return err
+ }
+ rsp.Prev = ctr.Val
+ ctr.Val += r.Delta
+ rsp.Cur = ctr.Val
+ _, err := ds.Put(ctr)
+ return err
+ }, nil)
+ return
+}
+
+func init() {
+ mi["Add"] = &endpoints.MethodInfo{
+ Path: "counter/{Name}",
+ Desc: "Add an an amount to a particular counter",
+ }
+}
« no previous file with comments | « go/src/infra/gae/epservice/example/service.go ('k') | go/src/infra/gae/epservice/example/service_cas.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698