Continue work on database module

This commit is contained in:
Daniel 2018-09-06 19:06:13 +02:00
parent 7ad09b60c1
commit b8e7f90dbe
25 changed files with 962 additions and 1073 deletions

View file

@ -1,27 +1,69 @@
package database
import (
"github.com/Safing/portbase/database/record"
)
// Interface provides a method to access the database with attached options.
type Interface struct {}
type Interface struct {
options *Options
}
// Options holds options that may be set for an Interface instance.
type Options struct {
Local bool
Internal bool
AlwaysMakeSecret bool
AlwaysMakeCrownjewel bool
Local bool
Internal bool
AlwaysMakeSecret bool
AlwaysMakeCrownjewel bool
}
// NewInterface returns a new Interface to the database.
func NewInterface(opts *Options) *Interface {
return &Interface{
local: local,
internal: internal,
}
if opts == nil {
opts = &Options{}
}
return &Interface{
options: opts,
}
}
// Exists return whether a record with the given key exists.
func (i *Interface) Exists(key string) (bool, error) {
_, err := i.getRecord(key)
if err != nil {
if err == ErrNotFound {
return false, nil
}
return false, err
}
return true, nil
}
// Get return the record with the given key.
func (i *Interface) Get(key string) (record.Record, error) {
r, err := i.getRecord(key)
if err != nil {
return nil, err
}
controller
if !r.Meta().CheckPermission(i.options.Local, i.options.Internal) {
return nil, ErrPermissionDenied
}
return nil, nil
return r, nil
}
func (i *Interface) getRecord(key string) (record.Record, error) {
dbKey, db, err := splitKeyAndGetDatabase(key)
if err != nil {
return nil, err
}
r, err := db.Get(dbKey)
if err != nil {
return nil, err
}
return r, nil
}