analyze creates the data necessary to render the code of the given service. It records the user types needed by the service definition in userTypes.
(httpSvc *expr.HTTPServiceExpr)
| 616 | // analyze creates the data necessary to render the code of the given service. |
| 617 | // It records the user types needed by the service definition in userTypes. |
| 618 | func (sds *ServicesData) analyze(httpSvc *expr.HTTPServiceExpr) *ServiceData { |
| 619 | svc := sds.ServicesData.Get(httpSvc.ServiceExpr.Name) |
| 620 | scope := codegen.NewNameScope() |
| 621 | scope.Unique("c") // 'c' is reserved as the client's receiver name. |
| 622 | scope.Unique("v") // 'v' is reserved as the request builder payload argument name. |
| 623 | // Reserve 'websocket' to avoid collision with gorilla/websocket |
| 624 | scope.Unique("websocket") |
| 625 | // Reserve the service package name to avoid collision with parameter names in generated code |
| 626 | scope.Unique(svc.PkgName) |
| 627 | sd := &ServiceData{ |
| 628 | Service: svc, |
| 629 | ServerStruct: "Server", |
| 630 | MountPointStruct: "MountPoint", |
| 631 | ServerInit: "New", |
| 632 | MountServer: "Mount", |
| 633 | ServerService: "Service", |
| 634 | ClientStruct: "Client", |
| 635 | ServerTypeNames: make(map[string]bool), |
| 636 | ClientTypeNames: make(map[string]bool), |
| 637 | Scope: scope, |
| 638 | } |
| 639 | |
| 640 | for _, s := range httpSvc.FileServers { |
| 641 | paths := make([]string, len(s.RequestPaths)) |
| 642 | for i, p := range s.RequestPaths { |
| 643 | idx := strings.LastIndex(p, "/{") |
| 644 | switch { |
| 645 | case idx == 0: |
| 646 | paths[i] = "/" |
| 647 | case idx > 0: |
| 648 | paths[i] = p[:idx] |
| 649 | default: |
| 650 | paths[i] = p |
| 651 | } |
| 652 | } |
| 653 | var pp string |
| 654 | if s.IsDir() { |
| 655 | pp = expr.ExtractHTTPWildcards(s.RequestPaths[0])[0] |
| 656 | } |
| 657 | var redirect *RedirectData |
| 658 | if s.Redirect != nil { |
| 659 | redirect = &RedirectData{ |
| 660 | URL: s.Redirect.URL, |
| 661 | StatusCode: statusCodeToHTTPConst(s.Redirect.StatusCode), |
| 662 | } |
| 663 | } |
| 664 | data := &FileServerData{ |
| 665 | MountHandler: scope.Unique(fmt.Sprintf("Mount%s", codegen.Goify(s.FilePath, true))), |
| 666 | RequestPaths: paths, |
| 667 | FilePath: s.FilePath, |
| 668 | IsDir: s.IsDir(), |
| 669 | PathParam: pp, |
| 670 | Redirect: redirect, |
| 671 | VarName: scope.Unique(codegen.Goify(s.FilePath, true)), |
| 672 | ArgName: scope.Unique(fmt.Sprintf("fileSystem%s", codegen.Goify(s.FilePath, true))), |
| 673 | } |
| 674 | sd.FileServers = append(sd.FileServers, data) |
| 675 | } |
no test coverage detected