Skip to content

Commit

Permalink
Write a client in the codegen example
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkleeman committed Nov 7, 2024
1 parent 07b19db commit 0e33acb
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
38 changes: 38 additions & 0 deletions examples/codegen/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"fmt"

"github.com/restatedev/sdk-go/client"
helloworld "github.com/restatedev/sdk-go/examples/codegen/proto"
)

func main() {
ctx, err := client.Connect(context.Background(), "http://127.0.0.1:8080")
if err != nil {
panic(err)
}

greeter := helloworld.NewGreeterIngressClient(ctx)
greeting, err := greeter.SayHello().Request(&helloworld.HelloRequest{Name: "world"})
if err != nil {
panic(err)
}
fmt.Println(greeting.Message)

workflow := helloworld.NewWorkflowIngressClient(ctx, "my-workflow")
send, err := workflow.Run().Send(&helloworld.RunRequest{})
if err != nil {
panic(err)
}
status, err := workflow.Status().Request(&helloworld.StatusRequest{})
if err != nil {
panic(err)
}
fmt.Println("workflow running with invocation id", send.InvocationId, "and status", status.Status)

if _, err := workflow.Finish().Request(&helloworld.FinishRequest{}); err != nil {
panic(err)
}
}
49 changes: 49 additions & 0 deletions examples/codegen/proto/helloworld_restate.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions protoc-gen-go-restate/restate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ func generateIngressClientStruct(g *protogen.GeneratedFile, service *protogen.Se
g.P("type ", unexport(clientName), " struct {")
g.P("ctx ", contextPackage.Ident("Context"))
serviceType := proto.GetExtension(service.Desc.Options().(*descriptorpb.ServiceOptions), sdk.E_ServiceType).(sdk.ServiceType)
if serviceType == sdk.ServiceType_VIRTUAL_OBJECT {
switch serviceType {
case sdk.ServiceType_VIRTUAL_OBJECT:
g.P("key string")
case sdk.ServiceType_WORKFLOW:
g.P("workflowID string")
}
g.P("options []", clientPackage.Ident("IngressClientOption"))
g.P("}")
Expand All @@ -65,8 +68,11 @@ func generateNewIngressClientDefinitions(g *protogen.GeneratedFile, service *pro
g.P("return &", unexport(clientName), "{")
g.P("ctx,")
serviceType := proto.GetExtension(service.Desc.Options().(*descriptorpb.ServiceOptions), sdk.E_ServiceType).(sdk.ServiceType)
if serviceType == sdk.ServiceType_VIRTUAL_OBJECT {
switch serviceType {
case sdk.ServiceType_VIRTUAL_OBJECT:
g.P("key,")
case sdk.ServiceType_WORKFLOW:
g.P("workflowID,")
}
g.P("cOpts,")
g.P("}")
Expand Down Expand Up @@ -257,8 +263,11 @@ func genService(gen *protogen.Plugin, g *protogen.GeneratedFile, service *protog
}
g.P("// New", ingressClientName, " must be called with a ctx returned from github.com/restatedev/sdk-go/client.Connect")
newIngressClientSignature := "New" + ingressClientName + " (ctx " + g.QualifiedGoIdent(contextPackage.Ident("Context"))
if serviceType == sdk.ServiceType_VIRTUAL_OBJECT {
switch serviceType {
case sdk.ServiceType_VIRTUAL_OBJECT:
newIngressClientSignature += ", key string"
case sdk.ServiceType_WORKFLOW:
newIngressClientSignature += ", workflowID string"
}
newIngressClientSignature += ", opts..." + g.QualifiedGoIdent(clientPackage.Ident("IngressClientOption")) + ") " + ingressClientName

Expand Down

0 comments on commit 0e33acb

Please sign in to comment.