@@ -15,7 +15,6 @@ import (
1515// type definitions.
1616func 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+
252282func errorName (et * UserTypeData ) string {
253283 obj := expr .AsObject (et .Type )
254284 if obj != nil {
0 commit comments