Exemple #1
0
// unmarshalFact decodes a fact from it's binary representation. The domain and
// transaction ID are passed in since they are not encoded with the fact itself.
// This is because facts are stored relative to a domain and a transaction.
func unmarshalFact(m *ProtoFact, b []byte, d string, t uint64, f *origins.Fact) error {
	m.Reset()

	if err := proto.Unmarshal(b, m); err != nil {
		return err
	}

	f.Domain = d
	f.Transaction = t

	f.Entity = &origins.Ident{
		Domain: m.GetEntityDomain(),
		Name:   m.GetEntity(),
	}

	f.Attribute = &origins.Ident{
		Domain: m.GetAttributeDomain(),
		Name:   m.GetAttribute(),
	}

	f.Value = &origins.Ident{
		Domain: m.GetValueDomain(),
		Name:   m.GetValue(),
	}

	f.Time = chrono.MicroTime(m.GetTime())

	if m.GetAdded() {
		f.Operation = origins.Assertion
	} else {
		f.Operation = origins.Retraction
	}

	return nil
}
Exemple #2
0
// defaults fills in the default values for the fact.
func (tx *Transaction) defaults(f *origins.Fact) error {
	if f.Domain == "" {
		if tx.options.DefaultDomain == "" {
			return ErrNoDomain
		}

		f.Domain = tx.options.DefaultDomain
	}

	// Default to fact domain.
	if f.Entity.Domain == "" {
		f.Entity.Domain = f.Domain
	}

	if f.Attribute.Domain == "" {
		f.Attribute.Domain = f.Domain
	}

	// Set fact time to the transaction time if flagged to do so.
	if f.Time.IsZero() && tx.options.SetDefaultTime {
		f.Time = tx.StartTime
	}

	return nil
}