quick: Add yaml format support (#3833)

quick Save() and Load() infers config file's format from
file name extension.
This commit is contained in:
Anis Elleuch
2017-03-03 19:22:09 +01:00
committed by Harshavardhana
parent bc52d911ef
commit 6c00a57a7c
21 changed files with 9686 additions and 40 deletions

142
pkg/quick/encoding.go Normal file
View File

@@ -0,0 +1,142 @@
/*
* Quick - Quick key value store for config files and persistent state files
*
* Quick (C) 2017 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package quick
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
yaml "gopkg.in/yaml.v2"
)
// ConfigEncoding is a generic interface which
// marshal/unmarshal configuration.
type ConfigEncoding interface {
Unmarshal([]byte, interface{}) error
Marshal(interface{}) ([]byte, error)
}
// YAML encoding implements ConfigEncoding
type yamlEncoding struct{}
func (y yamlEncoding) Unmarshal(b []byte, v interface{}) error {
return yaml.Unmarshal(b, v)
}
func (y yamlEncoding) Marshal(v interface{}) ([]byte, error) {
return yaml.Marshal(v)
}
// JSON encoding implements ConfigEncoding
type jsonEncoding struct{}
func (j jsonEncoding) Unmarshal(b []byte, v interface{}) error {
err := json.Unmarshal(b, v)
if err != nil {
// Try to return a sophisticated json error message if possible
switch err := err.(type) {
case *json.SyntaxError:
return FormatJSONSyntaxError(bytes.NewReader(b), err)
default:
return err
}
}
return nil
}
func (j jsonEncoding) Marshal(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", "\t")
}
// Convert a file extension to the appropriate struct capable
// to marshal/unmarshal data
func ext2EncFormat(fileExtension string) ConfigEncoding {
// Lower the file extension
ext := strings.ToLower(fileExtension)
ext = strings.TrimPrefix(ext, ".")
// Return the appropriate encoder/decoder according
// to the extension
switch ext {
case "yml", "yaml":
// YAML
return yamlEncoding{}
default:
// JSON
return jsonEncoding{}
}
}
// toMarshaller returns the right marshal function according
// to the given file extension
func toMarshaller(ext string) func(interface{}) ([]byte, error) {
return ext2EncFormat(ext).Marshal
}
// toUnmarshaller returns the right marshal function according
// to the given file extension
func toUnmarshaller(ext string) func([]byte, interface{}) error {
return ext2EncFormat(ext).Unmarshal
}
// saveFileConfig marshals with the right encoding format
// according to the filename extension, if no extension is
// provided, json will be selected.
func saveFileConfig(filename string, v interface{}) error {
// Fetch filename's extension
ext := filepath.Ext(filename)
// Marshal data
dataBytes, err := toMarshaller(ext)(v)
if err != nil {
return err
}
if runtime.GOOS == "windows" {
dataBytes = []byte(strings.Replace(string(dataBytes), "\n", "\r\n", -1))
}
// Save data.
return writeFile(filename, dataBytes)
}
// loadFileConfig unmarshals the file's content with the right
// decoder format according to the filename extension. If no
// extension is provided, json will be selected by default.
func loadFileConfig(filename string, v interface{}) error {
_, err := os.Stat(filename)
if err != nil {
return err
}
fileData, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
if runtime.GOOS == "windows" {
fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
}
ext := filepath.Ext(filename)
// Unmarshal file's content
if err := toUnmarshaller(ext)(fileData, v); err != nil {
return err
}
return nil
}

View File

@@ -1,7 +1,7 @@
/*
* Quick - Quick key value store for config files and persistent state files
*
* Minio Client (C) 2015 Minio, Inc.
* Quick (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,7 +1,7 @@
/*
* Quick - Quick key value store for config files and persistent state files
*
* Minio Client (C) 2015 Minio, Inc.
* Quick (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,14 +19,11 @@
package quick
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"runtime"
"strings"
"sync"
"github.com/fatih/structs"
@@ -151,7 +148,9 @@ func (d config) String() string {
return string(configBytes)
}
// Save writes config data in JSON format to a file.
// Save writes config data to a file. Data format
// is selected based on file extension or JSON if
// not provided.
func (d config) Save(filename string) error {
d.lock.Lock()
defer d.lock.Unlock()
@@ -179,53 +178,26 @@ func (d config) Save(filename string) error {
return err
}
}
// Proceed to create or overwrite file.
jsonData, err := json.MarshalIndent(d.data, "", "\t")
if err != nil {
return err
}
if runtime.GOOS == "windows" {
jsonData = []byte(strings.Replace(string(jsonData), "\n", "\r\n", -1))
}
// Save data.
return writeFile(filename, jsonData)
return saveFileConfig(filename, d.data)
}
// Load - loads JSON config from file and merge with currently set values
// Load - loads config from file and merge with currently set values
// File content format is guessed from the file name extension, if not
// available, consider that we have JSON.
func (d *config) Load(filename string) error {
d.lock.Lock()
defer d.lock.Unlock()
_, err := os.Stat(filename)
if err != nil {
return err
}
fileData, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
if runtime.GOOS == "windows" {
fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
}
st := structs.New(d.data)
f, ok := st.FieldOk("Version")
if !ok {
return fmt.Errorf("Argument struct [%s] does not contain field \"Version\"", st.Name())
}
err = json.Unmarshal(fileData, d.data)
if err != nil {
switch err := err.(type) {
case *json.SyntaxError:
return FormatJSONSyntaxError(bytes.NewReader(fileData), err)
default:
return err
}
if err := loadFileConfig(filename, d.data); err != nil {
return err
}
if err := CheckData(d.data); err != nil {

View File

@@ -1,7 +1,7 @@
/*
* Quick - Quick key value store for config files and persistent state files
*
* Minio Client (C) 2015 Minio, Inc.
* Quick (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +20,11 @@ package quick_test
import (
"encoding/json"
"io/ioutil"
"os"
"reflect"
"runtime"
"strings"
"testing"
"github.com/minio/minio/pkg/quick"
@@ -122,6 +126,106 @@ func (s *MySuite) TestLoadFile(c *C) {
c.Assert(saveMe2, DeepEquals, saveMe1)
}
func (s *MySuite) TestYAMLFormat(c *C) {
testYAML := "test.yaml"
defer os.RemoveAll(testYAML)
type myStruct struct {
Version string
User string
Password string
Directories []string
}
plainYAML := `version: "1"
user: guest
password: nopassword
directories:
- Work
- Documents
- Music
`
if runtime.GOOS == "windows" {
plainYAML = strings.Replace(plainYAML, "\n", "\r\n", -1)
}
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
// Save format using
config, err := quick.New(&saveMe)
c.Assert(err, IsNil)
c.Assert(config, Not(IsNil))
err = config.Save(testYAML)
c.Assert(err, IsNil)
// Check if the saved structure in actually an YAML format
bytes, err := ioutil.ReadFile(testYAML)
c.Assert(err, IsNil)
c.Assert(plainYAML, Equals, string(bytes))
// Check if the loaded data is the same as the saved one
loadMe := myStruct{}
config, err = quick.New(&loadMe)
err = config.Load(testYAML)
c.Assert(err, IsNil)
c.Assert(reflect.DeepEqual(saveMe, loadMe), Equals, true)
}
func (s *MySuite) TestJSONFormat(c *C) {
testJSON := "test.json"
defer os.RemoveAll(testJSON)
type myStruct struct {
Version string
User string
Password string
Directories []string
}
plainJSON := `{
"Version": "1",
"User": "guest",
"Password": "nopassword",
"Directories": [
"Work",
"Documents",
"Music"
]
}`
if runtime.GOOS == "windows" {
plainJSON = strings.Replace(plainJSON, "\n", "\r\n", -1)
}
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
// Save format using
config, err := quick.New(&saveMe)
c.Assert(err, IsNil)
c.Assert(config, Not(IsNil))
err = config.Save(testJSON)
c.Assert(err, IsNil)
// Check if the saved structure in actually an JSON format
bytes, err := ioutil.ReadFile(testJSON)
c.Assert(err, IsNil)
c.Assert(plainJSON, Equals, string(bytes))
// Check if the loaded data is the same as the saved one
loadMe := myStruct{}
config, err = quick.New(&loadMe)
err = config.Load(testJSON)
c.Assert(err, IsNil)
c.Assert(reflect.DeepEqual(saveMe, loadMe), Equals, true)
}
func (s *MySuite) TestVersion(c *C) {
defer os.RemoveAll("test.json")
type myStruct struct {