MCPcopy Create free account
hub / github.com/pace/bricks / generateRequestStruct

Method generateRequestStruct

http/jsonapi/generator/generate_handler.go:258–315  ·  view source on GitHub ↗
(route *route, schema *openapi3.T)

Source from the content-addressed store, hash-verified

256}
257
258func (g *Generator) generateRequestStruct(route *route, schema *openapi3.T) error {
259 body := route.operation.RequestBody
260 var fields []jen.Code
261
262 // add http request
263 fields = append(fields, jen.Id("Request").Op("*").Qual("net/http", "Request").Tag(noValidation))
264
265 // add request type
266 if body != nil {
267 if mt := body.Value.Content.Get(jsonapiContent); mt != nil {
268 ref, err := g.generateTypeReference(route.serviceFunc+"Content", mt.Schema, true)
269 if err != nil {
270 return err
271 }
272 // the content has noValidation to only check parametes first
273 // then unmarshal and then check the content after
274 fields = append(fields, jen.Id("Content").Add(ref).Tag(noValidation))
275 }
276 }
277
278 // add parameters
279 for _, param := range route.operation.Parameters {
280 paramName := generateParamName(param)
281 paramStmt := jen.Id(paramName)
282 tags := make(map[string]string)
283 if param.Value.Required {
284 tags["valid"] = "required"
285 } else {
286 tags["valid"] = "optional"
287 }
288
289 // add go type
290 if param.Value.Schema.Ref != "" {
291 paramStmt.Id(goNameHelper(filepath.Base(param.Value.Schema.Ref)))
292 } else {
293 tg := g.goType(paramStmt, param.Value.Schema.Value, tags)
294 tg.isParam = true
295
296 err := tg.invoke()
297 if err != nil {
298 return err
299 }
300 }
301
302 fields = append(fields, paramStmt.Tag(tags))
303 }
304
305 // add comment and generate type
306 if body != nil {
307 g.addGoDoc(route.requestType, body.Value.Description)
308 } else {
309 g.addGoDoc(route.requestType, "is a standard http.Request extended with the\n"+
310 "un-marshaled content object")
311 }
312 g.goSource.Type().Id(route.requestType).Struct(fields...)
313
314 return nil
315}

Callers 1

Calls 9

generateTypeReferenceMethod · 0.95
goTypeMethod · 0.95
addGoDocMethod · 0.95
generateParamNameFunction · 0.85
goNameHelperFunction · 0.85
invokeMethod · 0.80
TypeMethod · 0.80
GetMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected