Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow missing and null pointer fields to be nil. #6

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions swbemservices.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package wmi
Expand Down
7 changes: 4 additions & 3 deletions swbemservices_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package wmi
Expand Down Expand Up @@ -103,8 +104,8 @@ func WbemGetMemoryUsageMB(s *SWbemServices) (float64, float64, float64) {
//Run all benchmarks (should run for at least 60s to get a stable number):
//go test -run=NONE -bench=Version -benchtime=120s

//Individual benchmarks:
//go test -run=NONE -bench=NewVersion -benchtime=120s
// Individual benchmarks:
// go test -run=NONE -bench=NewVersion -benchtime=120s
func BenchmarkNewVersion(b *testing.B) {
s, err := InitializeSWbemServices(DefaultClient)
if err != nil {
Expand All @@ -128,7 +129,7 @@ func BenchmarkNewVersion(b *testing.B) {
}
}

//go test -run=NONE -bench=OldVersion -benchtime=120s
// go test -run=NONE -bench=OldVersion -benchtime=120s
func BenchmarkOldVersion(b *testing.B) {
var dst []Win32_OperatingSystem
q := CreateQuery(&dst, "")
Expand Down
13 changes: 7 additions & 6 deletions wmi.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

/*
Expand All @@ -20,7 +21,6 @@ Example code to print names of running processes:
println(i, v.Name)
}
}

*/
package wmi

Expand Down Expand Up @@ -338,11 +338,6 @@ func (c *Client) loadEntity(dst interface{}, src *ole.IDispatch) (errFieldMismat
f := v.Field(i)
of := f
isPtr := f.Kind() == reflect.Ptr
if isPtr {
ptr := reflect.New(f.Type().Elem())
f.Set(ptr)
f = f.Elem()
}
n := v.Type().Field(i).Name
if n[0] < 'A' || n[0] > 'Z' {
continue
Expand All @@ -367,6 +362,12 @@ func (c *Client) loadEntity(dst interface{}, src *ole.IDispatch) (errFieldMismat
}
defer prop.Clear()

if isPtr && !(c.PtrNil && prop.VT == 0x1) {
ptr := reflect.New(f.Type().Elem())
f.Set(ptr)
f = f.Elem()
}

if prop.VT == 0x1 { //VT_NULL
continue
}
Expand Down
82 changes: 82 additions & 0 deletions wmi_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package wmi
Expand Down Expand Up @@ -39,6 +40,87 @@ func TestFieldMismatch(t *testing.T) {
}
}

func TestMissingFields(t *testing.T) {
type s struct {
Name string
Missing uint32
MissingPointer *uint32
}

var dst []s

client := &Client{
AllowMissingFields: true,
}
err := client.Query("SELECT Name FROM Win32_Process", &dst)
if err != nil {
t.Fatal(err)
}
for i := range dst {
if dst[i].Missing != 0 {
t.Fatal("Expected Missing field to be 0")
}
if dst[i].MissingPointer != nil {
t.Fatal("Expected MissingPointer field to be nil")
}
}

// NonePtrZero and PtrNil should only affect the behavior of fields that
// exist as result properties, not missing fields.
client = &Client{
NonePtrZero: true,
PtrNil: true,
AllowMissingFields: true,
}
dst = []s{}
err = client.Query("SELECT Name FROM Win32_Process", &dst)
if err != nil {
t.Fatal(err)
}
for i := range dst {
if dst[i].Missing != 0 {
t.Fatal("Expected Missing field to be 0")
}
if dst[i].MissingPointer != nil {
t.Fatal("Expected MissingPointer field to be nil")
}
}
}

func TestNullPointerField(t *testing.T) {
type s struct {
Name string
Status *string
}

var dst []s

client := &Client{}
err := client.Query("SELECT Name, Status FROM Win32_Process WHERE Status IS NULL", &dst)
if err != nil {
t.Fatal(err)
}
for i := range dst {
if dst[i].Status == nil {
t.Fatal("Expected Status field to not be nil")
}
}

client = &Client{
PtrNil: true,
}
dst = []s{}
err = client.Query("SELECT Name, Status FROM Win32_Process WHERE Status IS NULL", &dst)
if err != nil {
t.Fatal(err)
}
for i := range dst {
if dst[i].Status != nil {
t.Fatal("Expected Status field to be nil")
}
}
}

func TestStrings(t *testing.T) {
printed := false
f := func() {
Expand Down