Пример #1
0
func TestFormatUUID(t *testing.T) {
	first5 := uuid.NewSHA1(uuid.NameSpace_URL, []byte("somewhere.com"))
	other5 := uuid.NewSHA1(uuid.NameSpace_URL, []byte("somewhereelse.com"))
	uuid := UUID(first5.String())
	str := string(other5.String())
	b := []byte(str)
	bj := []byte("\"" + str + "\"")

	err := uuid.UnmarshalText(b)
	assert.NoError(t, err)
	assert.EqualValues(t, UUID(other5.String()), string(b))

	b, err = uuid.MarshalText()
	assert.NoError(t, err)
	assert.Equal(t, []byte(other5.String()), b)

	err = uuid.UnmarshalJSON(bj)
	assert.NoError(t, err)
	assert.EqualValues(t, UUID(str), string(b))

	b, err = uuid.MarshalJSON()
	assert.NoError(t, err)
	assert.Equal(t, bj, b)

	testValid(t, "uuid", str)
	testInvalid(t, "uuid", "not-a-uuid")
}
Пример #2
0
// UUID returns a global unique identifier for the given literal. It is
// implemented as the SHA1 UUID of the literal value.
func (l *Literal) UUID() uuid.UUID {
	var buffer bytes.Buffer

	switch v := l.v.(type) {
	case bool:
		if v {
			buffer.WriteString("true")
		} else {
			buffer.WriteString("false")
		}
	case int64:
		b := make([]byte, 8)
		binary.PutVarint(b, v)
		buffer.Write(b)
	case float64:
		bs := math.Float64bits(v)
		b := make([]byte, 8)
		binary.LittleEndian.PutUint64(b, bs)
		buffer.Write(b)
	case string:
		buffer.Write([]byte(v))
	case []byte:
		buffer.Write(v)
	}

	return uuid.NewSHA1(uuid.NIL, buffer.Bytes())
}
Пример #3
0
// id returns a stable uuid with a hash the given data or
// a random uuid if no data is provided.
func id(data ...string) string {
	if len(data) == 0 {
		return uuid.New()
	}

	b := []byte(strings.Join(data, ""))
	return uuid.NewSHA1(uuid.NameSpace_OID, b).String()
}
Пример #4
0
// UUID returns a global unique identifier for the given triple. It is
// implemented as the SHA1 UUID of the concatenated UUIDs of the subject,
// predicate, and object.
func (t *Triple) UUID() uuid.UUID {
	var buffer bytes.Buffer

	buffer.Write([]byte(t.s.UUID()))
	buffer.Write([]byte(t.p.UUID()))
	buffer.Write([]byte(t.o.UUID()))

	return uuid.NewSHA1(uuid.NIL, buffer.Bytes())
}
Пример #5
0
// UUID returns a global unique identifier for the given predicate. It is
// implemented as the SHA1 UUID of the predicate values.
func (p *Predicate) UUID() uuid.UUID {
	var buffer bytes.Buffer

	buffer.WriteString(string(p.id))
	if p.anchor == nil {
		buffer.WriteString("immutable")
	} else {
		b := make([]byte, 16)
		binary.PutVarint(b, p.anchor.UnixNano())
		buffer.Write(b)
	}

	return uuid.NewSHA1(uuid.NIL, buffer.Bytes())
}
Пример #6
0
// Generates a uuid for a minion
func GenerateUUID(name string) uuid.UUID {
	u := uuid.NewSHA1(uuid.NameSpace_DNS, []byte(name))

	return u
}
Пример #7
0
// UUID returns a global unique identifier for the given node. It is
// implemented as the SHA1 UUID of the node values.
func (n *Node) UUID() uuid.UUID {
	var buffer bytes.Buffer
	buffer.WriteString(string(*n.t))
	buffer.WriteString(string(*n.id))
	return uuid.NewSHA1(uuid.NIL, buffer.Bytes())
}