sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/config/version.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package config 18 19 import ( 20 "encoding/json" 21 "errors" 22 "fmt" 23 "strconv" 24 "strings" 25 26 "sigs.k8s.io/kubebuilder/v3/pkg/model/stage" 27 ) 28 29 var ( 30 errNonPositive = errors.New("project version number must be positive") 31 errEmpty = errors.New("project version is empty") 32 ) 33 34 // Version is a project version containing a non-zero positive integer and a stage value that represents stability. 35 type Version struct { 36 // Number denotes the current version of a plugin. Two different numbers between versions 37 // indicate that they are incompatible. 38 Number int 39 // Stage indicates stability. 40 Stage stage.Stage 41 } 42 43 // Parse parses version inline, assuming it adheres to format: [1-9][0-9]*(-(alpha|beta))? 44 func (v *Version) Parse(version string) error { 45 if len(version) == 0 { 46 return errEmpty 47 } 48 49 substrings := strings.SplitN(version, "-", 2) 50 51 var err error 52 if v.Number, err = strconv.Atoi(substrings[0]); err != nil { 53 // Lets check if the `-` belonged to a negative number 54 if n, err := strconv.Atoi(version); err == nil && n < 0 { 55 return errNonPositive 56 } 57 return err 58 } else if v.Number == 0 { 59 return errNonPositive 60 } 61 62 if len(substrings) > 1 { 63 if err = v.Stage.Parse(substrings[1]); err != nil { 64 return err 65 } 66 } 67 68 return nil 69 } 70 71 // String returns the string representation of v. 72 func (v Version) String() string { 73 stageStr := v.Stage.String() 74 if len(stageStr) == 0 { 75 return fmt.Sprintf("%d", v.Number) 76 } 77 return fmt.Sprintf("%d-%s", v.Number, stageStr) 78 } 79 80 // Validate ensures that the version number is positive and the stage is one of the valid stages. 81 func (v Version) Validate() error { 82 if v.Number < 1 { 83 return errNonPositive 84 } 85 86 return v.Stage.Validate() 87 } 88 89 // Compare returns -1 if v < other, 0 if v == other, and 1 if v > other. 90 func (v Version) Compare(other Version) int { 91 if v.Number > other.Number { 92 return 1 93 } else if v.Number < other.Number { 94 return -1 95 } 96 97 return v.Stage.Compare(other.Stage) 98 } 99 100 // IsStable returns true if v is stable. 101 func (v Version) IsStable() bool { 102 return v.Stage.IsStable() 103 } 104 105 // MarshalJSON implements json.Marshaller 106 func (v Version) MarshalJSON() ([]byte, error) { 107 if err := v.Validate(); err != nil { 108 return []byte{}, err 109 } 110 111 return json.Marshal(v.String()) 112 } 113 114 // UnmarshalJSON implements json.Unmarshaller 115 func (v *Version) UnmarshalJSON(b []byte) error { 116 var str string 117 if err := json.Unmarshal(b, &str); err != nil { 118 return err 119 } 120 121 return v.Parse(str) 122 }