Beispiel #1
0
func decodeBSONData(d *decodeState, kind int, value reflect.Value) {
	start := d.offset
	d.skipValue(kind)
	bd := BSONData{Kind: kind, Data: make([]byte, d.offset-start)}
	copy(bd.Data, d.data[start:d.offset])
	value.SetValue(reflect.NewValue(bd))
}
Beispiel #2
0
func fitValue(x interface{}, v reflect.Value) os.Error {
	pv, ok := v.(*reflect.PtrValue)
	for ok {
		if x == nil {
			pv.PointTo(nil)
			return nil
		}
		pv.PointTo(reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem()))
		v = pv.Elem()
		pv, ok = v.(*reflect.PtrValue)
	}

	// Same type? No conversion is necessary.
	xv := reflect.NewValue(x)
	if xv != nil && v != nil && xv.Type() == v.Type() {
		v.SetValue(xv)
		return nil
	}

	// Going to the empty interface? No conversion is necessary.
	iv, ok := v.(*reflect.InterfaceValue)
	if ok && iv.Type().(*reflect.InterfaceType).NumMethod() == 0 {
		iv.Set(reflect.NewValue(x))
		return nil
	}

	switch t := x.(type) {
	case int64:
		iv, ok := v.(*reflect.IntValue)
		if ok {
			if iv.Overflow(t) {
				return os.ERANGE
			}
			iv.Set(t)
			break
		}
		uv, ok := v.(*reflect.UintValue)
		if ok {
			if t < 0 || uv.Overflow(uint64(t)) {
				return os.ERANGE
			}
			uv.Set(uint64(t))
			break
		}
		return &FitError{"cannot hold an integer", t, "slot", v.Type()}
	case uint64:
		uv, ok := v.(*reflect.UintValue)
		if ok {
			if uv.Overflow(t) {
				return os.ERANGE
			}
			uv.Set(t)
			break
		}
		iv, ok := v.(*reflect.IntValue)
		if ok {
			if t > math.MaxInt64 || iv.Overflow(int64(t)) {
				return os.ERANGE
			}
			iv.Set(int64(t))
			break
		}
		return &FitError{"cannot hold an integer", t, "slot", v.Type()}
	case []byte:
		return fitBytes(t, v)
	case nil:
		v.SetValue(reflect.MakeZero(v.Type()))
	case []interface{}:
		return fitSeq(t, v)
	default:
		return &FitError{"unknown source type", x, "slot", v.Type()}
	}

	return nil
}
Beispiel #3
0
// literal consumes a literal from d.data[d.off-1:], decoding into the value v.
// The first byte of the literal has been read already
// (that's how the caller knows it's a literal).
func (d *decodeState) literal(v reflect.Value) {
	// All bytes inside literal return scanContinue op code.
	start := d.off - 1
	op := d.scanWhile(scanContinue)

	// Scan read one byte too far; back up.
	d.off--
	d.scan.undo(op)
	item := d.data[start:d.off]

	// Check for unmarshaler.
	wantptr := item[0] == 'n' // null
	unmarshaler, pv := d.indirect(v, wantptr)
	if unmarshaler != nil {
		err := unmarshaler.UnmarshalJSON(item)
		if err != nil {
			d.error(err)
		}
		return
	}
	v = pv

	switch c := item[0]; c {
	case 'n': // null
		switch v.(type) {
		default:
			d.saveError(&UnmarshalTypeError{"null", v.Type()})
		case *reflect.InterfaceValue, *reflect.PtrValue, *reflect.MapValue:
			v.SetValue(nil)
		}

	case 't', 'f': // true, false
		value := c == 't'
		switch v := v.(type) {
		default:
			d.saveError(&UnmarshalTypeError{"bool", v.Type()})
		case *reflect.BoolValue:
			v.Set(value)
		case *reflect.InterfaceValue:
			v.Set(reflect.NewValue(value))
		}

	case '"': // string
		s, ok := unquote(item)
		if !ok {
			d.error(errPhase)
		}
		switch v := v.(type) {
		default:
			d.saveError(&UnmarshalTypeError{"string", v.Type()})
		case *reflect.StringValue:
			v.Set(s)
		case *reflect.InterfaceValue:
			v.Set(reflect.NewValue(s))
		}

	default: // number
		if c != '-' && (c < '0' || c > '9') {
			d.error(errPhase)
		}
		s := string(item)
		switch v := v.(type) {
		default:
			d.error(&UnmarshalTypeError{"number", v.Type()})
		case *reflect.InterfaceValue:
			n, err := strconv.Atof64(s)
			if err != nil {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(reflect.NewValue(n))

		case *reflect.IntValue:
			n, err := strconv.Atoi(s)
			if err != nil {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(n)
		case *reflect.Int8Value:
			n, err := strconv.Atoi(s)
			if err != nil || int(int8(n)) != n {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(int8(n))
		case *reflect.Int16Value:
			n, err := strconv.Atoi(s)
			if err != nil || int(int16(n)) != n {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(int16(n))
		case *reflect.Int32Value:
			n, err := strconv.Atoi(s)
			if err != nil || int(int32(n)) != n {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(int32(n))
		case *reflect.Int64Value:
			n, err := strconv.Atoi64(s)
			if err != nil {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(n)

		case *reflect.UintValue:
			n, err := strconv.Atoui(s)
			if err != nil {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(n)
		case *reflect.Uint8Value:
			n, err := strconv.Atoui(s)
			if err != nil || uint(uint8(n)) != n {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(uint8(n))
		case *reflect.Uint16Value:
			n, err := strconv.Atoui(s)
			if err != nil || uint(uint16(n)) != n {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(uint16(n))
		case *reflect.Uint32Value:
			n, err := strconv.Atoui(s)
			if err != nil || uint(uint32(n)) != n {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(uint32(n))
		case *reflect.Uint64Value:
			n, err := strconv.Atoui64(s)
			if err != nil {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(n)
		case *reflect.UintptrValue:
			n, err := strconv.Atoui64(s)
			if err != nil || uint64(uintptr(n)) != n {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(uintptr(n))

		case *reflect.FloatValue:
			n, err := strconv.Atof(s)
			if err != nil {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(n)
		case *reflect.Float32Value:
			n, err := strconv.Atof32(s)
			if err != nil {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(n)
		case *reflect.Float64Value:
			n, err := strconv.Atof64(s)
			if err != nil {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(n)
		}
	}
}
Beispiel #4
0
// literal consumes a literal from d.data[d.off-1:], decoding into the value v.
// The first byte of the literal has been read already
// (that's how the caller knows it's a literal).
func (d *decodeState) literal(v reflect.Value) {
	// All bytes inside literal return scanContinue op code.
	start := d.off - 1
	op := d.scanWhile(scanContinue)

	// Scan read one byte too far; back up.
	d.off--
	d.scan.undo(op)
	item := d.data[start:d.off]

	// Check for unmarshaler.
	wantptr := item[0] == 'n' // null
	unmarshaler, pv := d.indirect(v, wantptr)
	if unmarshaler != nil {
		err := unmarshaler.UnmarshalJSON(item)
		if err != nil {
			d.error(err)
		}
		return
	}
	v = pv

	switch c := item[0]; c {
	case 'n': // null
		switch v.(type) {
		default:
			d.saveError(&UnmarshalTypeError{"null", v.Type()})
		case *reflect.InterfaceValue, *reflect.PtrValue, *reflect.MapValue:
			v.SetValue(nil)
		}

	case 't', 'f': // true, false
		value := c == 't'
		switch v := v.(type) {
		default:
			d.saveError(&UnmarshalTypeError{"bool", v.Type()})
		case *reflect.BoolValue:
			v.Set(value)
		case *reflect.InterfaceValue:
			v.Set(reflect.NewValue(value))
		}

	case '"': // string
		s, ok := unquoteBytes(item)
		if !ok {
			d.error(errPhase)
		}
		switch v := v.(type) {
		default:
			d.saveError(&UnmarshalTypeError{"string", v.Type()})
		case *reflect.SliceValue:
			if v.Type() != byteSliceType {
				d.saveError(&UnmarshalTypeError{"string", v.Type()})
				break
			}
			b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
			n, err := base64.StdEncoding.Decode(b, s)
			if err != nil {
				d.saveError(err)
				break
			}
			v.Set(reflect.NewValue(b[0:n]).(*reflect.SliceValue))
		case *reflect.StringValue:
			v.Set(string(s))
		case *reflect.InterfaceValue:
			v.Set(reflect.NewValue(string(s)))
		}

	default: // number
		if c != '-' && (c < '0' || c > '9') {
			d.error(errPhase)
		}
		s := string(item)
		switch v := v.(type) {
		default:
			d.error(&UnmarshalTypeError{"number", v.Type()})
		case *reflect.InterfaceValue:
			n, err := strconv.Atof64(s)
			if err != nil {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(reflect.NewValue(n))

		case *reflect.IntValue:
			n, err := strconv.Atoi64(s)
			if err != nil || v.Overflow(n) {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(n)

		case *reflect.UintValue:
			n, err := strconv.Atoui64(s)
			if err != nil || v.Overflow(n) {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(n)

		case *reflect.FloatValue:
			n, err := strconv.AtofN(s, v.Type().Bits())
			if err != nil || v.Overflow(n) {
				d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
				break
			}
			v.Set(n)
		}
	}
}