From 030b0ff310e386aca4c60dd74c96d587dad098a7 Mon Sep 17 00:00:00 2001 From: Brian Bland Date: Wed, 17 Dec 2014 19:06:55 -0800 Subject: [PATCH] Allows storagedriver parameter values to be of type interface{} This enables use of nil, booleans, numeric types, and even complex structures for parameter values, assuming they can be parsed from yaml. --- configuration/configuration.go | 4 ++-- configuration/configuration_test.go | 12 ++++++------ storagedriver/factory/factory.go | 4 ++-- storagedriver/filesystem/driver.go | 6 +++--- storagedriver/inmemory/driver.go | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/configuration/configuration.go b/configuration/configuration.go index bbb88a0ed..8e68190da 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -103,7 +103,7 @@ func (storage Storage) Parameters() Parameters { } // setParameter changes the parameter at the provided key to the new value -func (storage Storage) setParameter(key, value string) { +func (storage Storage) setParameter(key string, value interface{}) { storage[storage.Type()][key] = value } @@ -143,7 +143,7 @@ func (storage Storage) MarshalYAML() (interface{}, error) { } // Parameters defines a key-value parameters mapping -type Parameters map[string]string +type Parameters map[string]interface{} // Reporting defines error reporting methods. type Reporting struct { diff --git a/configuration/configuration_test.go b/configuration/configuration_test.go index 75bec8187..e6fd62953 100644 --- a/configuration/configuration_test.go +++ b/configuration/configuration_test.go @@ -21,12 +21,12 @@ var configStruct = Configuration{ "region": "us-east-1", "bucket": "my-bucket", "rootpath": "/registry", - "encrypt": "true", - "secure": "false", + "encrypt": true, + "secure": false, "accesskey": "SAMPLEACCESSKEY", "secretkey": "SUPERSECRET", - "host": "", - "port": "", + "host": nil, + "port": 42, }, }, Reporting: Reporting{ @@ -50,7 +50,7 @@ storage: accesskey: SAMPLEACCESSKEY secretkey: SUPERSECRET host: ~ - port: ~ + port: 42 reporting: bugsnag: apikey: BugsnagApiKey @@ -142,7 +142,7 @@ func (suite *ConfigSuite) TestParseWithSameEnvStorage(c *C) { // Configuration struct func (suite *ConfigSuite) TestParseWithDifferentEnvStorageParams(c *C) { suite.expectedConfig.Storage.setParameter("region", "us-west-1") - suite.expectedConfig.Storage.setParameter("secure", "true") + suite.expectedConfig.Storage.setParameter("secure", true) suite.expectedConfig.Storage.setParameter("newparam", "some Value") os.Setenv("REGISTRY_STORAGE_S3_REGION", "us-west-1") diff --git a/storagedriver/factory/factory.go b/storagedriver/factory/factory.go index 0f8ca001c..254cd9bb2 100644 --- a/storagedriver/factory/factory.go +++ b/storagedriver/factory/factory.go @@ -16,7 +16,7 @@ type StorageDriverFactory interface { // Create returns a new storagedriver.StorageDriver with the given parameters // Parameters will vary by driver and may be ignored // Each parameter key must only consist of lowercase letters and numbers - Create(parameters map[string]string) (storagedriver.StorageDriver, error) + Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) } // Register makes a storage driver available by the provided name. @@ -37,7 +37,7 @@ func Register(name string, factory StorageDriverFactory) { // To run in-process, the StorageDriverFactory must first be registered with the given name // If no in-process drivers are found with the given name, this attempts to create an IPC driver // If no in-process or external drivers are found, an InvalidStorageDriverError is returned -func Create(name string, parameters map[string]string) (storagedriver.StorageDriver, error) { +func Create(name string, parameters map[string]interface{}) (storagedriver.StorageDriver, error) { driverFactory, ok := driverFactories[name] if !ok { return nil, InvalidStorageDriverError{name} diff --git a/storagedriver/filesystem/driver.go b/storagedriver/filesystem/driver.go index 3c3c950fe..635c13a2e 100644 --- a/storagedriver/filesystem/driver.go +++ b/storagedriver/filesystem/driver.go @@ -23,7 +23,7 @@ func init() { // filesystemDriverFactory implements the factory.StorageDriverFactory interface type filesystemDriverFactory struct{} -func (factory *filesystemDriverFactory) Create(parameters map[string]string) (storagedriver.StorageDriver, error) { +func (factory *filesystemDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) { return FromParameters(parameters), nil } @@ -36,12 +36,12 @@ type Driver struct { // FromParameters constructs a new Driver with a given parameters map // Optional Parameters: // - rootdirectory -func FromParameters(parameters map[string]string) *Driver { +func FromParameters(parameters map[string]interface{}) *Driver { var rootDirectory = defaultRootDirectory if parameters != nil { rootDir, ok := parameters["rootdirectory"] if ok { - rootDirectory = rootDir + rootDirectory = fmt.Sprint(rootDir) } } return New(rootDirectory) diff --git a/storagedriver/inmemory/driver.go b/storagedriver/inmemory/driver.go index 7481c4727..2e23c7588 100644 --- a/storagedriver/inmemory/driver.go +++ b/storagedriver/inmemory/driver.go @@ -21,7 +21,7 @@ func init() { // inMemoryDriverFacotry implements the factory.StorageDriverFactory interface. type inMemoryDriverFactory struct{} -func (factory *inMemoryDriverFactory) Create(parameters map[string]string) (storagedriver.StorageDriver, error) { +func (factory *inMemoryDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) { return New(), nil }