From 6fdb0f96323695f62fc7ff08ce1cabb657604809 Mon Sep 17 00:00:00 2001 From: st0nie Date: Tue, 14 Feb 2017 13:30:48 +0100 Subject: [PATCH] Modified "CreateQuery" Added anonymous struct support for "CreateQuery". It now accepts a optional variadic "class" parameter. So you can pass the wmi class name when your struct is anonymous and has no name or your struct is not named like the wmi class. --- wmi.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/wmi.go b/wmi.go index 5d0c7ab..736b7bc 100644 --- a/wmi.go +++ b/wmi.go @@ -437,7 +437,10 @@ func oleInt64(item *ole.IDispatch, prop string) (int64, error) { // CreateQuery returns a WQL query string that queries all columns of src. where // is an optional string that is appended to the query, to be used with WHERE // clauses. In such a case, the "WHERE" string should appear at the beginning. -func CreateQuery(src interface{}, where string) string { +// The wmi class is obtained by the name of the type. You can pass a optional +// class throught the variadic class parameter which is useful for anonymous +// structs. +func CreateQuery(src interface{}, where string, class ...string) string { var b bytes.Buffer b.WriteString("SELECT ") s := reflect.Indirect(reflect.ValueOf(src)) @@ -454,7 +457,11 @@ func CreateQuery(src interface{}, where string) string { } b.WriteString(strings.Join(fields, ", ")) b.WriteString(" FROM ") - b.WriteString(t.Name()) + if len(class) > 0{ + b.WriteString(class[0]) + } else { + b.WriteString(t.Name()) + } b.WriteString(" " + where) return b.String() }