github.com/ava-labs/avalanchego@v1.11.11/version/application.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  	"errors"
     8  	"fmt"
     9  	"sync"
    10  )
    11  
    12  var (
    13  	errDifferentMajor = errors.New("different major version")
    14  
    15  	_ fmt.Stringer = (*Application)(nil)
    16  )
    17  
    18  type Application struct {
    19  	Name  string `json:"name"  yaml:"name"`
    20  	Major int    `json:"major" yaml:"major"`
    21  	Minor int    `json:"minor" yaml:"minor"`
    22  	Patch int    `json:"patch" yaml:"patch"`
    23  
    24  	makeStrOnce sync.Once
    25  	str         string
    26  }
    27  
    28  // The only difference here between Application and Semantic is that Application
    29  // prepends the client name rather than "v".
    30  func (a *Application) String() string {
    31  	a.makeStrOnce.Do(a.initString)
    32  	return a.str
    33  }
    34  
    35  func (a *Application) initString() {
    36  	a.str = fmt.Sprintf(
    37  		"%s/%d.%d.%d",
    38  		a.Name,
    39  		a.Major,
    40  		a.Minor,
    41  		a.Patch,
    42  	)
    43  }
    44  
    45  func (a *Application) Compatible(o *Application) error {
    46  	switch {
    47  	case a.Major > o.Major:
    48  		return errDifferentMajor
    49  	default:
    50  		return nil
    51  	}
    52  }
    53  
    54  func (a *Application) Before(o *Application) bool {
    55  	return a.Compare(o) < 0
    56  }
    57  
    58  // Compare returns a positive number if s > o, 0 if s == o, or a negative number
    59  // if s < o.
    60  func (a *Application) Compare(o *Application) int {
    61  	if a.Major != o.Major {
    62  		return a.Major - o.Major
    63  	}
    64  	if a.Minor != o.Minor {
    65  		return a.Minor - o.Minor
    66  	}
    67  	return a.Patch - o.Patch
    68  }