github.com/ava-labs/avalanchego@v1.11.11/version/version.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package version
     5  
     6  import (
     7  	"fmt"
     8  	"sync/atomic"
     9  )
    10  
    11  var (
    12  	// V1_0_0 is a useful version to use in tests
    13  	Semantic1_0_0 = &Semantic{
    14  		Major: 1,
    15  		Minor: 0,
    16  		Patch: 0,
    17  	}
    18  
    19  	_ fmt.Stringer = (*Semantic)(nil)
    20  )
    21  
    22  type Semantic struct {
    23  	Major int `json:"major" yaml:"major"`
    24  	Minor int `json:"minor" yaml:"minor"`
    25  	Patch int `json:"patch" yaml:"patch"`
    26  
    27  	str atomic.Value
    28  }
    29  
    30  // The only difference here between Semantic and Application is that Semantic
    31  // prepends "v" rather than the client name.
    32  func (s *Semantic) String() string {
    33  	strIntf := s.str.Load()
    34  	if strIntf != nil {
    35  		return strIntf.(string)
    36  	}
    37  
    38  	str := fmt.Sprintf(
    39  		"v%d.%d.%d",
    40  		s.Major,
    41  		s.Minor,
    42  		s.Patch,
    43  	)
    44  	s.str.Store(str)
    45  	return str
    46  }
    47  
    48  // Compare returns a positive number if s > o, 0 if s == o, or a negative number
    49  // if s < o.
    50  func (s *Semantic) Compare(o *Semantic) int {
    51  	if s.Major != o.Major {
    52  		return s.Major - o.Major
    53  	}
    54  	if s.Minor != o.Minor {
    55  		return s.Minor - o.Minor
    56  	}
    57  	return s.Patch - o.Patch
    58  }