Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

A new interface is added to the server generated file #26

Merged
merged 1 commit into from
Aug 5, 2024

Conversation

hdheid
Copy link
Contributor

@hdheid hdheid commented Aug 5, 2024

Add server interface implementation

proto --go_out=.arrist.proto --go_out=.arrist.proto to generate arrist.svr.go. But when I write all the function logic and use the command again, all my code is gone.

So I borrowed from grpc and implemented an extra interface, which we can implement outside the file arith.svr.go, so we can not only successfully call the corresponding method, but also don't have to worry about writing logic overwritten! The updated arist.svr.go file looks like this:

// Code generated by protoc-gen-tinyrpc. DO NOT EDIT.

package message

// ArithServiceServer You need to define a struct to implement these methods and then call them
type ArithServiceServer interface {
	Add(args *ArithRequest, reply *ArithResponse) error
	Sub(args *ArithRequest, reply *ArithResponse) error
	Mul(args *ArithRequest, reply *ArithResponse) error
	Div(args *ArithRequest, reply *ArithResponse) error
}

// ArithService Defining Computational Digital Services
type ArithService struct{}

// Add addition
func (this *ArithService) Add(args *ArithRequest, reply *ArithResponse) error {
	// define your service ...
	return nil
}

// Sub subtraction
func (this *ArithService) Sub(args *ArithRequest, reply *ArithResponse) error {
	// define your service ...
	return nil
}

// Mul multiplication
func (this *ArithService) Mul(args *ArithRequest, reply *ArithResponse) error {
	// define your service ...
	return nil
}

// Div division
func (this *ArithService) Div(args *ArithRequest, reply *ArithResponse) error {
	// define your service ...
	return nil
}

The code section only changes the code in tinerpc/protoc-gen-tinyrpc/main.go. Add the following code to the Generate method:

// modified part
t.P("// Code generated by protoc-gen-tinyrpc. DO NOT EDIT.")

// new additions
comment := fmt.Sprintf("// %sServer You need to define a struct to implement these methods and then call them\n", s.Desc.Name())
serviceInterface := fmt.Sprintf("%stype %sServer interface{",
    comment, s.Desc.Name())
t.P(serviceInterface)
for _, m := range s.Methods {
    funcCode := fmt.Sprintf("%s(args *%s, reply *%s) error",
        m.Desc.Name(), m.Input.Desc.Name(), m.Output.Desc.Name())
    t.P(funcCode)
}
t.P("}")

I have tested go fmt and go vet locally.And generated corresponding files to implement the interface. There is no problem after testing.

@hyman-m hyman-m merged commit 1bbba39 into hyman-m:main Aug 5, 2024
2 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants