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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package example
6
7 import (
8 "golang.org/x/net/context"
9 "infra/gae/libs/wrapper"
10 "infra/gae/libs/wrapper/gae"
11 "infra/gae/libs/wrapper/gae/commonErrors"
12
13 "github.com/GoogleCloudPlatform/go-endpoints/endpoints"
14 )
15
16 // AddReq describes the input parameters to the 'Add' RPC. Name is required,
17 // which makes it show up in the REST path, and Delta will be encoded in the
18 // request body as JSON.
19 type AddReq struct {
20 Name string `endpoints:"required"`
21
22 Delta int64 `json:",string"`
23 }
24
25 // AddRsp describes the return value from the 'Add' RPC. Prev is the previous
26 // value, and Cur is the post-increment value.
27 type AddRsp struct {
28 Prev int64 `json:",string"`
29 Cur int64 `json:",string"`
30 }
31
32 // Add adds a value to the current counter, and returns the old+new values. It
33 // may cause a counter to come into existance.
34 func (Example) Add(c endpoints.Context, r *AddReq) (rsp *AddRsp, err error) {
35 rsp = &AddRsp{}
36
37 ds := wrapper.GetDS(gae.Use(context.Background(), c))
38 err = ds.RunInTransaction(func(context.Context) error {
39 ctr := &Counter{ID: r.Name}
40 if err := ds.Get(ctr); err != nil && err != commonErrors.ErrNoSu chEntityDS {
41 return err
42 }
43 rsp.Prev = ctr.Val
44 ctr.Val += r.Delta
45 rsp.Cur = ctr.Val
46 _, err := ds.Put(ctr)
47 return err
48 }, nil)
49 return
50 }
51
52 func init() {
53 mi["Add"] = &endpoints.MethodInfo{
54 Path: "counter/{Name}",
55 Desc: "Add an an amount to a particular counter",
56 }
57 }
OLDNEW
« 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