Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion codegen/generator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func Service(genpkg string, roots []eval.Root) ([]*codegen.File, error) {
}
for _, f := range files {
if len(f.SectionTemplates) > 0 {
service.AddServiceDataMetaTypeImports(f.SectionTemplates[0], s, services.Get(s.Name))
d := services.Get(s.Name)
service.AddServiceDataMetaTypeImports(f.SectionTemplates[0], s, d)
service.AddUserTypeImports(genpkg, f.SectionTemplates[0], d)
}
}
f, err := service.ConvertFile(r, s, services)
Expand Down
4 changes: 3 additions & 1 deletion codegen/generator/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func Transport(genpkg string, roots []eval.Root) ([]*codegen.File, error) {
for _, f := range files {
if len(f.SectionTemplates) > 0 {
for _, s := range r.Services {
service.AddServiceDataMetaTypeImports(f.SectionTemplates[0], s, services.Get(s.Name))
d := services.Get(s.Name)
service.AddServiceDataMetaTypeImports(f.SectionTemplates[0], s, d)
service.AddUserTypeImports(genpkg, f.SectionTemplates[0], d)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion codegen/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func ClientFile(_ string, service *expr.ServiceExpr, services *ServicesData) *co
{Path: "io"},
codegen.GoaImport(""),
}
imports = append(imports, svc.UserTypeImports...)
header := codegen.Header(service.Name+" client", svc.PkgName, imports)
def := &codegen.SectionTemplate{
Name: "client-struct",
Expand Down
1 change: 0 additions & 1 deletion codegen/service/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func EndpointFile(genpkg string, service *expr.ServiceExpr, services *ServicesDa
codegen.GoaImport("security"),
{Path: genpkg + "/" + svcName + "/" + "views", Name: svc.ViewsPkg},
}
imports = append(imports, svc.UserTypeImports...)
header := codegen.Header(service.Name+" endpoints", svc.PkgName, imports)
def := &codegen.SectionTemplate{
Name: "endpoints-struct",
Expand Down
1 change: 0 additions & 1 deletion codegen/service/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func interceptorFile(svc *Data, server bool) *codegen.File {
},
}
if len(interceptors) > 0 {
codegen.AddImport(sections[0], svc.UserTypeImports...)
sections = append(sections, &codegen.SectionTemplate{
Name: "interceptor-types",
Source: readTemplate("interceptors_types"),
Expand Down
34 changes: 32 additions & 2 deletions codegen/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
// type definitions.
func Files(genpkg string, service *expr.ServiceExpr, services *ServicesData, userTypePkgs map[string][]string) []*codegen.File {
svc := services.Get(service.Name)
svc.initUserTypeImports(genpkg)
svcName := svc.PathName
svcPath := filepath.Join(codegen.Gendir, svcName, "service.go")
seen := make(map[string]struct{})
Expand Down Expand Up @@ -161,7 +160,6 @@ func Files(genpkg string, service *expr.ServiceExpr, services *ServicesData, use
codegen.GoaImport("security"),
codegen.NewImport(svc.ViewsPkg, genpkg+"/"+svcName+"/views"),
}
imports = append(imports, svc.UserTypeImports...)
header := codegen.Header(service.Name+" service", svc.PkgName, imports)
def := &codegen.SectionTemplate{
Name: "service",
Expand Down Expand Up @@ -249,6 +247,38 @@ func AddServiceDataMetaTypeImports(header *codegen.SectionTemplate, svcExpr *exp
}
}

// AddUserTypeImports sets the import paths for the user types defined in the
// service. User types may be declared in multiple packages when defined with
// the Meta key "struct:pkg:path".
func AddUserTypeImports(genpkg string, header *codegen.SectionTemplate, d *Data) {
importsByPath := make(map[string]*codegen.ImportSpec)

initLoc := func(loc *codegen.Location) {
if loc == nil {
return
}
importsByPath[loc.FilePath] = &codegen.ImportSpec{Name: loc.PackageName(), Path: genpkg + "/" + loc.RelImportPath}
}

for _, m := range d.Methods {
initLoc(m.PayloadLoc)
initLoc(m.ResultLoc)
for _, l := range m.ErrorLocs {
initLoc(l)
}
for _, ut := range d.userTypes {
initLoc(ut.Loc)
}
for _, et := range d.errorTypes {
initLoc(et.Loc)
}
}

for _, imp := range importsByPath { // Order does not matter, imports are sorted during formatting.
codegen.AddImport(header, imp)
}
}

func errorName(et *UserTypeData) string {
obj := expr.AsObject(et.Type)
if obj != nil {
Expand Down
39 changes: 0 additions & 39 deletions codegen/service/service_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ type (
Scope *codegen.NameScope
// ViewScope initialized with all the viewed types.
ViewScope *codegen.NameScope
// UserTypeImports lists the import specifications for the user
// types used by the service.
UserTypeImports []*codegen.ImportSpec
// ProtoImports lists the import specifications for the custom
// proto types used by the service.
ProtoImports []*codegen.ImportSpec
Expand Down Expand Up @@ -599,42 +596,6 @@ func (d *Data) Method(name string) *MethodData {
return nil
}

// initUserTypeImports sets the import paths for the user types defined in the
// service. User types may be declared in multiple packages when defined with
// the Meta key "struct:pkg:path".
func (d *Data) initUserTypeImports(genpkg string) {
importsByPath := make(map[string]*codegen.ImportSpec)

initLoc := func(loc *codegen.Location) {
if loc == nil {
return
}
importsByPath[loc.FilePath] = &codegen.ImportSpec{Name: loc.PackageName(), Path: genpkg + "/" + loc.RelImportPath}
}

for _, m := range d.Methods {
initLoc(m.PayloadLoc)
initLoc(m.ResultLoc)
for _, l := range m.ErrorLocs {
initLoc(l)
}
for _, ut := range d.userTypes {
initLoc(ut.Loc)
}
for _, et := range d.errorTypes {
initLoc(et.Loc)
}
}

imports := make([]*codegen.ImportSpec, len(importsByPath))
i := 0
for _, imp := range importsByPath { // Order does not matter, imports are sorted during formatting.
imports[i] = imp
i++
}
d.UserTypeImports = imports
}

// Scheme returns the scheme data with the given scheme name.
func (r RequirementsData) Scheme(name string) *SchemeData {
for _, req := range r {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

{{- if ne .SendTypeRef "" }}

{{ comment (print "Unwrap returns the underlying stream type.") }}
func (w *wrapped{{ .Interface }}) Unwrap() interface{} {
return w.stream
}

{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor." .SendName .Interface) }}
func (w *wrapped{{ .Interface }}) {{ .SendName }}(v {{ .SendTypeRef }}) error {
return w.SendWithContext(w.ctx, v)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{{- if .WrappedServerStreams }}
type Unwrap interface {
Unwrap() interface{}
}
{{- end }}

{{- range .WrappedServerStreams }}

{{ comment (printf "wrapped%s is a server interceptor wrapper for the %s stream." .Interface .Interface) }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{{- range .WrappedServerStreams }}

{{ comment (print "Unwrap returns the underlying stream type.") }}
func (w *wrapped{{ .Interface }}) Unwrap() interface{} {
return w.stream
}

{{- if ne .SendTypeRef "" }}

{{ comment (printf "%s streams instances of \"%s\" after executing the applied interceptor." .SendName .Interface) }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

type Unwrap interface {
Unwrap() interface{}
}

// wrappedMethodServerStream is a server interceptor wrapper for the
// MethodServerStream stream.
Expand Down Expand Up @@ -80,6 +83,11 @@ func wrapClientMethodLogging(endpoint goa.Endpoint, i ClientInterceptors) goa.En
}
}

// Unwrap returns the underlying stream type.
func (w *wrappedMethodServerStream) Unwrap() interface{} {
return w.stream
}

// Recv reads instances of "MethodServerStream" from the stream after executing
// the applied interceptor.
func (w *wrappedMethodServerStream) Recv() (*MethodStreamingPayload, error) {
Expand All @@ -100,6 +108,11 @@ func (w *wrappedMethodServerStream) Close() error {
return w.stream.Close()
}

// Unwrap returns the underlying stream type.
func (w *wrappedMethodClientStream) Unwrap() interface{} {
return w.stream
}

// Send streams instances of "MethodClientStream" after executing the applied
// interceptor.
func (w *wrappedMethodClientStream) Send(v *MethodStreamingPayload) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

type Unwrap interface {
Unwrap() interface{}
}

// wrappedMethodServerStream is a server interceptor wrapper for the
// MethodServerStream stream.
Expand Down Expand Up @@ -68,6 +71,11 @@ func wrapClientMethodLogging(endpoint goa.Endpoint, i ClientInterceptors) goa.En
}
}

// Unwrap returns the underlying stream type.
func (w *wrappedMethodServerStream) Unwrap() interface{} {
return w.stream
}

// Send streams instances of "MethodServerStream" after executing the applied
// interceptor.
func (w *wrappedMethodServerStream) Send(v *MethodResult) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

type Unwrap interface {
Unwrap() interface{}
}

// wrappedMethodServerStream is a server interceptor wrapper for the
// MethodServerStream stream.
Expand Down Expand Up @@ -95,6 +98,11 @@ func wrapClientMethodLogging(endpoint goa.Endpoint, i ClientInterceptors) goa.En
}
}

// Unwrap returns the underlying stream type.
func (w *wrappedMethodServerStream) Unwrap() interface{} {
return w.stream
}

// Send streams instances of "MethodServerStream" after executing the applied
// interceptor.
func (w *wrappedMethodServerStream) Send(v *MethodResult) error {
Expand Down Expand Up @@ -130,6 +138,11 @@ func (w *wrappedMethodServerStream) Close() error {
return w.stream.Close()
}

// Unwrap returns the underlying stream type.
func (w *wrappedMethodClientStream) Unwrap() interface{} {
return w.stream
}

// Send streams instances of "MethodClientStream" after executing the applied
// interceptor.
func (w *wrappedMethodClientStream) Send(v *MethodStreamingPayload) error {
Expand Down
2 changes: 0 additions & 2 deletions grpc/codegen/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func clientFile(genpkg string, svc *expr.GRPCServiceExpr, services *ServicesData
{Path: path.Join(genpkg, svcName, "views"), Name: data.Service.ViewsPkg},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName},
}
imports = append(imports, data.Service.UserTypeImports...)
sections = []*codegen.SectionTemplate{
codegen.Header(svc.Name()+" gRPC client", "client", imports),
}
Expand Down Expand Up @@ -135,7 +134,6 @@ func clientEncodeDecode(genpkg string, svc *expr.GRPCServiceExpr, services *Serv
{Path: path.Join(genpkg, svcName, "views"), Name: data.Service.ViewsPkg},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName},
}
imports = append(imports, data.Service.UserTypeImports...)
sections = []*codegen.SectionTemplate{codegen.Header(svc.Name()+" gRPC client encoders and decoders", "client", imports)}
fm := transTmplFuncs(svc, services)
fm["metadataEncodeDecodeData"] = metadataEncodeDecodeData
Expand Down
2 changes: 0 additions & 2 deletions grpc/codegen/client_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func endpointParser(genpkg string, services *ServicesData, svr *expr.ServerExpr,
specs = append(specs,
&codegen.ImportSpec{Path: path.Join(genpkg, "grpc", svcName, "client"), Name: sd.Service.PkgName + "c"},
&codegen.ImportSpec{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: svcName + pbPkgName})
specs = append(specs, sd.Service.UserTypeImports...)
// Add interceptors import if service has client interceptors
if len(sd.Service.ClientInterceptors) > 0 {
specs = append(specs, &codegen.ImportSpec{
Expand Down Expand Up @@ -118,7 +117,6 @@ func payloadBuilders(genpkg string, svc *expr.GRPCServiceExpr, data *cli.Command
{Path: path.Join(genpkg, svcName), Name: sd.Service.PkgName},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: sd.PkgName},
}
specs = append(specs, sd.Service.UserTypeImports...)
sections := []*codegen.SectionTemplate{
codegen.Header(title, "client", specs),
}
Expand Down
1 change: 0 additions & 1 deletion grpc/codegen/client_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func clientType(genpkg string, svc *expr.GRPCServiceExpr, services *ServicesData
{Path: path.Join(genpkg, svcName, "views"), Name: sd.Service.ViewsPkg},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: sd.PkgName},
}
imports = append(imports, sd.Service.UserTypeImports...)
imports = append(imports, sd.Service.ProtoImports...)
sections = []*codegen.SectionTemplate{codegen.Header(svc.Name()+" gRPC client types", "client", imports)}
for _, init := range initData {
Expand Down
2 changes: 0 additions & 2 deletions grpc/codegen/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func serverFile(genpkg string, svc *expr.GRPCServiceExpr, services *ServicesData
{Path: path.Join(genpkg, svcName, "views"), Name: data.Service.ViewsPkg},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName},
}
imports = append(imports, data.Service.UserTypeImports...)
sections = []*codegen.SectionTemplate{
codegen.Header(svc.Name()+" gRPC server", "server", imports),
{
Expand Down Expand Up @@ -142,7 +141,6 @@ func serverEncodeDecode(genpkg string, svc *expr.GRPCServiceExpr, services *Serv
{Path: path.Join(genpkg, svcName, "views"), Name: data.Service.ViewsPkg},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: data.PkgName},
}
imports = append(imports, data.Service.UserTypeImports...)
sections = []*codegen.SectionTemplate{codegen.Header(title, "server", imports)}

for _, e := range data.Endpoints {
Expand Down
1 change: 0 additions & 1 deletion grpc/codegen/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func serverType(genpkg string, svc *expr.GRPCServiceExpr, services *ServicesData
{Path: path.Join(genpkg, svcName, "views"), Name: sd.Service.ViewsPkg},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: sd.PkgName},
}
imports = append(imports, sd.Service.UserTypeImports...)
imports = append(imports, sd.Service.ProtoImports...)
sections = []*codegen.SectionTemplate{codegen.Header(svc.Name()+" gRPC server types", "server", imports)}
for _, init := range initData {
Expand Down
2 changes: 1 addition & 1 deletion grpc/codegen/service_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ type (
// NewServicesData creates a new ServicesData instance for the given service data.
func NewServicesData(services *service.ServicesData) *ServicesData {
return &ServicesData{
ServicesData: services,
ServicesData: services,
GRPCServices: make(map[string]*ServiceData),
}
}
Expand Down
1 change: 0 additions & 1 deletion http/codegen/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func clientEncodeDecodeFile(genpkg string, svc *expr.HTTPServiceExpr, services *
{Path: genpkg + "/" + svcName, Name: data.Service.PkgName},
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
}
imports = append(imports, data.Service.UserTypeImports...)
sections := []*codegen.SectionTemplate{codegen.Header(title, "client", imports)}

for _, e := range data.Endpoints {
Expand Down
1 change: 0 additions & 1 deletion http/codegen/client_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ func payloadBuilders(genpkg string, svc *expr.HTTPServiceExpr, data *cli.Command
codegen.GoaNamedImport("http", "goahttp"),
{Path: genpkg + "/" + sd.Service.PathName, Name: sd.Service.PkgName},
}
specs = append(specs, sd.Service.UserTypeImports...)
sections := []*codegen.SectionTemplate{
codegen.Header(title, "client", specs),
}
Expand Down
1 change: 0 additions & 1 deletion http/codegen/client_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func clientType(genpkg string, svc *expr.HTTPServiceExpr, seen map[string]struct
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
codegen.GoaImport(""),
}
imports = append(imports, data.Service.UserTypeImports...)
header := codegen.Header(svc.Name()+" HTTP client types", "client", imports)

var (
Expand Down
2 changes: 0 additions & 2 deletions http/codegen/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func serverFile(genpkg string, svc *expr.HTTPServiceExpr, services *ServicesData
{Path: genpkg + "/" + svcName, Name: data.Service.PkgName},
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
}
imports = append(imports, data.Service.UserTypeImports...)
sections := []*codegen.SectionTemplate{
codegen.Header(title, "server", imports),
}
Expand Down Expand Up @@ -140,7 +139,6 @@ func serverEncodeDecodeFile(genpkg string, svc *expr.HTTPServiceExpr, services *
{Path: genpkg + "/" + svcName, Name: data.Service.PkgName},
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
}
imports = append(imports, data.Service.UserTypeImports...)
sections := []*codegen.SectionTemplate{codegen.Header(title, "server", imports)}

for _, e := range data.Endpoints {
Expand Down
1 change: 0 additions & 1 deletion http/codegen/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func serverType(genpkg string, svc *expr.HTTPServiceExpr, _ map[string]struct{},
codegen.GoaImport(""),
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
}
imports = append(imports, data.Service.UserTypeImports...)
header := codegen.Header(svc.Name()+" HTTP server types", "server", imports)

var (
Expand Down
1 change: 0 additions & 1 deletion http/codegen/service_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var (
)
)


type (
// ServicesData encapsulates the data computed from the design.
ServicesData struct {
Expand Down
Loading