route_reader.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package restful
  2. // Copyright 2021 Ernest Micklei. All rights reserved.
  3. // Use of this source code is governed by a license
  4. // that can be found in the LICENSE file.
  5. type RouteReader interface {
  6. Method() string
  7. Consumes() []string
  8. Path() string
  9. Doc() string
  10. Notes() string
  11. Operation() string
  12. ParameterDocs() []*Parameter
  13. // Returns a copy
  14. Metadata() map[string]interface{}
  15. Deprecated() bool
  16. }
  17. type routeAccessor struct {
  18. route *Route
  19. }
  20. func (r routeAccessor) Method() string {
  21. return r.route.Method
  22. }
  23. func (r routeAccessor) Consumes() []string {
  24. return r.route.Consumes[:]
  25. }
  26. func (r routeAccessor) Path() string {
  27. return r.route.Path
  28. }
  29. func (r routeAccessor) Doc() string {
  30. return r.route.Doc
  31. }
  32. func (r routeAccessor) Notes() string {
  33. return r.route.Notes
  34. }
  35. func (r routeAccessor) Operation() string {
  36. return r.route.Operation
  37. }
  38. func (r routeAccessor) ParameterDocs() []*Parameter {
  39. return r.route.ParameterDocs[:]
  40. }
  41. // Returns a copy
  42. func (r routeAccessor) Metadata() map[string]interface{} {
  43. return copyMap(r.route.Metadata)
  44. }
  45. func (r routeAccessor) Deprecated() bool {
  46. return r.route.Deprecated
  47. }
  48. // https://stackoverflow.com/questions/23057785/how-to-copy-a-map
  49. func copyMap(m map[string]interface{}) map[string]interface{} {
  50. cp := make(map[string]interface{})
  51. for k, v := range m {
  52. vm, ok := v.(map[string]interface{})
  53. if ok {
  54. cp[k] = copyMap(vm)
  55. } else {
  56. cp[k] = v
  57. }
  58. }
  59. return cp
  60. }