sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/model/stage/stage.go (about)

     1  /*
     2  Copyright 2020 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 stage
    18  
    19  import (
    20  	"errors"
    21  )
    22  
    23  var errInvalid = errors.New("invalid version stage")
    24  
    25  // Stage represents the stability of a version
    26  type Stage uint8
    27  
    28  // Order Stage in decreasing degree of stability for comparison purposes.
    29  // Stable must be 0 so that it is the default Stage
    30  const ( // The order in this const declaration will be used to order version stages except for Stable
    31  	// Stable should be used for plugins that are rarely changed in backwards-compatible ways, e.g. bug fixes.
    32  	Stable Stage = iota
    33  	// Beta should be used for plugins that may be changed in minor ways and are not expected to break between uses.
    34  	Beta Stage = iota
    35  	// Alpha should be used for plugins that are frequently changed and may break between uses.
    36  	Alpha Stage = iota
    37  )
    38  
    39  const (
    40  	alpha  = "alpha"
    41  	beta   = "beta"
    42  	stable = ""
    43  )
    44  
    45  // ParseStage parses stage into a Stage, assuming it is one of the valid stages
    46  func ParseStage(stage string) (Stage, error) {
    47  	var s Stage
    48  	return s, s.Parse(stage)
    49  }
    50  
    51  // Parse parses stage inline, assuming it is one of the valid stages
    52  func (s *Stage) Parse(stage string) error {
    53  	switch stage {
    54  	case alpha:
    55  		*s = Alpha
    56  	case beta:
    57  		*s = Beta
    58  	case stable:
    59  		*s = Stable
    60  	default:
    61  		return errInvalid
    62  	}
    63  	return nil
    64  }
    65  
    66  // String returns the string representation of s
    67  func (s Stage) String() string {
    68  	switch s {
    69  	case Alpha:
    70  		return alpha
    71  	case Beta:
    72  		return beta
    73  	case Stable:
    74  		return stable
    75  	default:
    76  		panic(errInvalid)
    77  	}
    78  }
    79  
    80  // Validate ensures that the stage is one of the valid stages
    81  func (s Stage) Validate() error {
    82  	switch s {
    83  	case Alpha:
    84  	case Beta:
    85  	case Stable:
    86  	default:
    87  		return errInvalid
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  // Compare returns -1 if s < other, 0 if s == other, and 1 if s > other.
    94  func (s Stage) Compare(other Stage) int {
    95  	if s == other {
    96  		return 0
    97  	}
    98  
    99  	// Stage are sorted in decreasing order
   100  	if s > other {
   101  		return -1
   102  	}
   103  	return 1
   104  }
   105  
   106  // IsStable returns whether the stage is stable or not
   107  func (s Stage) IsStable() bool {
   108  	return s == Stable
   109  }