Skip to content

Commit

Permalink
osversion: implement stringer interface, deprecate ToString() (#1547)
Browse files Browse the repository at this point in the history
This allows the type to be used with fm.Sprintf() and similar uses.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah authored Feb 24, 2023
1 parent b9dbd86 commit 276c435
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
13 changes: 10 additions & 3 deletions osversion/osversion_windows.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build windows

package osversion

import (
Expand Down Expand Up @@ -47,6 +45,15 @@ func Build() uint16 {
return Get().Build
}

func (osv OSVersion) ToString() string {
// String returns the OSVersion formatted as a string. It implements the
// [fmt.Stringer] interface.
func (osv OSVersion) String() string {
return fmt.Sprintf("%d.%d.%d", osv.MajorVersion, osv.MinorVersion, osv.Build)
}

// ToString returns the OSVersion formatted as a string.
//
// Deprecated: use [OSVersion.String].
func (osv OSVersion) ToString() string {
return osv.String()
}
20 changes: 20 additions & 0 deletions osversion/osversion_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package osversion

import (
"fmt"
"testing"
)

func TestOSVersionString(t *testing.T) {
v := OSVersion{
Version: 809042555,
MajorVersion: 123,
MinorVersion: 2,
Build: 12345,
}
expected := "the version is: 123.2.12345"
actual := fmt.Sprintf("the version is: %s", v)
if actual != expected {
t.Errorf("expected: %q, got: %q", expected, actual)
}
}

0 comments on commit 276c435

Please sign in to comment.