Skip to content

Conversation

@raphael
Copy link
Member

@raphael raphael commented Oct 29, 2025

Summary

Fixes #206 - Testing plugin was generating invalid Go syntax for method return types when the result was an array or slice.

Problem

The testing plugin generated syntactically invalid code like:

func (c *Client) GetAccessControl(ctx context.Context, 
  p *bff.GetAccessControlPayload) (*bff.[]*AccessControl, error)

This is invalid because you cannot apply the pointer operator * before an array type with package qualification in between.

Solution

Leveraged Goa's existing GoFullTypeRef method from codegen/scope.go to properly generate package-qualified type references. This method correctly handles:

  • Arrays: []*Result[]*pkg.Result
  • Maps: map[K]*Vmap[K]*pkg.V
  • Nested types: []map[K]*V[]map[K]*pkg.V
  • Primitives and user types with proper pointer handling

Changes

  1. Added PkgResultRef field to clientMethodData struct
  2. Used scope.GoFullTypeRef(m.Result, svcData.PkgName) to generate correctly qualified types
  3. Updated client_methods.go.tpl template to use {{ $method.PkgResultRef }}

Testing

  • All existing tests pass
  • Verified code generation for all examples (httpgrpc, calculator, jsonrpc)
  • Generated code compiles and runs successfully

Benefits

  • Robust: Handles all edge cases including nested types
  • Maintainable: Uses Goa's tested infrastructure instead of custom logic
  • Consistent: Same approach Goa uses throughout its codegen
  • Future-proof: Automatically supports new type features

This fixes issue #206 where the testing plugin was generating invalid Go
syntax for method return types. The problem occurred when the result type
was an array or slice - the template was incorrectly placing the pointer
operator before the package qualifier and array brackets.

For example, a method returning []*AccessControl would generate:
  func GetAccessControl(...) (*bff.[]*AccessControl, error)

This is syntactically invalid. The correct syntax should be:
  func GetAccessControl(...) ([]*bff.AccessControl, error)

The fix:
- Added PkgResultRef field to clientMethodData to store the properly
  package-qualified result type reference
- Use Goa's GoFullTypeRef method to generate the package-qualified type
  reference, which correctly handles arrays, maps, primitives, and user
  types
- Updated the client_methods template to use PkgResultRef instead of
  manually constructing the type reference

This leverages Goa's existing codegen infrastructure which properly
handles all type cases including:
- Pointer types: *Result -> *pkg.Result
- Array/slice types: []*Result -> []*pkg.Result
- Map types: map[K]*Result -> map[K]*pkg.Result
- Nested types: []map[K]*Result -> []map[K]*pkg.Result

Fixes #206
@raphael raphael merged commit bd0a993 into v3 Oct 29, 2025
5 checks passed
@raphael raphael deleted the fix-206-testing-plugin-type-qualification branch October 29, 2025 00:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

testing plugin: syntax error in test Client code

2 participants