Skip to content

Commit

Permalink
go/common/version: Expose Less function
Browse files Browse the repository at this point in the history
  • Loading branch information
martintomazic committed Dec 17, 2024
1 parent 0753184 commit a7d5fbd
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions go/common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ func FromU64(v uint64) Version {
}
}

// compare compares the current Version with another.
// Returns:
// - Positive int if the current version is greater than the other.
// - Zero if the versions are equal.
// - Negative int if the current version is less than the other.
func (v Version) compare(other Version) int {
if diff := v.Major - other.Major; diff != 0 {
return int(diff)
}
if diff := v.Minor - other.Minor; diff != 0 {
return int(diff)
}
return int(v.Patch - other.Patch)
}

// Less checks if the current Version is strictly less than another Version.
func (v Version) Less(other Version) bool {
return v.compare(other) < 0
}

// String returns the protocol version as a string.
func (v Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
Expand Down

0 comments on commit a7d5fbd

Please sign in to comment.