Beispiel #1
0
func processField(value string, field reflect.Value) error {
	typ := field.Type()

	decoder := decoderFrom(field)
	if decoder != nil {
		return decoder.Decode(value)
	}

	switch typ.Kind() {
	case reflect.String:
		field.SetString(value)
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		var (
			val int64
			err error
		)
		if field.Kind() == reflect.Int64 && typ.PkgPath() == "time" && typ.Name() == "Duration" {
			var d time.Duration
			d, err = time.ParseDuration(value)
			val = int64(d)
		} else {
			val, err = strconv.ParseInt(value, 0, typ.Bits())
		}
		if err != nil {
			return err
		}

		field.SetInt(val)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		val, err := strconv.ParseUint(value, 0, typ.Bits())
		if err != nil {
			return err
		}
		field.SetUint(val)
	case reflect.Bool:
		val, err := strconv.ParseBool(value)
		if err != nil {
			return err
		}
		field.SetBool(val)
	case reflect.Float32, reflect.Float64:
		val, err := strconv.ParseFloat(value, typ.Bits())
		if err != nil {
			return err
		}
		field.SetFloat(val)
	case reflect.Slice:
		vals := strings.Split(value, ",")
		sl := reflect.MakeSlice(typ, len(vals), len(vals))
		for i, val := range vals {
			err := processField(val, sl.Index(i))
			if err != nil {
				return err
			}
		}
		field.Set(sl)
	}

	return nil
}
Beispiel #2
0
func decodeBool(b []byte, v reflect.Value) error {
	if len(b) != 1 {
		return SyntaxError(fmt.Sprintf("booleans must be only one byte (len = %d)", len(b)))
	}
	v.SetBool(b[0] != 0)
	return nil
}
Beispiel #3
0
func (d *Decoder) decodeField(v reflect.Value, val string) error {
	switch v.Type() {
	case reflect.TypeOf(time.Duration(0)):
		a, err := time.ParseDuration(val)
		if err != nil {
			return err
		}
		v.SetInt(int64(a))
	default:
		switch v.Kind() {
		case reflect.String:
			v.SetString(val)
		case reflect.Int, reflect.Int32, reflect.Int64:
			v.Type()
			nv, err := strconv.ParseInt(val, 10, 64)
			if err != nil {
				return err
			}
			v.SetInt(nv)
		case reflect.Bool:
			b, err := strconv.ParseBool(val)
			if err != nil {
				return err
			}
			v.SetBool(b)
		case reflect.Float32, reflect.Float64:
			f, err := strconv.ParseFloat(val, 64)
			if err != nil {
				return err
			}
			v.SetFloat(f)
		}
	}
	return nil
}
Beispiel #4
0
func (par *boolParser) ParseValue(ctx *parseContext, valueOf reflect.Value, location int, err *Error) int {
	if strAt(ctx.str, location, "true") {
		valueOf.SetBool(true)
		location += 4
	} else if strAt(ctx.str, location, "false") {
		valueOf.SetBool(false)
		location += 5
	} else {
		err.Location = location
		err.Message = boolError
		return -1
	}

	if location < len(ctx.str) {
		if ctx.str[location] == '_' ||
			(ctx.str[location] >= 'a' && ctx.str[location] <= 'z') ||
			(ctx.str[location] >= 'A' && ctx.str[location] <= 'Z') ||
			(ctx.str[location] >= '0' && ctx.str[location] <= '9') {
			err.Location = location
			err.Message = boolError
			return -1
		}
	}

	return location
}
Beispiel #5
0
func (tbl *Table) setVal(field *reflect.Value, val interface{}) {
	str, ok := val.(string)
	log.Println("trying to set")
	if ok {
		log.Println("setting string")
		field.SetString(str)
		return
	}
	i, ok := val.(int64)
	if ok {
		field.SetInt(i)
		return
	}
	ui, ok := val.(uint64)
	if ok {
		field.SetUint(ui)
		return
	}
	b, ok := val.(bool)
	if ok {
		field.SetBool(b)
		return
	}
	f, ok := val.(float64)
	if ok {
		field.SetFloat(f)
		return
	}
	t, ok := val.(time.Time)
	if ok {
		field.Set(reflect.ValueOf(t))
		return
	}

}
Beispiel #6
0
func luaU_FetchBool(l Lua_State, v *reflect.Value) (ok bool) {
	if ok = Lua_isboolean(l, -1); ok {
		v.SetBool(Lua_toboolean(l, -1))
	}
	Lua_pop(l, 1)
	return
}
Beispiel #7
0
// 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
}
Beispiel #8
0
// 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
}
Beispiel #9
0
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
}
Beispiel #10
0
// type(1) | name length(1) | raw name bytes | 0x00 | 0x00/0x01
func (d *decodeState) bool(v reflect.Value) {
	d.off += 1 // type

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

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

	val := d.data[d.off]
	d.off += 1

	if !v.IsValid() {
		return
	}
	if val == 0 {
		v.SetBool(false)
	} else {
		v.SetBool(true)
	}
}
Beispiel #11
0
// 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)
	}
}
Beispiel #12
0
func unifyBool(data interface{}, rv reflect.Value) error {
	if b, ok := data.(bool); ok {
		rv.SetBool(b)
		return nil
	}
	return badtype("integer", data)
}
Beispiel #13
0
// setFieldVal first converts v to the type of structField, then uses reflection
// to set the field to the converted value.
func setFieldVal(structField reflect.Value, name string, v string) error {
	switch structField.Kind() {
	case reflect.String:
		structField.SetString(v)
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		vInt, err := strconv.Atoi(v)
		if err != nil {
			return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
		}
		structField.SetInt(int64(vInt))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		vUint, err := strconv.ParseUint(v, 10, 64)
		if err != nil {
			return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
		}
		structField.SetUint(uint64(vUint))
	case reflect.Float32, reflect.Float64:
		vFloat, err := strconv.ParseFloat(v, 64)
		if err != nil {
			return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
		}
		structField.SetFloat(vFloat)
	case reflect.Bool:
		vBool, err := strconv.ParseBool(v)
		if err != nil {
			return fmt.Errorf("envvar: Error parsing environment variable %s: %s\n%s", name, v, err.Error())
		}
		structField.SetBool(vBool)
	default:
		return fmt.Errorf("envvar: Unsupported struct field type: %s", structField.Type().String())
	}
	return nil
}
Beispiel #14
0
func convertAssignBulkString(d reflect.Value, s []byte) (err error) {
	switch d.Type().Kind() {
	case reflect.Float32, reflect.Float64:
		var x float64
		x, err = strconv.ParseFloat(string(s), d.Type().Bits())
		d.SetFloat(x)
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		var x int64
		x, err = strconv.ParseInt(string(s), 10, d.Type().Bits())
		d.SetInt(x)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		var x uint64
		x, err = strconv.ParseUint(string(s), 10, d.Type().Bits())
		d.SetUint(x)
	case reflect.Bool:
		var x bool
		x, err = strconv.ParseBool(string(s))
		d.SetBool(x)
	case reflect.String:
		d.SetString(string(s))
	case reflect.Slice:
		if d.Type().Elem().Kind() != reflect.Uint8 {
			err = cannotConvert(d, s)
		} else {
			d.SetBytes(s)
		}
	default:
		err = cannotConvert(d, s)
	}
	return
}
Beispiel #15
0
// 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(kind reflect.Kind, key *Key, field reflect.Value, delim string) error {
	switch 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:
		intVal, err := key.Int64()
		if err != nil {
			return nil
		}
		field.SetInt(intVal)
	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:
		vals := key.Strings(delim)
		numVals := len(vals)
		if numVals == 0 {
			return nil
		}

		sliceOf := field.Type().Elem().Kind()

		var times []time.Time
		if sliceOf == reflectTime {
			times = key.Times(delim)
		}

		slice := reflect.MakeSlice(field.Type(), numVals, numVals)
		for i := 0; i < numVals; i++ {
			switch sliceOf {
			case reflectTime:
				slice.Index(i).Set(reflect.ValueOf(times[i]))
			default:
				slice.Index(i).Set(reflect.ValueOf(vals[i]))
			}
		}
		field.Set(slice)
	default:
		return fmt.Errorf("unsupported type '%s'", kind)
	}
	return nil
}
Beispiel #16
0
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)
	}
}
Beispiel #17
0
func booleanDecoder(node RMNode, data interface{}, v reflect.Value) {
	val, err := redis.Bool(data, nil)
	if err != nil {
		panic(err)
	}
	v.SetBool(val)
}
Beispiel #18
0
func decodeBoolValue(v reflect.Value, b []byte) error {
	if !v.CanSet() {
		return errorf("pg: Decode(nonsettable %s)", v.Type())
	}
	v.SetBool(len(b) == 1 && b[0] == 't')
	return nil
}
Beispiel #19
0
func (d base) setModelValue(driverValue, fieldValue reflect.Value) error {
	switch fieldValue.Type().Kind() {
	case reflect.Bool:
		fieldValue.SetBool(d.dialect.parseBool(driverValue.Elem()))
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		fieldValue.SetInt(driverValue.Elem().Int())
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		// reading uint from int value causes panic
		switch driverValue.Elem().Kind() {
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			fieldValue.SetUint(uint64(driverValue.Elem().Int()))
		default:
			fieldValue.SetUint(driverValue.Elem().Uint())
		}
	case reflect.Float32, reflect.Float64:
		fieldValue.SetFloat(driverValue.Elem().Float())
	case reflect.String:
		fieldValue.SetString(string(driverValue.Elem().Bytes()))
	case reflect.Slice:
		if reflect.TypeOf(driverValue.Interface()).Elem().Kind() == reflect.Uint8 {
			fieldValue.SetBytes(driverValue.Elem().Bytes())
		}
	case reflect.Struct:
		switch fieldValue.Interface().(type) {
		case time.Time:
			fieldValue.Set(driverValue.Elem())
		default:
			if scanner, ok := fieldValue.Addr().Interface().(sql.Scanner); ok {
				return scanner.Scan(driverValue.Interface())
			}
		}

	}
	return nil
}
Beispiel #20
0
func convertAssignInt(d reflect.Value, s int64) (err error) {
	switch d.Type().Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		d.SetInt(s)
		if d.Int() != s {
			err = strconv.ErrRange
			d.SetInt(0)
		}
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		if s < 0 {
			err = strconv.ErrRange
		} else {
			x := uint64(s)
			d.SetUint(x)
			if d.Uint() != x {
				err = strconv.ErrRange
				d.SetUint(0)
			}
		}
	case reflect.Bool:
		d.SetBool(s != 0)
	default:
		err = cannotConvert(d, s)
	}
	return
}
Beispiel #21
0
func (d *base) SetModelValue(driverValue, fieldValue reflect.Value) error {
	// ignore zero types
	if !driverValue.Elem().IsValid() {
		return nil
	}
	switch fieldValue.Type().Kind() {
	case reflect.Bool:
		fieldValue.SetBool(d.Dialect.ParseBool(driverValue.Elem()))
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		fieldValue.SetInt(driverValue.Elem().Int())
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		// reading uint from int value causes panic
		switch driverValue.Elem().Kind() {
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			fieldValue.SetUint(uint64(driverValue.Elem().Int()))
		default:
			fieldValue.SetUint(driverValue.Elem().Uint())
		}
	case reflect.Float32, reflect.Float64:
		fieldValue.SetFloat(driverValue.Elem().Float())
	case reflect.String:
		fieldValue.SetString(string(driverValue.Elem().Bytes()))
	case reflect.Slice:
		if reflect.TypeOf(driverValue.Interface()).Elem().Kind() == reflect.Uint8 {
			fieldValue.SetBytes(driverValue.Elem().Bytes())
		}
	case reflect.Struct:
		if _, ok := fieldValue.Interface().(time.Time); ok {
			fieldValue.Set(driverValue.Elem())
		}
	}
	return nil
}
Beispiel #22
0
// 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()))
	}
}
Beispiel #23
0
func (p *Decoder) unmarshalLaxString(s string, val reflect.Value) {
	switch val.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		i := mustParseInt(s, 10, 64)
		val.SetInt(i)
		return
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		i := mustParseUint(s, 10, 64)
		val.SetUint(i)
		return
	case reflect.Float32, reflect.Float64:
		f := mustParseFloat(s, 64)
		val.SetFloat(f)
		return
	case reflect.Bool:
		b := mustParseBool(s)
		val.SetBool(b)
		return
	case reflect.Struct:
		if val.Type() == timeType {
			t, err := time.Parse(textPlistTimeLayout, s)
			if err != nil {
				panic(err)
			}
			val.Set(reflect.ValueOf(t.In(time.UTC)))
			return
		}
		fallthrough
	default:
		panic(&incompatibleDecodeTypeError{val.Type(), String})
	}
}
Beispiel #24
0
func (md *MetaData) unifyBool(data interface{}, rv reflect.Value) error {
	if b, ok := data.(bool); ok {
		rv.SetBool(b)
		return nil
	}
	return badtype("boolean", data)
}
Beispiel #25
0
func setVal(val string, field reflect.Value) error {
	switch field.Kind() {
	case reflect.String:
		field.SetString(val)
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		intVal, err := strconv.Atoi(val)
		if err != nil {
			return err
		}
		field.SetInt(int64(intVal))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		intVal, err := strconv.Atoi(val)
		if err != nil {
			return err
		}
		field.SetUint(uint64(intVal))
	case reflect.Bool:
		boolVal, err := strconv.ParseBool(val)
		if err != nil {
			return err
		}
		field.SetBool(boolVal)

	}
	return nil
}
Beispiel #26
0
func _reflect(b []byte, v reflect.Value) int {
	switch v.Kind() {
	case reflect.String:
		s := ScanString(b)
		v.SetString(s)
		return len(s) + 1
	case reflect.Bool:
		v.SetBool(Bool(b))
		return 1
	case reflect.Int:
		v.SetInt(int64(Int(b)))
		return 8
	case reflect.Uint:
		v.SetUint(uint64(Uint(b)))
		return 8
	case reflect.Int8:
		v.SetInt(int64(Int8(b)))
	case reflect.Uint8:
		v.SetUint(uint64(Uint8(b)))
	case reflect.Int16:
		v.SetInt(int64(Int16(b)))
	case reflect.Uint16:
		v.SetUint(uint64(Uint16(b)))
	case reflect.Int32:
		v.SetInt(int64(Int32(b)))
	case reflect.Uint32:
		v.SetUint(uint64(Uint32(b)))
	case reflect.Int64:
		v.SetInt(Int64(b))
	case reflect.Uint64:
		v.SetUint(Uint64(b))
	case reflect.Float32:
		v.SetFloat(float64(Float32(b)))
	case reflect.Float64:
		v.SetFloat(Float64(b))
	case reflect.Complex64:
		v.SetComplex(complex128(Complex64(b)))
	case reflect.Complex128:
		v.SetComplex(Complex128(b))
	case reflect.Struct:
		sum := 0
		for i, n := 0, v.NumField(); i < n; i++ {
			if f := v.Field(i); f.CanSet() {
				s := _reflect(b[sum:], f)
				if s < 0 {
					return -1
				}
				sum += s
			}
		}
		if sum == 0 {
			return -1
		}
		return sum
	default:
		return -1
	}
	return int(v.Type().Size())
}
Beispiel #27
0
func set(v *reflect.Value, s string) error {
	switch v.Kind() {
	case reflect.Bool:
		b, err := strconv.ParseBool(s)
		if err != nil {
			return err
		}

		v.SetBool(b)
		return nil
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		if s == "" {
			s = "0"
		}

		n, err := strconv.ParseInt(s, 10, v.Type().Bits())
		if err != nil {
			return err
		}

		v.SetInt(n)
		return nil
	case reflect.Ptr:
		if !v.Elem().IsValid() {
			v.Set(reflect.New(v.Type().Elem()))
		}

		switch v.Interface().(type) {
		case *bool:
			b, err := strconv.ParseBool(s)
			if err != nil {
				return err
			}

			v.Elem().SetBool(b)
			return nil
		case *int, *int8, *int16, *int32, *int64:
			n, err := strconv.ParseInt(s, 10, v.Elem().Type().Bits())
			if err != nil {
				return err
			}

			v.Elem().SetInt(n)
			return nil
		case *string:
			v.Elem().SetString(s)
			return nil
		default:
			// TODO: Do something with unknown pointer types
		}
	case reflect.String:
		v.SetString(s)
		return nil
	default:
		// TODO: Do something with unknown reflect types
	}

	return nil
}
Beispiel #28
0
func convertBool(in interface{}, val reflect.Value) *ConfigError {
	if b, ok := in.(bool); !ok {
		return &ConfigError{ErrInvalidType, ""}
	} else {
		val.SetBool(b)
	}
	return nil
}
Beispiel #29
0
	/*
		if v.Addr().Type().NumMethod() > 0 {
			if u, ok := v.Addr().Interface().(Unmarshaler); ok {
				return decUnmarshaler(s, u)
			}
		}
	*/
	switch v.Kind() {
	case reflect.String:
		if v.Type() == rawMessageType {
			return decRaw(s, v)
		}
		return decString(s, v)
	case reflect.Int:
		return decInt(s, v)
	case reflect.Float64:
		return decFloat(s, v)
	case reflect.Bool:
		return decBool(s, v)
	case reflect.Struct:
		return decStruct(s, v)
	case reflect.Map:
		return decMap(s, v.Addr())
	case reflect.Slice:
		return decSlice(s, v.Addr())
	case reflect.Ptr:
		if strings.HasPrefix(s, "null") {
			return skipWhitespace(s[4:]), nil
		}
		pv := reflect.New(v.Type().Elem())
		s, err := decValue(s, pv.Elem())
		if err != nil {
			return s, err
		}
		v.Set(pv)
		return s, nil
	default:
		return s, ErrUnsupportedType
	}
}

// space returns the number of leading whitespace bytes
func space(s string) int {
	for i, v := range s {
		switch v {
		case ' ', '\t', '\n': // TODO: unicode.IsSpace
		default:
			return i
		}
	}
	return len(s)
}

// skipWhitespace trims leading whitespace
func skipWhitespace(s string) string {
	// TrimLeftFunc is slower than space(), but more correct.
	return strings.TrimLeftFunc(s, unicode.IsSpace)
	// return s[space(s):]
}

// decString reads /"..."\s*/
func decString(s string, v reflect.Value) (string, error) {
	ss, l, err := nextString(s)
	if err != nil {
		return "", err
	}
	if l == 0 {
		return "", ErrSyntax
	}
	v.SetString(ss)
	return skipWhitespace(s[l:]), nil
}

// decInt reads /\d\s*/
func decInt(s string, v reflect.Value) (string, error) {
	n, l, err := nextInt(s)
	if err != nil {
		return "", err
	}
	if l == 0 {
		return "", ErrSyntax
	}
	v.SetInt(n)
	return skipWhitespace(s[l:]), nil
}

// decFloat reads /<float>\s*/
func decFloat(s string, v reflect.Value) (string, error) {
	n, l, err := nextNumber(s)
	if err != nil {
		return "", err
	}
	if l == 0 {
		return "", ErrSyntax
	}
	v.SetFloat(n)
	return skipWhitespace(s[l:]), nil
}

// decBool reads /(true|false)\s*/
func decBool(s string, v reflect.Value) (string, error) {
	t, l, err := nextBool(s)
	if err != nil {
		return "", err
	}
	v.SetBool(t)
	return skipWhitespace(s[l:]), nil
}
Beispiel #30
0
func decodeBool(s *Stream, val reflect.Value) error {
	b, err := s.Bool()
	if err != nil {
		return wrapStreamError(err, val.Type())
	}
	val.SetBool(b)
	return nil
}