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

Function buildServers

http/codegen/openapi/v3/builder.go:476–537  ·  view source on GitHub ↗

buildServers builds the OpenAPI Server objects from the given server expressions.

(servers []*expr.ServerExpr)

Source from the content-addressed store, hash-verified

474// buildServers builds the OpenAPI Server objects from the given server
475// expressions.
476func buildServers(servers []*expr.ServerExpr) []*Server {
477 var svrs []*Server
478 for _, svr := range servers {
479 if !openapi.MustGenerate(svr.Meta) {
480 continue
481 }
482 var server *Server
483 for _, host := range svr.Hosts {
484 if !openapi.MustGenerate(host.Meta) {
485 continue
486 }
487
488 var (
489 serverVariable = make(map[string]*ServerVariable)
490 defaultValue any
491 validationValues []any
492 )
493
494 // Get the first URL expression in the host by default.
495 // Host expression must have at least one URI (validations would have failed
496 // otherwise).
497 uExpr := host.URIs[0]
498 // attempt to find the first HTTP/HTTPS URL
499 for _, ue := range host.URIs {
500 s := ue.Scheme()
501 if s == "http" || s == "https" {
502 uExpr = ue
503 break
504 }
505 }
506
507 // retrieve host variables
508 vars := expr.AsObject(host.Variables.Type)
509 for _, v := range *vars {
510 defaultValue = v.Attribute.DefaultValue
511
512 if v.Attribute.Validation != nil && len(v.Attribute.Validation.Values) > 0 {
513 validationValues = append(validationValues, v.Attribute.Validation.Values...)
514 if defaultValue == nil {
515 defaultValue = v.Attribute.Validation.Values[0]
516 }
517 }
518
519 if defaultValue != nil {
520 serverVariable[v.Name] = &ServerVariable{
521 Enum: validationValues,
522 Default: defaultValue,
523 Description: host.Variables.Description,
524 }
525 }
526 }
527
528 server = &Server{
529 URL: string(uExpr),
530 Description: svr.Description,
531 Variables: serverVariable,
532 }
533 svrs = append(svrs, server)

Callers 1

NewFunction · 0.85

Calls 3

MustGenerateFunction · 0.92
AsObjectFunction · 0.92
SchemeMethod · 0.45

Tested by

no test coverage detected