version.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package version
  2. import (
  3. "fmt"
  4. )
  5. // Version is a structured representation of a version number.
  6. type Version struct {
  7. Major int // Major version number.
  8. Minor int // Minor version number.
  9. Patch int // Patch version number.
  10. Extension string // Version extension, such as pre-release number or build metdata.
  11. Text string // Original version string, if this version was created via the Parse function.
  12. }
  13. // Compare this version (a) with another version (b).
  14. // This function returns -1 if a is less than b, 1 if a is greater than b, or 0 if a is equal to b.
  15. //
  16. // Extensions such as pre-release version or build metadata are ignored when comparing versions.
  17. func (a *Version) Compare(b *Version) int {
  18. if a == nil && b != nil {
  19. return -1
  20. } else if a != nil && b == nil {
  21. return 1
  22. } else if a == nil && b == nil {
  23. return 0
  24. }
  25. if a.Major == b.Major {
  26. if a.Minor == b.Minor {
  27. if a.Patch == b.Patch {
  28. return 0
  29. } else if a.Patch > b.Patch {
  30. return 1
  31. }
  32. } else if a.Minor > b.Minor {
  33. return 1
  34. }
  35. } else if a.Major > b.Major {
  36. return 1
  37. }
  38. return -1
  39. }
  40. // Equal checks for equality between two versions.
  41. //
  42. // Extensions such as pre-release version or build metadata are ignored when comparing versions.
  43. func (a *Version) Equal(b *Version) bool {
  44. return a.Compare(b) == 0
  45. }
  46. // Match tests the version against a constraint.
  47. // Gt and Lt take precedence over Gte and Lte.
  48. func (v *Version) Match(c *Constraint) bool {
  49. if v == nil {
  50. return false
  51. }
  52. if c == nil {
  53. return true
  54. }
  55. if c.Gt != nil {
  56. if v.Compare(c.Gt) <= 0 {
  57. return false
  58. }
  59. } else if c.Gte != nil {
  60. if v.Compare(c.Gte) < 0 {
  61. return false
  62. }
  63. }
  64. if c.Lt != nil {
  65. if v.Compare(c.Lt) >= 0 {
  66. return false
  67. }
  68. } else if c.Lte != nil {
  69. if v.Compare(c.Lte) > 0 {
  70. return false
  71. }
  72. }
  73. return true
  74. }
  75. // Less performs a simple comparison of this version (a) with another version (b).
  76. // This function returns true if a is less than b, or false otherwise.
  77. //
  78. // Extensions such as pre-release version or build metadata are ignored when comparing versions.
  79. func (a *Version) Less(b *Version) bool {
  80. return a.Compare(b) < 0
  81. }
  82. // SemanticString returns a version string conforming to the standard described in Semantic Versioning 2.0.0.
  83. //
  84. // See https://semver.org/#is-v123-a-semantic-version
  85. func (v *Version) SemanticString() string {
  86. if v == nil {
  87. return ""
  88. }
  89. return fmt.Sprintf("%d.%d.%d%s", v.Major, v.Minor, v.Patch, v.Extension)
  90. }
  91. func (v *Version) String() string {
  92. if v == nil {
  93. return ""
  94. }
  95. if v.Text != "" {
  96. return v.Text
  97. }
  98. return v.SemanticString()
  99. }