group_version.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Copyright 2015 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. // TODO: This GetVersion/GetGroup arrangement is temporary and will be replaced
  14. // with a GroupAndVersion type.
  15. package util
  16. import "strings"
  17. func GetVersion(groupVersion string) string {
  18. s := strings.Split(groupVersion, "/")
  19. if len(s) != 2 {
  20. // e.g. return "v1" for groupVersion="v1"
  21. return s[len(s)-1]
  22. }
  23. return s[1]
  24. }
  25. func GetGroup(groupVersion string) string {
  26. s := strings.Split(groupVersion, "/")
  27. if len(s) == 1 {
  28. // e.g. return "" for groupVersion="v1"
  29. return ""
  30. }
  31. return s[0]
  32. }
  33. // GetGroupVersion returns the "group/version". It returns "version" is if group
  34. // is empty. It returns "group/" if version is empty.
  35. func GetGroupVersion(group, version string) string {
  36. if len(group) == 0 {
  37. return version
  38. }
  39. return group + "/" + version
  40. }