Ejemplo n.º 1
0
func randLiteral() rdf.Literal {
	r := rnd.Intn(100)
	switch {
	case r < 60: // 60% strings
		v, _ := quick.Value(reflect.TypeOf(""), rnd)
		return rdf.NewLiteral(v.String())
	case r < 70: // 10% language tagged strings
		v, _ := quick.Value(reflect.TypeOf(""), rnd)
		return rdf.NewLangLiteral(v.String(), randLang())
	case r < 75: // 5% int64
		v, _ := quick.Value(reflect.TypeOf(1), rnd)
		return rdf.NewLiteral(v.Int())
	case r < 80: // 5% int32
		v, _ := quick.Value(reflect.TypeOf(int32(1)), rnd)
		return rdf.NewLiteral(int32(v.Int()))
	case r < 82: // 2% float64
		v, _ := quick.Value(reflect.TypeOf(3.14), rnd)
		return rdf.NewLiteral(v.Float())
	case r < 84: // 2% float64
		v, _ := quick.Value(reflect.TypeOf(float32(3.14)), rnd)
		return rdf.NewLiteral(float32(v.Float()))
	case r < 86: // 2% boolean
		v, _ := quick.Value(reflect.TypeOf(true), rnd)
		return rdf.NewLiteral(v.Bool())
	case r < 87: // 1% int8
		v, _ := quick.Value(reflect.TypeOf(int8(0)), rnd)
		return rdf.NewLiteral(int8(v.Int()))
	case r < 88: // 1% int16
		v, _ := quick.Value(reflect.TypeOf(int16(0)), rnd)
		return rdf.NewLiteral(int16(v.Int()))
	case r < 88: // 1% uint8
		v, _ := quick.Value(reflect.TypeOf(uint8(0)), rnd)
		return rdf.NewLiteral(uint8(v.Uint()))
	case r < 89: // 1% uint16
		v, _ := quick.Value(reflect.TypeOf(uint16(0)), rnd)
		return rdf.NewLiteral(uint16(v.Uint()))
	case r < 91: // 2% uint32
		v, _ := quick.Value(reflect.TypeOf(uint32(0)), rnd)
		return rdf.NewLiteral(uint32(v.Uint()))
	case r < 93: // 2% uint64
		v, _ := quick.Value(reflect.TypeOf(uint64(0)), rnd)
		return rdf.NewLiteral(v.Uint())
	default: // 6% time.Time
		s := rand.Int63()
		n := rand.Int63()
		return rdf.NewLiteral(time.Unix(s, n))
	}
}
Ejemplo n.º 2
0
Archivo: db.go Proyecto: boutros/sopp
func (db *DB) decode(b []byte) (rdf.Term, error) {
	// We control the encoding, so the only way for this method to fail to decode
	// into a RDF term is if the underlying stoarge has been corrupted on the file system level.
	if len(b) == 0 {
		return nil, errors.New("cannot decode empty byte slice into RDF term")
	}

	var dt rdf.URI
	switch b[0] {
	case 0x00:
		return rdf.URI(db.base + string(b[1:])), nil
	case 0x01:
		return rdf.URI(string(b[1:])), nil
	case 0x02:
		return rdf.NewTypedLiteral(string(b[1:]), rdf.XSDstring), nil
	case 0x03:
		if len(b) < 2 {
			return nil, fmt.Errorf("cannot decode as rdf:langString: %v", b)
		}
		ll := int(b[1])
		if len(b) < ll+2 {
			return nil, fmt.Errorf("cannot decode as rdf:langString: %v", b)
		}
		return rdf.NewLangLiteral(string(b[ll+2:]), string(b[2:2+ll])), nil
	case 0x04:
		dt = rdf.XSDboolean
	case 0x05:
		dt = rdf.XSDbyte
	case 0x06:
		dt = rdf.XSDint
	case 0x07:
		dt = rdf.XSDshort
	case 0x08:
		dt = rdf.XSDlong
	case 0x09:
		dt = rdf.XSDinteger
	case 0x0A:
		dt = rdf.XSDunsignedShort
	case 0x0B:
		dt = rdf.XSDunsignedInt
	case 0x0C:
		dt = rdf.XSDunsignedLong
	case 0x0D:
		dt = rdf.XSDunsignedByte
	case 0x0E:
		dt = rdf.XSDfloat
	case 0x0F:
		dt = rdf.XSDdouble
	case 0x10:
		dt = rdf.XSDdateTimeStamp
	case 0xFF:
		if len(b) < 2 {
			return nil, fmt.Errorf("cannot decode as literal: %v", b)
		}
		ll := int(b[1])
		if len(b) < ll {
			return nil, fmt.Errorf("cannot decode as literal: %v", b)
		}
		return rdf.NewTypedLiteral(string(b[ll+2:]), rdf.NewURI(string(b[2:2+ll]))), nil
	default:
		return nil, fmt.Errorf("cannot decode RDF term: %v", b)
	}

	return rdf.NewTypedLiteral(string(b[1:]), dt), nil
}