Merge pull request #892 from harshavardhana/add-quick-version

Add quick.CheckVersion() to verify config version quickly before unma…
master
Harshavardhana 9 years ago
commit e719adec8b
  1. 108
      pkg/quick/quick.go
  2. 23
      pkg/quick/quick_test.go

@ -82,6 +82,80 @@ func New(data interface{}) (Config, *probe.Error) {
return d, nil
}
// CheckVersion - loads json and compares the version number provided returns back true or false - any failure
// is returned as error.
func CheckVersion(filename string, version string) (bool, *probe.Error) {
_, err := os.Stat(filename)
if err != nil {
return false, probe.NewError(err)
}
fileData, err := ioutil.ReadFile(filename)
if err != nil {
return false, probe.NewError(err)
}
if runtime.GOOS == "windows" {
fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
}
data := struct {
Version string
}{
Version: "",
}
err = json.Unmarshal(fileData, &data)
if err != nil {
switch err := err.(type) {
case *json.SyntaxError:
return false, probe.NewError(FormatJSONSyntaxError(bytes.NewReader(fileData), err))
default:
return false, probe.NewError(err)
}
}
config, perr := New(data)
if perr != nil {
return false, perr.Trace()
}
if config.Version() != version {
return false, nil
}
return true, nil
}
// Load - loads json config from filename for the a given struct data
func Load(filename string, data interface{}) (Config, *probe.Error) {
_, err := os.Stat(filename)
if err != nil {
return nil, probe.NewError(err)
}
fileData, err := ioutil.ReadFile(filename)
if err != nil {
return nil, probe.NewError(err)
}
if runtime.GOOS == "windows" {
fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
}
err = json.Unmarshal(fileData, &data)
if err != nil {
switch err := err.(type) {
case *json.SyntaxError:
return nil, probe.NewError(FormatJSONSyntaxError(bytes.NewReader(fileData), err))
default:
return nil, probe.NewError(err)
}
}
config, perr := New(data)
if perr != nil {
return nil, perr.Trace()
}
return config, nil
}
// Version returns the current config file format version
func (d config) Version() string {
st := structs.New(d.data)
@ -127,40 +201,6 @@ func (d config) Save(filename string) *probe.Error {
return nil
}
// Load - loads json config
func Load(filename string, data interface{}) (Config, *probe.Error) {
_, err := os.Stat(filename)
if err != nil {
return nil, probe.NewError(err)
}
fileData, err := ioutil.ReadFile(filename)
if err != nil {
return nil, probe.NewError(err)
}
if runtime.GOOS == "windows" {
fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
}
err = json.Unmarshal(fileData, &data)
if err != nil {
switch err := err.(type) {
case *json.SyntaxError:
return nil, probe.NewError(FormatJSONSyntaxError(bytes.NewReader(fileData), err))
default:
return nil, probe.NewError(err)
}
}
config, perr := New(data)
if perr != nil {
return nil, perr.Trace()
}
return config, nil
}
// Load - loads JSON config from file and merge with currently set values
func (d *config) Load(filename string) *probe.Error {
d.lock.Lock()

@ -57,6 +57,29 @@ func (s *MySuite) TestCheckData(c *C) {
c.Assert(err, IsNil)
}
func (s *MySuite) TestVersion(c *C) {
defer os.RemoveAll("test.json")
type myStruct struct {
Version string
User string
Password string
Folders []string
}
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
config, err := quick.New(&saveMe)
c.Assert(err, IsNil)
c.Assert(config, Not(IsNil))
config.Save("test.json")
valid, err := quick.CheckVersion("test.json", "1")
c.Assert(err, IsNil)
c.Assert(valid, Equals, true)
valid, err = quick.CheckVersion("test.json", "2")
c.Assert(err, IsNil)
c.Assert(valid, Equals, false)
}
func (s *MySuite) TestSaveLoad(c *C) {
defer os.RemoveAll("test.json")
type myStruct struct {

Loading…
Cancel
Save