当前位置: 首页>>代码示例>>Golang>>正文


Golang Value.CanSet方法代码示例

本文整理汇总了Golang中reflect.Value.CanSet方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.CanSet方法的具体用法?Golang Value.CanSet怎么用?Golang Value.CanSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reflect.Value的用法示例。


在下文中一共展示了Value.CanSet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: structAuto

func (sw *TReader) structAuto(r *marshal.Reader, v reflect.Value) {
	if !v.CanSet() {
		sw.structFixed(r, v)
		return
	}
	l := r.IntUint32()
	flds := sw.Reader.Flds
	switch sw.Tail {
	/*case NoTail:
	if l < len(flds) {
		r.Err = fmt.Errorf("Wrong field count: expect %d, got %d", len(flds), l)
		return
	}*/
	case Tail, TailSplit:
		if l < len(flds)-1 {
			break
			/*r.Err = fmt.Errorf("Wrong field count: expect at least %d, got %d", len(flds)-1, l)
			return*/
		}
		tail := l - len(flds) + 1
		last := flds[len(flds)-1]
		if sw.Tail == TailSplit {
			llast := len(last.Flds)
			tail /= llast
		}
		last.TReader.SetCount(v.Field(last.I), tail)
	}
	sw.structRead(r, l, v)
}
开发者ID:moxian,项目名称:go-iproto,代码行数:29,代码来源:reader.go

示例2: AnySettableValue

//AnySettableValue find if exist one value that you can set.
func AnySettableValue(val reflect.Value) bool {
	switch val.Kind() {
	case reflect.Array:
		for i := 0; i < val.Type().Len(); i++ {
			if AnySettableValue(val.Index(i)) {
				return true
			}
		}
	case reflect.Interface:
		return AnySettableValue(val.Elem())
	case reflect.Map:
		for _, key := range val.MapKeys() {
			if AnySettableValue(val.MapIndex(key)) {
				return true
			}
		}
	case reflect.Ptr:
		return AnySettableValue(val.Elem())
	case reflect.Slice:
		for i := 0; i < val.Len(); i++ {
			if AnySettableValue(val.Index(i)) {
				return true
			}
		}
	case reflect.Struct:
		for i := 0; i < val.Type().NumField(); i++ {
			if AnySettableValue(val.Field(i)) {
				return true
			}
		}
	default:
		return val.CanSet()
	}
	return false
}
开发者ID:fcavani,项目名称:monlite,代码行数:36,代码来源:types.go

示例3: indirect

func (d *decodeState) indirect(v reflect.Value, decodingNull bool) reflect.Value {
	if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
		v = v.Addr()
	}
	for {
		if v.Kind() == reflect.Interface && !v.IsNil() {
			e := v.Elem()
			if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
				v = e
				continue
			}
		}

		if v.Kind() != reflect.Ptr {
			break
		}

		if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
			break
		}
		if v.IsNil() {
			v.Set(reflect.New(v.Type().Elem()))
		}
		v = v.Elem()
	}
	return v
}
开发者ID:NorgannasAddOns,项目名称:go-anyproto,代码行数:27,代码来源:decode.go

示例4: decodeValue

func (q *queryDecoder) decodeValue(output reflect.Value, prefix string, tag reflect.StructTag) error {
	if !q.containsPrefix(prefix) {
		return nil
	}

	if output.Kind() == reflect.Ptr {
		if output.IsNil() {
			output.Set(reflect.New(output.Type().Elem()))
		}
	}
	output = elemOf(output)

	if !output.CanSet() {
		panic("can't set " + prefix)
	}

	switch getFieldType(tag, output.Kind()) {
	case "structure":
		return q.decodeStruct(output, prefix)
	case "list":
		return q.decodeList(output, prefix, tag)
	case "map":
		return q.decodeMap(output, prefix, tag)
	default:
		return q.decodeScalar(output, prefix)
	}
}
开发者ID:rosenhouse,项目名称:awsfaker,代码行数:27,代码来源:decode.go

示例5: tryCustom

// tryCustom searches for custom handlers, and returns true iff it finds a match
// and successfully randomizes v.
func (f Fuzzer) tryCustom(v reflect.Value) bool {
	doCustom, ok := f.customFuzz[v.Type()]
	if !ok {
		return false
	}

	switch v.Kind() {
	case reflect.Ptr:
		if v.IsNil() {
			if !v.CanSet() {
				return false
			}
			v.Set(reflect.New(v.Type().Elem()))
		}
	case reflect.Map:
		if v.IsNil() {
			if !v.CanSet() {
				return false
			}
			v.Set(reflect.MakeMap(v.Type()))
		}
	default:
		return false
	}

	doCustom(v)
	return true
}
开发者ID:nyaxt,项目名称:kubernetes,代码行数:30,代码来源:fuzz.go

示例6: setUint

func setUint(rv reflect.Value, u uint64) error {
	switch rv.Kind() {
	case reflect.Ptr:
		if rv.IsNil() {
			if rv.CanSet() {
				rv.Set(reflect.New(rv.Type().Elem()))
				// fall through to set indirect below
			} else {
				return fmt.Errorf("trying to put uint into unsettable nil ptr")
			}
		}
		return setUint(reflect.Indirect(rv), u)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		if rv.OverflowUint(u) {
			return fmt.Errorf("value %d does not fit into target of type %s", u, rv.Kind().String())
		}
		rv.SetUint(u)
		return nil
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		if (u == 0xffffffffffffffff) || rv.OverflowInt(int64(u)) {
			return fmt.Errorf("value %d does not fit into target of type %s", u, rv.Kind().String())
		}
		rv.SetInt(int64(u))
		return nil
	case reflect.Interface:
		rv.Set(reflect.ValueOf(u))
		return nil
	default:
		return fmt.Errorf("cannot assign uint into Kind=%s Type=%#v %#v", rv.Kind().String(), rv.Type(), rv)
	}
}
开发者ID:semantic-machines,项目名称:veda,代码行数:31,代码来源:cbor.go

示例7: callCustom

// callCustom calls 'custom' with sv & dv. custom must be a conversion function.
func (c *Converter) callCustom(sv, dv, custom reflect.Value, scope *scope) error {
	if !sv.CanAddr() {
		sv2 := reflect.New(sv.Type())
		sv2.Elem().Set(sv)
		sv = sv2
	} else {
		sv = sv.Addr()
	}
	if !dv.CanAddr() {
		if !dv.CanSet() {
			return scope.errorf("can't addr or set dest.")
		}
		dvOrig := dv
		dv := reflect.New(dvOrig.Type())
		defer func() { dvOrig.Set(dv) }()
	} else {
		dv = dv.Addr()
	}
	args := []reflect.Value{sv, dv, reflect.ValueOf(scope)}
	ret := custom.Call(args)[0].Interface()
	// This convolution is necessary because nil interfaces won't convert
	// to errors.
	if ret == nil {
		return nil
	}
	return ret.(error)
}
开发者ID:MohamedFAhmed,项目名称:heapster,代码行数:28,代码来源:converter.go

示例8: isFieldModifiable

// Checks if field can be modified and
// field kind matches give kind.
func isFieldModifiable(field *reflect.Value, name string, kind reflect.Kind) (err error) {
	if !field.IsValid() {
		return fmt.Errorf("error, field not valid: %s", name)
	}
	if !field.CanSet() {
		return fmt.Errorf("error, field cannot be set: %s", name)
	}

	if field.Kind() != kind {
		switch kind {
		case reflect.String:
			return fmt.Errorf("error, field must be string: %s", name)

		case reflect.Slice:
			return fmt.Errorf("error, field must be slice: %s", name)

		case reflect.Map:
			return fmt.Errorf("error, field must be map: %s", name)

		default:
			return fmt.Errorf("error, not yet supported type for field: %s", name)
		}
	}
	return
}
开发者ID:hoffoo,项目名称:skini,代码行数:27,代码来源:reflector.go

示例9: setValueWithType

func setValueWithType(field *reflect.Value, val string) error {
	if !field.CanSet() {
		return errored.Errorf("Cannot set value %q for struct element %q", val, field.Kind().String())
	}

	// navigate the kinds using the reflect types. fallthrough until we can get
	// at a convertible type. If nothing is applicable, error out.
	switch field.Kind() {
	case reflect.Int:
		fallthrough
	case reflect.Int32:
		return castInt32(field, val)
	case reflect.Int64:
		return castInt64(field, val)
	case reflect.Uint:
		fallthrough
	case reflect.Uint32:
		return castUint32(field, val)
	case reflect.Uint64:
		return castUint64(field, val)
	case reflect.Bool:
		return castBool(field, val)
	case reflect.Ptr:
		return castPtr(field, val)
	case reflect.String:
		return castString(field, val)
	}

	return errored.Errorf("Could not find appropriate type %q", field.Kind().String())
}
开发者ID:contiv,项目名称:volplugin,代码行数:30,代码来源:merge.go

示例10: SaveByPath

func (t getElemByStringEditorabler) SaveByPath(v *reflect.Value, path Path, value string) (err error) {
	if len(path) == 0 {
		return nil
	}
	ev, et, err := t.GetElemByString(*v, path[0])
	if err != nil {
		return err
	}
	oEv := ev
	err = et.SaveByPath(&oEv, path[1:], value)
	if err != nil {
		return err
	}

	//not change this type
	if oEv == ev {
		return nil
	}
	if v.CanSet() {
		return nil
	}
	output := reflect.New(t.GetReflectType()).Elem()
	output.Set(*v)
	*v = output
	ev, _, err = t.GetElemByString(*v, path[0])
	if err != nil {
		return err
	}
	ev.Set(oEv)
	return nil
}
开发者ID:iizotop,项目名称:kmg,代码行数:31,代码来源:util.go

示例11: setValue

// setValue decodes a YAML value from node into the value field,
// filtering out the fields not listed in the supportedYamlTags map.
func setValue(node yaml.Node, fieldType reflect.Type, field reflect.Value, supportedYamlTags map[string]bool) error {
	if !field.CanSet() {
		return fmt.Errorf("Unable to set the value [%v] for the field [%v] of kind [%v]", node, field, field.Kind())
	}

	// invoke custom unmarshaler if implemented, else proceed with best-effort parsing using reflections
	if field.CanAddr() {
		if u, ok := field.Addr().Interface().(Unmarshaler); ok {
			return u.UnmarshalYAML(func(val interface{}) error {
				return unmarshal(node, val, supportedYamlTags)
			})
		}
	}

	switch field.Kind() {
	case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64:
		return unmarshalScalar(node, field, supportedYamlTags)
	case reflect.Slice:
		return unmarshalSlice(node, field, supportedYamlTags)
	case reflect.Struct:
		return unmarshalStruct(node, fieldType, field, supportedYamlTags)
	case reflect.Map:
		return unmarshalMap(node, field, supportedYamlTags)
	case reflect.Ptr:
		fieldValue := reflect.New(fieldType.Elem())
		if err := setValue(node, fieldType.Elem(), fieldValue.Elem(), supportedYamlTags); err != nil {
			return err
		}
		field.Set(fieldValue)
		return nil
	// TODO, case reflect.Interface:
	default:
		return fmt.Errorf("Cannot setValue for the field [%v] of kind [%v]. Undefined operation.", field, field.Kind())
	}
}
开发者ID:jloper3,项目名称:amazon-ecs-cli,代码行数:37,代码来源:yaml.go

示例12: passThougthDeleteByPath

//can not pass through map
func passThougthDeleteByPath(t GetElemByStringAndReflectTypeGetterInterface, v *reflect.Value, path Path) (err error) {
	ev, et, err := t.GetElemByString(*v, path[0])
	if err != nil {
		return err
	}
	oEv := ev
	err = et.DeleteByPath(&oEv, path[1:])
	if err != nil {
		return err
	}
	if oEv == ev {
		return
	}
	if v.CanSet() {
		return
	}
	output := reflect.New(t.GetReflectType()).Elem()
	output.Set(*v)
	*v = output
	ev, _, err = t.GetElemByString(*v, path[0])
	if err != nil {
		return err
	}
	ev.Set(oEv)
	return nil
}
开发者ID:iizotop,项目名称:kmg,代码行数:27,代码来源:util.go

示例13: unmarshalValue

func unmarshalValue(value reflect.Value, settable reflect.Value) error {
	if !settable.CanSet() {
		return fmt.Errorf("Value is not settable")
	}

	if settable.Kind() == reflect.Ptr {
		// If a nil pointer, allocate a new object to unmarshal into
		if settable.IsNil() {
			newPtr := reflect.New(settable.Type().Elem())
			settable.Set(newPtr)
		}
		// We want unmarshal into the pointed value
		settable = settable.Elem()
	}

	val := value.Interface()

	switch value.Kind() {
	case reflect.Float64:
		return unmarshalNumber(val.(float64), settable)
	case reflect.Bool:
		return unmarshalBoolean(val.(bool), settable)
	case reflect.String:
		return unmarshalString(val.(string), settable)
	case reflect.Map:
		return unmarshalStruct(val.(map[string]interface{}), settable)
	case reflect.Slice:
		return unmarshalSlice(val.([]interface{}), settable)
	default:
		panic(fmt.Sprintf("Unexpected unmarshal type: %s", value.Kind()))
	}
}
开发者ID:hamishm,项目名称:component-tech,代码行数:32,代码来源:strict_json.go

示例14: indirect

// indirect walks down v allocating pointers as needed,
// until it gets to a non-pointer.
// if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
func (d *decodeState) indirect(v reflect.Value, decodingNull bool) reflect.Value {
	// If v is a named type and is addressable,
	// start with its address, so that if the type has pointer methods,
	// we find them.
	if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
		v = v.Addr()
	}
	for {
		// Load value from interface, but only if the result will be
		// usefully addressable.
		if v.Kind() == reflect.Interface && !v.IsNil() {
			e := v.Elem()
			if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
				v = e
				continue
			}
		}

		if v.Kind() != reflect.Ptr {
			break
		}

		if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
			break
		}
		if v.IsNil() {
			v.Set(reflect.New(v.Type().Elem()))
		}
		v = v.Elem()
	}
	return v
}
开发者ID:jagregory,项目名称:pq,代码行数:35,代码来源:decode.go

示例15: setSlice

// parse a value as the appropriate type and store it in the struct
func setSlice(dest reflect.Value, values []string) error {
	if !dest.CanSet() {
		return fmt.Errorf("field is not writable")
	}

	var ptr bool
	elem := dest.Type().Elem()
	if elem.Kind() == reflect.Ptr {
		ptr = true
		elem = elem.Elem()
	}

	// Truncate the dest slice in case default values exist
	if !dest.IsNil() {
		dest.SetLen(0)
	}

	for _, s := range values {
		v := reflect.New(elem)
		if err := setScalar(v.Elem(), s); err != nil {
			return err
		}
		if !ptr {
			v = v.Elem()
		}
		dest.Set(reflect.Append(dest, v))
	}
	return nil
}
开发者ID:alexflint,项目名称:go-arg,代码行数:30,代码来源:parse.go


注:本文中的reflect.Value.CanSet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。