當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Value.FieldByNameFunc方法代碼示例

本文整理匯總了Golang中reflect.Value.FieldByNameFunc方法的典型用法代碼示例。如果您正苦於以下問題:Golang Value.FieldByNameFunc方法的具體用法?Golang Value.FieldByNameFunc怎麽用?Golang Value.FieldByNameFunc使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在reflect.Value的用法示例。


在下文中一共展示了Value.FieldByNameFunc方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: getVariablePtrValue

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
	}

}
開發者ID:pw1,項目名稱:conf,代碼行數:26,代碼來源:setvariable.go

示例2: Next

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
}
開發者ID:kidaa,項目名稱:lucy,代碼行數:52,代碼來源:search.go

示例3: buildValues

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
}
開發者ID:princeofdatamining,項目名稱:golib,代碼行數:36,代碼來源:scan.go

示例4: getSectionValue

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
}
開發者ID:pw1,項目名稱:conf,代碼行數:24,代碼來源:setvariable.go

示例5: fieldFold

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)
	})
}
開發者ID:0987363,項目名稱:zfswatcher,代碼行數:6,代碼來源:set.go


注:本文中的reflect.Value.FieldByNameFunc方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。