Skip to content

Commit

Permalink
Self-including examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Apr 16, 2024
1 parent f9f960d commit 35dbd6b
Showing 1 changed file with 107 additions and 99 deletions.
206 changes: 107 additions & 99 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,101 +13,13 @@ import (
"github.com/googollee/go-espresso"
)

func ProvideLogger(ctx context.Context) (*slog.Logger, error) {
removeTime := func(groups []string, a slog.Attr) slog.Attr {
// Remove time from the output for predictable test output.
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
}

opt := slog.HandlerOptions{
ReplaceAttr: removeTime,
}
return slog.New(slog.NewTextHandler(os.Stdout, &opt)), nil
}

type Book struct {
ID int `json:"id"`
Title string `json:"title"`
}

type Books map[int]Book

func (books Books) GetBookHTTP(ctx espresso.Context) error {
var id int
if err := ctx.Endpoint(http.MethodGet, "/book/{id}").
BindPath("id", &id).
End(); err != nil {
return err
}

book, ok := books[id]
if !ok {
return espresso.Error(http.StatusNotFound, fmt.Errorf("not found"))
}

if err := espresso.CodecsModule.Value(ctx).EncodeResponse(ctx, &book); err != nil {
return err
}

return nil
}

func (books Books) CreateBookHTTP(ctx espresso.Context) error {
if err := ctx.Endpoint(http.MethodPost, "/book").
End(); err != nil {
return err
}

codecs := espresso.CodecsModule.Value(ctx)

var book Book
if err := codecs.DecodeRequest(ctx, &book); err != nil {
return espresso.Error(http.StatusBadRequest, err)
}

book.ID = len(books)
books[book.ID] = book

if err := codecs.EncodeResponse(ctx, &book); err != nil {
return err
}

return nil
}

func (books Books) GetBookRPC(ctx espresso.Context) (*Book, error) {
var id int
if err := ctx.Endpoint(http.MethodGet, "/book/{id}").
BindPath("id", &id).
End(); err != nil {
return nil, err
}

book, ok := books[id]
if !ok {
return nil, espresso.Error(http.StatusNotFound, fmt.Errorf("not found"))
}

return &book, nil

}

func (books Books) CreateBookRPC(ctx espresso.Context, book *Book) (*Book, error) {
if err := ctx.Endpoint(http.MethodPost, "/book").
End(); err != nil {
return nil, err
func ExampleEspresso() {
type Book struct {
ID int `json:"id"`
Title string `json:"title"`
}
type Books map[int]Book

book.ID = len(books)
books[book.ID] = *book

return book, nil
}

func ExampleEspresso() {
books := make(Books)
books[1] = Book{
ID: 1,
Expand All @@ -120,12 +32,65 @@ func ExampleEspresso() {

espo := espresso.New()
// Log to stdout for Output
espo.AddModule(espresso.LogModule.ProvideWithFunc(ProvideLogger))
espo.AddModule(espresso.LogModule.ProvideWithFunc(func(ctx context.Context) (*slog.Logger, error) {
removeTime := func(groups []string, a slog.Attr) slog.Attr {
// Remove time from the output for predictable test output.
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
}

opt := slog.HandlerOptions{
ReplaceAttr: removeTime,
}
return slog.New(slog.NewTextHandler(os.Stdout, &opt)), nil
}))

espo.AddModule(espresso.ProvideCodecs)

router := espo.WithPrefix("/http")
router.HandleFunc(books.GetBookHTTP)
router.HandleFunc(books.CreateBookHTTP)
router.HandleFunc(func(ctx espresso.Context) error {
var id int
if err := ctx.Endpoint(http.MethodGet, "/book/{id}").
BindPath("id", &id).
End(); err != nil {
return err
}

book, ok := books[id]
if !ok {
return espresso.Error(http.StatusNotFound, fmt.Errorf("not found"))
}

if err := espresso.CodecsModule.Value(ctx).EncodeResponse(ctx, &book); err != nil {
return err
}

return nil
})
router.HandleFunc(func(ctx espresso.Context) error {
if err := ctx.Endpoint(http.MethodPost, "/book").
End(); err != nil {
return err
}

codecs := espresso.CodecsModule.Value(ctx)

var book Book
if err := codecs.DecodeRequest(ctx, &book); err != nil {
return espresso.Error(http.StatusBadRequest, err)
}

book.ID = len(books)
books[book.ID] = book

if err := codecs.EncodeResponse(ctx, &book); err != nil {
return err
}

return nil
})

svr := httptest.NewServer(espo)
defer svr.Close()
Expand Down Expand Up @@ -185,6 +150,12 @@ func ExampleEspresso() {
}

func ExampleEspresso_rpc() {
type Book struct {
ID int `json:"id"`
Title string `json:"title"`
}
type Books map[int]Book

books := make(Books)
books[1] = Book{
ID: 1,
Expand All @@ -197,12 +168,49 @@ func ExampleEspresso_rpc() {

espo := espresso.New()
// Log to stdout for Output
espo.AddModule(espresso.LogModule.ProvideWithFunc(ProvideLogger))
espo.AddModule(espresso.LogModule.ProvideWithFunc(func(ctx context.Context) (*slog.Logger, error) {
removeTime := func(groups []string, a slog.Attr) slog.Attr {
// Remove time from the output for predictable test output.
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
}

opt := slog.HandlerOptions{
ReplaceAttr: removeTime,
}
return slog.New(slog.NewTextHandler(os.Stdout, &opt)), nil
}))
espo.AddModule(espresso.ProvideCodecs)

router := espo.WithPrefix("/rpc")
router.HandleFunc(espresso.RPCRetrive(books.GetBookRPC))
router.HandleFunc(espresso.RPC(books.CreateBookRPC))
router.HandleFunc(espresso.RPCRetrive(func(ctx espresso.Context) (*Book, error) {
var id int
if err := ctx.Endpoint(http.MethodGet, "/book/{id}").
BindPath("id", &id).
End(); err != nil {
return nil, err
}

book, ok := books[id]
if !ok {
return nil, espresso.Error(http.StatusNotFound, fmt.Errorf("not found"))
}

return &book, nil
}))
router.HandleFunc(espresso.RPC(func(ctx espresso.Context, book *Book) (*Book, error) {
if err := ctx.Endpoint(http.MethodPost, "/book").
End(); err != nil {
return nil, err
}

book.ID = len(books)
books[book.ID] = *book

return book, nil
}))

svr := httptest.NewServer(espo)
defer svr.Close()
Expand Down

0 comments on commit 35dbd6b

Please sign in to comment.