Document functions for handling StoreComposers

This commit is contained in:
Marius 2016-03-11 20:17:43 +01:00
parent ab1c5be7d6
commit be8fd83d0d
7 changed files with 50 additions and 21 deletions

View File

@ -1,5 +1,8 @@
package tusd
// StoreComposer represents a composable data store. It consists of the core
// data store and optional extensions. Please consult the package's overview
// for a more detailed introduction in how to use this structure.
type StoreComposer struct {
Core DataStore
@ -15,11 +18,15 @@ type StoreComposer struct {
Concater ConcaterDataStore
}
// NewStoreComposer creates a new and empty store composer.
func NewStoreComposer() *StoreComposer {
return &StoreComposer{}
}
func NewStoreComposerFromDataStore(store DataStore) *StoreComposer {
// newStoreComposerFromDataStore creates a new store composer and attempts to
// extract the extensions for the provided store. This is intended to be used
// for transitioning from data stores to composers.
func newStoreComposerFromDataStore(store DataStore) *StoreComposer {
composer := NewStoreComposer()
composer.UseCore(store)
@ -42,6 +49,8 @@ func NewStoreComposerFromDataStore(store DataStore) *StoreComposer {
return composer
}
// Capabilities returns a string representing the provided extensions in a
// human-readable format meant for debugging.
func (store *StoreComposer) Capabilities() string {
str := "Core: "
@ -85,27 +94,29 @@ func (store *StoreComposer) Capabilities() string {
return str
}
// UseCore will set the used core data store. If the argument is nil, the
// property will be unset.
func (store *StoreComposer) UseCore(core DataStore) {
store.Core = core
}
func (store *StoreComposer) UseTerminater(x TerminaterDataStore) {
store.UsesTerminater = x != nil
store.Terminater = x
func (store *StoreComposer) UseTerminater(ext TerminaterDataStore) {
store.UsesTerminater = ext != nil
store.Terminater = ext
}
func (store *StoreComposer) UseFinisher(x FinisherDataStore) {
store.UsesFinisher = x != nil
store.Finisher = x
func (store *StoreComposer) UseFinisher(ext FinisherDataStore) {
store.UsesFinisher = ext != nil
store.Finisher = ext
}
func (store *StoreComposer) UseLocker(x LockerDataStore) {
store.UsesLocker = x != nil
store.Locker = x
func (store *StoreComposer) UseLocker(ext LockerDataStore) {
store.UsesLocker = ext != nil
store.Locker = ext
}
func (store *StoreComposer) UseGetReader(x GetReaderDataStore) {
store.UsesGetReader = x != nil
store.GetReader = x
func (store *StoreComposer) UseGetReader(ext GetReaderDataStore) {
store.UsesGetReader = ext != nil
store.GetReader = ext
}
func (store *StoreComposer) UseConcater(x ConcaterDataStore) {
store.UsesConcater = x != nil
store.Concater = x
func (store *StoreComposer) UseConcater(ext ConcaterDataStore) {
store.UsesConcater = ext != nil
store.Concater = ext
}

View File

@ -1,8 +1,9 @@
package tusd
#define USE_FUNC(TYPE) func (store *StoreComposer) Use ## TYPE(x TYPE ## DataStore) { \
store.Uses ## TYPE = x != nil; \
store.TYPE = x; \
#define USE_FUNC(TYPE) \
func (store *StoreComposer) Use ## TYPE(ext TYPE ## DataStore) { \
store.Uses ## TYPE = ext != nil; \
store.TYPE = ext; \
}
#define USE_FIELD(TYPE) Uses ## TYPE bool; \
@ -19,6 +20,9 @@ if store.Uses ## TYPE { \
str += "✗" \
}
// StoreComposer represents a composable data store. It consists of the core
// data store and optional extensions. Please consult the package's overview
// for a more detailed introduction in how to use this structure.
type StoreComposer struct {
Core DataStore
@ -29,11 +33,15 @@ type StoreComposer struct {
USE_FIELD(Concater)
}
// NewStoreComposer creates a new and empty store composer.
func NewStoreComposer() *StoreComposer {
return &StoreComposer{}
}
func NewStoreComposerFromDataStore(store DataStore) *StoreComposer {
// newStoreComposerFromDataStore creates a new store composer and attempts to
// extract the extensions for the provided store. This is intended to be used
// for transitioning from data stores to composers.
func newStoreComposerFromDataStore(store DataStore) *StoreComposer {
composer := NewStoreComposer()
composer.UseCore(store)
@ -46,6 +54,8 @@ func NewStoreComposerFromDataStore(store DataStore) *StoreComposer {
return composer
}
// Capabilities returns a string representing the provided extensions in a
// human-readable format meant for debugging.
func (store *StoreComposer) Capabilities() string {
str := "Core: "
@ -64,6 +74,8 @@ func (store *StoreComposer) Capabilities() string {
return str
}
// UseCore will set the used core data store. If the argument is nil, the
// property will be unset.
func (store *StoreComposer) UseCore(core DataStore) {
store.Core = core
}

View File

@ -55,7 +55,7 @@ func (config *Config) validate() error {
config.isAbs = uri.IsAbs()
if config.StoreComposer == nil {
config.StoreComposer = NewStoreComposerFromDataStore(config.DataStore)
config.StoreComposer = newStoreComposerFromDataStore(config.DataStore)
} else if config.DataStore != nil {
// TODO: consider returning an error
}

View File

@ -45,6 +45,7 @@ func New(client *consul.Client) *ConsulLocker {
}
}
// UseIn adds this locker to the passed composer.
func (locker *ConsulLocker) UseIn(composer *tusd.StoreComposer) {
composer.UseLocker(locker)
}

View File

@ -46,6 +46,8 @@ func New(path string) FileStore {
return FileStore{path}
}
// UseIn sets this store as the core data store in the passed composer and adds
// all possible extension to it.
func (store FileStore) UseIn(composer *tusd.StoreComposer) {
composer.UseCore(store)
composer.UseGetReader(store)

View File

@ -39,6 +39,7 @@ func New() *MemoryLocker {
}
}
// UseIn adds this locker to the passed composer.
func (locker *MemoryLocker) UseIn(composer *tusd.StoreComposer) {
composer.UseLocker(locker)
}

View File

@ -131,6 +131,8 @@ func New(bucket string, service s3iface.S3API) S3Store {
}
}
// UseIn sets this store as the core data store in the passed composer and adds
// all possible extension to it.
func (store S3Store) UseIn(composer *tusd.StoreComposer) {
composer.UseCore(store)
composer.UseTerminater(store)