-
-
Notifications
You must be signed in to change notification settings - Fork 561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial interceptors implementation #3616
Open
raphael
wants to merge
6
commits into
v3
Choose a base branch
from
interceptors
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0955550
Initial interceptors implementation
raphael 02a856d
Remove Twitter badge
raphael 879a0fe
Improve generated comments
raphael a3843cb
Merge branch 'v3' into interceptors
raphael 08d126a
Add support for interceptors in generated example code
raphael 74c7ccd
Update dependencies
raphael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package service | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"goa.design/goa/v3/codegen" | ||
"goa.design/goa/v3/expr" | ||
) | ||
|
||
type ( | ||
// ServiceInterceptorData contains all data needed for generating interceptor code | ||
ServiceInterceptorData struct { | ||
Service string | ||
PkgName string | ||
Methods []*MethodInterceptorData | ||
ServerInterceptors []*InterceptorData | ||
ClientInterceptors []*InterceptorData | ||
AllInterceptors []*InterceptorData | ||
HasPrivateImplementationTypes bool | ||
} | ||
|
||
// MethodInterceptorData contains interceptor data for a single method | ||
MethodInterceptorData struct { | ||
Service string | ||
Method string | ||
MethodVarName string | ||
PayloadRef string | ||
ResultRef string | ||
ServerInterceptors []*InterceptorData | ||
ClientInterceptors []*InterceptorData | ||
} | ||
|
||
// InterceptorData describes a single interceptor. | ||
InterceptorData struct { | ||
Name string | ||
DesignName string | ||
UnexportedName string | ||
Description string | ||
PayloadRef string | ||
ResultRef string | ||
ReadPayload []*AttributeData | ||
WritePayload []*AttributeData | ||
ReadResult []*AttributeData | ||
WriteResult []*AttributeData | ||
ServerStreamInputStruct string | ||
ClientStreamInputStruct string | ||
} | ||
|
||
// AttributeData describes a single attribute. | ||
AttributeData struct { | ||
Name string | ||
TypeRef string | ||
FieldPointer bool | ||
} | ||
) | ||
|
||
// InterceptorsFile returns the interceptors file for the given service. | ||
func InterceptorsFile(genpkg string, service *expr.ServiceExpr) *codegen.File { | ||
svc := Services.Get(service.Name) | ||
data := interceptorsData(service) | ||
if len(data.ServerInterceptors) == 0 && len(data.ClientInterceptors) == 0 { | ||
return nil | ||
} | ||
|
||
path := filepath.Join(codegen.Gendir, svc.PathName, "interceptors.go") | ||
sections := []*codegen.SectionTemplate{ | ||
codegen.Header(service.Name+" interceptors", svc.PkgName, []*codegen.ImportSpec{ | ||
{Path: "context"}, | ||
codegen.GoaImport(""), | ||
}), | ||
{ | ||
Name: "interceptors", | ||
Source: readTemplate("interceptors"), | ||
Data: data, | ||
}, | ||
} | ||
|
||
return &codegen.File{Path: path, SectionTemplates: sections} | ||
} | ||
|
||
func interceptorsData(service *expr.ServiceExpr) *ServiceInterceptorData { | ||
svc := Services.Get(service.Name) | ||
scope := svc.Scope | ||
|
||
// Build method data first | ||
methods := make([]*MethodInterceptorData, 0, len(service.Methods)) | ||
seenInts := make(map[string]*InterceptorData) | ||
var serviceServerInts, serviceClientInts, allInts []*InterceptorData | ||
var hasTypes bool | ||
|
||
for _, m := range service.Methods { | ||
methodServerInts, methodClientInts := buildMethodInterceptors(m, scope) | ||
if len(methodServerInts) == 0 && len(methodClientInts) == 0 { | ||
continue | ||
} | ||
hasTypes = hasTypes || hasPrivateImplementationTypes(methodServerInts) || hasPrivateImplementationTypes(methodClientInts) | ||
|
||
// Add method data | ||
methods = append(methods, &MethodInterceptorData{ | ||
Service: svc.Name, | ||
Method: m.Name, | ||
MethodVarName: codegen.Goify(m.Name, true), | ||
PayloadRef: scope.GoFullTypeRef(m.Payload, ""), | ||
ResultRef: scope.GoFullTypeRef(m.Result, ""), | ||
ServerInterceptors: methodServerInts, | ||
ClientInterceptors: methodClientInts, | ||
}) | ||
|
||
// Collect unique interceptors | ||
for _, i := range methodServerInts { | ||
if _, ok := seenInts[i.Name]; !ok { | ||
seenInts[i.Name] = i | ||
serviceServerInts = append(serviceServerInts, i) | ||
allInts = append(allInts, i) | ||
} | ||
} | ||
for _, i := range methodClientInts { | ||
if _, ok := seenInts[i.Name]; !ok { | ||
seenInts[i.Name] = i | ||
serviceClientInts = append(serviceClientInts, i) | ||
allInts = append(allInts, i) | ||
} | ||
} | ||
} | ||
|
||
return &ServiceInterceptorData{ | ||
Service: service.Name, | ||
PkgName: svc.PkgName, | ||
Methods: methods, | ||
ServerInterceptors: serviceServerInts, | ||
ClientInterceptors: serviceClientInts, | ||
AllInterceptors: allInts, | ||
HasPrivateImplementationTypes: hasTypes, | ||
} | ||
} | ||
|
||
func buildMethodInterceptors(m *expr.MethodExpr, scope *codegen.NameScope) ([]*InterceptorData, []*InterceptorData) { | ||
svc := Services.Get(m.Service.Name) | ||
methodData := svc.Method(m.Name) | ||
var serverEndpointStruct, clientEndpointStruct string | ||
if methodData.ServerStream != nil { | ||
serverEndpointStruct = methodData.ServerStream.EndpointStruct | ||
} | ||
if methodData.ClientStream != nil { | ||
clientEndpointStruct = methodData.ClientStream.EndpointStruct | ||
} | ||
var hasPrivateImplementationTypes bool | ||
buildInterceptor := func(intr *expr.InterceptorExpr) *InterceptorData { | ||
hasPrivateImplementationTypes = hasPrivateImplementationTypes || | ||
intr.ReadPayload != nil || intr.WritePayload != nil || intr.ReadResult != nil || intr.WriteResult != nil | ||
|
||
return &InterceptorData{ | ||
Name: codegen.Goify(intr.Name, true), | ||
DesignName: intr.Name, | ||
UnexportedName: codegen.Goify(intr.Name, false), | ||
Description: intr.Description, | ||
PayloadRef: methodData.PayloadRef, | ||
ResultRef: methodData.ResultRef, | ||
ServerStreamInputStruct: serverEndpointStruct, | ||
ClientStreamInputStruct: clientEndpointStruct, | ||
ReadPayload: collectAttributes(intr.ReadPayload, m.Payload, scope), | ||
WritePayload: collectAttributes(intr.WritePayload, m.Payload, scope), | ||
ReadResult: collectAttributes(intr.ReadResult, m.Result, scope), | ||
WriteResult: collectAttributes(intr.WriteResult, m.Result, scope), | ||
} | ||
} | ||
|
||
serverInts := make([]*InterceptorData, len(m.ServerInterceptors)) | ||
for i, intr := range m.ServerInterceptors { | ||
serverInts[i] = buildInterceptor(intr) | ||
} | ||
|
||
clientInts := make([]*InterceptorData, len(m.ClientInterceptors)) | ||
for i, intr := range m.ClientInterceptors { | ||
clientInts[i] = buildInterceptor(intr) | ||
} | ||
|
||
return serverInts, clientInts | ||
} | ||
|
||
// hasPrivateImplementationTypes returns true if any of the interceptors have | ||
// private implementation types. | ||
func hasPrivateImplementationTypes(interceptors []*InterceptorData) bool { | ||
for _, intr := range interceptors { | ||
if intr.ReadPayload != nil || intr.WritePayload != nil || intr.ReadResult != nil || intr.WriteResult != nil { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// collectAttributes builds AttributeData from an AttributeExpr | ||
func collectAttributes(attrNames, parent *expr.AttributeExpr, scope *codegen.NameScope) []*AttributeData { | ||
if attrNames == nil { | ||
return nil | ||
} | ||
|
||
obj := expr.AsObject(attrNames.Type) | ||
if obj == nil { | ||
return nil | ||
} | ||
|
||
data := make([]*AttributeData, len(*obj)) | ||
for i, nat := range *obj { | ||
parentAttr := parent.Find(nat.Name) | ||
if parentAttr == nil { | ||
continue | ||
} | ||
|
||
data[i] = &AttributeData{ | ||
Name: codegen.Goify(nat.Name, true), | ||
TypeRef: scope.GoTypeRef(parentAttr), | ||
FieldPointer: parent.IsPrimitivePointer(nat.Name, true), | ||
} | ||
} | ||
return data | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also generate example interceptor implementation like example service implementation?