Change internal structure in Property object
This commit is contained in:
10
parser.go
10
parser.go
@@ -9,10 +9,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ParseFunction -- type
|
// ParseFunction -- type
|
||||||
type ParseFunction func(string, string) (map[string]string, error)
|
type ParseFunction func(string, string) (map[int]Pair, error)
|
||||||
|
|
||||||
// Default parse method for parsing key - value file
|
// Default parse method for parsing key - value file
|
||||||
func defaultParse(path, fileName string) (m map[string]string, err error) {
|
func defaultParse(path, fileName string) (m map[int]Pair, err error) {
|
||||||
absolutePathFile, err := filepath.Abs(filepath.Join(path, fileName))
|
absolutePathFile, err := filepath.Abs(filepath.Join(path, fileName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -23,8 +23,9 @@ func defaultParse(path, fileName string) (m map[string]string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
m = make(map[string]string)
|
m = make(map[int]Pair)
|
||||||
reader := bufio.NewReader(file)
|
reader := bufio.NewReader(file)
|
||||||
|
index := 0
|
||||||
for {
|
for {
|
||||||
line, err := reader.ReadString('\n')
|
line, err := reader.ReadString('\n')
|
||||||
|
|
||||||
@@ -37,7 +38,7 @@ func defaultParse(path, fileName string) (m map[string]string, err error) {
|
|||||||
value = strings.TrimSpace(strings.Replace(line[equal+1:], "\"", "", -1))
|
value = strings.TrimSpace(strings.Replace(line[equal+1:], "\"", "", -1))
|
||||||
}
|
}
|
||||||
// assign the values map
|
// assign the values map
|
||||||
m[key] = value
|
m[index] = Pair{key, value}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
@@ -46,6 +47,7 @@ func defaultParse(path, fileName string) (m map[string]string, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
index++
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,16 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Pair struct {
|
||||||
|
First string
|
||||||
|
Second string
|
||||||
|
}
|
||||||
|
|
||||||
// Properties -- type
|
// Properties -- type
|
||||||
type Properties struct {
|
type Properties struct {
|
||||||
fileName string
|
fileName string
|
||||||
path string
|
path string
|
||||||
values map[string]string
|
values map[int]Pair
|
||||||
length int
|
length int
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,7 +25,7 @@ func New(path, fileName string) Properties {
|
|||||||
return Properties{
|
return Properties{
|
||||||
fileName: fileName,
|
fileName: fileName,
|
||||||
path: filepath.Clean(path),
|
path: filepath.Clean(path),
|
||||||
values: make(map[string]string),
|
values: make(map[int]Pair),
|
||||||
length: 0,
|
length: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,7 +56,7 @@ func (p Properties) Length() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Values -- Getter values of property file
|
// Values -- Getter values of property file
|
||||||
func (p Properties) Values() map[string]string {
|
func (p Properties) Values() map[int]Pair {
|
||||||
return p.values
|
return p.values
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +66,7 @@ func (p *Properties) Put(key, value string) error {
|
|||||||
return e.New("Key value is nil")
|
return e.New("Key value is nil")
|
||||||
}
|
}
|
||||||
if p.values != nil {
|
if p.values != nil {
|
||||||
p.values[key] = value
|
p.values[p.length+1] = Pair{key, value}
|
||||||
p.length++
|
p.length++
|
||||||
} else {
|
} else {
|
||||||
return e.New("Property values is nil")
|
return e.New("Property values is nil")
|
||||||
@@ -74,10 +79,11 @@ func (p Properties) Get(key string) (string, error) {
|
|||||||
if key == "" || len(strings.TrimSpace(key)) == 0 {
|
if key == "" || len(strings.TrimSpace(key)) == 0 {
|
||||||
return "", e.New("Key value is nil")
|
return "", e.New("Key value is nil")
|
||||||
}
|
}
|
||||||
if _, ok := p.values[key]; !ok {
|
index := p.index(key)
|
||||||
|
if _, ok := p.values[index]; !ok {
|
||||||
return "", e.New("Key not found")
|
return "", e.New("Key not found")
|
||||||
}
|
}
|
||||||
return p.values[key], nil
|
return p.values[index].Second, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove -- Remove property with key
|
// Remove -- Remove property with key
|
||||||
@@ -86,7 +92,8 @@ func (p *Properties) Remove(key string) (string, error) {
|
|||||||
return "", e.New("Key value is nil")
|
return "", e.New("Key value is nil")
|
||||||
}
|
}
|
||||||
lenghtBefore := len(p.values)
|
lenghtBefore := len(p.values)
|
||||||
delete(p.values, key)
|
index := p.index(key)
|
||||||
|
delete(p.values, index)
|
||||||
if len(p.values) != lenghtBefore {
|
if len(p.values) != lenghtBefore {
|
||||||
p.length--
|
p.length--
|
||||||
}
|
}
|
||||||
@@ -95,12 +102,24 @@ func (p *Properties) Remove(key string) (string, error) {
|
|||||||
|
|
||||||
// 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 i := range p.values {
|
||||||
|
key := p.values[i].First
|
||||||
keys = append(keys, key)
|
keys = append(keys, key)
|
||||||
}
|
}
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p Properties) index(key string) int {
|
||||||
|
index := -1
|
||||||
|
for i, item := range p.values {
|
||||||
|
if item.First == key {
|
||||||
|
index = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index
|
||||||
|
}
|
||||||
|
|
||||||
// DefaultLoad -- Load file in Properties object using default parse function
|
// DefaultLoad -- Load file in Properties object using default parse function
|
||||||
func (p *Properties) DefaultLoad() (int, error) {
|
func (p *Properties) DefaultLoad() (int, error) {
|
||||||
return p.Load(defaultParse)
|
return p.Load(defaultParse)
|
||||||
|
|||||||
1
store.go
1
store.go
@@ -10,7 +10,6 @@ import (
|
|||||||
// StoringFunction -- type
|
// StoringFunction -- type
|
||||||
type StoringFunction func(Properties) (*os.File, error)
|
type StoringFunction func(Properties) (*os.File, error)
|
||||||
|
|
||||||
// FIXME when you remove a property it not work fine
|
|
||||||
func defaultStore(p Properties) (*os.File, error) {
|
func defaultStore(p Properties) (*os.File, error) {
|
||||||
absolutePathFile, err := filepath.Abs(filepath.Join(p.Path(), p.FileName()))
|
absolutePathFile, err := filepath.Abs(filepath.Join(p.Path(), p.FileName()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user