You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bog/infrastructure/sqlite/namespace/record.go

46 lines
1.1 KiB
Go

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
}