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

Method buildRequestBodyType

http/codegen/service_data.go:2104–2218  ·  view source on GitHub ↗

buildRequestBodyType builds the TypeData for a request body. The data makes it possible to generate a function on the client side that creates the body from the service method payload. body is the HTTP request body att is the payload attribute e is the HTTP endpoint expression svr is true if the

(body, att *expr.AttributeExpr, e *expr.HTTPEndpointExpr, svr bool, sd *ServiceData)

Source from the content-addressed store, hash-verified

2102//
2103// sd is the service data
2104func (sds *ServicesData) buildRequestBodyType(body, att *expr.AttributeExpr, e *expr.HTTPEndpointExpr, svr bool, sd *ServiceData) *TypeData {
2105 if body.Type == expr.Empty {
2106 return nil
2107 }
2108 var (
2109 name string
2110 varname string
2111 desc string
2112 def string
2113 ref string
2114 validateDef string
2115 validateRef string
2116
2117 svc = sd.Service
2118 httpctx = httpContext(sd.Scope, true, svr)
2119 ep = sd.Service.Method(e.Name())
2120 pkg = pkgWithDefault(ep.PayloadLoc, sd.Service.PkgName)
2121 svcctx = serviceContext(pkg, sd.Service.Scope)
2122 )
2123 name = body.Type.Name()
2124 ref = sd.Scope.GoTypeRef(body)
2125
2126 addMarshalTags(body, make(map[string]struct{}))
2127
2128 if ut, ok := body.Type.(expr.UserType); ok {
2129 varname = codegen.Goify(ut.Name(), true)
2130 def = goTypeDef(sd.Scope, ut.Attribute(), svr, !svr)
2131 desc = fmt.Sprintf("%s is the type of the %q service %q endpoint HTTP request body.",
2132 varname, svc.Name, e.Name())
2133 if svr {
2134 // generate validation code for unmarshaled type (server-side).
2135 validateDef = codegen.ValidationCode(ut.Attribute(), ut, httpctx, true, expr.IsAlias(ut), false, "body")
2136 if validateDef != "" {
2137 validateRef = fmt.Sprintf("err = Validate%s(&body)", varname)
2138 }
2139 }
2140 } else {
2141 // Generate validation code first because inline struct validation is removed.
2142 ctx := codegen.NewAttributeContext(!expr.IsPrimitive(body.Type), false, !svr, "", sd.Scope)
2143 validateRef = codegen.ValidationCode(body, nil, ctx, true, expr.IsAlias(body.Type), false, "body")
2144 if svr && expr.IsObject(body.Type) {
2145 // Body is an explicit object described in the design and in
2146 // this case the GoTypeRef is an inline struct definition. We
2147 // want to force all attributes to be pointers because we are
2148 // generating the server body type pre-validation.
2149 body.Validation = nil
2150 }
2151 varname = sd.Scope.GoTypeRef(body)
2152 desc = body.Description
2153 }
2154 var init *InitData
2155 if !svr && att.Type != expr.Empty && needInit(body.Type) {
2156 var (
2157 name string
2158 desc string
2159 code string
2160 origin string
2161 err error

Callers 2

buildPayloadDataMethod · 0.95
initWebSocketDataMethod · 0.95

Calls 15

GoifyFunction · 0.92
ValidationCodeFunction · 0.92
IsAliasFunction · 0.92
NewAttributeContextFunction · 0.92
IsPrimitiveFunction · 0.92
IsObjectFunction · 0.92
AsObjectFunction · 0.92
AppendHelpersFunction · 0.92
httpContextFunction · 0.85
serviceContextFunction · 0.85
addMarshalTagsFunction · 0.85
goTypeDefFunction · 0.85

Tested by

no test coverage detected