Views and Result Types
Understanding how Goa handles result type views, including view computation and marshalling
Result Type Views Overview
Views in Goa provide a powerful way to control how result types are rendered in responses. When a method returns a result type with views:
- The service method includes an additional view parameter
- A dedicated views package is generated at the service level
- View-specific validation logic is automatically generated
View Computation Process
Service Method Generation
When a method returns a result type with multiple views:
- The method signature includes an extra view parameter
- The generated endpoint function uses this view to create a viewed result type
- Constructors are generated to convert between regular and viewed result types
Views Package
The generated views package at the service level contains:
- Viewed result type definitions for each method result
- Fields use pointers to enable view-specific validation
- Conversion functions between regular and viewed result types
Marshalling and Transport
Server-Side Response
- The viewed result type is marshalled into a server type
- Nil attributes are omitted from the response
- The view name is passed in the “Goa-View” header
Client-Side Response
- Response is unmarshalled into the client type
- Transformed into the viewed result type
- View name is extracted from the “Goa-View” header
- View-specific validation is performed
- Viewed result type is converted back to service result type
Default View Behavior
- If no views are defined, Goa adds a “default” view automatically
- To bypass view-specific logic, use the
Type
DSL instead of result type - The default view includes all basic fields of the result type
Best Practices
- Use views to control data visibility for different contexts
- Consider performance implications when designing views
- Document view behavior in your API specification
- Use meaningful view names that reflect their purpose