refactor(store): use descriptive configuration names
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
2ff98991a1
commit
0ea38777d7
2 changed files with 34 additions and 34 deletions
20
scope.go
20
scope.go
|
|
@ -50,18 +50,18 @@ type ScopedStoreConfig struct {
|
|||
}
|
||||
|
||||
// Usage example: `if err := (store.ScopedStoreConfig{Namespace: "tenant-a", Quota: store.QuotaConfig{MaxKeys: 100, MaxGroups: 10}}).Validate(); err != nil { return }`
|
||||
func (config ScopedStoreConfig) Validate() error {
|
||||
if !validNamespace.MatchString(config.Namespace) {
|
||||
func (scopedConfig ScopedStoreConfig) Validate() error {
|
||||
if !validNamespace.MatchString(scopedConfig.Namespace) {
|
||||
return core.E(
|
||||
"store.ScopedStoreConfig.Validate",
|
||||
core.Sprintf("namespace %q is invalid; use names like %q or %q", config.Namespace, "tenant-a", "tenant-42"),
|
||||
core.Sprintf("namespace %q is invalid; use names like %q or %q", scopedConfig.Namespace, "tenant-a", "tenant-42"),
|
||||
nil,
|
||||
)
|
||||
}
|
||||
if config.Quota.MaxKeys < 0 || config.Quota.MaxGroups < 0 {
|
||||
if scopedConfig.Quota.MaxKeys < 0 || scopedConfig.Quota.MaxGroups < 0 {
|
||||
return core.E(
|
||||
"store.ScopedStoreConfig.Validate",
|
||||
core.Sprintf("quota values must be zero or positive; got MaxKeys=%d MaxGroups=%d", config.Quota.MaxKeys, config.Quota.MaxGroups),
|
||||
core.Sprintf("quota values must be zero or positive; got MaxKeys=%d MaxGroups=%d", scopedConfig.Quota.MaxKeys, scopedConfig.Quota.MaxGroups),
|
||||
nil,
|
||||
)
|
||||
}
|
||||
|
|
@ -102,16 +102,16 @@ func NewScoped(storeInstance *Store, namespace string) *ScopedStore {
|
|||
}
|
||||
|
||||
// Usage example: `scopedStore, err := store.NewScopedConfigured(storeInstance, store.ScopedStoreConfig{Namespace: "tenant-a", Quota: store.QuotaConfig{MaxKeys: 100, MaxGroups: 10}}); if err != nil { return }`
|
||||
func NewScopedConfigured(storeInstance *Store, config ScopedStoreConfig) (*ScopedStore, error) {
|
||||
func NewScopedConfigured(storeInstance *Store, scopedConfig ScopedStoreConfig) (*ScopedStore, error) {
|
||||
if storeInstance == nil {
|
||||
return nil, core.E("store.NewScopedConfigured", "store instance is nil", nil)
|
||||
}
|
||||
if err := config.Validate(); err != nil {
|
||||
if err := scopedConfig.Validate(); err != nil {
|
||||
return nil, core.E("store.NewScopedConfigured", "validate config", err)
|
||||
}
|
||||
scopedStore := NewScoped(storeInstance, config.Namespace)
|
||||
scopedStore.MaxKeys = config.Quota.MaxKeys
|
||||
scopedStore.MaxGroups = config.Quota.MaxGroups
|
||||
scopedStore := NewScoped(storeInstance, scopedConfig.Namespace)
|
||||
scopedStore.MaxKeys = scopedConfig.Quota.MaxKeys
|
||||
scopedStore.MaxGroups = scopedConfig.Quota.MaxGroups
|
||||
return scopedStore, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
48
store.go
48
store.go
|
|
@ -44,22 +44,22 @@ type StoreConfig struct {
|
|||
}
|
||||
|
||||
// Usage example: `if err := (store.StoreConfig{DatabasePath: ":memory:", PurgeInterval: 30 * time.Second}).Validate(); err != nil { return }`
|
||||
func (config StoreConfig) Validate() error {
|
||||
if config.DatabasePath == "" {
|
||||
func (storeConfig StoreConfig) Validate() error {
|
||||
if storeConfig.DatabasePath == "" {
|
||||
return core.E(
|
||||
"store.StoreConfig.Validate",
|
||||
"database path is empty",
|
||||
nil,
|
||||
)
|
||||
}
|
||||
if config.Journal != (JournalConfiguration{}) && !config.Journal.isConfigured() {
|
||||
if storeConfig.Journal != (JournalConfiguration{}) && !storeConfig.Journal.isConfigured() {
|
||||
return core.E(
|
||||
"store.StoreConfig.Validate",
|
||||
"journal configuration must include endpoint URL, organisation, and bucket name",
|
||||
nil,
|
||||
)
|
||||
}
|
||||
if config.PurgeInterval < 0 {
|
||||
if storeConfig.PurgeInterval < 0 {
|
||||
return core.E("store.StoreConfig.Validate", "purge interval must be zero or positive", nil)
|
||||
}
|
||||
return nil
|
||||
|
|
@ -139,11 +139,11 @@ func (storeInstance *Store) ensureReady(operation string) error {
|
|||
|
||||
// Usage example: `storeInstance, err := store.New("/tmp/go-store.db", store.WithJournal("http://127.0.0.1:8086", "core", "events"))`
|
||||
func WithJournal(endpointURL, organisation, bucketName string) StoreOption {
|
||||
return func(config *StoreConfig) {
|
||||
if config == nil {
|
||||
return func(storeConfig *StoreConfig) {
|
||||
if storeConfig == nil {
|
||||
return
|
||||
}
|
||||
config.Journal = JournalConfiguration{
|
||||
storeConfig.Journal = JournalConfiguration{
|
||||
EndpointURL: endpointURL,
|
||||
Organisation: organisation,
|
||||
BucketName: bucketName,
|
||||
|
|
@ -205,40 +205,40 @@ func (storeInstance *Store) IsClosed() bool {
|
|||
|
||||
// Usage example: `storeInstance, err := store.New(":memory:", store.WithPurgeInterval(20*time.Millisecond))`
|
||||
func WithPurgeInterval(interval time.Duration) StoreOption {
|
||||
return func(config *StoreConfig) {
|
||||
if config == nil {
|
||||
return func(storeConfig *StoreConfig) {
|
||||
if storeConfig == nil {
|
||||
return
|
||||
}
|
||||
if interval > 0 {
|
||||
config.PurgeInterval = interval
|
||||
storeConfig.PurgeInterval = interval
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Usage example: `storeInstance, err := store.NewConfigured(store.StoreConfig{DatabasePath: ":memory:", Journal: store.JournalConfiguration{EndpointURL: "http://127.0.0.1:8086", Organisation: "core", BucketName: "events"}, PurgeInterval: 20 * time.Millisecond})`
|
||||
func NewConfigured(config StoreConfig) (*Store, error) {
|
||||
return openConfiguredStore("store.NewConfigured", config)
|
||||
func NewConfigured(storeConfig StoreConfig) (*Store, error) {
|
||||
return openConfiguredStore("store.NewConfigured", storeConfig)
|
||||
}
|
||||
|
||||
func openConfiguredStore(operation string, config StoreConfig) (*Store, error) {
|
||||
if err := config.Validate(); err != nil {
|
||||
func openConfiguredStore(operation string, storeConfig StoreConfig) (*Store, error) {
|
||||
if err := storeConfig.Validate(); err != nil {
|
||||
return nil, core.E(operation, "validate config", err)
|
||||
}
|
||||
|
||||
storeInstance, err := openSQLiteStore(operation, config.DatabasePath)
|
||||
storeInstance, err := openSQLiteStore(operation, storeConfig.DatabasePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if config.Journal != (JournalConfiguration{}) {
|
||||
if storeConfig.Journal != (JournalConfiguration{}) {
|
||||
storeInstance.journalConfiguration = journalConfiguration{
|
||||
endpointURL: config.Journal.EndpointURL,
|
||||
organisation: config.Journal.Organisation,
|
||||
bucketName: config.Journal.BucketName,
|
||||
endpointURL: storeConfig.Journal.EndpointURL,
|
||||
organisation: storeConfig.Journal.Organisation,
|
||||
bucketName: storeConfig.Journal.BucketName,
|
||||
}
|
||||
}
|
||||
if config.PurgeInterval > 0 {
|
||||
storeInstance.purgeInterval = config.PurgeInterval
|
||||
if storeConfig.PurgeInterval > 0 {
|
||||
storeInstance.purgeInterval = storeConfig.PurgeInterval
|
||||
}
|
||||
|
||||
// New() performs a non-destructive orphan scan so callers can discover
|
||||
|
|
@ -250,13 +250,13 @@ func openConfiguredStore(operation string, config StoreConfig) (*Store, error) {
|
|||
|
||||
// Usage example: `storeInstance, err := store.New("/tmp/go-store.db", store.WithJournal("http://127.0.0.1:8086", "core", "events"))`
|
||||
func New(databasePath string, options ...StoreOption) (*Store, error) {
|
||||
config := StoreConfig{DatabasePath: databasePath}
|
||||
storeConfig := StoreConfig{DatabasePath: databasePath}
|
||||
for _, option := range options {
|
||||
if option != nil {
|
||||
option(&config)
|
||||
option(&storeConfig)
|
||||
}
|
||||
}
|
||||
return openConfiguredStore("store.New", config)
|
||||
return openConfiguredStore("store.New", storeConfig)
|
||||
}
|
||||
|
||||
func openSQLiteStore(operation, databasePath string) (*Store, error) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue