package namespace import ( "errors" "time" "caj-larsson/bog/dataswamp/namespace" ) type NamespaceRecord struct { ID int64 Name string LastSeen string AllowanceSeconds int64 QuotaKB int64 QuotaUsedKB int64 } var ErrUnparseableRecord = errors.New("record could not be mapped to entity") func (r *NamespaceRecord) toEntity() (*namespace.Namespace, error) { lastseen, err := time.Parse(time.RFC3339, r.LastSeen) if err != nil { return nil, ErrUnparseableRecord } var ns = new(namespace.Namespace) ns.ID = r.ID ns.Name = r.Name ns.LastSeen = lastseen ns.AllowanceDuration = time.Duration(r.AllowanceSeconds * int64(time.Second)) ns.FileQuota = namespace.FileSizeQuota { r.QuotaKB, r.QuotaUsedKB } return ns, err } func fromEntity(ns namespace.Namespace) (*NamespaceRecord, error) { var record = new(NamespaceRecord) record.ID = ns.ID record.Name = ns.Name record.LastSeen = ns.LastSeen.Format(time.RFC3339) record.AllowanceSeconds = int64(ns.AllowanceDuration.Seconds()) record.QuotaKB = ns.FileQuota.AllowanceKB record.QuotaUsedKB = ns.FileQuota.CurrentUsage return record, nil }