Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
onokonem committed Sep 30, 2024
1 parent 1bf70f9 commit 8aa8c52
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Please rise the issue in case of any difference found in these plugins output.
### Install

```
go install github.com/Djarvur/protoc-gen-python-grpc/cmd/protoc-gen-python-grpc@v0.1.3
go install github.com/Djarvur/protoc-gen-python-grpc/cmd/protoc-gen-python-grpc@v0.1.4
```

Make sure your [protoc](https://grpc.io/docs/protoc-installation/)/[buf](https://buf.build/docs/installation) compiler can see the `protoc-gen-python-grpc` in the path.
Expand Down
8 changes: 1 addition & 7 deletions cmd/protoc-gen-python-grpc/internal/flags/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ func Root() *cobra.Command {
}

func runRoot(suffix, templateSource string) {
err := protokit.RunPlugin(
&generator.Generator{
Suffix: suffix,
Template: templateSource,
},
)
if err != nil {
if err := protokit.RunPlugin(must(generator.New(suffix, templateSource))); err != nil {
panic(err)
}
}
34 changes: 26 additions & 8 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,30 @@ type Method struct {
// SupportedFeatures describes a flag setting for supported features.
const SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)

// Generator describes a protoc code generate plugin.
// It's an implementation of Generator from github.com/pseudomuto/protokit.
type Generator struct {
var _ protokit.Plugin = (*generator)(nil)

// generator describes a protoc code generate plugin.
// It's an implementation of generator from github.com/pseudomuto/protokit.
type generator struct {
Suffix string
Template string
Template *template.Template
}

func New(suffix, tmplSrc string) (*generator, error) {
tmpl, err := buildTemplate(tmplSrc)
if err != nil {
return nil, err
}

return &generator{
Suffix: suffix,
Template: tmpl,
}, nil
}

// Generate compiles the documentation and generates the CodeGeneratorResponse to send back to protoc. It does this
// by rendering a template based on the options parsed from the CodeGeneratorRequest.
func (p *Generator) Generate(r *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorResponse, error) {
func (p *generator) Generate(r *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorResponse, error) {
resp := new(pluginpb.CodeGeneratorResponse)

for _, fds := range protokit.ParseCodeGenRequest(r) {
Expand Down Expand Up @@ -75,7 +89,7 @@ func (p *Generator) Generate(r *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGe
return resp, nil
}

func executeTemplate(tmplSrc string, data interface{}) (string, error) {
func buildTemplate(tmplSrc string) (*template.Template, error) {
tmplFuncs := template.FuncMap{
"trimSuffix": strings.TrimSuffix,
"baseName": strings.BaseName,
Expand All @@ -86,12 +100,16 @@ func executeTemplate(tmplSrc string, data interface{}) (string, error) {

tmpl, err := template.New("").Funcs(tmplFuncs).Parse(tmplSrc)
if err != nil {
return "", fmt.Errorf("parsing template: %w", err)
return nil, fmt.Errorf("parsing template: %w", err)
}

return tmpl, nil
}

func executeTemplate(tmpl *template.Template, data interface{}) (string, error) {
buf := new(bytes.Buffer)

if err = tmpl.Execute(buf, data); err != nil {
if err := tmpl.Execute(buf, data); err != nil {
return "", fmt.Errorf("executing template: %w", err)
}

Expand Down

0 comments on commit 8aa8c52

Please sign in to comment.