2021-02-03 05:06:34 +01:00
|
|
|
package storage
|
|
|
|
|
2021-11-05 02:33:13 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
2021-10-29 01:35:46 +02:00
|
|
|
|
|
|
|
var (
|
2021-11-09 06:06:41 +01:00
|
|
|
ErrSQLStorageRequiresPath = errors.New("sql storage requires a non-empty path to be defined")
|
|
|
|
ErrMemoryStorageDoesNotSupportPath = errors.New("memory storage does not support persistence, use sqlite if you want persistence on file")
|
2021-10-29 01:35:46 +02:00
|
|
|
)
|
|
|
|
|
2021-07-16 04:07:30 +02:00
|
|
|
// Config is the configuration for storage
|
2021-02-03 05:06:34 +01:00
|
|
|
type Config struct {
|
2021-11-05 02:33:13 +01:00
|
|
|
// Path is the path used by the store to achieve persistence
|
|
|
|
// If blank, persistence is disabled.
|
|
|
|
// Note that not all Type support persistence
|
|
|
|
Path string `yaml:"path"`
|
|
|
|
|
2021-07-16 04:07:30 +02:00
|
|
|
// Type of store
|
|
|
|
// If blank, uses the default in-memory store
|
|
|
|
Type Type `yaml:"type"`
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
2021-10-29 01:35:46 +02:00
|
|
|
|
|
|
|
// ValidateAndSetDefaults validates the configuration and sets the default values (if applicable)
|
|
|
|
func (c *Config) ValidateAndSetDefaults() error {
|
2021-11-05 02:33:13 +01:00
|
|
|
if c.Type == "" {
|
|
|
|
c.Type = TypeMemory
|
|
|
|
}
|
|
|
|
if (c.Type == TypePostgres || c.Type == TypeSQLite) && len(c.Path) == 0 {
|
2021-11-09 06:06:41 +01:00
|
|
|
return ErrSQLStorageRequiresPath
|
2021-10-29 01:35:46 +02:00
|
|
|
}
|
2021-11-05 02:33:13 +01:00
|
|
|
if c.Type == TypeMemory && len(c.Path) > 0 {
|
2022-06-15 05:36:18 +02:00
|
|
|
return ErrMemoryStorageDoesNotSupportPath
|
2021-11-05 02:33:13 +01:00
|
|
|
}
|
2021-10-29 01:35:46 +02:00
|
|
|
return nil
|
|
|
|
}
|