Example #1
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
}
Example #2
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
}
Example #3
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})
	}
}
Example #4
0
File: base.go Project: cgolang/qbs
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
}
Example #5
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
}
Example #6
0
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())
	}
}
Example #7
0
func unmarshalInteger(i int64, v reflect.Value) {
	switch v.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		if v.OverflowInt(i) {
			panic(&UnmarshalOverflowError{"integer " + strconv.FormatInt(i, 10), v.Type()})
		}
		v.SetInt(i)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		if i < 0 {
			panic(&UnmarshalOverflowError{"integer " + strconv.FormatInt(i, 10), v.Type()})
		}
		u := uint64(i)
		if v.OverflowUint(u) {
			panic(&UnmarshalOverflowError{"integer " + strconv.FormatUint(u, 10), v.Type()})
		}
		v.SetUint(u)
	case reflect.Interface:
		if v.NumMethod() == 0 {
			v.Set(reflect.ValueOf(i))
			return
		}
		fallthrough
	default:
		panic(&UnmarshalTypeError{"integer " + strconv.FormatInt(i, 10), v.Type()})
	}
}
Example #8
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
	}

}
Example #9
0
func luaU_FetchInt(l Lua_State, v *reflect.Value) (ok bool) {
	if ok = Lua_isnumber(l, -1); ok {
		v.SetInt(int64(Lua_tointeger(l, -1)))
	}
	Lua_pop(l, 1)
	return
}
Example #10
0
func (d *decoder) unmarshalUint(size uint, offset uint, result reflect.Value, uintType uint) (uint, error) {
	if size > uintType/8 {
		return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (uint%v size of %v)", uintType, size)
	}

	value, newOffset, err := d.decodeUint(size, offset)
	if err != nil {
		return 0, err
	}

	switch result.Kind() {
	default:
		return newOffset, newUnmarshalTypeError(value, result.Type())
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		n := int64(value)
		if result.OverflowInt(n) {
			return 0, newUnmarshalTypeError(value, result.Type())
		}
		result.SetInt(n)
		return newOffset, nil
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		if result.OverflowUint(value) {
			return 0, newUnmarshalTypeError(value, result.Type())
		}
		result.SetUint(value)
		return newOffset, nil
	case reflect.Interface:
		result.Set(reflect.ValueOf(value))
		return newOffset, nil
	}
}
Example #11
0
// setFieldValue in a json object, there is only the number type, which defaults to float64. This method convertes float64 to the value
// of the underlying struct field, for example uint64, or int32 etc...
// If the field type is not one of the integers, it just sets the value
func setFieldValue(field *reflect.Value, value reflect.Value) (err error) {
	// catch all invalid types and return an error
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("Value '%v' had wrong type", value.Interface())
		}
	}()

	switch field.Type().Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		field.SetInt(int64(value.Float()))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		field.SetUint(uint64(value.Float()))
	default:
		// try to set it with json.Unmarshaler interface, if that does not work, set value directly
		switch target := field.Addr().Interface().(type) {
		case json.Unmarshaler:
			marshaledValue, err := json.Marshal(value.Interface())
			if err != nil {
				return err
			}

			err = target.UnmarshalJSON(marshaledValue)
			if err != nil {
				return err
			}
		default:
			field.Set(value)
		}
	}

	return nil
}
Example #12
0
func (db *DataBinder) submitProperty(prop Property, field reflect.Value) error {
	value := prop.Get()
	if value == nil {
		// This happens e.g. if CurrentIndex() of a ComboBox returns -1.
		// FIXME: Should we handle this differently?
		return nil
	}
	if err, ok := value.(error); ok {
		return err
	}

	if f64, ok := value.(float64); ok {
		switch field.Kind() {
		case reflect.Float32, reflect.Float64:
			field.SetFloat(f64)

		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			field.SetInt(int64(f64))

		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
			field.SetUint(uint64(f64))

		default:
			return newError(fmt.Sprintf("Field '%s': Can't convert float64 to %s.", prop.Source().(string), field.Type().Name()))
		}

		return nil
	}

	field.Set(reflect.ValueOf(value))

	return nil
}
Example #13
0
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
}
Example #14
0
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))
		}
	}
}
Example #15
0
func (d *MySQL) ValueToField(value reflect.Value, field reflect.Value) error {
	switch field.Type().Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		field.SetInt(value.Elem().Int())
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		// reading uint from int value causes panic
		switch value.Elem().Kind() {
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			field.SetUint(uint64(value.Elem().Int()))
		default:
			field.SetUint(value.Elem().Uint())
		}
	case reflect.Float32, reflect.Float64:
		field.SetFloat(value.Elem().Float())
	case reflect.String:
		if !value.Elem().IsValid() {
			field.SetString("")
			return nil
		}
		field.SetString(string(value.Elem().Bytes()))
	case reflect.Slice:
		if reflect.TypeOf(value.Interface()).Elem().Kind() == reflect.Uint8 {
			field.SetBytes(value.Elem().Bytes())
		}
	case reflect.Struct:
		if field.Type() == reflect.TypeOf(time.Time{}) {
			field.Set(value.Elem())
		}
	}
	return nil
}
Example #16
0
func parseStringToOutStruct(inValue reflect.Value, inField reflect.StructField, outValue reflect.Value) error {
	var outField reflect.Value
	outEl := outValue.Elem()

	if boolTag := inField.Tag.Get("parseBool"); boolTag != "" {
		split := strings.Split(boolTag, ",")
		boolOutName := split[0]
		boolTrueVals := split[1:]
		outToSet := parseStringToBool(inValue.String(), boolTrueVals...)
		outField = outEl.FieldByName(boolOutName)
		outField.Set(reflect.ValueOf(outToSet))
		return nil
	}

	outField = outEl.FieldByName(inField.Name)

	switch outField.Kind() {
	case reflect.String:
		outField.Set(inValue)
	case reflect.Int:
		outField.SetInt(parseStringToInt(inValue.String()))
	case reflect.Float64:
		outField.SetFloat(parseStringToFloat(inValue.String()))
	default:
		if _, ok := outField.Interface().(time.Time); ok {
			outField.Set(reflect.ValueOf(parseStringToTime(inValue.String())))
		} else {
			return errors.New(fmt.Sprintf("Unsupported: %s", outField.Kind().String()))
		}
	}

	return nil
}
Example #17
0
// decInt32 decodes an integer and stores it as an int32 in value.
func decInt32(i *decInstr, state *decoderState, value reflect.Value) {
	v := state.decodeInt()
	if v < math.MinInt32 || math.MaxInt32 < v {
		error_(i.ovfl)
	}
	value.SetInt(v)
}
Example #18
0
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)
	}
}
Example #19
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()))
	}
}
Example #20
0
func unifyInt(data interface{}, rv reflect.Value) error {
	if num, ok := data.(int64); ok {
		switch rv.Kind() {
		case reflect.Int:
			fallthrough
		case reflect.Int8:
			fallthrough
		case reflect.Int16:
			fallthrough
		case reflect.Int32:
			fallthrough
		case reflect.Int64:
			rv.SetInt(int64(num))
		case reflect.Uint:
			fallthrough
		case reflect.Uint8:
			fallthrough
		case reflect.Uint16:
			fallthrough
		case reflect.Uint32:
			fallthrough
		case reflect.Uint64:
			rv.SetUint(uint64(num))
		default:
			panic("bug")
		}
		return nil
	}
	return badtype("integer", data)
}
func read(lex *lexer, v reflect.Value) {
	switch lex.token {
	case scanner.Ident:
		// The only valid identifiers are
		// "nil" and struct field names.
		if lex.text() == "nil" {
			v.Set(reflect.Zero(v.Type()))
			lex.next()
			return
		}
	case scanner.String:
		s, _ := strconv.Unquote(lex.text()) // NOTE: ignoring errors
		v.SetString(s)
		lex.next()
		return
	case scanner.Int:
		i, _ := strconv.Atoi(lex.text()) // NOTE: ignoring errors
		v.SetInt(int64(i))
		lex.next()
		return
	case '(':
		lex.next()
		readList(lex, v)
		lex.next() // consume ')'
		return
	}
	panic(fmt.Sprintf("unexpected token %q", lex.text()))
}
Example #22
0
func setBignum(rv reflect.Value, x *big.Int) error {
	switch rv.Kind() {
	case reflect.Ptr:
		return setBignum(reflect.Indirect(rv), x)
	case reflect.Interface:
		rv.Set(reflect.ValueOf(*x))
		return nil
	case reflect.Int32:
		if x.BitLen() < 32 {
			rv.SetInt(x.Int64())
			return nil
		} else {
			return fmt.Errorf("int too big for int32 target")
		}
	case reflect.Int, reflect.Int64:
		if x.BitLen() < 64 {
			rv.SetInt(x.Int64())
			return nil
		} else {
			return fmt.Errorf("int too big for int64 target")
		}
	default:
		return fmt.Errorf("cannot assign bignum into Kind=%s Type=%s %#v", rv.Kind().String(), rv.Type().String(), rv)
	}
}
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)
}
Example #24
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
}
Example #25
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)
	}
}
Example #26
0
func boolAsIntDecoder(dv, sv reflect.Value) {
	if sv.Bool() {
		dv.SetInt(1)
	} else {
		dv.SetInt(0)
	}
}
Example #27
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
}
Example #28
0
File: sitef.go Project: beoran/woe
func (me Record) GetValue(key string, value reflect.Value) (err error) {
	/*stringer, ok := value.Interface().(fmt.Stringer)
	  if ok {
	      me.Gut(key, stringer.String())
	      return
	  }*/
	monolog.Debug("GetValue: %s %v", key, value)

	switch value.Kind() {
	case reflect.Int, reflect.Int32, reflect.Int64:
		value.SetInt(int64(me.GetIntDefault(key, 0)))
	case reflect.Uint, reflect.Uint32, reflect.Uint64:
		value.SetUint(uint64(me.GetIntDefault(key, 0)))
	case reflect.Float32, reflect.Float64:
		f, err := me.GetFloat(key)
		if err != nil {
			return err
		}
		value.SetFloat(f)
	case reflect.String:
		s, ok := me.MayGet(key)
		if !ok {
			return fmt.Errorf("Could not get string for key %s", key)
		}
		value.SetString(s)
	case reflect.Struct:
		me.GetStruct(key+".", value.Addr().Interface())
	default:
		monolog.Warning("Don't know what to do with %v", value)
	}
	return nil
}
Example #29
0
func (par *intParser) ParseValue(ctx *parseContext, valueOf reflect.Value, location int, err *Error) int {
	if valueOf.Type().Bits() == 32 && location < len(ctx.str) && ctx.str[location] == '\'' {
		location++
		r, location := ctx.parseUnicodeValue(location, err)
		if location < 0 {
			return location
		}

		if location >= len(ctx.str) || ctx.str[location] != '\'' {
			err.Message = "Waiting for closing quote in unicode character"
			err.Location = location
			return -1
		}
		location++

		valueOf.SetInt(int64(r))

		return location
	}

	r, l := ctx.parseInt64(location, uint(valueOf.Type().Bits()), err)
	if l < 0 {
		return l
	}

	valueOf.SetInt(r)
	return l
}
Example #30
0
File: decode.go Project: walle/cfg
// 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
}