当前位置: 首页>>代码示例>>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;未经允许,请勿转载。