Skip to content

Commit

Permalink
Error on non existent field
Browse files Browse the repository at this point in the history
This attempts to duplicate the datastore API: this error is non fatal, and
processing will continue. It is up to the caller to decide what to do.
  • Loading branch information
maddyblue committed Dec 9, 2013
1 parent 871fa51 commit c3df812
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions wmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (e *ErrFieldMismatch) Error() string {
var timeType = reflect.TypeOf(time.Time{})

// loadEntity loads a SWbemObject into a struct pointer.
func loadEntity(dst interface{}, src *ole.IDispatch) error {
func loadEntity(dst interface{}, src *ole.IDispatch) (errFieldMismatch error) {
v := reflect.ValueOf(dst).Elem()
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
Expand All @@ -135,6 +135,11 @@ func loadEntity(dst interface{}, src *ole.IDispatch) error {
}
prop, err := oleutil.GetProperty(src, n)
if err != nil {
errFieldMismatch = &ErrFieldMismatch{
StructType: f.Type(),
FieldName: n,
Reason: "no such struct field",
}
continue
}
switch val := prop.Value(); reflect.ValueOf(val).Kind() {
Expand Down Expand Up @@ -195,7 +200,7 @@ func loadEntity(dst interface{}, src *ole.IDispatch) error {
}
}
}
return nil
return errFieldMismatch
}

type multiArgType int
Expand Down
13 changes: 13 additions & 0 deletions wmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,16 @@ func TestQuery(t *testing.T) {
t.Fatal(err)
}
}

func TestFieldMismatch(t *testing.T) {
type s struct {
Name string
HandleCount uint32
Blah uint32
}
var dst[]s
err := Query("SELECT Name, HandleCount FROM Win32_Process", &dst)
if err == nil || err.Error() != `wmi: cannot load field "Blah" into a "uint32": no such struct field` {
t.Error("Expected err field mismatch")
}
}

0 comments on commit c3df812

Please sign in to comment.