Skip to content

Commit c75e57d

Browse files
authored
Codegen fixes (#3741)
* Fix importing user types * Fix handling error for wrapped http stream * Check for stream nil * Fix tests
1 parent 30316d2 commit c75e57d

29 files changed

+211
-112
lines changed

codegen/generator/service.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ func Service(genpkg string, roots []eval.Root) ([]*codegen.File, error) {
3131
}
3232
for _, f := range files {
3333
if len(f.SectionTemplates) > 0 {
34-
service.AddServiceDataMetaTypeImports(f.SectionTemplates[0], s, services.Get(s.Name))
34+
d := services.Get(s.Name)
35+
service.AddServiceDataMetaTypeImports(f.SectionTemplates[0], s, d)
36+
service.AddUserTypeImports(genpkg, f.SectionTemplates[0], d)
3537
}
3638
}
3739
f, err := service.ConvertFile(r, s, services)

codegen/generator/transport.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ func Transport(genpkg string, roots []eval.Root) ([]*codegen.File, error) {
4444
for _, f := range files {
4545
if len(f.SectionTemplates) > 0 {
4646
for _, s := range r.Services {
47-
service.AddServiceDataMetaTypeImports(f.SectionTemplates[0], s, services.Get(s.Name))
47+
d := services.Get(s.Name)
48+
service.AddServiceDataMetaTypeImports(f.SectionTemplates[0], s, d)
49+
service.AddUserTypeImports(genpkg, f.SectionTemplates[0], d)
4850
}
4951
}
5052
}

codegen/service/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func ClientFile(_ string, service *expr.ServiceExpr, services *ServicesData) *co
2626
{Path: "io"},
2727
codegen.GoaImport(""),
2828
}
29-
imports = append(imports, svc.UserTypeImports...)
3029
header := codegen.Header(service.Name+" client", svc.PkgName, imports)
3130
def := &codegen.SectionTemplate{
3231
Name: "client-struct",

codegen/service/endpoint.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func EndpointFile(genpkg string, service *expr.ServiceExpr, services *ServicesDa
8080
codegen.GoaImport("security"),
8181
{Path: genpkg + "/" + svcName + "/" + "views", Name: svc.ViewsPkg},
8282
}
83-
imports = append(imports, svc.UserTypeImports...)
8483
header := codegen.Header(service.Name+" endpoints", svc.PkgName, imports)
8584
def := &codegen.SectionTemplate{
8685
Name: "endpoints-struct",

codegen/service/interceptors.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func interceptorFile(svc *Data, server bool) *codegen.File {
7878
},
7979
}
8080
if len(interceptors) > 0 {
81-
codegen.AddImport(sections[0], svc.UserTypeImports...)
8281
sections = append(sections, &codegen.SectionTemplate{
8382
Name: "interceptor-types",
8483
Source: readTemplate("interceptors_types"),

codegen/service/service.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
// type definitions.
1616
func Files(genpkg string, service *expr.ServiceExpr, services *ServicesData, userTypePkgs map[string][]string) []*codegen.File {
1717
svc := services.Get(service.Name)
18-
svc.initUserTypeImports(genpkg)
1918
svcName := svc.PathName
2019
svcPath := filepath.Join(codegen.Gendir, svcName, "service.go")
2120
seen := make(map[string]struct{})
@@ -161,7 +160,6 @@ func Files(genpkg string, service *expr.ServiceExpr, services *ServicesData, use
161160
codegen.GoaImport("security"),
162161
codegen.NewImport(svc.ViewsPkg, genpkg+"/"+svcName+"/views"),
163162
}
164-
imports = append(imports, svc.UserTypeImports...)
165163
header := codegen.Header(service.Name+" service", svc.PkgName, imports)
166164
def := &codegen.SectionTemplate{
167165
Name: "service",
@@ -249,6 +247,38 @@ func AddServiceDataMetaTypeImports(header *codegen.SectionTemplate, svcExpr *exp
249247
}
250248
}
251249

250+
// AddUserTypeImports sets the import paths for the user types defined in the
251+
// service. User types may be declared in multiple packages when defined with
252+
// the Meta key "struct:pkg:path".
253+
func AddUserTypeImports(genpkg string, header *codegen.SectionTemplate, d *Data) {
254+
importsByPath := make(map[string]*codegen.ImportSpec)
255+
256+
initLoc := func(loc *codegen.Location) {
257+
if loc == nil {
258+
return
259+
}
260+
importsByPath[loc.FilePath] = &codegen.ImportSpec{Name: loc.PackageName(), Path: genpkg + "/" + loc.RelImportPath}
261+
}
262+
263+
for _, m := range d.Methods {
264+
initLoc(m.PayloadLoc)
265+
initLoc(m.ResultLoc)
266+
for _, l := range m.ErrorLocs {
267+
initLoc(l)
268+
}
269+
for _, ut := range d.userTypes {
270+
initLoc(ut.Loc)
271+
}
272+
for _, et := range d.errorTypes {
273+
initLoc(et.Loc)
274+
}
275+
}
276+
277+
for _, imp := range importsByPath { // Order does not matter, imports are sorted during formatting.
278+
codegen.AddImport(header, imp)
279+
}
280+
}
281+
252282
func errorName(et *UserTypeData) string {
253283
obj := expr.AsObject(et.Type)
254284
if obj != nil {

codegen/service/service_data.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ type (
7373
Scope *codegen.NameScope
7474
// ViewScope initialized with all the viewed types.
7575
ViewScope *codegen.NameScope
76-
// UserTypeImports lists the import specifications for the user
77-
// types used by the service.
78-
UserTypeImports []*codegen.ImportSpec
7976
// ProtoImports lists the import specifications for the custom
8077
// proto types used by the service.
8178
ProtoImports []*codegen.ImportSpec
@@ -599,42 +596,6 @@ func (d *Data) Method(name string) *MethodData {
599596
return nil
600597
}
601598

602-
// initUserTypeImports sets the import paths for the user types defined in the
603-
// service. User types may be declared in multiple packages when defined with
604-
// the Meta key "struct:pkg:path".
605-
func (d *Data) initUserTypeImports(genpkg string) {
606-
importsByPath := make(map[string]*codegen.ImportSpec)
607-
608-
initLoc := func(loc *codegen.Location) {
609-
if loc == nil {
610-
return
611-
}
612-
importsByPath[loc.FilePath] = &codegen.ImportSpec{Name: loc.PackageName(), Path: genpkg + "/" + loc.RelImportPath}
613-
}
614-
615-
for _, m := range d.Methods {
616-
initLoc(m.PayloadLoc)
617-
initLoc(m.ResultLoc)
618-
for _, l := range m.ErrorLocs {
619-
initLoc(l)
620-
}
621-
for _, ut := range d.userTypes {
622-
initLoc(ut.Loc)
623-
}
624-
for _, et := range d.errorTypes {
625-
initLoc(et.Loc)
626-
}
627-
}
628-
629-
imports := make([]*codegen.ImportSpec, len(importsByPath))
630-
i := 0
631-
for _, imp := range importsByPath { // Order does not matter, imports are sorted during formatting.
632-
imports[i] = imp
633-
i++
634-
}
635-
d.UserTypeImports = imports
636-
}
637-
638599
// Scheme returns the scheme data with the given scheme name.
639600
func (r RequirementsData) Scheme(name string) *SchemeData {
640601
for _, req := range r {

codegen/service/templates/client_interceptor_stream_wrappers.go.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
{{- if ne .SendTypeRef "" }}
44

5+
{{ comment (print "Unwrap returns the underlying stream type.") }}
6+
func (w *wrapped{{ .Interface }}) Unwrap() interface{} {
7+
return w.stream
8+
}
9+
510
{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor." .SendName .Interface) }}
611
func (w *wrapped{{ .Interface }}) {{ .SendName }}(v {{ .SendTypeRef }}) error {
712
return w.SendWithContext(w.ctx, v)

codegen/service/templates/server_interceptor_stream_wrapper_types.go.tpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
{{- if .WrappedServerStreams }}
2+
type Unwrap interface {
3+
Unwrap() interface{}
4+
}
5+
{{- end }}
6+
17
{{- range .WrappedServerStreams }}
28

39
{{ comment (printf "wrapped%s is a server interceptor wrapper for the %s stream." .Interface .Interface) }}

codegen/service/templates/server_interceptor_stream_wrappers.go.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{{- range .WrappedServerStreams }}
22

3+
{{ comment (print "Unwrap returns the underlying stream type.") }}
4+
func (w *wrapped{{ .Interface }}) Unwrap() interface{} {
5+
return w.stream
6+
}
7+
38
{{- if ne .SendTypeRef "" }}
49

510
{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor." .SendName .Interface) }}

0 commit comments

Comments
 (0)