Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,212 changes: 606 additions & 606 deletions example/bookstore/v1/bookstore.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/bookstore/v1/bookstore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ message Book {
int32 edition = 4 [(google.api.field_behavior) = REQUIRED];

// Field for author.
repeated Author author = 5 [(google.api.field_behavior) = REQUIRED];
repeated Author author = 5;

// Field for path.
string path = 10018;
Expand Down
3 changes: 1 addition & 2 deletions example/bookstore/v1/bookstore.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1515,8 +1515,7 @@
"isbn",
"price",
"published",
"edition",
"author"
"edition"
]
},
"v1BookEdition": {
Expand Down
2 changes: 1 addition & 1 deletion example/bookstore/v1/bookstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ resources:
parents: ["publisher"]
schema:
type: object
required: ["author", "edition", "isbn", "price", "published"]
required: ["edition", "isbn", "price", "published"]
properties:
isbn:
type: array
Expand Down
1 change: 0 additions & 1 deletion example/bookstore/v1/bookstore_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,6 @@
]
},
"required": [
"author",
"edition",
"isbn",
"price",
Expand Down
1 change: 0 additions & 1 deletion example/bookstore/v1/bookstore_openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ components:
type: boolean
x-aep-field-number: 3
required:
- author
- edition
- isbn
- price
Expand Down
57 changes: 40 additions & 17 deletions example/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ func NewBookstoreServer(db *sql.DB) *BookstoreServer {
}

func (s BookstoreServer) CreateBook(_ context.Context, r *bpb.CreateBookRequest) (*bpb.Book, error) {
book := proto.Clone(r.Book).(*bpb.Book)
book, err := NewSerializableBook(proto.Clone(r.Book).(*bpb.Book))
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create book: %v", err)
}
log.Printf("creating book %q", r)
if r.Id == "" {
var maxID int
Expand All @@ -87,23 +90,25 @@ func (s BookstoreServer) CreateBook(_ context.Context, r *bpb.CreateBookRequest)
path := fmt.Sprintf("%v/books/%v", r.Parent, r.Id)
book.Path = path

_, err := s.db.Exec(`
_, err = s.db.Exec(`
INSERT INTO books (path, author, price, published, edition, isbn)
VALUES (?, ?, ?, ?, ?, ?)`,
book.Path, book.Author, book.Price, book.Published, book.Edition, book.Isbn)
book.Path, book.AuthorSerialized, book.Price, book.Published, book.Edition, book.IsbnSerialized)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create book: %v", err)
}

log.Printf("created book %q", path)
return book, nil
return book.Book, nil
}

func (s BookstoreServer) ApplyBook(_ context.Context, r *bpb.ApplyBookRequest) (*bpb.Book, error) {
book, err := NewSerializableBook(proto.Clone(r.Book).(*bpb.Book))
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create book: %v", err)
}
log.Printf("applying book request: %v", r)
book := proto.Clone(r.Book).(*bpb.Book)
book.Path = r.Path

result, err := s.db.Exec(`
INSERT INTO books (path, author, price, published, edition, isbn)
VALUES (?, ?, ?, ?, ?, ?)
Expand All @@ -113,7 +118,7 @@ func (s BookstoreServer) ApplyBook(_ context.Context, r *bpb.ApplyBookRequest) (
published = excluded.published,
edition = excluded.edition,
isbn = excluded.isbn`,
book.Path, book.Author, book.Price, book.Published, book.Edition, book.Isbn)
book.Path, book.AuthorSerialized, book.Price, book.Published, book.Edition, book.IsbnSerialized)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to apply book: %v", err)
}
Expand All @@ -126,18 +131,20 @@ func (s BookstoreServer) ApplyBook(_ context.Context, r *bpb.ApplyBookRequest) (
}

log.Printf("applied book %q", book.Path)
return book, nil
return book.Book, nil
}

func (s BookstoreServer) UpdateBook(_ context.Context, r *bpb.UpdateBookRequest) (*bpb.Book, error) {
book := proto.Clone(r.Book).(*bpb.Book)
book, err := NewSerializableBook(proto.Clone(r.Book).(*bpb.Book))
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create book: %v", err)
}
book.Path = r.Path

result, err := s.db.Exec(`
UPDATE books
SET author = ?, price = ?, published = ?, edition = ?
WHERE path = ?`,
book.Author, book.Price, book.Published, book.Edition, book.Path)
book.AuthorSerialized, book.Price, book.Published, book.Edition, book.Path)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update book: %v", err)
}
Expand All @@ -151,7 +158,7 @@ func (s BookstoreServer) UpdateBook(_ context.Context, r *bpb.UpdateBookRequest)
}

log.Printf("updated book %q", book.Path)
return book, nil
return book.Book, nil
}

func (s BookstoreServer) DeleteBook(_ context.Context, r *bpb.DeleteBookRequest) (*emptypb.Empty, error) {
Expand All @@ -174,23 +181,31 @@ func (s BookstoreServer) DeleteBook(_ context.Context, r *bpb.DeleteBookRequest)

func (s BookstoreServer) GetBook(_ context.Context, r *bpb.GetBookRequest) (*bpb.Book, error) {
book := &bpb.Book{}

// Deserialize the 'author' field from JSON when reading from the database
var authorsSerialized string
var isbnSerialized string
err := s.db.QueryRow(`
SELECT path, author, price, published, edition
SELECT path, author, price, published, edition, isbn
FROM books WHERE path = ?`, r.Path).Scan(
&book.Path, &book.Author, &book.Price, &book.Published, &book.Edition)

&book.Path, &authorsSerialized, &book.Price, &book.Published, &book.Edition, isbnSerialized)
if err == sql.ErrNoRows {
return nil, status.Errorf(codes.NotFound, "book %q not found", r.Path)
}
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get book: %v", err)
}
err = UnmarshalIntoBook(authorsSerialized, isbnSerialized, book)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to deserialize book: %v", err)
}

return book, nil
}

func (s BookstoreServer) ListBooks(_ context.Context, r *bpb.ListBooksRequest) (*bpb.ListBooksResponse, error) {
rows, err := s.db.Query(`
SELECT path, author, price, published, edition
SELECT path, author, price, published, edition, isbn
FROM books`)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list books: %v", err)
Expand All @@ -200,9 +215,17 @@ func (s BookstoreServer) ListBooks(_ context.Context, r *bpb.ListBooksRequest) (
var books []*bpb.Book
for rows.Next() {
book := &bpb.Book{}
if err := rows.Scan(&book.Path, &book.Author, &book.Price, &book.Published, &book.Edition); err != nil {

// Deserialize the 'author' field from JSON when listing books
var authorsSerialized string
var isbnSerialized string
if err := rows.Scan(&book.Path, &authorsSerialized, &book.Price, &book.Published, &book.Edition, &isbnSerialized); err != nil {
return nil, status.Errorf(codes.Internal, "failed to scan book: %v", err)
}
if err := UnmarshalIntoBook(authorsSerialized, isbnSerialized, book); err != nil {
return nil, status.Errorf(codes.Internal, "failed to deserialize authors: %v", err)
}

books = append(books, book)
}
if err = rows.Err(); err != nil {
Expand Down
43 changes: 43 additions & 0 deletions example/service/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package service

import (
"encoding/json"
"fmt"

bpb "github.com/aep-dev/aepc/example/bookstore/v1"
)

type SerializableBook struct {
*bpb.Book
AuthorSerialized string
IsbnSerialized string
}

func NewSerializableBook(b *bpb.Book) (*SerializableBook, error) {
authorSerialized, err := json.Marshal(b.Author)
if err != nil {
return nil, fmt.Errorf("failed to serialize author: %v", err)
}
isbnSerialized, err := json.Marshal(b.Isbn)
if err != nil {
return nil, fmt.Errorf("failed to serialize author: %v", err)
}
return &SerializableBook{
Book: b,
AuthorSerialized: string(authorSerialized),
IsbnSerialized: string(isbnSerialized),
}, nil
}

func UnmarshalIntoBook(authorsSerialized, isbnSerialized string, b *bpb.Book) error {
err := json.Unmarshal([]byte(authorsSerialized), &b.Author)
if err != nil {
return fmt.Errorf("failed to deserialize authors: %v", err)
}

err = json.Unmarshal([]byte(isbnSerialized), &b.Isbn)
if err != nil {
return fmt.Errorf("failed to deserialize isbn: %v", err)
}
return nil
}