implemeted store function [partial]
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
TEST="RIUSCITO"
|
key1="value1"
|
||||||
TEST2="PROVIAMO DDDDD"
|
key2="value2"
|
||||||
URL="http://www.google.com"
|
key3="value3"
|
||||||
|
key4="value4"
|
||||||
|
key5="value5"
|
||||||
@@ -2,6 +2,7 @@ package properties
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
e "errors"
|
e "errors"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -79,6 +80,19 @@ func (p Properties) Get(key string) (string, error) {
|
|||||||
return p.values[key], nil
|
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
|
// GetProperties -- Get all key value in Properties object
|
||||||
func (p Properties) GetProperties() (keys []string) {
|
func (p Properties) GetProperties() (keys []string) {
|
||||||
for key := range p.values {
|
for key := range p.values {
|
||||||
@@ -103,7 +117,16 @@ func (p *Properties) Load(pf ParseFunction) (int, error) {
|
|||||||
return len(m), nil
|
return len(m), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store -- Create or modify property file with
|
// DefaultStore -- Create or modify property file
|
||||||
func (p Properties) Store() {
|
func (p Properties) DefaultStore() (*os.File, error) {
|
||||||
// TODO method not implemeted
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
func TestDefaultLoad(t *testing.T) {
|
||||||
path, _ := os.Getwd()
|
path, _ := os.Getwd()
|
||||||
p := prop.New(path, FILENAME)
|
p := prop.New(path, FILENAME)
|
||||||
@@ -184,3 +219,79 @@ func TestDefaultLoad(t *testing.T) {
|
|||||||
t.Failed()
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
54
store.go
Normal file
54
store.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user