// PopulateStruct populates the fields of the supplied struct
// with values received form a Result. Care must be taken when
// declaring the struct. It must match the types returned by WMI.
PopulateStruct populates the fields of the supplied struct
with values received form a Result. Care must be taken when
declaring the struct. It must match the types returned by WMI.
// PopulateStruct populates the fields of the supplied struct // with values received form a Result . Care must be taken when // declaring the struct . It must match the types returned by WMI .
func PopulateStruct(j *Result, s interface{}) (err error) {
	var name string
	var fieldType interface{}
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("Invalid field type (%T) for %s: %s", fieldType, name, r)
		}
	}()
	valuePtr := reflect.ValueOf(s)
	elem := valuePtr.Elem()
	typeOfElem := elem.Type()
	for i := 0; i < elem.NumField(); i++ {
		field := elem.Field(i)
		if typeOfElem.Field(i).Tag.Get("tag") == "ignore" {
			continue
		}
		name = typeOfElem.Field(i).Name
		fieldType = field.Interface()
		res, err := j.GetProperty(name)
		if err != nil {
			return fmt.Errorf("Failed to get property %s: %s", name, err)
		}
		wmiFieldValue := res.Value()
		if wmiFieldValue == nil {
			continue
		}
		var fieldValue interface{}
		switch field.Interface().(type) {
		case []uint16:
			if c := res.ToArray(); c != nil {
				val := c.ToValueArray()
				asString := make([]uint16, len(val))
				for k, v := range val {
					asString[k] = v.(uint16)
				}
				fieldValue = asString
			}
		case []string:
			if c := res.ToArray(); c != nil {
				val := c.ToValueArray()
				asString := make([]string, len(val))
				for k, v := range val {
					asString[k] = v.(string)
				}
				fieldValue = asString
			}
		case []uint32:
			if c := res.ToArray(); c != nil {
				val := c.ToValueArray()
				asString := make([]uint32, len(val))
				for k, v := range val {
					asString[k] = v.(uint32)
				}
				fieldValue = asString
			}
		case []int32:
			if c := res.ToArray(); c != nil {
				val := c.ToValueArray()
				asString := make([]int32, len(val))
				for k, v := range val {
					asString[k] = v.(int32)
				}
				fieldValue = asString
			}
		case []int64:
			if c := res.ToArray(); c != nil {
				val := c.ToValueArray()
				asString := make([]int64, len(val))
				for k, v := range val {
					asString[k] = v.(int64)
				}
				fieldValue = asString
			}
		default:
			fieldValue = wmiFieldValue
		}
		v := reflect.ValueOf(fieldValue)
		if v.Kind() != field.Kind() {
			return fmt.Errorf("Invalid type returned by query for field %s (%v): %v", name, v.Kind(), field.Kind())
		}
		if field.CanSet() {
			field.Set(v)
		}
	}
	return nil
}
func PopulateStruct ( j * Result , s interface { } ) ( err error ) { var name string var fieldType interface { } defer func ( ) { if r := recover ( ) ; r != nil { err = fmt . Errorf ( " " , fieldType , name , r ) } } ( ) valuePtr := reflect . ValueOf ( s ) elem := valuePtr . Elem ( ) typeOfElem := elem . Type ( ) for i := 0 ; i < elem . NumField ( ) ; i ++ { field := elem . Field ( i ) if typeOfElem . Field ( i ) . Tag . Get ( " " ) == " " { continue } name = typeOfElem . Field ( i ) . Name fieldType = field . Interface ( ) res , err := j . GetProperty ( name ) if err != nil { return fmt . Errorf ( " " , name , err ) } wmiFieldValue := res . Value ( ) if wmiFieldValue == nil { continue } var fieldValue interface { } switch field . Interface ( ) . ( type ) { case [ ] uint16 : if c := res . ToArray ( ) ; c != nil { val := c . ToValueArray ( ) asString := make ( [ ] uint16 , len ( val ) ) for k , v := range val { asString [ k ] = v . ( uint16 ) } fieldValue = asString } case [ ] string : if c := res . ToArray ( ) ; c != nil { val := c . ToValueArray ( ) asString := make ( [ ] string , len ( val ) ) for k , v := range val { asString [ k ] = v . ( string ) } fieldValue = asString } case [ ] uint32 : if c := res . ToArray ( ) ; c != nil { val := c . ToValueArray ( ) asString := make ( [ ] uint32 , len ( val ) ) for k , v := range val { asString [ k ] = v . ( uint32 ) } fieldValue = asString } case [ ] int32 : if c := res . ToArray ( ) ; c != nil { val := c . ToValueArray ( ) asString := make ( [ ] int32 , len ( val ) ) for k , v := range val { asString [ k ] = v . ( int32 ) } fieldValue = asString } case [ ] int64 : if c := res . ToArray ( ) ; c != nil { val := c . ToValueArray ( ) asString := make ( [ ] int64 , len ( val ) ) for k , v := range val { asString [ k ] = v . ( int64 ) } fieldValue = asString } default : fieldValue = wmiFieldValue } v := reflect . ValueOf ( fieldValue ) if v . Kind ( ) != field . Kind ( ) { return fmt . Errorf ( " " , name , v . Kind ( ) , field . Kind ( ) ) } if field . CanSet ( ) { field . Set ( v ) } } return nil }
PopulateStruct populates the fields of the supplied struct
with values received form a Result.
PopulateStruct populates the fields of the supplied struct with values received form a Result .