-
Notifications
You must be signed in to change notification settings - Fork 6
/
sample_server.go
128 lines (108 loc) · 3.1 KB
/
sample_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package test
import (
"context"
"net/http/httptest"
"strconv"
"github.com/Clever/wag/samples/gen-go/models"
"github.com/Clever/wag/samples/gen-go/server"
)
// ControllerImpl implements the test server controller interface.
type ControllerImpl struct {
books map[int64]*models.Book
maxID int64
pageSize int
authors []*models.Author
nilPutBookCount int
}
// GetBooks returns a list of books.
func (c *ControllerImpl) GetBooks(ctx context.Context, input *models.GetBooksInput) ([]models.Book, int64, error) {
var bookList []models.Book
var nextPage int64 // default to no next page
idx := int64(0)
if input.StartingAfter != nil {
idx = *input.StartingAfter + 1
}
// loop through all indices to get predictable ordering
for ; idx <= c.maxID; idx++ {
if book, ok := c.books[idx]; ok {
bookList = append(bookList, *book)
if len(bookList) == c.pageSize {
nextPage = idx
break
}
}
}
return bookList, nextPage, nil
}
// GetBookByID returns a book by ID.
func (c *ControllerImpl) GetBookByID(ctx context.Context, input *models.GetBookByIDInput) (*models.Book, error) {
if input.BookID == 400 {
return nil, models.BadRequest{Message: "My 400 failure"}
}
book, ok := c.books[input.BookID]
if !ok {
return nil, models.Error{}
}
return book, nil
}
// GetBookByID2 returns a book by ID.
func (c *ControllerImpl) GetBookByID2(ctx context.Context, id string) (*models.Book, error) {
i, err := strconv.Atoi("-42")
if err != nil {
return nil, err
}
book, ok := c.books[int64(i)]
if !ok {
return nil, models.Error{}
}
return book, nil
}
// CreateBook creates a book.
func (c *ControllerImpl) CreateBook(ctx context.Context, input *models.Book) (*models.Book, error) {
if input.ID > c.maxID {
c.maxID = input.ID
}
c.books[input.ID] = input
return input, nil
}
// PutBook creates a book.
func (c *ControllerImpl) PutBook(ctx context.Context, input *models.Book) (*models.Book, error) {
if input == nil {
c.nilPutBookCount++
return nil, nil
}
if input.ID > c.maxID {
c.maxID = input.ID
}
c.books[input.ID] = input
return input, nil
}
// GetAuthors gets authors.
func (c *ControllerImpl) GetAuthors(ctx context.Context, i *models.GetAuthorsInput) (*models.AuthorsResponse, string, error) {
return &models.AuthorsResponse{
AuthorSet: &models.AuthorSet{
RandomProp: int64(3),
Results: models.AuthorArray(c.authors),
},
}, "", nil
}
// GetAuthorsWithPut gets authors with a PUT (because it needs a body).
func (c *ControllerImpl) GetAuthorsWithPut(ctx context.Context, i *models.GetAuthorsWithPutInput) (
*models.AuthorsResponse, string, error,
) {
return &models.AuthorsResponse{
AuthorSet: &models.AuthorSet{
RandomProp: int64(3),
Results: models.AuthorArray(c.authors),
},
}, "", nil
}
// HealthCheck returns nil always.
func (c *ControllerImpl) HealthCheck(ctx context.Context) error {
return nil
}
func setupServer() (*httptest.Server, *ControllerImpl) {
controller := ControllerImpl{books: make(map[int64]*models.Book), pageSize: 100}
s := server.New(&controller, ":8080")
return httptest.NewServer(s.Handler), &controller
}