예제 #1
0
파일: setvariable.go 프로젝트: pw1/conf
func getVariablePtrValue(sectionValue reflect.Value, section, varName string) (reflect.Value, error) {
	// Get a reference to the name-value-pair within the section
	nvPairValue := sectionValue.FieldByNameFunc(func(fieldName string) bool {
		return (strings.ToLower(fieldName) == varName)
	})
	if !nvPairValue.IsValid() {
		return reflect.Value{}, NewParseError("Unknown variable name " + varName +
			" in section " + section)
	}

	if nvPairValue.Kind() == reflect.Ptr {
		// The name-value-pair is a pointer. So we first need to make sure the object is allocated.
		if !nvPairValue.Elem().IsValid() {
			// The object is not yet allocated, so we allocate it now.
			newObj := reflect.New(nvPairValue.Type().Elem())
			nvPairValue.Set(newObj)
		}

		// Return the pointer to the newly created object
		return nvPairValue, nil
	} else {
		// The object is already allocated, so we can just return the pointer to this object.
		return nvPairValue.Addr(), nil
	}

}
예제 #2
0
파일: search.go 프로젝트: kidaa/lucy
func (obj *HitsIMP) Next(hit interface{}) bool {
	self := ((*C.lucy_Hits)(unsafe.Pointer(obj.TOPTR())))
	// TODO: accept a HitDoc object and populate score.

	// Get reflection value and type for the supplied struct.
	var hitValue reflect.Value
	if reflect.ValueOf(hit).Kind() == reflect.Ptr {
		temp := reflect.ValueOf(hit).Elem()
		if temp.Kind() == reflect.Struct {
			if temp.CanSet() {
				hitValue = temp
			}
		}
	}
	if hitValue == (reflect.Value{}) {
		mess := fmt.Sprintf("Arg not writeable struct pointer: %v",
			reflect.TypeOf(hit))
		obj.err = clownfish.NewErr(mess)
		return false
	}

	var docC *C.lucy_HitDoc
	errCallingNext := clownfish.TrapErr(func() {
		docC = C.LUCY_Hits_Next(self)
	})
	if errCallingNext != nil {
		obj.err = errCallingNext
		return false
	}
	if docC == nil {
		return false
	}
	defer C.cfish_dec_refcount(unsafe.Pointer(docC))

	fields := (*C.cfish_Hash)(unsafe.Pointer(C.LUCY_HitDoc_Get_Fields(docC)))
	iterator := C.cfish_HashIter_new(fields)
	defer C.cfish_dec_refcount(unsafe.Pointer(iterator))
	for C.CFISH_HashIter_Next(iterator) {
		keyC := C.CFISH_HashIter_Get_Key(iterator)
		valC := C.CFISH_HashIter_Get_Value(iterator)
		key := clownfish.CFStringToGo(unsafe.Pointer(keyC))
		val := clownfish.CFStringToGo(unsafe.Pointer(valC))
		match := func(name string) bool {
			return strings.EqualFold(key, name)
		}
		structField := hitValue.FieldByNameFunc(match)
		if structField != (reflect.Value{}) {
			structField.SetString(val)
		}
	}
	return true
}
예제 #3
0
func buildValues(columns []string, values []interface{}, data []interface{}, dummy interface{}) bool {
	n := len(data)
	var v reflect.Value
	var instruct bool

	if n == 1 {
		v = reflect.Indirect(reflect.ValueOf(data[0]))
		instruct = v.Kind() == reflect.Struct
	}
	if instruct {
		for i, cname := range columns {
			f := v.FieldByNameFunc(func(s string) bool {
				return strings.ToLower(s) == strings.ToLower(cname)
			})
			if f.IsValid() && f.CanSet() {
				values[i] = f.Addr().Interface()
			} else {
				values[i] = dummy
			}
		}
		return true
	}

	if true {
		for i := range values {
			if i < n {
				values[i] = data[i]
			} else {
				values[i] = dummy
			}
		}
		return true
	}

	return false
}
예제 #4
0
파일: setvariable.go 프로젝트: pw1/conf
func getSectionValue(configValue reflect.Value, section string) (reflect.Value, error) {
	// Get a reference to the current section struct
	sectionValue := configValue.FieldByNameFunc(func(name string) bool {
		return (strings.ToLower(name) == section)
	})
	if !sectionValue.IsValid() {
		return reflect.Value{}, NewParseError("Unknown section: " + section)
	}

	if sectionValue.Kind() == reflect.Ptr {
		// The section value is a pointer. So we first need to make sure the object is allocated.
		if !sectionValue.Elem().IsValid() {
			// The object is not yet allocated, so we allocate it now.
			newObj := reflect.New(sectionValue.Type().Elem())
			sectionValue.Set(newObj)
		}

		// sectionValue is now pointer to the actual object. We use Elem() to get the object to
		// which it points.
		sectionValue = sectionValue.Elem()
	}

	return sectionValue, nil
}
예제 #5
0
파일: set.go 프로젝트: 0987363/zfswatcher
func fieldFold(v reflect.Value, name string) reflect.Value {
	n := strings.Replace(name, "-", "_", -1)
	return v.FieldByNameFunc(func(fieldName string) bool {
		return strings.EqualFold(n, fieldName)
	})
}