util.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2017 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 util
  14. import (
  15. "reflect"
  16. "strings"
  17. )
  18. // [DEPRECATED] ToCanonicalName converts Golang package/type canonical name into REST friendly OpenAPI name.
  19. // This method is deprecated because it has a misleading name. Please use ToRESTFriendlyName
  20. // instead
  21. //
  22. // NOTE: actually the "canonical name" in this method should be named "REST friendly OpenAPI name",
  23. // which is different from "canonical name" defined in GetCanonicalTypeName. The "canonical name" defined
  24. // in GetCanonicalTypeName means Go type names with full package path.
  25. //
  26. // Examples of REST friendly OpenAPI name:
  27. //
  28. // Input: k8s.io/api/core/v1.Pod
  29. // Output: io.k8s.api.core.v1.Pod
  30. //
  31. // Input: k8s.io/api/core/v1
  32. // Output: io.k8s.api.core.v1
  33. //
  34. // Input: csi.storage.k8s.io/v1alpha1.CSINodeInfo
  35. // Output: io.k8s.storage.csi.v1alpha1.CSINodeInfo
  36. func ToCanonicalName(name string) string {
  37. return ToRESTFriendlyName(name)
  38. }
  39. // ToRESTFriendlyName converts Golang package/type canonical name into REST friendly OpenAPI name.
  40. //
  41. // Examples of REST friendly OpenAPI name:
  42. //
  43. // Input: k8s.io/api/core/v1.Pod
  44. // Output: io.k8s.api.core.v1.Pod
  45. //
  46. // Input: k8s.io/api/core/v1
  47. // Output: io.k8s.api.core.v1
  48. //
  49. // Input: csi.storage.k8s.io/v1alpha1.CSINodeInfo
  50. // Output: io.k8s.storage.csi.v1alpha1.CSINodeInfo
  51. func ToRESTFriendlyName(name string) string {
  52. nameParts := strings.Split(name, "/")
  53. // Reverse first part. e.g., io.k8s... instead of k8s.io...
  54. if len(nameParts) > 0 && strings.Contains(nameParts[0], ".") {
  55. parts := strings.Split(nameParts[0], ".")
  56. for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 {
  57. parts[i], parts[j] = parts[j], parts[i]
  58. }
  59. nameParts[0] = strings.Join(parts, ".")
  60. }
  61. return strings.Join(nameParts, ".")
  62. }
  63. // OpenAPICanonicalTypeNamer is an interface for models without Go type to seed model name.
  64. //
  65. // OpenAPI canonical names are Go type names with full package path, for uniquely indentifying
  66. // a model / Go type. If a Go type is vendored from another package, only the path after "/vendor/"
  67. // should be used. For custom resource definition (CRD), the canonical name is expected to be
  68. //
  69. // group/version.kind
  70. //
  71. // Examples of canonical name:
  72. //
  73. // Go type: k8s.io/kubernetes/pkg/apis/core.Pod
  74. // CRD: csi.storage.k8s.io/v1alpha1.CSINodeInfo
  75. //
  76. // Example for vendored Go type:
  77. //
  78. // Original full path: k8s.io/kubernetes/vendor/k8s.io/api/core/v1.Pod
  79. // Canonical name: k8s.io/api/core/v1.Pod
  80. //
  81. // Original full path: vendor/k8s.io/api/core/v1.Pod
  82. // Canonical name: k8s.io/api/core/v1.Pod
  83. type OpenAPICanonicalTypeNamer interface {
  84. OpenAPICanonicalTypeName() string
  85. }
  86. // OpenAPIModelNamer is an interface Go types may implement to provide an OpenAPI model name.
  87. //
  88. // This takes precedence over OpenAPICanonicalTypeNamer, and should be used when a Go type has a model
  89. // name that differs from its canonical type name as determined by Go package name reflection.
  90. type OpenAPIModelNamer interface {
  91. OpenAPIModelName() string
  92. }
  93. // GetCanonicalTypeName will find the canonical type name of a sample object, removing
  94. // the "vendor" part of the path
  95. func GetCanonicalTypeName(model interface{}) string {
  96. switch namer := model.(type) {
  97. case OpenAPIModelNamer:
  98. return namer.OpenAPIModelName()
  99. case OpenAPICanonicalTypeNamer:
  100. return namer.OpenAPICanonicalTypeName()
  101. }
  102. t := reflect.TypeOf(model)
  103. if t.Kind() == reflect.Ptr {
  104. t = t.Elem()
  105. }
  106. if t.PkgPath() == "" {
  107. return t.Name()
  108. }
  109. path := t.PkgPath()
  110. if strings.Contains(path, "/vendor/") {
  111. path = path[strings.Index(path, "/vendor/")+len("/vendor/"):]
  112. } else if strings.HasPrefix(path, "vendor/") {
  113. path = strings.TrimPrefix(path, "vendor/")
  114. }
  115. return path + "." + t.Name()
  116. }