Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
Properly integrating types and methods of surface model (#16)
Browse files Browse the repository at this point in the history
* Adjusting renderers

* Updating services and corresponding test cases

* Adjusting surface model types and methods (same pre processing steps as in gnostic-grpc)
  • Loading branch information
LorenzHW authored Mar 26, 2020
1 parent e56fb2f commit 221987e
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 251 deletions.
25 changes: 11 additions & 14 deletions examples/v2.0/bookstore/bookstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func TestBookstore(t *testing.T) {
t.Log("list shelves failed")
t.Fail()
}
if (response == nil) || (response.OK == nil) || (response.OK.Shelves != nil) {
t.Log(fmt.Sprintf("list shelves failed %+v", response.OK))
t.Log(fmt.Sprintf("list shelves failed len=%d", len(response.OK.Shelves)))
if response != nil && (len(response.Shelves) != 0) {
t.Log(fmt.Sprintf("list shelves failed %+v", response.Shelves))
t.Log(fmt.Sprintf("list shelves failed len=%d", len(response.Shelves)))
t.Fail()
}
}
Expand Down Expand Up @@ -78,8 +78,7 @@ func TestBookstore(t *testing.T) {
t.Log("create shelf mysteries failed")
t.Fail()
}
if (response.OK.Name != "shelves/1") ||
(response.OK.Theme != "mysteries") {
if response != nil && ((response.Name != "shelves/1") || (response.Theme != "mysteries")) {
t.Log("create shelf mysteries failed")
t.Fail()
}
Expand All @@ -93,8 +92,7 @@ func TestBookstore(t *testing.T) {
t.Log("create shelf comedies failed")
t.Fail()
}
if (response.OK.Name != "shelves/2") ||
(response.OK.Theme != "comedies") {
if (response.Name != "shelves/2") || (response.Theme != "comedies") {
t.Log("create shelf comedies failed")
t.Fail()
}
Expand All @@ -106,8 +104,7 @@ func TestBookstore(t *testing.T) {
t.Log("get shelf mysteries failed")
t.Fail()
}
if (response.OK.Name != "shelves/1") ||
(response.OK.Theme != "mysteries") {
if (response.Name != "shelves/1") || (response.Theme != "mysteries") {
t.Log("get shelf mysteries failed")
t.Fail()
}
Expand All @@ -119,7 +116,7 @@ func TestBookstore(t *testing.T) {
t.Log("list shelves failed")
t.Fail()
}
if len(response.OK.Shelves) != 2 {
if len(response.Shelves) != 2 {
t.Log("list shelves failed")
t.Fail()
}
Expand All @@ -139,7 +136,7 @@ func TestBookstore(t *testing.T) {
t.Log("list shelves failed")
t.Fail()
}
if len(response.OK.Shelves) != 1 {
if len(response.Shelves) != 1 {
t.Log("list shelves failed")
t.Fail()
}
Expand All @@ -151,7 +148,7 @@ func TestBookstore(t *testing.T) {
t.Log("list books failed")
t.Fail()
}
if len(response.OK.Books) != 0 {
if len(response.Books) != 0 {
t.Log("list books failed")
t.Fail()
}
Expand Down Expand Up @@ -193,7 +190,7 @@ func TestBookstore(t *testing.T) {
t.Log("list books failed")
t.Fail()
}
if len(response.OK.Books) != 2 {
if len(response.Books) != 2 {
t.Log("list books failed")
t.Fail()
}
Expand All @@ -213,7 +210,7 @@ func TestBookstore(t *testing.T) {
t.Log("list books failed")
t.Fail()
}
if len(response.OK.Books) != 1 {
if len(response.Books) != 1 {
t.Log("list books failed")
t.Fail()
}
Expand Down
45 changes: 19 additions & 26 deletions examples/v2.0/bookstore/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"errors"
"fmt"
"net/http"
"sync"

"github.com/googleapis/gnostic-go-generator/examples/v2.0/bookstore/bookstore"
Expand All @@ -46,30 +45,28 @@ func NewService() *Service {
}
}

func (service *Service) ListShelves(responses *bookstore.ListShelvesResponses) (err error) {
func (service *Service) ListShelves(response *bookstore.ListShelvesResponse) (err error) {
service.Mutex.Lock()
defer service.Mutex.Unlock()
// copy shelf ids from Shelves map keys
shelves := make([]bookstore.Shelf, 0, len(service.Shelves))
for _, shelf := range service.Shelves {
shelves = append(shelves, *shelf)
}
response := &bookstore.ListShelvesResponse{}
response.Shelves = shelves
(*responses).OK = response
(*response).Shelves = shelves
return err
}

func (service *Service) CreateShelf(parameters *bookstore.CreateShelfParameters, responses *bookstore.CreateShelfResponses) (err error) {
func (service *Service) CreateShelf(parameters *bookstore.CreateShelfParameters, response *bookstore.Shelf) (err error) {
service.Mutex.Lock()
defer service.Mutex.Unlock()
// assign an id and name to a shelf and add it to the Shelves map.
shelf := parameters.Shelf
service.LastShelfID++
sid := service.LastShelfID
shelf.Name = fmt.Sprintf("shelves/%d", sid)
service.Shelves[sid] = &shelf
(*responses).OK = &shelf
service.Shelves[sid] = shelf
*response = *shelf
return err
}

Expand All @@ -84,16 +81,15 @@ func (service *Service) DeleteShelves() (err error) {
return nil
}

func (service *Service) GetShelf(parameters *bookstore.GetShelfParameters, responses *bookstore.GetShelfResponses) (err error) {
func (service *Service) GetShelf(parameters *bookstore.GetShelfParameters, response *bookstore.Shelf) (err error) {
service.Mutex.Lock()
defer service.Mutex.Unlock()
// look up a shelf from the Shelves map.
shelf, err := service.getShelf(parameters.Shelf)
if err != nil {
(*responses).Default = &bookstore.Error{Code: int32(http.StatusNotFound), Message: err.Error()}
return nil
return err
} else {
(*responses).OK = shelf
*response = *shelf
return nil
}
}
Expand All @@ -107,34 +103,31 @@ func (service *Service) DeleteShelf(parameters *bookstore.DeleteShelfParameters)
return nil
}

func (service *Service) ListBooks(parameters *bookstore.ListBooksParameters, responses *bookstore.ListBooksResponses) (err error) {
func (service *Service) ListBooks(parameters *bookstore.ListBooksParameters, response *bookstore.ListBooksResponse) (err error) {
service.Mutex.Lock()
defer service.Mutex.Unlock()
// list the books in a shelf
_, err = service.getShelf(parameters.Shelf)
if err != nil {
(*responses).Default = &bookstore.Error{Code: int32(http.StatusNotFound), Message: err.Error()}
return nil
return err
}
shelfBooks := service.Books[parameters.Shelf]
books := make([]bookstore.Book, 0, len(shelfBooks))
for _, book := range shelfBooks {
books = append(books, *book)
}
response := &bookstore.ListBooksResponse{}
response.Books = books
(*responses).OK = response

(*response).Books = books
return nil
}

func (service *Service) CreateBook(parameters *bookstore.CreateBookParameters, responses *bookstore.CreateBookResponses) (err error) {
func (service *Service) CreateBook(parameters *bookstore.CreateBookParameters, response *bookstore.Book) (err error) {
service.Mutex.Lock()
defer service.Mutex.Unlock()
// return "not found" if the shelf doesn't exist
shelf, err := service.getShelf(parameters.Shelf)
if err != nil {
(*responses).Default = &bookstore.Error{Code: int32(http.StatusNotFound), Message: err.Error()}
return nil
return err
}
// assign an id and name to a book and add it to the Books map.
service.LastBookID++
Expand All @@ -144,20 +137,20 @@ func (service *Service) CreateBook(parameters *bookstore.CreateBookParameters, r
if service.Books[parameters.Shelf] == nil {
service.Books[parameters.Shelf] = make(map[int64]*bookstore.Book)
}
service.Books[parameters.Shelf][bid] = &book
(*responses).OK = &book
service.Books[parameters.Shelf][bid] = book
*response = *book
return err
}

func (service *Service) GetBook(parameters *bookstore.GetBookParameters, responses *bookstore.GetBookResponses) (err error) {
func (service *Service) GetBook(parameters *bookstore.GetBookParameters, responses *bookstore.Book) (err error) {
service.Mutex.Lock()
defer service.Mutex.Unlock()
// get a book from the Books map
book, err := service.getBook(parameters.Shelf, parameters.Book)
if err != nil {
(*responses).Default = &bookstore.Error{Code: int32(http.StatusNotFound), Message: err.Error()}
return err
} else {
(*responses).OK = book
*responses = *book
}
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions examples/v2.0/sample/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func TestSample(t *testing.T) {
t.Log("get sample failed")
t.Fail()
}
if response.OK.Id != message || response.OK.Count != int32(len(message)) {
t.Log(fmt.Sprintf("get sample received %+v", response.OK))
if response == nil || response.Id != message || response.Count != int32(len(message)) {
t.Log(fmt.Sprintf("get sample received %+v", response))
t.Fail()
}
if (response == nil) || (response.OK == nil) {
t.Log(fmt.Sprintf("get sample failed %+v", response.OK))
if response == nil {
t.Log(fmt.Sprintf("get sample failed %+v", response))
t.Fail()
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/v2.0/sample/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func NewService() *Service {
return &Service{}
}

func (service *Service) GetSample(parameters *sample.GetSampleParameters, responses *sample.GetSampleResponses) (err error) {
(*responses).OK = &sample.Sample{
func (service *Service) GetSample(parameters *sample.GetSampleParameters, response *sample.Sample) (err error) {
*response = sample.Sample{
Id: parameters.Id,
Thing: map[string]interface{}{"thing": 123},
Thing: &sample.Thing{"thing": 123},
Count: int32(len(parameters.Id))}
return err
}
29 changes: 13 additions & 16 deletions examples/v3.0/bookstore/bookstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,25 @@ func TestBookstore(t *testing.T) {
t.Log("list shelves failed")
t.Fail()
}
if (response == nil) || (response.OK == nil) || (response.OK.Shelves != nil) {
t.Log(fmt.Sprintf("list shelves failed %+v", response.OK))
t.Log(fmt.Sprintf("list shelves failed len=%d", len(response.OK.Shelves)))
if response != nil && (len(response.Shelves) != 0) {
t.Log(fmt.Sprintf("list shelves failed %+v", response.Shelves))
t.Log(fmt.Sprintf("list shelves failed len=%d", len(response.Shelves)))
t.Fail()
}
}
// attempting to get a shelf should return an error
{
response, err := b.GetShelf(1)
if err == nil {
t.Logf("get shelf failed to return an error (%+v)", response.OK)
t.Logf("get shelf failed to return an error (%+v)", response)
t.Fail()
}
}
// attempting to get a book should return an error
{
response, err := b.GetBook(1, 2)
if err == nil {
t.Logf("get book failed to return an error (%+v)", response.OK)
t.Logf("get book failed to return an error (%+v)", response)
t.Fail()
}
}
Expand All @@ -78,8 +78,7 @@ func TestBookstore(t *testing.T) {
t.Log("create shelf mysteries failed")
t.Fail()
}
if (response.OK.Name != "shelves/1") ||
(response.OK.Theme != "mysteries") {
if (response == nil) || (response.Name != "shelves/1") || (response.Theme != "mysteries") {
t.Log("create shelf mysteries failed")
t.Fail()
}
Expand All @@ -93,8 +92,7 @@ func TestBookstore(t *testing.T) {
t.Log("create shelf comedies failed")
t.Fail()
}
if (response.OK.Name != "shelves/2") ||
(response.OK.Theme != "comedies") {
if (response == nil) || (response.Name != "shelves/2") || (response.Theme != "comedies") {
t.Log("create shelf comedies failed")
t.Fail()
}
Expand All @@ -106,8 +104,7 @@ func TestBookstore(t *testing.T) {
t.Log("get shelf mysteries failed")
t.Fail()
}
if (response.OK.Name != "shelves/1") ||
(response.OK.Theme != "mysteries") {
if (response == nil) || (response.Name != "shelves/1") || (response.Theme != "mysteries") {
t.Log("get shelf mysteries failed")
t.Fail()
}
Expand All @@ -119,7 +116,7 @@ func TestBookstore(t *testing.T) {
t.Log("list shelves failed")
t.Fail()
}
if len(response.OK.Shelves) != 2 {
if len(response.Shelves) != 2 {
t.Log("list shelves failed")
t.Fail()
}
Expand All @@ -139,7 +136,7 @@ func TestBookstore(t *testing.T) {
t.Log("list shelves failed")
t.Fail()
}
if len(response.OK.Shelves) != 1 {
if len(response.Shelves) != 1 {
t.Log("list shelves failed")
t.Fail()
}
Expand All @@ -151,7 +148,7 @@ func TestBookstore(t *testing.T) {
t.Log("list books failed")
t.Fail()
}
if len(response.OK.Books) != 0 {
if len(response.Books) != 0 {
t.Log("list books failed")
t.Fail()
}
Expand Down Expand Up @@ -193,7 +190,7 @@ func TestBookstore(t *testing.T) {
t.Log("list books failed")
t.Fail()
}
if len(response.OK.Books) != 2 {
if len(response.Books) != 2 {
t.Log("list books failed")
t.Fail()
}
Expand All @@ -213,7 +210,7 @@ func TestBookstore(t *testing.T) {
t.Log("list books failed")
t.Fail()
}
if len(response.OK.Books) != 1 {
if len(response.Books) != 1 {
t.Log("list books failed")
t.Fail()
}
Expand Down
Loading

0 comments on commit 221987e

Please sign in to comment.