Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael committed Jan 19, 2025
1 parent 1c5f19c commit b9cab9e
Show file tree
Hide file tree
Showing 41 changed files with 339 additions and 264 deletions.
89 changes: 47 additions & 42 deletions codegen/service/service_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ type (
// MethodInterceptorData contains the data required to render the
// method-level interceptor code.
MethodInterceptorData struct {
// Name is the name of the interceptor.
Name string
// MethodName is the name of the method.
MethodName string
// PayloadAccess is the name of the payload access struct.
Expand Down Expand Up @@ -1203,47 +1201,48 @@ func buildInterceptorData(svc *expr.ServiceExpr, methods []*MethodData, i *expr.
DesignName: i.Name,
Description: i.Description,
}
if len(svc.Methods) > 0 {
payload, result := svc.Methods[0].Payload, svc.Methods[0].Result
data.ReadPayload = collectAttributes(i.ReadPayload, payload, scope)
data.WritePayload = collectAttributes(i.WritePayload, payload, scope)
data.ReadResult = collectAttributes(i.ReadResult, result, scope)
data.WriteResult = collectAttributes(i.WriteResult, result, scope)
if len(data.ReadPayload) > 0 || len(data.WritePayload) > 0 {
data.HasPayloadAccess = true
if len(svc.Methods) == 0 {
return data
}
payload, result := svc.Methods[0].Payload, svc.Methods[0].Result
data.ReadPayload = collectAttributes(i.ReadPayload, payload, scope)
data.WritePayload = collectAttributes(i.WritePayload, payload, scope)
data.ReadResult = collectAttributes(i.ReadResult, result, scope)
data.WriteResult = collectAttributes(i.WriteResult, result, scope)
if len(data.ReadPayload) > 0 || len(data.WritePayload) > 0 {
data.HasPayloadAccess = true
}
if len(data.ReadResult) > 0 || len(data.WriteResult) > 0 {
data.HasResultAccess = true
}
for _, m := range svc.Methods {
applies := false
intExprs := m.ServerInterceptors
if !server {
intExprs = m.ClientInterceptors
}
for _, in := range intExprs {
if in.Name == i.Name {
applies = true
break
}
}
if len(data.ReadResult) > 0 || len(data.WriteResult) > 0 {
data.HasResultAccess = true
if !applies {
continue
}
for _, m := range svc.Methods {
applies := false
intExprs := m.ServerInterceptors
if !server {
intExprs = m.ClientInterceptors
}
for _, in := range intExprs {
if in.Name == i.Name {
applies = true
break
}
}
if !applies {
continue
}
var md *MethodData
for _, mt := range methods {
if m.Name == mt.Name {
md = mt
break
}
}
data.Methods = append(data.Methods, buildInterceptorMethodData(i, md))
if server {
md.ServerInterceptors = append(md.ServerInterceptors, i.Name)
} else {
md.ClientInterceptors = append(md.ClientInterceptors, i.Name)
var md *MethodData
for _, mt := range methods {
if m.Name == mt.Name {
md = mt
break
}
}
data.Methods = append(data.Methods, buildInterceptorMethodData(i, md))
if server {
md.ServerInterceptors = append(md.ServerInterceptors, i.Name)
} else {
md.ClientInterceptors = append(md.ClientInterceptors, i.Name)
}
}
return data
}
Expand All @@ -1258,11 +1257,17 @@ func buildInterceptorMethodData(i *expr.InterceptorExpr, md *MethodData) *Method
if md.ClientStream != nil {
clientStream = md.ClientStream.VarName
}
var payloadAccess, resultAccess string
if i.ReadPayload != nil || i.WritePayload != nil {
payloadAccess = codegen.Goify(i.Name, false) + md.VarName + "Payload"
}
if i.ReadResult != nil || i.WriteResult != nil {
resultAccess = codegen.Goify(i.Name, false) + md.VarName + "Result"
}
return &MethodInterceptorData{
Name: i.Name,
MethodName: md.VarName,
PayloadAccess: codegen.Goify(i.Name, false) + md.VarName + "Payload",
ResultAccess: codegen.Goify(i.Name, false) + md.VarName + "Result",
PayloadAccess: payloadAccess,
ResultAccess: resultAccess,
PayloadRef: md.PayloadRef,
ResultRef: md.ResultRef,
ClientStreamInputStruct: clientStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func wrapClient{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i
RawPayload: req,
{{- end }}
}
return i.{{ .Name }}(ctx, info, endpoint)
return i.{{ $interceptor.Name }}(ctx, info, endpoint)
}
}
{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion codegen/service/templates/interceptors_types.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ type (
{{- end }}
{{- end }}
)
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func wrap{{ .MethodName }}{{ $interceptor.Name }}(endpoint goa.Endpoint, i Serve
RawPayload: req,
{{- end }}
}
return i.{{ .Name }}(ctx, info, endpoint)
return i.{{ $interceptor.Name }}(ctx, info, endpoint)
}
}
{{- end }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ type ChainedInterceptorServiceClientInterceptors struct {
func NewChainedInterceptorServiceClientInterceptors() *ChainedInterceptorServiceClientInterceptors {
return &ChainedInterceptorServiceClientInterceptors{}
}
func (i *ChainedInterceptorServiceClientInterceptors) API(ctx context.Context, info *interceptors.APIInfo, next goa.Endpoint) (any, error) {
log.Printf(ctx, "[API] Sending request: %v", info.RawPayload)
resp, err := next(ctx, info.RawPayload)
if err != nil {
log.Printf(ctx, "[API] Error: %v", err)
return nil, err
}
log.Printf(ctx, "[API] Received response: %v", resp)
return resp, nil
}
func (i *ChainedInterceptorServiceClientInterceptors) Method(ctx context.Context, info *interceptors.MethodInfo, next goa.Endpoint) (any, error) {
log.Printf(ctx, "[Method] Sending request: %v", info.RawPayload)
resp, err := next(ctx, info.RawPayload)
Expand All @@ -43,13 +53,3 @@ func (i *ChainedInterceptorServiceClientInterceptors) Service(ctx context.Contex
log.Printf(ctx, "[Service] Received response: %v", resp)
return resp, nil
}
func (i *ChainedInterceptorServiceClientInterceptors) API(ctx context.Context, info *interceptors.APIInfo, next goa.Endpoint) (any, error) {
log.Printf(ctx, "[API] Sending request: %v", info.RawPayload)
resp, err := next(ctx, info.RawPayload)
if err != nil {
log.Printf(ctx, "[API] Error: %v", err)
return nil, err
}
log.Printf(ctx, "[API] Received response: %v", resp)
return resp, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ type ChainedInterceptorServiceServerInterceptors struct {
func NewChainedInterceptorServiceServerInterceptors() *ChainedInterceptorServiceServerInterceptors {
return &ChainedInterceptorServiceServerInterceptors{}
}
func (i *ChainedInterceptorServiceServerInterceptors) API(ctx context.Context, info *interceptors.APIInfo, next goa.Endpoint) (any, error) {
log.Printf(ctx, "[API] Processing request: %v", info.RawPayload)
resp, err := next(ctx, info.RawPayload)
if err != nil {
log.Printf(ctx, "[API] Error: %v", err)
return nil, err
}
log.Printf(ctx, "[API] Response: %v", resp)
return resp, nil
}
func (i *ChainedInterceptorServiceServerInterceptors) Method(ctx context.Context, info *interceptors.MethodInfo, next goa.Endpoint) (any, error) {
log.Printf(ctx, "[Method] Processing request: %v", info.RawPayload)
resp, err := next(ctx, info.RawPayload)
Expand All @@ -43,13 +53,3 @@ func (i *ChainedInterceptorServiceServerInterceptors) Service(ctx context.Contex
log.Printf(ctx, "[Service] Response: %v", resp)
return resp, nil
}
func (i *ChainedInterceptorServiceServerInterceptors) API(ctx context.Context, info *interceptors.APIInfo, next goa.Endpoint) (any, error) {
log.Printf(ctx, "[API] Processing request: %v", info.RawPayload)
resp, err := next(ctx, info.RawPayload)
if err != nil {
log.Printf(ctx, "[API] Error: %v", err)
return nil, err
}
log.Printf(ctx, "[API] Response: %v", resp)
return resp, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ type ClientInterceptors interface {
// WrapMethodClientEndpoint wraps the Method endpoint with the client
// interceptors defined in the design.
func WrapMethodClientEndpoint(endpoint goa.Endpoint, i ClientInterceptors) goa.Endpoint {
endpoint = wrapClientValidation(endpoint, i, "Method")
endpoint = wrapClientMethodvalidation(endpoint, i)
return endpoint
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@

// wrapValidation applies the validation interceptor to endpoints.
func wrapValidation(endpoint goa.Endpoint, i ServerInterceptors, method string) goa.Endpoint {

// wrapValidationMethod applies the validation server interceptor to endpoints.
func wrapMethodValidation(endpoint goa.Endpoint, i ServerInterceptors) goa.Endpoint {
return func(ctx context.Context, req any) (any, error) {
info := &ValidationInfo{
Service: "InterceptorWithReadPayload",
Method: method,
Method: "Method",
Endpoint: endpoint,
RawPayload: req,
}
return i.Validation(ctx, info, endpoint)
}
}

// wrapClientValidation applies the validation interceptor to endpoints.
func wrapClientValidation(endpoint goa.Endpoint, i ClientInterceptors, method string) goa.Endpoint {
// wrapClientValidationMethod applies the validation client interceptor to
// endpoints.
func wrapClientMethodValidation(endpoint goa.Endpoint, i ClientInterceptors) goa.Endpoint {
return func(ctx context.Context, req any) (any, error) {
info := &ValidationInfo{
Service: "InterceptorWithReadPayload",
Method: method,
Method: "Method",
Endpoint: endpoint,
RawPayload: req,
}
return i.Validation(ctx, info, endpoint)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,37 @@ type (
// It includes service name, method name, and access to the endpoint.
ValidationInfo goa.InterceptorInfo

// ValidationPayloadAccess provides type-safe access to the method payload.
// ValidationPayload provides type-safe access to the method payload.
// It allows reading and writing specific fields of the payload as defined
// in the design.
ValidationPayloadAccess interface {
ValidationPayload interface {
Name() string
}
)

// Private implementation types
type (
validationPayloadAccess struct {
validationMethodPayload struct {
payload *MethodPayload
}
)

// WrapMethodEndpoint wraps the Method endpoint with the server-side
// interceptors defined in the design.
func WrapMethodEndpoint(endpoint goa.Endpoint, i ServerInterceptors) goa.Endpoint {
endpoint = wrapMethodvalidation(endpoint, i)
return endpoint
}

// Public accessor methods for Info types

// Payload returns a type-safe accessor for the method payload.
func (info *ValidationInfo) Payload() ValidationPayloadAccess {
return &validationPayloadAccess{payload: info.RawPayload.(*MethodPayload)}
func (info *ValidationInfo) Payload() ValidationPayload {
return &validationMethodPayload{payload: info.RawPayload.(*MethodPayload)}
}

// Private implementation methods
func (p *validationPayloadAccess) Name() string {
return p.payload.Name
}

// WrapMethodEndpoint wraps the Method endpoint with the server-side
// interceptors defined in the design.
func WrapMethodEndpoint(endpoint goa.Endpoint, i ServerInterceptors) goa.Endpoint {
endpoint = wrapValidation(endpoint, i, "Method")
return endpoint
func (p *validationMethodPayload) Name() string {
return p.payload.Name
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ type ClientInterceptors interface {
// WrapMethodClientEndpoint wraps the Method endpoint with the client
// interceptors defined in the design.
func WrapMethodClientEndpoint(endpoint goa.Endpoint, i ClientInterceptors) goa.Endpoint {
endpoint = wrapClientCaching(endpoint, i, "Method")
endpoint = wrapClientMethodcaching(endpoint, i)
return endpoint
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@

// wrapCaching applies the caching interceptor to endpoints.
func wrapCaching(endpoint goa.Endpoint, i ServerInterceptors, method string) goa.Endpoint {

// wrapCachingMethod applies the caching server interceptor to endpoints.
func wrapMethodCaching(endpoint goa.Endpoint, i ServerInterceptors) goa.Endpoint {
return func(ctx context.Context, req any) (any, error) {
info := &CachingInfo{
Service: "InterceptorWithReadResult",
Method: method,
Method: "Method",
Endpoint: endpoint,
RawPayload: req,
}
return i.Caching(ctx, info, endpoint)
}
}

// wrapClientCaching applies the caching interceptor to endpoints.
func wrapClientCaching(endpoint goa.Endpoint, i ClientInterceptors, method string) goa.Endpoint {
// wrapClientCachingMethod applies the caching client interceptor to endpoints.
func wrapClientMethodCaching(endpoint goa.Endpoint, i ClientInterceptors) goa.Endpoint {
return func(ctx context.Context, req any) (any, error) {
info := &CachingInfo{
Service: "InterceptorWithReadResult",
Method: method,
Method: "Method",
Endpoint: endpoint,
RawPayload: req,
}
return i.Caching(ctx, info, endpoint)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,36 @@ type (
// It includes service name, method name, and access to the endpoint.
CachingInfo goa.InterceptorInfo

// CachingResultAccess provides type-safe access to the method result.
// CachingResult provides type-safe access to the method result.
// It allows reading and writing specific fields of the result as defined
// in the design.
CachingResultAccess interface {
CachingResult interface {
Data() string
}
)

// Private implementation types
type (
cachingResultAccess struct {
cachingMethodResult struct {
result *MethodResult
}
)

// WrapMethodEndpoint wraps the Method endpoint with the server-side
// interceptors defined in the design.
func WrapMethodEndpoint(endpoint goa.Endpoint, i ServerInterceptors) goa.Endpoint {
endpoint = wrapMethodcaching(endpoint, i)
return endpoint
}

// Public accessor methods for Info types
// Result returns a type-safe accessor for the method result.
func (info *CachingInfo) Result(res any) CachingResultAccess {
return &cachingResultAccess{result: res.(*MethodResult)}
func (info *CachingInfo) Result(res any) CachingResult {
return &cachingMethodResult{result: res.(*MethodResult)}
}

// Private implementation methods
func (r *cachingResultAccess) Data() string {
return r.result.Data
}

// WrapMethodEndpoint wraps the Method endpoint with the server-side
// interceptors defined in the design.
func WrapMethodEndpoint(endpoint goa.Endpoint, i ServerInterceptors) goa.Endpoint {
endpoint = wrapCaching(endpoint, i, "Method")
return endpoint
func (r *cachingMethodResult) Data() string {
return r.result.Data
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ type ClientInterceptors interface {
// WrapMethodClientEndpoint wraps the Method endpoint with the client
// interceptors defined in the design.
func WrapMethodClientEndpoint(endpoint goa.Endpoint, i ClientInterceptors) goa.Endpoint {
endpoint = wrapClientValidation(endpoint, i, "Method")
endpoint = wrapClientMethodvalidation(endpoint, i)
return endpoint
}
Loading

0 comments on commit b9cab9e

Please sign in to comment.