// 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 }
// Facts encodes the schema as facts. This is primarily used for programmatically // creating a schema and then writing the facts to storage for later use. func (s *Schema) Facts() origins.Facts { var ( err error attr *Attribute fact *origins.Fact facts origins.Facts buf = origins.NewBuffer(nil) entity *origins.Ident ) for _, attr = range s.attrs { entity = &origins.Ident{ Domain: attr.Domain, Name: attr.Name, } if facts, err = origins.Reflect(attr); err != nil { panic(err) } for _, fact = range facts { fact.Domain = s.Domain fact.Entity = entity // Fill in the defaults. if fact.Attribute.Domain == "" { fact.Attribute.Domain = fact.Domain } buf.Write(fact) } } return buf.Facts() }