Skip to content

Commit e32a1db

Browse files
authored
Merge pull request #23 from ulfox/feat/delete-getpath-global
feat: add Global GetPath and Delete
2 parents 8215258 + 89e98da commit e32a1db

File tree

10 files changed

+335
-137
lines changed

10 files changed

+335
-137
lines changed

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: clean
22
clean:
3-
rm -rf .test
3+
rm -rf tests/.test;
44

55
.PHONY: clean
66
lint:
@@ -11,5 +11,6 @@ lint:
1111

1212

1313
.PHONY: test
14-
test:
15-
go test -v .
14+
test: clean
15+
go test -v ./tests/
16+
rmdir ./tests/.test

db/constants.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ const (
1616
arrayMapStringArrayInterface
1717
)
1818

19+
// Inormational Error constants. Used during a return err
20+
const (
21+
notAMap = "target object is not a map"
22+
notArrayObj = "received a non array object but expected []interface{}"
23+
keyDoesNotExist = "the given key [%s] does not exist"
24+
fileNotExist = "the given file [%s] does not exist"
25+
dictNotFile = "can not create file [%s], a directory exists with that name"
26+
notAnIndex = "object (%s) is not an index. Index example: some.path.[someInteger].someKey"
27+
arrayOutOfRange = "index value (%s) is bigger than the length (%s) of the array to be indexed"
28+
invalidKeyPath = "the key||path [%s] that was given is not valid"
29+
emptyKey = "path [%s] contains an empty key"
30+
libOutOfIndex = "lib out of index"
31+
docNotExists = "doc [%s] does not exist in lib"
32+
fieldNotString = "[%s] with value [%s] is not a string"
33+
notAType = "value is not a %s"
34+
)
35+
1936
func getObjectType(o interface{}) objectType {
2037
_, isMap := o.(map[interface{}]interface{})
2138
if isMap {

db/convert.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package db
22

33
import (
4-
"fmt"
5-
64
"gopkg.in/yaml.v2"
75
)
86

@@ -64,7 +62,7 @@ func (a *AssertData) GetString() (string, error) {
6462

6563
s, isString := a.cache.V1.(string)
6664
if !isString {
67-
a.setErr(wrapErr(fmt.Errorf(notAType, "string"), getFn()))
65+
a.setErr(wrapErr(notAType, "string"))
6866
return "", a.GetError()
6967
}
7068

@@ -79,7 +77,7 @@ func (a *AssertData) GetInt() (int, error) {
7977

8078
i, isInt := a.cache.V1.(int)
8179
if !isInt {
82-
a.setErr(wrapErr(fmt.Errorf(notAType, "int"), getFn()))
80+
a.setErr(wrapErr(notAType, "int"))
8381
return 0, a.GetError()
8482
}
8583

@@ -112,7 +110,7 @@ func (a *AssertData) GetArray() ([]string, error) {
112110

113111
_, isArray := a.cache.V1.([]interface{})
114112
if !isArray {
115-
a.setErr(wrapErr(fmt.Errorf(notArrayObj), getFn()))
113+
a.setErr(wrapErr(notArrayObj))
116114
return nil, a.GetError()
117115
}
118116

@@ -138,7 +136,7 @@ func (a *AssertData) Key(k string) *AssertData {
138136

139137
_, isMap := a.cache.V1.(map[interface{}]interface{})
140138
if !isMap {
141-
return a.setErr(wrapErr(fmt.Errorf(notAMap), getFn()))
139+
return a.setErr(wrapErr(notAMap))
142140
}
143141

144142
a.cache.V1 = a.cache.V1.(map[interface{}]interface{})[k]
@@ -154,7 +152,7 @@ func (a *AssertData) Index(i int) *AssertData {
154152

155153
_, isArray := a.cache.V1.([]interface{})
156154
if !isArray {
157-
return a.setErr(wrapErr(fmt.Errorf(notArrayObj), getFn()))
155+
return a.setErr(wrapErr(notArrayObj))
158156
}
159157
a.cache.V1 = a.cache.V1.([]interface{})[i]
160158

db/errors.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

db/sql.go

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package db
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"strconv"
76
"strings"
87

9-
"github.com/pkg/errors"
108
"gopkg.in/yaml.v2"
119
)
1210

@@ -100,34 +98,31 @@ func (s *SQL) getArrayObject(k string, o interface{}) (interface{}, bool) {
10098

10199
func (s *SQL) getIndex(k string) (int, error) {
102100
if !strings.HasPrefix(k, "[") || !strings.HasSuffix(k, "]") {
103-
return 0, wrapErr(fmt.Errorf(notAnIndex, k), getFn())
101+
return 0, wrapErr(notAnIndex, k)
104102
}
105103

106104
intVar, err := strconv.Atoi(k[1 : len(k)-1])
107105
if err != nil {
108-
return 0, wrapErr(err, getFn())
106+
return 0, wrapErr(err)
109107
}
110108
return intVar, nil
111109
}
112110

113111
func (s *SQL) getFromIndex(k []string, o interface{}) (interface{}, error) {
114112
if getObjectType(o) != arrayObj {
115-
return nil, wrapErr(errors.New(notArrayObj), getFn())
113+
return nil, wrapErr(notArrayObj)
116114
}
117115

118116
i, err := s.getIndex(k[0])
119117
if err != nil {
120-
return nil, wrapErr(err, getFn())
118+
return nil, wrapErr(err)
121119
}
122120

123121
if i > len(o.([]interface{}))-1 {
124122
return nil, wrapErr(
125-
fmt.Errorf(
126-
arrayOutOfRange,
127-
strconv.Itoa(i),
128-
strconv.Itoa(len(o.([]interface{}))-1),
129-
),
130-
getFn(),
123+
arrayOutOfRange,
124+
strconv.Itoa(i),
125+
strconv.Itoa(len(o.([]interface{}))-1),
131126
)
132127
}
133128

@@ -140,7 +135,7 @@ func (s *SQL) getFromIndex(k []string, o interface{}) (interface{}, error) {
140135

141136
func (s *SQL) getPath(k []string, o interface{}) (interface{}, error) {
142137
if err := checkKeyPath(k); err != nil {
143-
return nil, wrapErr(err, getFn())
138+
return nil, wrapErr(err)
144139
}
145140

146141
obj, err := interfaceToMap(o)
@@ -149,7 +144,7 @@ func (s *SQL) getPath(k []string, o interface{}) (interface{}, error) {
149144
}
150145

151146
if len(k) == 0 {
152-
return nil, wrapErr(fmt.Errorf(keyDoesNotExist, k[0]), getFn())
147+
return nil, wrapErr(keyDoesNotExist, k[0])
153148
}
154149

155150
for thisKey, thisObj := range obj {
@@ -163,12 +158,12 @@ func (s *SQL) getPath(k []string, o interface{}) (interface{}, error) {
163158

164159
objFinal, err := s.getPath(k[1:], thisObj)
165160
if err != nil {
166-
return nil, wrapErr(err, getFn())
161+
return nil, wrapErr(err)
167162
}
168163
return objFinal, nil
169164
}
170165

171-
return nil, wrapErr(fmt.Errorf(keyDoesNotExist, k[0]), getFn())
166+
return nil, wrapErr(keyDoesNotExist, k[0])
172167
}
173168

174169
func (s *SQL) deleteArrayItem(k string, o interface{}) bool {
@@ -202,29 +197,29 @@ func (s *SQL) deleteItem(k string, o interface{}) bool {
202197
func (s *SQL) delPath(k string, o interface{}) error {
203198
keys := strings.Split(k, ".")
204199
if err := checkKeyPath(keys); err != nil {
205-
return wrapErr(err, getFn())
200+
return wrapErr(err)
206201
}
207202

208203
if len(keys) == 0 {
209-
return wrapErr(fmt.Errorf(invalidKeyPath, k), getFn())
204+
return wrapErr(invalidKeyPath, k)
210205
}
211206

212207
if len(keys) == 1 {
213208
if !s.deleteItem(keys[0], o) {
214-
return wrapErr(fmt.Errorf(keyDoesNotExist, k), getFn())
209+
return wrapErr(keyDoesNotExist, k)
215210
}
216211
return nil
217212
}
218213

219214
s.Cache.dropKeys()
220215
obj, err := s.getPath(keys[:len(keys)-1], o)
221216
if err != nil {
222-
return wrapErr(err, getFn())
217+
return wrapErr(err)
223218
}
224219

225220
s.Cache.dropKeys()
226221
if !s.deleteItem(keys[len(keys)-1], obj) {
227-
return wrapErr(fmt.Errorf(keyDoesNotExist, k), getFn())
222+
return wrapErr(keyDoesNotExist, k)
228223
}
229224

230225
return nil
@@ -237,7 +232,7 @@ func (s *SQL) get(k string, o interface{}) ([]string, error) {
237232
s.Clear()
238233
s.Cache.V1, err = copyMap(o)
239234
if err != nil {
240-
return nil, wrapErr(err, getFn())
235+
return nil, wrapErr(err)
241236
}
242237

243238
for {
@@ -249,7 +244,7 @@ func (s *SQL) get(k string, o interface{}) ([]string, error) {
249244
s.Query.KeysFound = append(s.Query.KeysFound, key)
250245

251246
if err := s.delPath(key, s.Cache.V1); err != nil {
252-
return s.Query.KeysFound, wrapErr(err, getFn())
247+
return s.Query.KeysFound, wrapErr(err)
253248
}
254249
s.Cache.dropKeys()
255250
}
@@ -262,22 +257,22 @@ func (s *SQL) getFirst(k string, o interface{}) (interface{}, error) {
262257

263258
keys, err := s.get(k, o)
264259
if err != nil {
265-
return nil, wrapErr(err, getFn())
260+
return nil, wrapErr(err)
266261
}
267262

268263
if len(keys) == 0 {
269-
return nil, wrapErr(fmt.Errorf(keyDoesNotExist, k), getFn())
264+
return nil, wrapErr(keyDoesNotExist, k)
270265
}
271266

272267
keySlice := strings.Split(keys[0], ".")
273268
if err := checkKeyPath(keySlice); err != nil {
274-
return nil, wrapErr(err, getFn())
269+
return nil, wrapErr(err)
275270
}
276271

277272
s.Cache.C1 = len(keySlice)
278273
if len(keys) == 1 {
279274
path, err := s.getPath(keySlice, o)
280-
return path, wrapErr(err, getFn())
275+
return path, wrapErr(err)
281276
}
282277

283278
for i, key := range keys[1:] {
@@ -288,19 +283,19 @@ func (s *SQL) getFirst(k string, o interface{}) (interface{}, error) {
288283
}
289284

290285
path, err := s.getPath(strings.Split(keys[s.Cache.C2], "."), o)
291-
return path, wrapErr(err, getFn())
286+
return path, wrapErr(err)
292287
}
293288

294289
func (s *SQL) upsertRecursive(k []string, o, v interface{}) error {
295290
s.Clear()
296291

297292
if err := checkKeyPath(k); err != nil {
298-
return wrapErr(err, getFn())
293+
return wrapErr(err)
299294
}
300295

301296
obj, err := interfaceToMap(o)
302297
if err != nil {
303-
return wrapErr(err, getFn())
298+
return wrapErr(err)
304299
}
305300

306301
for thisKey, thisObj := range obj {
@@ -309,7 +304,7 @@ func (s *SQL) upsertRecursive(k []string, o, v interface{}) error {
309304
}
310305

311306
if len(k) > 1 {
312-
return wrapErr(s.upsertRecursive(k[1:], thisObj, v), getFn())
307+
return wrapErr(s.upsertRecursive(k[1:], thisObj, v))
313308
}
314309

315310
switch getObjectType(thisObj) {
@@ -325,7 +320,7 @@ func (s *SQL) upsertRecursive(k []string, o, v interface{}) error {
325320
obj[k[0]] = make(map[interface{}]interface{})
326321

327322
if len(k) > 1 {
328-
return wrapErr(s.upsertRecursive(k[1:], obj[k[0]], v), getFn())
323+
return wrapErr(s.upsertRecursive(k[1:], obj[k[0]], v))
329324
}
330325

331326
obj[k[0]] = v
@@ -338,29 +333,29 @@ func (s *SQL) mergeDBs(path string, o interface{}) error {
338333

339334
ok, err := fileExists(path)
340335
if err != nil {
341-
return wrapErr(err, getFn())
336+
return wrapErr(err)
342337
}
343338

344339
if !ok {
345-
return wrapErr(fmt.Errorf(fileNotExist, path), getFn())
340+
return wrapErr(fileNotExist, path)
346341
}
347342

348343
f, err := ioutil.ReadFile(path)
349344
if err != nil {
350-
return wrapErr(err, getFn())
345+
return wrapErr(err)
351346
}
352347

353348
yaml.Unmarshal(f, &dataNew)
354349

355350
obj, err := interfaceToMap(dataNew)
356351
if err != nil {
357-
return wrapErr(err, getFn())
352+
return wrapErr(err)
358353
}
359354

360355
for kn, vn := range obj {
361356
err = s.upsertRecursive(strings.Split(kn.(string), "."), o, vn)
362357
if err != nil {
363-
return wrapErr(err, getFn())
358+
return wrapErr(err)
364359
}
365360
}
366361
return nil

0 commit comments

Comments
 (0)