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


Golang Value.SetString方法代码示例

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


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

示例1: setWithProperType

// This sets the value in a struct of an indeterminate type to the
// matching value from the request (via Form middleware) in the
// same type, so that not all deserialized values have to be strings.
// Supported types are string, int, float, and bool.
func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value, nameInTag string, errors Errors) {
	if scan, ok := structField.Addr().Interface().(sql.Scanner); ok {
		scan.Scan(val)
		return
	}
	switch valueKind {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		if val == "" {
			val = "0"
		}
		intVal, err := strconv.ParseInt(val, 10, 64)
		if err != nil {
			errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as integer")
		} else {
			structField.SetInt(intVal)
		}
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		if val == "" {
			val = "0"
		}
		uintVal, err := strconv.ParseUint(val, 10, 64)
		if err != nil {
			errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as unsigned integer")
		} else {
			structField.SetUint(uintVal)
		}
	case reflect.Bool:
		if val == "" {
			val = "false"
		}
		boolVal, err := strconv.ParseBool(val)
		if err != nil {
			errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as boolean")
		} else {
			structField.SetBool(boolVal)
		}
	case reflect.Float32:
		if val == "" {
			val = "0.0"
		}
		floatVal, err := strconv.ParseFloat(val, 32)
		if err != nil {
			errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as 32-bit float")
		} else {
			structField.SetFloat(floatVal)
		}
	case reflect.Float64:
		if val == "" {
			val = "0.0"
		}
		floatVal, err := strconv.ParseFloat(val, 64)
		if err != nil {
			errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as 64-bit float")
		} else {
			structField.SetFloat(floatVal)
		}
	case reflect.String:
		structField.SetString(val)
	}
}
开发者ID:Boomerangz,项目名称:binding,代码行数:64,代码来源:binding.go

示例2: parseAndSet

func parseAndSet(target reflect.Value, val string) error {
	if !target.CanSet() {
		return fmt.Errorf("Cannot set %v to %v", target, val)
	}
	switch kind := target.Type().Kind(); kind {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		intVal, err := strconv.ParseInt(val, 10, 64)
		if err == nil {
			target.SetInt(intVal)
		}
		return err
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		intVal, err := strconv.ParseUint(val, 10, 64)
		if err == nil {
			target.SetUint(intVal)
		}
		return err
	case reflect.Float32, reflect.Float64:
		floatVal, err := strconv.ParseFloat(val, 64)
		if err == nil {
			target.SetFloat(floatVal)
		}
		return err
	case reflect.String:
		target.SetString(val)
		return nil
	}
	return fmt.Errorf("Field %v has type %v, cannot set to %v", target, target.Type(), val)
}
开发者ID:antongulenko,项目名称:http-isolation-proxy,代码行数:29,代码来源:redis-store.go

示例3: setValue

func setValue(elmv *reflect.Value, f Field) {
	switch f.Kind {
	case "int", "int64", "long":
		itemValue, innerr := strconv.ParseInt(f.Value, 10, 64)
		if innerr != nil {
			itemValue = 0
		}
		elmv.SetInt(itemValue)
	case "float", "float64", "double":
		itemValue, innerr := strconv.ParseFloat(f.Value, 64)
		if innerr != nil {
			itemValue = 0
		}
		elmv.SetFloat(itemValue)
	case "bool":
		itemValue, innerr := strconv.ParseBool(f.Value)
		if innerr != nil {
			itemValue = false
		}
		elmv.SetBool(itemValue)
	default:
		itemValue := f.Value
		elmv.SetString(itemValue)
	}
}
开发者ID:foolin,项目名称:gocsv,代码行数:25,代码来源:gocsv.go

示例4: getField

// Fill a single reflected field if it has the conf tag.
//
func (c *Config) getField(elem reflect.Value, group, key string) {
	var e error
	switch elem.Interface().(type) {

	case bool:
		var val bool
		val, e = c.Bool(group, key)
		elem.SetBool(val)

	case int:
		var val int
		val, e = c.Int(group, key)
		elem.SetInt(int64(val))

	case string:
		var val string
		val, e = c.String(group, key)
		elem.SetString(val)

	case float64:
		var val float64
		val, e = c.Float(group, key)
		elem.SetFloat(val)

	default:
		c.logError(errors.New("Parse conf: wrong type: " + elem.Kind().String()))
	}
	if e != nil {
		c.logError(errors.New("Parse conf: " + e.Error()))
	}
}
开发者ID:jcswart,项目名称:godock,代码行数:33,代码来源:config.go

示例5: decodeString

func (d *Decoder) decodeString(s *string, v reflect.Value, fieldTag tag) error {
	if fieldTag.AsString {
		return d.decodeNumber(s, v)
	}

	// To maintain backwards compatibility with ConvertFrom family of methods which
	// converted strings to time.Time structs
	if _, ok := v.Interface().(time.Time); ok {
		t, err := time.Parse(time.RFC3339, *s)
		if err != nil {
			return err
		}
		v.Set(reflect.ValueOf(t))
		return nil
	}

	switch v.Kind() {
	case reflect.String:
		v.SetString(*s)
	case reflect.Interface:
		// Ensure type aliasing is handled properly
		v.Set(reflect.ValueOf(*s).Convert(v.Type()))
	default:
		return &UnmarshalTypeError{Value: "string", Type: v.Type()}
	}

	return nil
}
开发者ID:ColourboxDevelopment,项目名称:aws-sdk-go,代码行数:28,代码来源:decode.go

示例6: Unpack

func (f *Field) Unpack(buf []byte, val reflect.Value, length int, options *Options) error {
	typ := f.Type.Resolve(options)
	if typ == Pad || f.kind == reflect.String {
		if typ == Pad {
			return nil
		} else {
			val.SetString(string(buf))
			return nil
		}
	} else if f.Slice {
		if val.Cap() < length {
			val.Set(reflect.MakeSlice(val.Type(), length, length))
		} else if val.Len() < length {
			val.Set(val.Slice(0, length))
		}
		// special case byte slices for performance
		if !f.Array && typ == Uint8 && f.defType == Uint8 {
			copy(val.Bytes(), buf[:length])
			return nil
		}
		pos := 0
		size := typ.Size()
		for i := 0; i < length; i++ {
			if err := f.unpackVal(buf[pos:pos+size], val.Index(i), 1, options); err != nil {
				return err
			}
			pos += size
		}
		return nil
	} else {
		return f.unpackVal(buf, val, length, options)
	}
}
开发者ID:arunk-s,项目名称:libaudit-go,代码行数:33,代码来源:field.go

示例7: Marshal

func (defaultMarshaler) Marshal(v reflect.Value, s string) error {
	if v.Kind() == reflect.Slice {
		n := reflect.New(v.Type().Elem())
		m := valueMarshaler(n.Elem())
		err := m.Marshal(n.Elem(), s)
		if err != nil {
			return err
		}
		v.Set(reflect.Append(v, n.Elem()))
		return nil
	}
	switch v.Kind() {
	case reflect.Int:
		x, err := strconv.ParseInt(s, 0, 0)
		v.SetInt(x)
		return err
	case reflect.Uint:
		x, err := strconv.ParseUint(s, 0, 0)
		v.SetUint(x)
		return err
	case reflect.Int64:
		x, err := strconv.ParseInt(s, 0, 64)
		v.SetInt(x)
		return err
	case reflect.String:
		v.SetString(s)
		return nil
	default:
		return fmt.Errorf("unhandled builtin type: %s", v.Type().String())
	}
}
开发者ID:anacrolix,项目名称:tagflag,代码行数:31,代码来源:marshalers.go

示例8: setFieldDefaults

func setFieldDefaults(v reflect.Value, sf reflect.StructField, s reflect.Value) {
	if v.CanInterface() && reflect.DeepEqual(
		v.Interface(), reflect.Zero(v.Type()).Interface()) {
		tag := sf.Tag.Get("ebmldef")
		if tag != "" {
			switch v.Kind() {
			case reflect.Int, reflect.Int64:
				u, _ := strconv.ParseInt(tag, 10, 0)
				v.SetInt(int64(u))
			case reflect.Uint, reflect.Uint64:
				u, _ := strconv.ParseUint(tag, 10, 0)
				v.SetUint(u)
			case reflect.Float32, reflect.Float64:
				f, _ := strconv.ParseFloat(tag, 64)
				v.SetFloat(f)
			case reflect.String:
				v.SetString(tag)
			default:
				log.Panic("Unsupported default value")
			}
		}
		ltag := sf.Tag.Get("ebmldeflink")
		if ltag != "" {
			v.Set(s.FieldByName(ltag))
		}
	}
}
开发者ID:ebml-go,项目名称:ebml-go,代码行数:27,代码来源:decode.go

示例9: readField

func (e *Element) readField(v reflect.Value) (err error) {
	switch v.Kind() {
	case reflect.Struct:
		err = e.readStruct(v)
	case reflect.Slice:
		err = e.readSlice(v)
	case reflect.Array:
		for i, l := 0, v.Len(); i < l && err == nil; i++ {
			err = e.readStruct(v.Index(i))
		}
	case reflect.String:
		var s string
		s, err = e.readString()
		v.SetString(s)
	case reflect.Int, reflect.Int64:
		var u uint64
		u, err = e.readUint64()
		v.SetInt(int64(u))
	case reflect.Uint, reflect.Uint64:
		var u uint64
		u, err = e.readUint64()
		v.SetUint(u)
	case reflect.Float32, reflect.Float64:
		var f float64
		f, err = e.readFloat()
		v.SetFloat(f)
	default:
		err = errors.New("Unknown type: " + v.String())
	}
	return
}
开发者ID:ebml-go,项目名称:ebml-go,代码行数:31,代码来源:decode.go

示例10: setValue

// setValue updates the field value in fv to the data extracted from config
// with key.
func setValue(fv *reflect.Value, c *Config, key string) error {
	// Update the value with the correct type
	switch fv.Kind() {
	case reflect.Int:
		val, err := c.GetInt(key)
		if err != nil {
			return err
		}
		fv.SetInt(int64(val))
	case reflect.Float64:
		val, err := c.GetFloat(key)
		if err != nil {
			return err
		}
		fv.SetFloat(val)
	case reflect.Bool:
		val, err := c.GetBool(key)
		if err != nil {
			return err
		}
		fv.SetBool(val)
	case reflect.String:
		val, err := c.GetString(key)
		if err != nil {
			return err
		}
		fv.SetString(val)
	}

	return nil
}
开发者ID:walle,项目名称:cfg,代码行数:33,代码来源:decode.go

示例11: setWithProperType

// setWithProperType sets proper value to field based on its type,
// but it does not return error for failing parsing,
// because we want to use default value that is already assigned to strcut.
func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim string) error {
	switch t.Kind() {
	case reflect.String:
		if len(key.String()) == 0 {
			return nil
		}
		field.SetString(key.String())
	case reflect.Bool:
		boolVal, err := key.Bool()
		if err != nil {
			return nil
		}
		field.SetBool(boolVal)
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		durationVal, err := key.Duration()
		// Skip zero value
		if err == nil && int(durationVal) > 0 {
			field.Set(reflect.ValueOf(durationVal))
			return nil
		}

		intVal, err := key.Int64()
		if err != nil || intVal == 0 {
			return nil
		}
		field.SetInt(intVal)
	//	byte is an alias for uint8, so supporting uint8 breaks support for byte
	case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		durationVal, err := key.Duration()
		// Skip zero value
		if err == nil && int(durationVal) > 0 {
			field.Set(reflect.ValueOf(durationVal))
			return nil
		}

		uintVal, err := key.Uint64()
		if err != nil {
			return nil
		}
		field.SetUint(uintVal)

	case reflect.Float64:
		floatVal, err := key.Float64()
		if err != nil {
			return nil
		}
		field.SetFloat(floatVal)
	case reflectTime:
		timeVal, err := key.Time()
		if err != nil {
			return nil
		}
		field.Set(reflect.ValueOf(timeVal))
	case reflect.Slice:
		return setSliceWithProperType(key, field, delim)
	default:
		return fmt.Errorf("unsupported type '%s'", t)
	}
	return nil
}
开发者ID:stellar,项目名称:bridge-server,代码行数:63,代码来源:struct.go

示例12: shortString

// type(1) | name length(1) | content length(1) | raw name bytes |
// 0x00 | content bytes | 0x00
func (d *decodeState) shortString(v reflect.Value) {
	d.off += 1 // type

	klen := int(Int8(d.data[d.off:]))
	d.off += 1 // name length

	vlen := int(Int8(d.data[d.off:]))
	d.off += 1 // content length

	if klen > 0 {
		key := d.data[d.off : d.off+klen-1]
		d.off += klen // name and 0x00
		v = fieldByTag(v, key)
		u, pv := d.indirect(v, false)
		if u != nil {
			d.off -= 1 + 1 + 1 + klen
			if err := u.UnmarshalMCPACK(d.next()); err != nil {
				d.error(err)
			}
			return
		}
		v = pv
	}

	val := string(d.data[d.off : d.off+vlen-1])
	d.off += vlen // value and 0x00

	if !v.IsValid() {
		return
	}
	v.SetString(val)
}
开发者ID:qiangmzsx,项目名称:golang,代码行数:34,代码来源:decode.go

示例13: setConfigVal

func setConfigVal(str string, field reflect.Value) error {
	t := field.Kind()
	switch {
	case t == reflect.String:
		field.SetString(str)
	case t >= reflect.Int && t <= reflect.Int64:
		if x, err := strconv.ParseInt(str, 10, 64); err != nil {
			return err
		} else {
			field.SetInt(x)
		}
	case t >= reflect.Uint && t <= reflect.Uint64:
		if x, err := strconv.ParseUint(str, 10, 64); err != nil {
			return err
		} else {
			field.SetUint(x)
		}
	case t >= reflect.Float32 && t <= reflect.Float64:
		if x, err := strconv.ParseFloat(str, 64); err != nil {
			return err
		} else {
			field.SetFloat(x)
		}
	case t == reflect.Bool:
		if x, err := strconv.ParseBool(str); err != nil {
			return err
		} else {
			field.SetBool(x)
		}
	default:
		return nil
	}
	return nil
}
开发者ID:rhinoman,项目名称:wikifeat,代码行数:34,代码来源:config_etcd.go

示例14: decodeString

// decodeString decodes a string of size s into a v.
// If s is zero, the size of a string is read from a stream.
// The return value n indicates the number of bytes decoded.
func (d *Decoder) decodeString(s int, v reflect.Value) (n int, err error) {
	// Read the size if it is a zero
	if s == 0 {
		s, err = d.readSize()
		if err != nil {
			return 0, err
		}
		// We have successfully read one byte of size already
		n++
		if s == 0 {
			return n, nil
		}
	}

	// Decode data of size s
	data := make([]byte, s)
	if err := binary.Read(d.r, d.bo, data); err != nil {
		return n, err
	}
	n += s
	// Trim null bytes
	data = bytes.TrimRight(data, "\x00")
	// Set the string
	v.SetString(string(data))
	return n, nil
}
开发者ID:billyboar,项目名称:GCSolutions,代码行数:29,代码来源:decoder.go

示例15: setWithProperType

func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value) error {
	switch valueKind {
	case reflect.Int:
		return setIntField(val, 0, structField)
	case reflect.Int8:
		return setIntField(val, 8, structField)
	case reflect.Int16:
		return setIntField(val, 16, structField)
	case reflect.Int32:
		return setIntField(val, 32, structField)
	case reflect.Int64:
		return setIntField(val, 64, structField)
	case reflect.Uint:
		return setUintField(val, 0, structField)
	case reflect.Uint8:
		return setUintField(val, 8, structField)
	case reflect.Uint16:
		return setUintField(val, 16, structField)
	case reflect.Uint32:
		return setUintField(val, 32, structField)
	case reflect.Uint64:
		return setUintField(val, 64, structField)
	case reflect.Bool:
		return setBoolField(val, structField)
	case reflect.Float32:
		return setFloatField(val, 32, structField)
	case reflect.Float64:
		return setFloatField(val, 64, structField)
	case reflect.String:
		structField.SetString(val)
	default:
		return errors.New("Unknown type")
	}
	return nil
}
开发者ID:4Second2None,项目名称:gin,代码行数:35,代码来源:form_mapping.go


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