util.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package builder
  14. import (
  15. "sort"
  16. "github.com/emicklei/go-restful"
  17. "github.com/go-openapi/spec"
  18. )
  19. type parameters []spec.Parameter
  20. func (s parameters) Len() int { return len(s) }
  21. func (s parameters) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  22. // byNameIn used in sorting parameters by Name and In fields.
  23. type byNameIn struct {
  24. parameters
  25. }
  26. func (s byNameIn) Less(i, j int) bool {
  27. return s.parameters[i].Name < s.parameters[j].Name || (s.parameters[i].Name == s.parameters[j].Name && s.parameters[i].In < s.parameters[j].In)
  28. }
  29. // SortParameters sorts parameters by Name and In fields.
  30. func sortParameters(p []spec.Parameter) {
  31. sort.Sort(byNameIn{p})
  32. }
  33. func groupRoutesByPath(routes []restful.Route) map[string][]restful.Route {
  34. pathToRoutes := make(map[string][]restful.Route)
  35. for _, r := range routes {
  36. pathToRoutes[r.Path] = append(pathToRoutes[r.Path], r)
  37. }
  38. return pathToRoutes
  39. }
  40. func mapKeyFromParam(param *restful.Parameter) interface{} {
  41. return struct {
  42. Name string
  43. Kind int
  44. }{
  45. Name: param.Data().Name,
  46. Kind: param.Data().Kind,
  47. }
  48. }