From ccfe5ab055669a4c4f16ed44df0c66524e7e5ca2 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Tue, 10 Oct 2017 12:23:05 +0200 Subject: [PATCH] implemeted store function [partial] --- ini_test.properties | 8 ++-- properties.go | 29 ++++++++++-- properties_test.go | 111 ++++++++++++++++++++++++++++++++++++++++++++ store.go | 54 +++++++++++++++++++++ 4 files changed, 196 insertions(+), 6 deletions(-) create mode 100644 store.go diff --git a/ini_test.properties b/ini_test.properties index 9e42047..491abba 100644 --- a/ini_test.properties +++ b/ini_test.properties @@ -1,3 +1,5 @@ -TEST="RIUSCITO" -TEST2="PROVIAMO DDDDD" -URL="http://www.google.com" +key1="value1" +key2="value2" +key3="value3" +key4="value4" +key5="value5" \ No newline at end of file diff --git a/properties.go b/properties.go index ae4c423..e875cfb 100644 --- a/properties.go +++ b/properties.go @@ -2,6 +2,7 @@ package properties import ( e "errors" + "os" "path/filepath" "strings" ) @@ -79,6 +80,19 @@ func (p Properties) Get(key string) (string, error) { return p.values[key], nil } +// Remove -- Remove property with key +func (p *Properties) Remove(key string) (string, error) { + if key == "" || len(strings.TrimSpace(key)) == 0 { + return "", e.New("Key value is nil") + } + lenghtBefore := len(p.values) + delete(p.values, key) + if len(p.values) != lenghtBefore { + p.length-- + } + return key, nil +} + // GetProperties -- Get all key value in Properties object func (p Properties) GetProperties() (keys []string) { for key := range p.values { @@ -103,7 +117,16 @@ func (p *Properties) Load(pf ParseFunction) (int, error) { return len(m), nil } -// Store -- Create or modify property file with -func (p Properties) Store() { - // TODO method not implemeted +// DefaultStore -- Create or modify property file +func (p Properties) DefaultStore() (*os.File, error) { + return p.Store(defaultStore) +} + +// Store -- Create or modify property file with specific StoringFunction +func (p Properties) Store(sf StoringFunction) (*os.File, error) { + file, err := sf(p) + if err != nil { + return nil, err + } + return file, nil } diff --git a/properties_test.go b/properties_test.go index 9799f19..d855b42 100644 --- a/properties_test.go +++ b/properties_test.go @@ -169,6 +169,41 @@ func TestGetProperties(t *testing.T) { } } +func TestRemove(t *testing.T) { + path, _ := filepath.Abs(FILENAME) + p := prop.New(path, FILENAME) + p.Put("key1", "value1") + p.Put("key2", "value2") + p.Put("key3", "value3") + p.Put("key4", "value4") + p.Put("key5", "value5") + lengthBefore := len(p.Values()) + p.Remove("key4") + if p.Length() >= lengthBefore { + t.Logf("\n[!!] Failed error %s\n", "Property length is major") + t.Failed() + } +} + +func TestRemoveWithKeyNotFound(t *testing.T) { + path, _ := filepath.Abs(FILENAME) + p := prop.New(path, FILENAME) + p.Put("key1", "value1") + p.Put("key2", "value2") + p.Put("key3", "value3") + p.Put("key4", "value4") + p.Put("key5", "value5") + lengthBefore := len(p.Values()) + if _, err := p.Remove("KEY_NOT_FOUND"); err != nil { + t.Logf("\n[!!] Failed error %s\n", err.Error()) + t.Failed() + } + if p.Length() != lengthBefore { + t.Logf("\n[!!] Failed error %s\n", "Property length is different") + t.Failed() + } +} + func TestDefaultLoad(t *testing.T) { path, _ := os.Getwd() p := prop.New(path, FILENAME) @@ -184,3 +219,79 @@ func TestDefaultLoad(t *testing.T) { t.Failed() } } + +func TestDefaultStoreModifyOneValue(t *testing.T) { + path, _ := os.Getwd() + p := prop.New(path, FILENAME) + if _, err := p.DefaultLoad(); err != nil { + t.Logf("\n[!!] Failed error %s\n", err.Error()) + t.FailNow() + } + keys := p.GetProperties() + if len(keys) > 0 { + key := keys[0] + p.Put(key, "MODIFY_TEST") + + file, err := p.DefaultStore() + if err != nil { + t.Logf("\n[!!] Failed error %s\n", err.Error()) + t.Failed() + } + + if file == nil { + t.Logf("\n[!!] Failed error %s\n", "File is nil") + t.Failed() + } + } +} + +func TestDefaultStoreAddNewValue(t *testing.T) { + path, _ := os.Getwd() + p := prop.New(path, FILENAME) + if _, err := p.DefaultLoad(); err != nil { + t.Logf("\n[!!] Failed error %s\n", err.Error()) + t.FailNow() + } + + p.Put("NEW_KEY", "NEW_VALUE") + file, err := p.DefaultStore() + if err != nil { + t.Logf("\n[!!] Failed error %s\n", err.Error()) + t.Failed() + } + + if file == nil { + t.Logf("\n[!!] Failed error %s\n", "File is nil") + t.Failed() + } +} + +func TestDefaultStoreRemoveOneValue(t *testing.T) { + path, _ := os.Getwd() + p := prop.New(path, "testfile.properties") + + // Add values in Properties object + p.Put("key1", "value1") + p.Put("key2", "value2") + p.Put("key3", "value3") + p.Put("key4", "value4") + p.Put("key5", "value5") + + if p.Length() != 5 { + t.Logf("\n[!!] Failed error %s\n", "File is nil") + t.Failed() + } + + p.Remove("key3") + + file, err := p.DefaultStore() + if err != nil { + t.Logf("\n[!!] Failed error %s\n", err.Error()) + t.Failed() + } + + if file == nil { + t.Logf("\n[!!] Failed error %s\n", "File is nil") + t.Failed() + } +} diff --git a/store.go b/store.go new file mode 100644 index 0000000..6c59ff2 --- /dev/null +++ b/store.go @@ -0,0 +1,54 @@ +package properties + +import ( + "errors" + "os" + "path/filepath" + "strings" +) + +// StoringFunction -- type +type StoringFunction func(Properties) (*os.File, error) + +// FIXME when you remove a property it not work fine +func defaultStore(p Properties) (*os.File, error) { + absolutePathFile, err := filepath.Abs(filepath.Join(p.Path(), p.FileName())) + if err != nil { + return nil, err + } + file, err := os.OpenFile(absolutePathFile, os.O_CREATE, 0644) + if err != nil { + return nil, err + } + + // Write this file + defer file.Close() + for _, key := range p.GetProperties() { + value, err := p.Get(key) + if err != nil { + return nil, err + } + + row, err := buildRow(key, value) + if err != nil { + return nil, err + } + + if _, err := file.Write([]byte(row + "\n")); err != nil { + return nil, err + } + } + + return file, nil +} + +func buildRow(key, value string) (row string, err error) { + if key == "" || value == "" { + return "", errors.New("Key or Value param is a empty string") + } + + newKey := strings.Replace(key, "\"", "", -1) + newValue := strings.Join([]string{"\"", value, "\""}, "") + row = strings.Join([]string{newKey, newValue}, "=") + return strings.TrimSpace(row), nil +}