Skip to content

Commit

Permalink
feat: generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Raezil committed Nov 5, 2024
1 parent 0cc29d3 commit 06e15ae
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module main

go 1.22.2

require google.golang.org/protobuf v1.35.1 // indirect
2 changes: 2 additions & 0 deletions cmd/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
80 changes: 80 additions & 0 deletions cmd/protoc-gen-rpc-impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"fmt"

"google.golang.org/protobuf/compiler/protogen"
)

func main() {
protogen.Options{}.Run(func(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if !f.Generate {
continue
}
generateFile(gen, f)
}
return nil
})
}

func generateFile(gen *protogen.Plugin, file *protogen.File) {
if len(file.Services) == 0 {
return
}

filename := file.GeneratedFilenamePrefix + "_server.go"
g := gen.NewGeneratedFile(filename, file.GoImportPath)

g.P("// Code generated by protoc-gen-rpc-impl. DO NOT EDIT.")
g.P()
g.P("package ", file.GoPackageName)
g.P()

// Write imports
g.P("import (")
g.P(` "context"`)
g.P(")")
g.P()

// Generate implementation struct and methods for each service
for _, service := range file.Services {
generateServiceImplementation(g, service)
}
}

func generateServiceImplementation(g *protogen.GeneratedFile, service *protogen.Service) {
structName := service.GoName + "ServiceServer"

// Generate struct
g.P("type ", structName, " struct {")
g.P(" Unimplemented", service.GoName, "Server")
g.P("}")
g.P()

// Constructor
g.P("func New", structName, "() *", structName, " {")
g.P(" return &", structName, "{}")
g.P("}")
g.P()

// Generate methods
for _, method := range service.Methods {
generateMethodImplementation(g, method, structName)
}
}

func generateMethodImplementation(g *protogen.GeneratedFile, method *protogen.Method, structName string) {
methodName := method.GoName
inputType := method.Input.GoIdent.GoName
outputType := method.Output.GoIdent.GoName

signature := fmt.Sprintf("func (s *%s) %s(ctx context.Context, req *%s) (*%s, error)",
structName, methodName, inputType, outputType)

g.P(signature, " {")
g.P(" // TODO: Implement ", methodName)
g.P(" return &", outputType, "{}, nil")
g.P("}")
g.P()
}
1 change: 1 addition & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func main() {
"--go-grpc_opt=paths=source_relative",
"--grpc-gateway_out=./backend",
"--grpc-gateway_opt=paths=source_relative",
"--rpc-impl_out=.",
proto,
); err != nil {
log.Fatalf("Error executing protoc command: %v", err)
Expand Down

0 comments on commit 06e15ae

Please sign in to comment.