git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/version/version.go (about)

     1  package version
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
     7  )
     8  
     9  // Version represents revision number in SemVer scheme.
    10  //
    11  // Version is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs.Version
    12  // message. See ReadFromV2 / WriteToV2 methods.
    13  //
    14  // Instances can be created using built-in var declaration.
    15  //
    16  // Note that direct typecast is not safe and may result in loss of compatibility:
    17  //
    18  //	_ = Version(refs.Version{}) // not recommended
    19  type Version refs.Version
    20  
    21  const sdkMjr, sdkMnr = 2, 13
    22  
    23  // Current returns Version instance that initialized to the
    24  // latest supported FrostFS API revision number in SDK.
    25  func Current() (v Version) {
    26  	v.SetMajor(sdkMjr)
    27  	v.SetMinor(sdkMnr)
    28  	return v
    29  }
    30  
    31  // Major returns major number of the revision.
    32  func (v *Version) Major() uint32 {
    33  	return (*refs.Version)(v).GetMajor()
    34  }
    35  
    36  // SetMajor sets major number of the revision.
    37  func (v *Version) SetMajor(val uint32) {
    38  	(*refs.Version)(v).SetMajor(val)
    39  }
    40  
    41  // Minor returns minor number of the revision.
    42  func (v *Version) Minor() uint32 {
    43  	return (*refs.Version)(v).GetMinor()
    44  }
    45  
    46  // SetMinor sets minor number of the revision.
    47  func (v *Version) SetMinor(val uint32) {
    48  	(*refs.Version)(v).SetMinor(val)
    49  }
    50  
    51  // WriteToV2 writes Version to the refs.Version message.
    52  // The message must not be nil.
    53  //
    54  // See also ReadFromV2.
    55  func (v Version) WriteToV2(m *refs.Version) {
    56  	*m = (refs.Version)(v)
    57  }
    58  
    59  // ReadFromV2 reads Version from the refs.Version message. Checks if the message
    60  // conforms to FrostFS API V2 protocol.
    61  //
    62  // See also WriteToV2.
    63  func (v *Version) ReadFromV2(m refs.Version) error {
    64  	*v = Version(m)
    65  	return nil
    66  }
    67  
    68  // String implements fmt.Stringer.
    69  //
    70  // String is designed to be human-readable, and its format MAY differ between
    71  // SDK versions.
    72  func (v Version) String() string {
    73  	return EncodeToString(v)
    74  }
    75  
    76  // EncodeToString encodes version according to format from specification:
    77  // semver formatted value without patch and with v prefix, e.g. 'v2.1'.
    78  func EncodeToString(v Version) string {
    79  	return fmt.Sprintf("v%d.%d", v.Major(), v.Minor())
    80  }
    81  
    82  // Equal returns true if versions are identical.
    83  func (v Version) Equal(v2 Version) bool {
    84  	return v.Major() == v2.Major() &&
    85  		v.Minor() == v2.Minor()
    86  }