MCPcopy Create free account
hub / github.com/goadesign/goa / buildResponses

Method buildResponses

http/codegen/service_data.go:1590–1866  ·  view source on GitHub ↗

buildResponses builds the response data for all the responses in the endpoint expression. The response headers, cookies and body for each response are inferred from the method's result expression if not specified explicitly. viewed parameter indicates if the method result uses views.

(e *expr.HTTPEndpointExpr, result *expr.AttributeExpr, viewed bool, sd *ServiceData)

Source from the content-addressed store, hash-verified

1588//
1589// viewed parameter indicates if the method result uses views.
1590func (sds *ServicesData) buildResponses(e *expr.HTTPEndpointExpr, result *expr.AttributeExpr, viewed bool, sd *ServiceData) []*ResponseData {
1591 var (
1592 responses []*ResponseData
1593
1594 svc = sd.Service
1595 md = svc.Method(e.Name())
1596 pkg = pkgWithDefault(md.ResultLoc, svc.PkgName)
1597 httpclictx = httpContext(sd.Scope, false, false)
1598 scope = svc.Scope
1599 svcctx = serviceContext(pkg, sd.Service.Scope)
1600 )
1601 {
1602 if viewed {
1603 scope = svc.ViewScope
1604 svcctx = viewContext(sd.Service.ViewsPkg, sd.Service.ViewScope)
1605 }
1606 notag := -1
1607 for i, resp := range e.Responses {
1608 resp.Body = expr.DupAtt(resp.Body)
1609 resp.Body = makeHTTPType(resp.Body)
1610 if resp.Tag[0] == "" {
1611 if notag > -1 {
1612 continue // we don't want more than one response with no tag
1613 }
1614 notag = i
1615 }
1616 var (
1617 headersData []*HeaderData
1618 cookiesData []*CookieData
1619 serverBodyData []*TypeData
1620 clientBodyData *TypeData
1621 init *InitData
1622 origin string
1623 mustValidate bool
1624 clientRespBody = resp.Body
1625
1626 resAttr = result
1627 )
1628 {
1629 headersData = sds.extractHeaders(resp.Headers, result, svcctx, scope)
1630 cookiesData = sds.extractCookies(resp.Cookies, result, svcctx, scope)
1631 if resp.Body.Type != expr.Empty {
1632 // If design uses Body("name") syntax we need to use the
1633 // corresponding attribute in the result type for body
1634 // transformation.
1635 if o, ok := resp.Body.Meta["origin:attribute"]; ok {
1636 origin = o[0]
1637 resAttr = expr.AsObject(resAttr.Type).Attribute(origin)
1638 }
1639 }
1640 if viewed {
1641 vname := ""
1642 clientView := clientResponseViewName(e, md)
1643 if origin != "" {
1644 // Response body is explicitly set to an attribute in the method
1645 // result type. No need to do any view-based projections server side.
1646 if sbd := sds.buildResponseBodyType(resp.Body, result, md.ResultLoc, e, true, &vname, sd); sbd != nil {
1647 serverBodyData = append(serverBodyData, sbd)

Callers 1

buildResultDataMethod · 0.95

Calls 15

extractHeadersMethod · 0.95
extractCookiesMethod · 0.95
buildResponseBodyTypeMethod · 0.95
DupAttFunction · 0.92
AsObjectFunction · 0.92
GoifyFunction · 0.92
IsObjectFunction · 0.92
ValidationCodeFunction · 0.92
IsAliasFunction · 0.92
AppendHelpersFunction · 0.92
IsArrayFunction · 0.92
IsMapFunction · 0.92

Tested by

no test coverage detected