2016-02-21 22:25:35 +00:00
|
|
|
package tusd
|
|
|
|
|
2016-03-11 19:17:43 +00:00
|
|
|
#define USE_FUNC(TYPE) \
|
|
|
|
func (store *StoreComposer) Use ## TYPE(ext TYPE ## DataStore) { \
|
|
|
|
store.Uses ## TYPE = ext != nil; \
|
|
|
|
store.TYPE = ext; \
|
2016-02-21 22:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define USE_FIELD(TYPE) Uses ## TYPE bool; \
|
|
|
|
TYPE TYPE ## DataStore
|
|
|
|
|
|
|
|
#define USE_FROM(TYPE) if mod, ok := store.(TYPE ## DataStore); ok { \
|
|
|
|
composer.Use ## TYPE (mod) \
|
|
|
|
}
|
|
|
|
|
2016-02-22 10:00:20 +00:00
|
|
|
#define USE_CAP(TYPE) str += ` TYPE: `; \
|
|
|
|
if store.Uses ## TYPE { \
|
|
|
|
str += "✓" \
|
|
|
|
} else { \
|
|
|
|
str += "✗" \
|
|
|
|
}
|
|
|
|
|
2016-03-11 19:17:43 +00:00
|
|
|
// 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.
|
2016-02-21 22:25:35 +00:00
|
|
|
type StoreComposer struct {
|
|
|
|
Core DataStore
|
|
|
|
|
|
|
|
USE_FIELD(Terminater)
|
|
|
|
USE_FIELD(Finisher)
|
|
|
|
USE_FIELD(Locker)
|
|
|
|
USE_FIELD(GetReader)
|
|
|
|
USE_FIELD(Concater)
|
2018-05-13 14:27:38 +00:00
|
|
|
USE_FIELD(LengthDeferrer)
|
2016-02-21 22:25:35 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 19:17:43 +00:00
|
|
|
// NewStoreComposer creates a new and empty store composer.
|
2016-02-21 22:25:35 +00:00
|
|
|
func NewStoreComposer() *StoreComposer {
|
|
|
|
return &StoreComposer{}
|
|
|
|
}
|
|
|
|
|
2016-03-11 19:17:43 +00:00
|
|
|
// 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 {
|
2016-02-21 22:25:35 +00:00
|
|
|
composer := NewStoreComposer()
|
|
|
|
composer.UseCore(store)
|
|
|
|
|
|
|
|
USE_FROM(Terminater)
|
|
|
|
USE_FROM(Finisher)
|
|
|
|
USE_FROM(Locker)
|
|
|
|
USE_FROM(GetReader)
|
|
|
|
USE_FROM(Concater)
|
2018-05-13 14:27:38 +00:00
|
|
|
USE_FROM(LengthDeferrer)
|
2016-02-21 22:25:35 +00:00
|
|
|
|
|
|
|
return composer
|
|
|
|
}
|
|
|
|
|
2016-03-11 19:17:43 +00:00
|
|
|
// Capabilities returns a string representing the provided extensions in a
|
|
|
|
// human-readable format meant for debugging.
|
2016-02-22 10:00:20 +00:00
|
|
|
func (store *StoreComposer) Capabilities() string {
|
|
|
|
str := "Core: "
|
|
|
|
|
|
|
|
if store.Core != nil {
|
|
|
|
str += "✓"
|
|
|
|
} else {
|
|
|
|
str += "✗"
|
|
|
|
}
|
|
|
|
|
|
|
|
USE_CAP(Terminater)
|
|
|
|
USE_CAP(Finisher)
|
|
|
|
USE_CAP(Locker)
|
|
|
|
USE_CAP(GetReader)
|
|
|
|
USE_CAP(Concater)
|
2018-05-13 14:27:38 +00:00
|
|
|
USE_CAP(LengthDeferrer)
|
2016-02-22 10:00:20 +00:00
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2016-03-11 19:17:43 +00:00
|
|
|
// UseCore will set the used core data store. If the argument is nil, the
|
|
|
|
// property will be unset.
|
2016-02-21 22:25:35 +00:00
|
|
|
func (store *StoreComposer) UseCore(core DataStore) {
|
|
|
|
store.Core = core
|
|
|
|
}
|
|
|
|
|
|
|
|
USE_FUNC(Terminater)
|
|
|
|
USE_FUNC(Finisher)
|
|
|
|
USE_FUNC(Locker)
|
|
|
|
USE_FUNC(GetReader)
|
|
|
|
USE_FUNC(Concater)
|
2018-05-13 14:27:38 +00:00
|
|
|
USE_FUNC(LengthDeferrer)
|