Skip to content

Commit 63e2e66

Browse files
Stephen MartinisCommit Bot
authored andcommitted
Fix example go program
Change-Id: If9343e981a39a4606b30bbc55465b6954b2a3fcc Reviewed-on: https://chromium-review.googlesource.com/924568 Reviewed-by: Robbie Iannucci <[email protected]> Commit-Queue: Stephen Martinis <[email protected]>
1 parent f695821 commit 63e2e66

File tree

4 files changed

+69
-105
lines changed

4 files changed

+69
-105
lines changed

doc.go

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -47,60 +47,6 @@
4747
// - filter/* extra filter functionality for the services, agnostic to the
4848
// underlying implementation.
4949
//
50-
// TLDR
51-
//
52-
// In production, do:
53-
//
54-
// import (
55-
// "fmt"
56-
// "net/http"
57-
//
58-
// "go.chromium.org/gae/impl/prod"
59-
// "go.chromium.org/gae/service/datastore"
60-
// "golang.org/x/net/context"
61-
// )
62-
//
63-
// func handler(w http.ResponseWriter, r *http.Request) {
64-
// c := prod.UseRequest(r)
65-
// // add production filters, etc. here
66-
// innerHandler(c, w)
67-
// }
68-
//
69-
// type CoolStruct struct {
70-
// ID `gae:"$id"`
71-
//
72-
// Value string
73-
// }
74-
//
75-
// func innerHandler(c context.Context, w http.ResponseWriter) {
76-
// obj := &CoolStruct{Value: "hello"}
77-
// if err := rds.Put(c, obj); err != nil {
78-
// http.Error(w, err.String(), http.StatusInternalServerError)
79-
// }
80-
// fmt.Fprintf(w, "I wrote: %s", ds.KeyForObj(obj))
81-
// }
82-
//
83-
// And in your test do:
84-
//
85-
// import (
86-
// "testing"
87-
// "fmt"
88-
// "net/http"
89-
//
90-
// "go.chromium.org/gae/impl/memory"
91-
// "go.chromium.org/gae/service/datastore"
92-
// "golang.org/x/net/context"
93-
// )
94-
//
95-
// func TestHandler(t *testing.T) {
96-
// t.Parallel()
97-
// c := memory.Use(context.Background())
98-
// // use datastore here to monkey with the database, install testing
99-
// // filters like featureBreaker to test error conditions in innerHandler,
100-
// // etc.
101-
// innerHandler(c, ...)
102-
// }
103-
//
10450
// Service Definitions
10551
//
10652
// A service defintion lives under the `service` subfolder, and defines the
@@ -167,30 +113,6 @@
167113
// which API method was called. They're useful to embed in filter or service
168114
// implementations as stubs while you're implementing the filter.
169115
//
170-
// Usage
171-
//
172-
// You will typically access one of the service interfaces in your code like:
173-
// // This is the 'production' code
174-
// func HTTPHandler(r *http.Request) {
175-
// c := prod.Use(appengine.NewContext(r))
176-
// CoolFunc(c)
177-
// }
178-
//
179-
// // This is the 'testing' code
180-
// func TestCoolFunc(t *testing.T) {
181-
// c := memory.Use(context.Background())
182-
// CoolFunc(c)
183-
// }
184-
//
185-
// func CoolFunc(c context.Context, ...) {
186-
// SomeOtherFunction(c, ...)
187-
//
188-
// // because you might need to:
189-
// ds.RunInTransaction(c, func (c context.Context) error {
190-
// SomeOtherFunction(c, ...) // c contains transactional versions of everything
191-
// }, nil)
192-
// }
193-
//
194116
// Filters
195117
//
196118
// Each service also supports "filters". Filters are proxy objects which have

example_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2018 The LUCI Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package gae
16+
17+
import (
18+
"fmt"
19+
"net/http"
20+
"net/http/httptest"
21+
22+
"go.chromium.org/gae/impl/memory"
23+
"go.chromium.org/gae/impl/prod"
24+
"go.chromium.org/gae/service/datastore"
25+
"golang.org/x/net/context"
26+
)
27+
28+
func Example() {
29+
// This is suitable for use in tests.
30+
c := memory.Use(context.Background())
31+
w := httptest.NewRecorder()
32+
_, err := http.NewRequest("GET", "/doesntmatter", nil)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
innerHandler(c, w)
38+
fmt.Printf(string(w.Body.Bytes()))
39+
// Output: I wrote: dev~app::/CoolStruct,"struct-id"
40+
}
41+
42+
// This is what you would use in production.
43+
func handler(w http.ResponseWriter, r *http.Request) {
44+
c := context.Background()
45+
c = prod.Use(c, r)
46+
// add production filters, etc. here
47+
innerHandler(c, w)
48+
}
49+
50+
type CoolStruct struct {
51+
ID string `gae:"$id"`
52+
53+
Value string
54+
}
55+
56+
func innerHandler(c context.Context, w http.ResponseWriter) {
57+
obj := &CoolStruct{ID: "struct-id", Value: "hello"}
58+
if err := datastore.Put(c, obj); err != nil {
59+
http.Error(w, err.Error(), http.StatusInternalServerError)
60+
}
61+
fmt.Fprintf(w, "I wrote: %s", datastore.KeyForObj(c, obj))
62+
}

service/datastore/errors.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ import (
1818
"fmt"
1919
"reflect"
2020

21-
"go.chromium.org/gae"
22-
2321
"go.chromium.org/luci/common/errors"
2422

2523
"google.golang.org/appengine/datastore"
2624
)
2725

26+
type stopErr struct{}
27+
28+
func (stopErr) Error() string { return "stop iteration" }
29+
2830
// These errors are returned by various datastore.Interface methods.
2931
var (
3032
ErrNoSuchEntity = datastore.ErrNoSuchEntity
3133
ErrConcurrentTransaction = datastore.ErrConcurrentTransaction
3234

33-
// Stop is an alias for "go.chromium.org/gae".Stop
34-
Stop = gae.Stop
35+
// Stop is understood by various services to stop iterative processes. Examples
36+
// include datastore.Interface.Run's callback.
37+
Stop = stopErr{}
3538
)
3639

3740
// MakeErrInvalidKey returns an errors.Annotator instance that wraps an invalid

symbols.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)