Migrate pkg/quick from mc

master
Harshavardhana 9 years ago
parent e4dc8ee7b4
commit e9c5a51bc6
  1. 20
      pkg/quick/quick.go
  2. 21
      pkg/quick/quick_test.go

@ -45,7 +45,7 @@ type Config interface {
// config - implements quick.Config interface
type config struct {
data *interface{}
data interface{}
lock *sync.RWMutex
}
@ -76,14 +76,14 @@ func New(data interface{}) (Config, *probe.Error) {
}
d := new(config)
d.data = &data
d.data = data
d.lock = new(sync.RWMutex)
return d, nil
}
// Version returns the current config file format version
func (d config) Version() string {
st := structs.New(*d.data)
st := structs.New(d.data)
f, ok := st.FieldOk("Version")
if !ok {
@ -100,7 +100,7 @@ func (d config) Version() string {
// String converts JSON config to printable string
func (d config) String() string {
configBytes, _ := json.MarshalIndent(*d.data, "", "\t")
configBytes, _ := json.MarshalIndent(d.data, "", "\t")
return string(configBytes)
}
@ -128,8 +128,8 @@ func (d config) Save(filename string) *probe.Error {
// Load - loads JSON config from file and merge with currently set values
func (d *config) Load(filename string) *probe.Error {
(*d).lock.Lock()
defer (*d).lock.Unlock()
d.lock.Lock()
defer d.lock.Unlock()
_, err := os.Stat(filename)
if err != nil {
@ -145,16 +145,16 @@ func (d *config) Load(filename string) *probe.Error {
fileData = []byte(strings.Replace(string(fileData), "\r\n", "\n", -1))
}
err = json.Unmarshal(fileData, (*d).data)
err = json.Unmarshal(fileData, d.data)
if err != nil {
return probe.NewError(err)
}
if err := CheckData(*(*d).data); err != nil {
if err := CheckData(d.data); err != nil {
return err.Trace()
}
st := structs.New(*(*d).data)
st := structs.New(d.data)
f, ok := st.FieldOk("Version")
if !ok {
return probe.NewError(fmt.Errorf("Argument struct [%s] does not contain field \"Version\".", st.Name()))
@ -169,7 +169,7 @@ func (d *config) Load(filename string) *probe.Error {
// Data - grab internal data map for reading
func (d config) Data() interface{} {
return *d.data
return d.data
}
//Diff - list fields that are in A but not in B

@ -16,13 +16,14 @@
* limitations under the License.
*/
package quick
package quick_test
import (
"os"
"testing"
. "github.com/minio/minio/internal/gopkg.in/check.v1"
"github.com/minio/minio/pkg/quick"
)
func Test(t *testing.T) { TestingT(t) }
@ -32,7 +33,7 @@ type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) TestCheckData(c *C) {
err := CheckData(nil)
err := quick.CheckData(nil)
c.Assert(err, Not(IsNil))
type myStructBad struct {
@ -41,7 +42,7 @@ func (s *MySuite) TestCheckData(c *C) {
Folders []string
}
saveMeBad := myStructBad{"guest", "nopassword", []string{"Work", "Documents", "Music"}}
err = CheckData(&saveMeBad)
err = quick.CheckData(&saveMeBad)
c.Assert(err, Not(IsNil))
type myStructGood struct {
@ -52,7 +53,7 @@ func (s *MySuite) TestCheckData(c *C) {
}
saveMeGood := myStructGood{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
err = CheckData(&saveMeGood)
err = quick.CheckData(&saveMeGood)
c.Assert(err, IsNil)
}
@ -65,13 +66,13 @@ func (s *MySuite) TestSaveLoad(c *C) {
Folders []string
}
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
config, err := New(&saveMe)
config, err := quick.New(&saveMe)
c.Assert(err, IsNil)
c.Assert(config, Not(IsNil))
config.Save("test.json")
loadMe := myStruct{Version: "1"}
newConfig, err := New(&loadMe)
newConfig, err := quick.New(&loadMe)
c.Assert(err, IsNil)
c.Assert(newConfig, Not(IsNil))
newConfig.Load("test.json")
@ -91,7 +92,7 @@ func (s *MySuite) TestDiff(c *C) {
Folders []string
}
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
config, err := New(&saveMe)
config, err := quick.New(&saveMe)
c.Assert(err, IsNil)
c.Assert(config, Not(IsNil))
@ -103,7 +104,7 @@ func (s *MySuite) TestDiff(c *C) {
}
mismatch := myNewStruct{"1", "nopassword", []string{"Work", "documents", "Music"}}
newConfig, err := New(&mismatch)
newConfig, err := quick.New(&mismatch)
c.Assert(err, IsNil)
c.Assert(newConfig, Not(IsNil))
@ -125,12 +126,12 @@ func (s *MySuite) TestDeepDiff(c *C) {
Folders []string
}
saveMe := myStruct{"1", "guest", "nopassword", []string{"Work", "Documents", "Music"}}
config, err := New(&saveMe)
config, err := quick.New(&saveMe)
c.Assert(err, IsNil)
c.Assert(config, Not(IsNil))
mismatch := myStruct{"1", "Guest", "nopassword", []string{"Work", "documents", "Music"}}
newConfig, err := New(&mismatch)
newConfig, err := quick.New(&mismatch)
c.Assert(err, IsNil)
c.Assert(newConfig, Not(IsNil))

Loading…
Cancel
Save