-
Notifications
You must be signed in to change notification settings - Fork 0
/
parrington.go
140 lines (110 loc) · 2.81 KB
/
parrington.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"errors"
"io/ioutil"
"strconv"
"strings"
)
// Parrington a GSDB parser written for
// internal usage inside the
// GSDB server for reading
// and deriving values from
// keys via methods for it
type Parrington struct {
body string
pairs []string
databasePath string
}
// EXTREMELY IMPORTANT FUNCTION
// to be called every time a
// variable of type Parrington
// is declared - it reads the
// file of the body and then
// fills the body parameter
// of the struct by referencing
// the variable by via a pointer
func (p *Parrington) writeToBody() {
// read file into `data`
data, err := ioutil.ReadFile(p.databasePath)
check(err)
// store inside local
// parameter of self
p.body = string(data)
}
// returns all key value
// paris of database raw
func (p Parrington) readBody() string {
return p.body
}
// EXTREMELY IMPORTANT FUNCTION
// fills the pairs[] parameter
// of the Parrington struct for
// easier access to reading lines
func (p *Parrington) writeToPairs() {
p.pairs = splitString(p.readBody(), "\n")
}
// returns value from key provided
// directory from the Parrington.paris
// by performing a linear search
func (p Parrington) getValue(key string) (string, error) {
for _, line := range p.pairs {
piece := splitString(line, " ")
if piece[0] == key {
returnValue := strings.Join(piece[2:], " ")
return dataInferer(returnValue) + " : " + returnValue, nil
}
}
return "", errors.New("no value found for key `" + key + "`")
}
// replaces line where
// where key is found
// with a backspace '\b'
func (p Parrington) delPair(key string) (string, error) {
input, err := ioutil.ReadFile(p.databasePath)
check(err)
lines := strings.Split(string(input), "\n")
for i, line := range lines {
if splitString(line, " ")[0] == key {
lines[i] = lines[len(lines)-1]
lines = lines[:len(lines)-1]
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(p.databasePath, []byte(output), 0644)
check(err)
return "deleted : " + key, nil
}
}
return "", errors.New("no value found for key `" + key + "`")
}
// returns all keys
// existing in database
func (p Parrington) getKeys() (string, error) {
var keys []string
for index, line := range p.pairs {
if len(line) == 0 {
continue
}
piece := splitString(line, " ")
keys = append(keys, strconv.Itoa(index+1)+" : "+piece[0])
}
if len(keys) != 0 {
return strings.Join(keys, "\n"), nil
}
return "", errors.New("no keys in database")
}
// returns all keys
// existing in database
// in a slice
func (p Parrington) getKeysSlice() ([]string, error) {
var keys []string
for _, line := range p.pairs {
if len(line) == 0 {
continue
}
piece := splitString(line, " ")
keys = append(keys, piece[0])
}
if len(keys) != 0 {
return keys, nil
}
return keys, errors.New("no keys in database")
}