Example #1
0
func decode(b []byte, t uint16) driver.Value {
	switch t {
	case typeBool:
		if len(b) >= 1 && b[0] != 0 {
			return true
		}
		return false
	case typeCounter:
		return int64(binary.BigEndian.Uint64(b))
	case typeBlob:
		return b
	case typeVarchar, typeText, typeAscii:
		return b
	case typeInt:
		return int64(int32(binary.BigEndian.Uint32(b)))
	case typeBigInt:
		return int64(binary.BigEndian.Uint64(b))
	case typeFloat:
		return float64(math.Float32frombits(binary.BigEndian.Uint32(b)))
	case typeDouble:
		return math.Float64frombits(binary.BigEndian.Uint64(b))
	case typeTimestamp:
		t := int64(binary.BigEndian.Uint64(b))
		sec := t / 1000
		nsec := (t - sec*1000) * 1000000
		return time.Unix(sec, nsec)
	case typeUUID, typeTimeUUID:
		return uuid.FromBytes(b)
	default:
		panic("unsupported type")
	}
	return b
}
Example #2
0
func encUUID(v interface{}) (driver.Value, error) {
	var u uuid.UUID
	switch v := v.(type) {
	case string:
		var err error
		u, err = uuid.ParseUUID(v)
		if err != nil {
			return nil, err
		}
	case []byte:
		u = uuid.FromBytes(v)
	case uuid.UUID:
		u = v
	default:
		return nil, fmt.Errorf("can not convert %T to a UUID", v)
	}
	return u.Bytes(), nil
}