semver.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright 2013-2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Semantic Versions http://semver.org
  15. package semver
  16. import (
  17. "bytes"
  18. "errors"
  19. "fmt"
  20. "strconv"
  21. "strings"
  22. )
  23. type Version struct {
  24. Major int64
  25. Minor int64
  26. Patch int64
  27. PreRelease PreRelease
  28. Metadata string
  29. }
  30. type PreRelease string
  31. func splitOff(input *string, delim string) (val string) {
  32. parts := strings.SplitN(*input, delim, 2)
  33. if len(parts) == 2 {
  34. *input = parts[0]
  35. val = parts[1]
  36. }
  37. return val
  38. }
  39. func New(version string) *Version {
  40. return Must(NewVersion(version))
  41. }
  42. func NewVersion(version string) (*Version, error) {
  43. v := Version{}
  44. if err := v.Set(version); err != nil {
  45. return nil, err
  46. }
  47. return &v, nil
  48. }
  49. // Must is a helper for wrapping NewVersion and will panic if err is not nil.
  50. func Must(v *Version, err error) *Version {
  51. if err != nil {
  52. panic(err)
  53. }
  54. return v
  55. }
  56. // Set parses and updates v from the given version string. Implements flag.Value
  57. func (v *Version) Set(version string) error {
  58. metadata := splitOff(&version, "+")
  59. preRelease := PreRelease(splitOff(&version, "-"))
  60. dotParts := strings.SplitN(version, ".", 3)
  61. if len(dotParts) != 3 {
  62. return fmt.Errorf("%s is not in dotted-tri format", version)
  63. }
  64. parsed := make([]int64, 3, 3)
  65. for i, v := range dotParts[:3] {
  66. val, err := strconv.ParseInt(v, 10, 64)
  67. parsed[i] = val
  68. if err != nil {
  69. return err
  70. }
  71. }
  72. v.Metadata = metadata
  73. v.PreRelease = preRelease
  74. v.Major = parsed[0]
  75. v.Minor = parsed[1]
  76. v.Patch = parsed[2]
  77. return nil
  78. }
  79. func (v Version) String() string {
  80. var buffer bytes.Buffer
  81. fmt.Fprintf(&buffer, "%d.%d.%d", v.Major, v.Minor, v.Patch)
  82. if v.PreRelease != "" {
  83. fmt.Fprintf(&buffer, "-%s", v.PreRelease)
  84. }
  85. if v.Metadata != "" {
  86. fmt.Fprintf(&buffer, "+%s", v.Metadata)
  87. }
  88. return buffer.String()
  89. }
  90. func (v *Version) UnmarshalYAML(unmarshal func(interface{}) error) error {
  91. var data string
  92. if err := unmarshal(&data); err != nil {
  93. return err
  94. }
  95. return v.Set(data)
  96. }
  97. func (v Version) MarshalJSON() ([]byte, error) {
  98. return []byte(`"` + v.String() + `"`), nil
  99. }
  100. func (v *Version) UnmarshalJSON(data []byte) error {
  101. l := len(data)
  102. if l == 0 || string(data) == `""` {
  103. return nil
  104. }
  105. if l < 2 || data[0] != '"' || data[l-1] != '"' {
  106. return errors.New("invalid semver string")
  107. }
  108. return v.Set(string(data[1 : l-1]))
  109. }
  110. // Compare tests if v is less than, equal to, or greater than versionB,
  111. // returning -1, 0, or +1 respectively.
  112. func (v Version) Compare(versionB Version) int {
  113. if cmp := recursiveCompare(v.Slice(), versionB.Slice()); cmp != 0 {
  114. return cmp
  115. }
  116. return preReleaseCompare(v, versionB)
  117. }
  118. // Equal tests if v is equal to versionB.
  119. func (v Version) Equal(versionB Version) bool {
  120. return v.Compare(versionB) == 0
  121. }
  122. // LessThan tests if v is less than versionB.
  123. func (v Version) LessThan(versionB Version) bool {
  124. return v.Compare(versionB) < 0
  125. }
  126. // Slice converts the comparable parts of the semver into a slice of integers.
  127. func (v Version) Slice() []int64 {
  128. return []int64{v.Major, v.Minor, v.Patch}
  129. }
  130. func (p PreRelease) Slice() []string {
  131. preRelease := string(p)
  132. return strings.Split(preRelease, ".")
  133. }
  134. func preReleaseCompare(versionA Version, versionB Version) int {
  135. a := versionA.PreRelease
  136. b := versionB.PreRelease
  137. /* Handle the case where if two versions are otherwise equal it is the
  138. * one without a PreRelease that is greater */
  139. if len(a) == 0 && (len(b) > 0) {
  140. return 1
  141. } else if len(b) == 0 && (len(a) > 0) {
  142. return -1
  143. }
  144. // If there is a prerelease, check and compare each part.
  145. return recursivePreReleaseCompare(a.Slice(), b.Slice())
  146. }
  147. func recursiveCompare(versionA []int64, versionB []int64) int {
  148. if len(versionA) == 0 {
  149. return 0
  150. }
  151. a := versionA[0]
  152. b := versionB[0]
  153. if a > b {
  154. return 1
  155. } else if a < b {
  156. return -1
  157. }
  158. return recursiveCompare(versionA[1:], versionB[1:])
  159. }
  160. func recursivePreReleaseCompare(versionA []string, versionB []string) int {
  161. // A larger set of pre-release fields has a higher precedence than a smaller set,
  162. // if all of the preceding identifiers are equal.
  163. if len(versionA) == 0 {
  164. if len(versionB) > 0 {
  165. return -1
  166. }
  167. return 0
  168. } else if len(versionB) == 0 {
  169. // We're longer than versionB so return 1.
  170. return 1
  171. }
  172. a := versionA[0]
  173. b := versionB[0]
  174. aInt := false
  175. bInt := false
  176. aI, err := strconv.Atoi(versionA[0])
  177. if err == nil {
  178. aInt = true
  179. }
  180. bI, err := strconv.Atoi(versionB[0])
  181. if err == nil {
  182. bInt = true
  183. }
  184. // Handle Integer Comparison
  185. if aInt && bInt {
  186. if aI > bI {
  187. return 1
  188. } else if aI < bI {
  189. return -1
  190. }
  191. }
  192. // Handle String Comparison
  193. if a > b {
  194. return 1
  195. } else if a < b {
  196. return -1
  197. }
  198. return recursivePreReleaseCompare(versionA[1:], versionB[1:])
  199. }
  200. // BumpMajor increments the Major field by 1 and resets all other fields to their default values
  201. func (v *Version) BumpMajor() {
  202. v.Major += 1
  203. v.Minor = 0
  204. v.Patch = 0
  205. v.PreRelease = PreRelease("")
  206. v.Metadata = ""
  207. }
  208. // BumpMinor increments the Minor field by 1 and resets all other fields to their default values
  209. func (v *Version) BumpMinor() {
  210. v.Minor += 1
  211. v.Patch = 0
  212. v.PreRelease = PreRelease("")
  213. v.Metadata = ""
  214. }
  215. // BumpPatch increments the Patch field by 1 and resets all other fields to their default values
  216. func (v *Version) BumpPatch() {
  217. v.Patch += 1
  218. v.PreRelease = PreRelease("")
  219. v.Metadata = ""
  220. }