Skip to content

Commit

Permalink
Use pointers for request/response structs instead of values in genera…
Browse files Browse the repository at this point in the history
…ting methods (#33)


Signed-off-by: Dmitry Volodin <dmvolod@gmail.com>
  • Loading branch information
dmvolod authored Mar 14, 2023
1 parent 35c6d8b commit d909512
Show file tree
Hide file tree
Showing 48 changed files with 286 additions and 209 deletions.
2 changes: 1 addition & 1 deletion examples/helloworld/greeting/greet.pb.go

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

17 changes: 9 additions & 8 deletions examples/helloworld/greeting/greet_host.pb.go

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

2 changes: 1 addition & 1 deletion examples/helloworld/greeting/greet_plugin.pb.go

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

4 changes: 2 additions & 2 deletions examples/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func run() error {
}
defer eveningPlugin.Close(ctx)

reply, err := morningPlugin.Greet(ctx, greeting.GreetRequest{
reply, err := morningPlugin.Greet(ctx, &greeting.GreetRequest{
Name: "go-plugin",
})
if err != nil {
Expand All @@ -42,7 +42,7 @@ func run() error {

fmt.Println(reply.GetMessage())

reply, err = eveningPlugin.Greet(ctx, greeting.GreetRequest{
reply, err = eveningPlugin.Greet(ctx, &greeting.GreetRequest{
Name: "go-plugin",
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions examples/helloworld/plugin-evening/evening.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type GoodEvening struct{}

var _ greeting.Greeter = (*GoodEvening)(nil)

func (m GoodEvening) Greet(_ context.Context, request greeting.GreetRequest) (greeting.GreetReply, error) {
return greeting.GreetReply{
func (m GoodEvening) Greet(_ context.Context, request *greeting.GreetRequest) (*greeting.GreetReply, error) {
return &greeting.GreetReply{
Message: fmt.Sprintf("Good evening, %s", request.GetName()),
}, nil
}
4 changes: 2 additions & 2 deletions examples/helloworld/plugin-morning/morning.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type GoodMorning struct{}

var _ greeting.Greeter = (*GoodMorning)(nil)

func (m GoodMorning) Greet(ctx context.Context, request greeting.GreetRequest) (greeting.GreetReply, error) {
return greeting.GreetReply{
func (m GoodMorning) Greet(_ context.Context, request *greeting.GreetRequest) (*greeting.GreetReply, error) {
return &greeting.GreetReply{
Message: "Good morning, " + request.GetName(),
}, nil
}
6 changes: 3 additions & 3 deletions examples/host-functions/greeting/greet.pb.go

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

21 changes: 11 additions & 10 deletions examples/host-functions/greeting/greet_host.pb.go

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

16 changes: 9 additions & 7 deletions examples/host-functions/greeting/greet_plugin.pb.go

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

14 changes: 7 additions & 7 deletions examples/host-functions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func run() error {
}
defer greetingPlugin.Close(ctx)

reply, err := greetingPlugin.Greet(ctx, greeting.GreetRequest{
reply, err := greetingPlugin.Greet(ctx, &greeting.GreetRequest{
Name: "go-plugin",
})
if err != nil {
Expand All @@ -49,24 +49,24 @@ type myHostFunctions struct{}
var _ greeting.HostFunctions = (*myHostFunctions)(nil)

// HttpGet is embedded into the plugin and can be called by the plugin.
func (myHostFunctions) HttpGet(ctx context.Context, request greeting.HttpGetRequest) (greeting.HttpGetResponse, error) {
func (myHostFunctions) HttpGet(_ context.Context, request *greeting.HttpGetRequest) (*greeting.HttpGetResponse, error) {
resp, err := http.Get(request.Url)
if err != nil {
return greeting.HttpGetResponse{}, err
return nil, err
}
defer resp.Body.Close()

buf, err := io.ReadAll(resp.Body)
if err != nil {
return greeting.HttpGetResponse{}, err
return nil, err
}

return greeting.HttpGetResponse{Response: buf}, nil
return &greeting.HttpGetResponse{Response: buf}, nil
}

// Log is embedded into the plugin and can be called by the plugin.
func (myHostFunctions) Log(ctx context.Context, request greeting.LogRequest) (emptypb.Empty, error) {
func (myHostFunctions) Log(_ context.Context, request *greeting.LogRequest) (*emptypb.Empty, error) {
// Use the host logger
log.Println(request.GetMessage())
return emptypb.Empty{}, nil
return &emptypb.Empty{}, nil
}
10 changes: 5 additions & 5 deletions examples/host-functions/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ type GreetingPlugin struct{}

var _ greeting.Greeter = (*GreetingPlugin)(nil)

func (m GreetingPlugin) Greet(ctx context.Context, request greeting.GreetRequest) (greeting.GreetReply, error) {
func (m GreetingPlugin) Greet(ctx context.Context, request *greeting.GreetRequest) (*greeting.GreetReply, error) {
hostFunctions := greeting.NewHostFunctions()

// Logging via the host function
hostFunctions.Log(ctx, greeting.LogRequest{
hostFunctions.Log(ctx, &greeting.LogRequest{
Message: "Sending a HTTP request...",
})

// HTTP GET via the host function
resp, err := hostFunctions.HttpGet(ctx, greeting.HttpGetRequest{Url: "http://ifconfig.me"})
resp, err := hostFunctions.HttpGet(ctx, &greeting.HttpGetRequest{Url: "http://ifconfig.me"})
if err != nil {
return greeting.GreetReply{}, err
return nil, err
}

return greeting.GreetReply{
return &greeting.GreetReply{
Message: "Hello, " + request.GetName() + " from " + string(resp.Response),
}, nil
}
2 changes: 1 addition & 1 deletion examples/known-types/known/known.pb.go

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

17 changes: 9 additions & 8 deletions examples/known-types/known/known_host.pb.go

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

Loading

0 comments on commit d909512

Please sign in to comment.