Skip to content

Commit

Permalink
Update for latest go sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkleeman committed Aug 21, 2024
1 parent f576c48 commit 4b64b25
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 151 deletions.
6 changes: 3 additions & 3 deletions tutorials/tour-of-restate-go/app/cartobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ func (CartObject) AddTicket(ctx restate.ObjectContext, ticketId string) (bool, e
return true, nil
}

func (CartObject) Checkout(ctx restate.ObjectContext, _ restate.Void) (bool, error) {
func (CartObject) Checkout(ctx restate.ObjectContext) (bool, error) {
return true, nil
}

func (CartObject) ExpireTicket(ctx restate.ObjectContext, ticketId string) (restate.Void, error) {
return restate.Void{}, nil
func (CartObject) ExpireTicket(ctx restate.ObjectContext, ticketId string) error {
return nil
}

// <end_user_session>
10 changes: 5 additions & 5 deletions tutorials/tour-of-restate-go/app/ticketobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import restate "github.com/restatedev/sdk-go"

type TicketObject struct{}

func (TicketObject) Reserve(ctx restate.ObjectContext, _ restate.Void) (bool, error) {
func (TicketObject) Reserve(ctx restate.ObjectContext) (bool, error) {
return true, nil
}

func (TicketObject) Unreserve(ctx restate.ObjectContext, _ restate.Void) (restate.Void, error) {
return restate.Void{}, nil
func (TicketObject) Unreserve(ctx restate.ObjectContext) error {
return nil
}

func (TicketObject) MarkAsSold(ctx restate.ObjectContext, _ restate.Void) (restate.Void, error) {
return restate.Void{}, nil
func (TicketObject) MarkAsSold(ctx restate.ObjectContext) error {
return nil
}
4 changes: 2 additions & 2 deletions tutorials/tour-of-restate-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module github.com/restatedev/examples/tutorials/tour-of-restate-go

go 1.22.5

require github.com/restatedev/sdk-go v0.10.0
require github.com/restatedev/sdk-go v0.10.1-0.20240821145953-f505c4c0cd3a

require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
8 changes: 4 additions & 4 deletions tutorials/tour-of-restate-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/restatedev/sdk-go v0.10.0 h1:JkPxHCqxF3jAT59qSu4TrW7eHFumMQOyv3JrvlDoULY=
github.com/restatedev/sdk-go v0.10.0/go.mod h1:6gHoU8pyP7YQfFWxKG2u94u/TSGen0qN7BWowWNDw4Y=
github.com/restatedev/sdk-go v0.10.1-0.20240821145953-f505c4c0cd3a h1:2RNe4dPWUFVEb8suN4vSOyYi0URoWirBZt9Jw+HsQBE=
github.com/restatedev/sdk-go v0.10.1-0.20240821145953-f505c4c0cd3a/go.mod h1:6gHoU8pyP7YQfFWxKG2u94u/TSGen0qN7BWowWNDw4Y=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
Expand All @@ -22,7 +22,7 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16 changes: 7 additions & 9 deletions tutorials/tour-of-restate-go/part1/cartobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type CartObject struct{}
// <start_add_ticket>
func (CartObject) AddTicket(ctx restate.ObjectContext, ticketId string) (bool, error) {
// withClass highlight-line
reservationSuccess, err := restate.CallAs[bool](ctx.Object("TicketObject", ticketId, "Reserve")).Request(restate.Void{})
reservationSuccess, err := restate.Object[bool](ctx, "TicketObject", ticketId, "Reserve").Request(restate.Void{})
if err != nil {
return false, err
}
Expand All @@ -18,10 +18,10 @@ func (CartObject) AddTicket(ctx restate.ObjectContext, ticketId string) (bool, e
// <end_add_ticket>

// <start_checkout>
func (CartObject) Checkout(ctx restate.ObjectContext, _ restate.Void) (bool, error) {
func (CartObject) Checkout(ctx restate.ObjectContext) (bool, error) {
// withClass(1:2) highlight-line
success, err := restate.CallAs[bool](ctx.Service("CheckoutService", "Handle")).
Request(CheckoutRequest{UserId: ctx.Key(), Tickets: []string{"seat2B"}})
success, err := restate.Service[bool](ctx, "CheckoutService", "Handle").
Request(CheckoutRequest{UserId: restate.Key(ctx), Tickets: []string{"seat2B"}})
if err != nil {
return false, err
}
Expand All @@ -32,13 +32,11 @@ func (CartObject) Checkout(ctx restate.ObjectContext, _ restate.Void) (bool, err
// <end_checkout>

// <start_expire_ticket>
func (CartObject) ExpireTicket(ctx restate.ObjectContext, ticketId string) (restate.Void, error) {
func (CartObject) ExpireTicket(ctx restate.ObjectContext, ticketId string) error {
// withClass highlight-line
if err := ctx.Object("TicketObject", ticketId, "Unreserve").Send(restate.Void{}, 0); err != nil {
return restate.Void{}, err
}
restate.ObjectSend(ctx, "TicketObject", ticketId, "Unreserve").Send(restate.Void{})

return restate.Void{}, nil
return nil
}

// <end_expire_ticket>
10 changes: 5 additions & 5 deletions tutorials/tour-of-restate-go/part1/ticketobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import restate "github.com/restatedev/sdk-go"

type TicketObject struct{}

func (TicketObject) Reserve(ctx restate.ObjectContext, _ restate.Void) (bool, error) {
func (TicketObject) Reserve(ctx restate.ObjectContext) (bool, error) {
return true, nil
}

func (TicketObject) Unreserve(ctx restate.ObjectContext, _ restate.Void) (restate.Void, error) {
return restate.Void{}, nil
func (TicketObject) Unreserve(ctx restate.ObjectContext) error {
return nil
}

func (TicketObject) MarkAsSold(ctx restate.ObjectContext, _ restate.Void) (restate.Void, error) {
return restate.Void{}, nil
func (TicketObject) MarkAsSold(ctx restate.ObjectContext) error {
return nil
}
20 changes: 8 additions & 12 deletions tutorials/tour-of-restate-go/part2/cartobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ type CartObject struct{}
// <start_add_ticket>
func (CartObject) AddTicket(ctx restate.ObjectContext, ticketId string) (bool, error) {
// withClass highlight-line
reservationSuccess, err := restate.CallAs[bool](ctx.Object("TicketObject", ticketId, "Reserve")).Request(restate.Void{})
reservationSuccess, err := restate.Object[bool](ctx, "TicketObject", ticketId, "Reserve").Request(restate.Void{})
if err != nil {
return false, err
}

if reservationSuccess {
// withClass highlight-line
if err := ctx.Object("CartObject", ctx.Key(), "ExpireTicket").Send(ticketId, 15*time.Minute); err != nil {
return false, err
}
restate.ObjectSend(ctx, "CartObject", restate.Key(ctx), "ExpireTicket").Send(ticketId, restate.WithDelay(15*time.Minute))
}

return reservationSuccess, nil
Expand All @@ -29,10 +27,10 @@ func (CartObject) AddTicket(ctx restate.ObjectContext, ticketId string) (bool, e
// <end_add_ticket>

// <start_checkout>
func (CartObject) Checkout(ctx restate.ObjectContext, _ restate.Void) (bool, error) {
func (CartObject) Checkout(ctx restate.ObjectContext) (bool, error) {
// withClass(1:2) highlight-line
success, err := restate.CallAs[bool](ctx.Service("CheckoutService", "Handle")).
Request(CheckoutRequest{UserId: ctx.Key(), Tickets: []string{"seat2B"}})
success, err := restate.Service[bool](ctx, "CheckoutService", "Handle").
Request(CheckoutRequest{UserId: restate.Key(ctx), Tickets: []string{"seat2B"}})
if err != nil {
return false, err
}
Expand All @@ -43,13 +41,11 @@ func (CartObject) Checkout(ctx restate.ObjectContext, _ restate.Void) (bool, err
// <end_checkout>

// <start_expire_ticket>
func (CartObject) ExpireTicket(ctx restate.ObjectContext, ticketId string) (restate.Void, error) {
func (CartObject) ExpireTicket(ctx restate.ObjectContext, ticketId string) error {
// withClass highlight-line
if err := ctx.Object("TicketObject", ticketId, "Unreserve").Send(restate.Void{}, 0); err != nil {
return restate.Void{}, err
}
restate.ObjectSend(ctx, "TicketObject", ticketId, "Unreserve").Send(restate.Void{})

return restate.Void{}, nil
return nil
}

// <end_expire_ticket>
10 changes: 5 additions & 5 deletions tutorials/tour-of-restate-go/part2/ticketobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import restate "github.com/restatedev/sdk-go"

type TicketObject struct{}

func (TicketObject) Reserve(ctx restate.ObjectContext, _ restate.Void) (bool, error) {
func (TicketObject) Reserve(ctx restate.ObjectContext) (bool, error) {
return true, nil
}

func (TicketObject) Unreserve(ctx restate.ObjectContext, _ restate.Void) (restate.Void, error) {
return restate.Void{}, nil
func (TicketObject) Unreserve(ctx restate.ObjectContext) error {
return nil
}

func (TicketObject) MarkAsSold(ctx restate.ObjectContext, _ restate.Void) (restate.Void, error) {
return restate.Void{}, nil
func (TicketObject) MarkAsSold(ctx restate.ObjectContext) error {
return nil
}
52 changes: 20 additions & 32 deletions tutorials/tour-of-restate-go/part3/cartobject.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"slices"
"time"

Expand All @@ -13,25 +12,21 @@ type CartObject struct{}
// <start_add_ticket>
func (CartObject) AddTicket(ctx restate.ObjectContext, ticketId string) (bool, error) {
// withClass highlight-line
reservationSuccess, err := restate.CallAs[bool](ctx.Object("TicketObject", ticketId, "Reserve")).Request(restate.Void{})
reservationSuccess, err := restate.Object[bool](ctx, "TicketObject", ticketId, "Reserve").Request(restate.Void{})
if err != nil {
return false, err
}

if reservationSuccess {
// withClass(1:8) highlight-line
tickets, err := restate.GetAs[[]string](ctx, "tickets")
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) {
// withClass(1:6) highlight-line
tickets, err := restate.Get[[]string](ctx, "tickets")
if err != nil {
return false, err
}
tickets = append(tickets, ticketId)
if err := ctx.Set("tickets", tickets); err != nil {
return false, err
}
restate.Set(ctx, "tickets", tickets)

if err := ctx.Object("CartObject", ctx.Key(), "ExpireTicket").Send(ticketId, 15*time.Minute); err != nil {
return false, err
}
restate.ObjectSend(ctx, "CartObject", restate.Key(ctx), "ExpireTicket").Send(ticketId, restate.WithDelay(15*time.Minute))
}

return reservationSuccess, nil
Expand All @@ -40,25 +35,22 @@ func (CartObject) AddTicket(ctx restate.ObjectContext, ticketId string) (bool, e
// <end_add_ticket>

// <start_checkout>
func (CartObject) Checkout(ctx restate.ObjectContext, _ restate.Void) (bool, error) {
// withClass(1:7) highlight-line
tickets, err := restate.GetAs[[]string](ctx, "tickets")
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) {
func (CartObject) Checkout(ctx restate.ObjectContext) (bool, error) {
// withClass(1:4) highlight-line
tickets, err := restate.Get[[]string](ctx, "tickets")
if err != nil || len(tickets) == 0 {
return false, err
}
if len(tickets) == 0 {
return false, nil
}

success, err := restate.CallAs[bool](ctx.Service("CheckoutService", "Handle")).
Request(CheckoutRequest{UserId: ctx.Key(), Tickets: []string{"seat2B"}})
success, err := restate.Service[bool](ctx, "CheckoutService", "Handle").
Request(CheckoutRequest{UserId: restate.Key(ctx), Tickets: []string{"seat2B"}})
if err != nil {
return false, err
}

if success {
// withClass highlight-line
ctx.Clear("tickets")
restate.Clear(ctx, "tickets")
}

return success, nil
Expand All @@ -67,25 +59,21 @@ func (CartObject) Checkout(ctx restate.ObjectContext, _ restate.Void) (bool, err
// <end_checkout>

// <start_expire_ticket>
func (CartObject) ExpireTicket(ctx restate.ObjectContext, ticketId string) (restate.Void, error) {
tickets, err := restate.GetAs[[]string](ctx, "tickets")
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) {
return restate.Void{}, err
func (CartObject) ExpireTicket(ctx restate.ObjectContext, ticketId string) error {
tickets, err := restate.Get[[]string](ctx, "tickets")
if err != nil {
return err
}
ticketI := slices.Index(tickets, ticketId)

if ticketI != -1 {
tickets = slices.Delete(tickets, ticketI, ticketI+1)
if err := ctx.Set("tickets", tickets); err != nil {
return restate.Void{}, err
}
restate.Set(ctx, "tickets", tickets)

if err := ctx.Object("TicketObject", ticketId, "Unreserve").Send(restate.Void{}, 0); err != nil {
return restate.Void{}, err
}
restate.ObjectSend(ctx, "TicketObject", ticketId, "Unreserve").Send(restate.Void{})
}

return restate.Void{}, nil
return nil
}

// <end_expire_ticket>
34 changes: 16 additions & 18 deletions tutorials/tour-of-restate-go/part3/ticketobject.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package main

import (
"errors"

"github.com/restatedev/examples/tutorials/tour-of-restate-go/auxiliary"
restate "github.com/restatedev/sdk-go"
)

type TicketObject struct{}

// <start_reserve>
func (TicketObject) Reserve(ctx restate.ObjectContext, _ restate.Void) (bool, error) {
status, err := restate.GetAs[auxiliary.TicketStatus](ctx, "status")
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) {
func (TicketObject) Reserve(ctx restate.ObjectContext) (bool, error) {
status, err := restate.Get[auxiliary.TicketStatus](ctx, "status")
if err != nil {
return false, err
}

if status == auxiliary.TicketStatusAvailable {
ctx.Set("status", auxiliary.TicketStatusReserved)
restate.Set(ctx, "status", auxiliary.TicketStatusReserved)
return true, nil
} else {
return false, nil
Expand All @@ -27,33 +25,33 @@ func (TicketObject) Reserve(ctx restate.ObjectContext, _ restate.Void) (bool, er
// <end_reserve>

// <start_unreserve>
func (TicketObject) Unreserve(ctx restate.ObjectContext, _ restate.Void) (restate.Void, error) {
status, err := restate.GetAs[auxiliary.TicketStatus](ctx, "status")
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) {
return restate.Void{}, err
func (TicketObject) Unreserve(ctx restate.ObjectContext) error {
status, err := restate.Get[auxiliary.TicketStatus](ctx, "status")
if err != nil {
return err
}

if status != auxiliary.TicketStatusSold {
ctx.Clear("status")
restate.Clear(ctx, "status")
}

return restate.Void{}, nil
return nil
}

// <end_unreserve>

// <start_mark_as_sold>
func (TicketObject) MarkAsSold(ctx restate.ObjectContext, _ restate.Void) (restate.Void, error) {
status, err := restate.GetAs[auxiliary.TicketStatus](ctx, "status")
if err != nil && !errors.Is(err, restate.ErrKeyNotFound) {
return restate.Void{}, err
func (TicketObject) MarkAsSold(ctx restate.ObjectContext) error {
status, err := restate.Get[auxiliary.TicketStatus](ctx, "status")
if err != nil {
return err
}

if status == auxiliary.TicketStatusReserved {
ctx.Set("status", auxiliary.TicketStatusSold)
restate.Set(ctx, "status", auxiliary.TicketStatusSold)
}

return restate.Void{}, nil
return nil
}

// <end_mark_as_sold>
Loading

0 comments on commit 4b64b25

Please sign in to comment.