Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang docs #442

Merged
merged 17 commits into from
Sep 4, 2024
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
28 changes: 26 additions & 2 deletions .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ on:
description: "sdk-java version (without prepending v)."
required: false
type: string
sdkGoVersion:
description: "sdk-go version (WITH the prepending v)."
required: false
type: string

jobs:
updates:
Expand Down Expand Up @@ -49,8 +53,8 @@ jobs:
- uses: actions/setup-java@v4
if: ${{ inputs.sdkJavaVersion != '' }}
with:
distribution: 'temurin'
java-version: '21'
distribution: "temurin"
java-version: "21"
- name: Setup Gradle
if: ${{ inputs.sdkJavaVersion != '' }}
uses: gradle/actions/setup-gradle@v3
Expand Down Expand Up @@ -132,6 +136,26 @@ jobs:
repository: restatedev/sdk-java
ref: v${{ inputs.sdkJavaVersion }}
path: temp-sdk-java

# Setup Go
- uses: actions/setup-go@v5
if: ${{ inputs.sdkGoVersion != '' }}
with:
go-version: "1.21"

# Upgrade Go code snippets if new version is provided
- name: Bump sdk-go
if: github.event.inputs.sdkGoVersion != ''
working-directory: code_snippets/go
run: |
go get github.com/restatedev/sdk-go@${{ github.event.inputs.sdkGoVersion }}
go mod tidy

# Check Go code snippets
- name: Test Go code snippets
working-directory: code_snippets/go
run: go test ./...

- name: Run the runtime generate script
if: ${{ inputs.sdkJavaVersion != '' }}
run: |
Expand Down
18 changes: 14 additions & 4 deletions .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
- name: Test build website
run: yarn build


# Test TypeScript code snippets
- name: Compile TypeScript code snippets
run: npm install --prefix code_snippets/ts && npm run build --prefix code_snippets/ts
Expand All @@ -44,8 +43,8 @@ jobs:
# Setup Java
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21'
distribution: "temurin"
java-version: "21"

# Check Java code snippets
- name: Test Java code snippets
Expand All @@ -59,4 +58,15 @@ jobs:
uses: gradle/gradle-build-action@v2
with:
arguments: check
build-root-directory: code_snippets/kotlin
build-root-directory: code_snippets/kotlin

# Setup Go
- uses: actions/setup-go@v5
if: github.event.inputs.sdkGoVersion != ''
with:
go-version: "1.21"

# Check Go code snippets
- name: Test Go code snippets
working-directory: code_snippets/go
run: go test ./...
80 changes: 80 additions & 0 deletions code_snippets/go/concepts/foodordering.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package concepts

import restate "github.com/restatedev/sdk-go"

type OrderProcessor struct{}

// <start_here>
// <mark_1>
func (OrderProcessor) Process(ctx restate.ObjectContext, order Order) error {
// </mark_1>
// 1. Set status
// <mark_4>
restate.Set(ctx, "status", Status_CREATED)
// </mark_4>

// 2. Handle payment
// <mark_5>
token := restate.Rand(ctx).UUID().String()
paid, err := restate.Run(ctx, func(ctx restate.RunContext) (bool, error) {
return paymentClnt.Charge(ctx, order.Id, token, order.TotalCost)
})
if err != nil {
return err
}
// </mark_5>

if !paid {
// <mark_4>
restate.Set(ctx, "status", Status_REJECTED)
// </mark_4>
return nil
}

// 3. Wait until the requested preparation time
// <mark_4>
restate.Set(ctx, "status", Status_SCHEDULED)
// </mark_4>
if err := restate.Sleep(ctx, order.DeliveryDelay); err != nil {
return err
}

// 4. Trigger preparation
// <mark_3>
preparationAwakeable := restate.Awakeable[restate.Void](ctx)
// <mark_5>
if _, err := restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) {
return restate.Void{}, restaurant.Prepare(order.Id, preparationAwakeable.Id())
}); err != nil {
return err
}
// </mark_5>
// </mark_3>
// <mark_4>
restate.Set(ctx, "status", Status_IN_PREPARATION)
// </mark_4>

// <mark_3>
if _, err := preparationAwakeable.Result(); err != nil {
return err
}
// </mark_3>
// <mark_4>
restate.Set(ctx, "status", Status_SCHEDULING_DELIVERY)
// </mark_4>

// 5. Find a driver and start delivery
// <mark_2>
if _, err := restate.Object[restate.Void](ctx, "DeliveryManager", order.Id, "StartDelivery").
Request(order); err != nil {
return err
}
// </mark_2>
// <mark_4>
restate.Set(ctx, "status", Status_DELIVERED)
// </mark_4>

return nil
}

// <end_here>
50 changes: 50 additions & 0 deletions code_snippets/go/concepts/invocations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package concepts

import (
"time"

restate "github.com/restatedev/sdk-go"
)

type MyService struct{}

/*
// <start_rpc_call>
func (MyService) MyRestateHandler(ctx restate.Context) error {
// focus
greet, err := restate.
// focus
Service[string](ctx, "Greeter", "Greet").
// focus
Request("Hi")
return err
}

// <end_rpc_call>

// <start_one_way_call>
func (MyService) MyRestateHandler(ctx restate.Context) error {
// focus
restate.
// focus
ServiceSend(ctx, "Greeter", "Greet").
// focus
Send("Hi")
return nil
}

// <end_one_way_call>
*/

// <start_delayed_call>
func (MyService) MyRestateHandler(ctx restate.Context) error {
// focus
restate.
// focus
ServiceSend(ctx, "Greeter", "Greet").
// focus
Send("Hi", restate.WithDelay(1*time.Second))
return nil
}

// <end_delayed_call>
87 changes: 87 additions & 0 deletions code_snippets/go/concepts/services/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"context"
"log"

restate "github.com/restatedev/sdk-go"
"github.com/restatedev/sdk-go/server"
)

type RoleUpdate struct{}

// <start_here>
// <mark_2>
func (RoleUpdate) ApplyRoleUpdate(ctx restate.Context, update UpdateRequest) error {
// </mark_2>

// <mark_1>
success, err := restate.Run(ctx, func(ctx restate.RunContext) (bool, error) {
return applyUserRole(update.UserId, update.Role)
})
if err != nil {
return err
}
// </mark_1>
// <mark_3>
if !success {
return nil
}
// </mark_3>

// <mark_3>
for _, permission := range update.Permissions {
// </mark_3>
// <mark_1>
if _, err := restate.Run(ctx, func(ctx restate.RunContext) (restate.Void, error) {
return restate.Void{}, applyPermission(update.UserId, permission)
}); err != nil {
return err
}
// </mark_1>
// <mark_3>
}
// </mark_3>

return nil
}

func main() {
if err := server.NewRestate().
Bind(restate.Reflect(RoleUpdate{})).
Start(context.Background(), ":9080"); err != nil {
log.Fatal(err)
}
}

// <end_here>

type UserRole struct {
RoleKey string
RoleDescription string
}

type Permission struct {
PermissionKey string
Setting string
}

type UpdateRequest struct {
UserId string
Role UserRole
Permissions []Permission
}

func applyUserRole(
userId string,
userRole UserRole,
) (bool, error) {
return true, nil
}

func applyPermission(
userId string,
permission Permission,
) error {
return nil
}
44 changes: 44 additions & 0 deletions code_snippets/go/concepts/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package concepts

import (
"context"
"time"
)

type Status int

const (
Status_NEW Status = iota
Status_CREATED
Status_SCHEDULED
Status_IN_PREPARATION
Status_SCHEDULING_DELIVERY
Status_WAITING_FOR_DRIVER
Status_IN_DELIVERY
Status_DELIVERED
Status_REJECTED
Status_CANCELLED
Status_UNKNOWN
)

type Order struct {
Id string
TotalCost int
DeliveryDelay time.Duration
}

type PaymentClient struct{}

func (PaymentClient) Charge(ctx context.Context, id string, token string, amount int) (bool, error) {
return true, nil
}

var paymentClnt PaymentClient

type RestaurantClient struct{}

func (RestaurantClient) Prepare(id string, cb string) error {
return nil
}

var restaurant = RestaurantClient{}
Loading
Loading