Open jvillafanez opened 2 years ago
Imported definitions aren't handled properly neither.
Comparing with both the generated go and go-micro code, there is a missing import statement for the imported definitions, and the usage also needs some touches.
(after commenting the unused imports)
..../github.com/jvillafanez/prototest001/gen@v0.0.0-20211202141858-aff6436eb7dd/ocis/services/accounts/v1/accounts.pb.web.go:55:11: undefined: Account
..../github.com/jvillafanez/prototest001/gen@v0.0.0-20211202141858-aff6436eb7dd/ocis/services/accounts/v1/accounts.pb.web.go:79:11: undefined: Account
..../github.com/jvillafanez/prototest001/gen@v0.0.0-20211202141858-aff6436eb7dd/ocis/services/accounts/v1/accounts.pb.web.go:103:11: undefined: Account
..../github.com/jvillafanez/prototest001/gen@v0.0.0-20211202141858-aff6436eb7dd/ocis/services/accounts/v1/accounts.pb.web.go:196:11: undefined: Group
..../github.com/jvillafanez/prototest001/gen@v0.0.0-20211202141858-aff6436eb7dd/ocis/services/accounts/v1/accounts.pb.web.go:220:11: undefined: Group
..../github.com/jvillafanez/prototest001/gen@v0.0.0-20211202141858-aff6436eb7dd/ocis/services/accounts/v1/accounts.pb.web.go:244:11: undefined: Group
..../github.com/jvillafanez/prototest001/gen@v0.0.0-20211202141858-aff6436eb7dd/ocis/services/accounts/v1/accounts.pb.web.go:291:11: undefined: Group
..../github.com/jvillafanez/prototest001/gen@v0.0.0-20211202141858-aff6436eb7dd/ocis/services/accounts/v1/accounts.pb.web.go:315:11: undefined: Group
Unfinished patch:
diff --git a/microweb.go b/microweb.go
index fc0e2ab..374b628 100644
--- a/microweb.go
+++ b/microweb.go
@@ -4,8 +4,8 @@ import (
"text/template"
"github.com/golang/protobuf/proto"
- "github.com/lyft/protoc-gen-star"
- "github.com/lyft/protoc-gen-star/lang/go"
+ pgs "github.com/lyft/protoc-gen-star"
+ pgsgo "github.com/lyft/protoc-gen-star/lang/go"
"google.golang.org/genproto/googleapis/api/annotations"
)
@@ -35,6 +35,7 @@ func (p *MicroWebModule) InitContext(ctx pgs.BuildContext) {
tpl := template.New("microweb").Funcs(map[string]interface{}{
"package": p.ctx.PackageName,
"name": p.ctx.Name,
+ "importPath": p.ctx.ImportPath,
"marshaler": p.marshaler,
"unmarshaler": p.unmarshaler,
"handlerPrefix": p.handlerPrefix,
@@ -140,6 +141,13 @@ package {{ package . }}
{{ range .Methods }}
{{ if or (eq .Input.Name "Empty") (eq .Output.Name "Empty") -}}
{{ $imports = extraImports "github.com/golang/protobuf/ptypes/empty" }}
+ {{ else }}
+ {{ if ne (importPath .Input) (importPath .File) }}
+ {{ $imports = extraImports (importPath .Input) }}
+ {{ end }}
+ {{ if ne (importPath .Output) (importPath .File) }}
+ {{ $imports = extraImports (importPath .Output) }}
+ {{ end }}
{{- end }}
{{ end }}
{{ end }}
@@ -147,11 +155,15 @@ package {{ package . }}
import (
"bytes"
"encoding/json"
+ {{ if gt (len .Services) 0 -}}
"net/http"
+ {{- end }}
"github.com/golang/protobuf/jsonpb"
+ {{ if gt (len .Services) 0 -}}
"github.com/go-chi/render"
"github.com/go-chi/chi/v5"
+ {{- end }}
{{ range $imports }}
"{{ . }}"
{{- end }}
That would cover the "message-only" proto files, and the import paths. The generated file doesn't use the imported paths yet.
Steps to reproduce
Current result
The generated code includes imports "net/http", "github.com/go-chi/chi/v5" and "github.com/go-chi/render" which aren't used.
Example generated code: https://github.com/jvillafanez/prototest001/blob/abf38bb9e4aa6da50ff0c97d59a26ba9905cffd6/gen/ocis/messages/v1/config.pb.web.go
Trying to use / compile the generated code fails.