Skip to content

Commit 9b05be5

Browse files
author
Christos Kotsis
committed
[cache/v1/v2] make fields private
1 parent 7c5211b commit 9b05be5

File tree

4 files changed

+154
-52
lines changed

4 files changed

+154
-52
lines changed

cache/v1/v1.go

Lines changed: 127 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,162 @@ package v1
55
// for condition, while []Keys is used by path discovery methods
66
// to keep track and derive the right path.
77
type Cache struct {
8-
V1, V2 interface{}
9-
V3 []interface{}
10-
B []byte
11-
E error
12-
C1, C2, C3 int
13-
Keys []string
8+
v1, v2 interface{}
9+
v3 []interface{}
10+
b []byte
11+
e error
12+
c1, c2, c3 int
13+
keys []string
1414
}
1515

1616
// NewCacheFactory for creating a new Cache
1717
func NewCacheFactory() Cache {
1818
cache := Cache{
19-
V3: make([]interface{}, 0),
20-
B: make([]byte, 0),
21-
Keys: make([]string, 0),
19+
v3: make([]interface{}, 0),
20+
b: make([]byte, 0),
21+
keys: make([]string, 0),
2222
}
2323
return cache
2424
}
2525

26-
// GetError returns cache error field
27-
func (c *Cache) GetError() error {
28-
return c.E
26+
// E can be used to set or get e. If no argument is passed
27+
// the method returns e value. If an argument is passed, the method
28+
// sets e's value to that of the arguments
29+
func (c *Cache) E(e ...error) error {
30+
if len(e) > 0 {
31+
c.e = e[0]
32+
}
33+
return c.e
2934
}
3035

3136
// Clear for clearing the cache and setting all
3237
// content to nil
3338
func (c *Cache) Clear() {
34-
c.V1, c.V2 = nil, nil
35-
c.V3 = make([]interface{}, 0)
36-
c.C1, c.C2, c.C3 = 0, 0, 0
37-
c.B = make([]byte, 0)
38-
c.E = nil
39-
c.Keys = make([]string, 0)
39+
c.v1, c.v2 = nil, nil
40+
c.v3 = make([]interface{}, 0)
41+
c.c1, c.c2, c.c3 = 0, 0, 0
42+
c.b = make([]byte, 0)
43+
c.e = nil
44+
c.keys = make([]string, 0)
4045
}
4146

4247
// DropLastKey removes the last key added from the list
4348
func (c *Cache) DropLastKey() {
44-
if len(c.Keys) > 0 {
45-
c.Keys = c.Keys[:len(c.Keys)-1]
49+
if len(c.keys) > 0 {
50+
c.keys = c.keys[:len(c.keys)-1]
4651
}
4752
}
4853

4954
// DropKeys removes all keys from the list
5055
func (c *Cache) DropKeys() {
51-
c.Keys = make([]string, 0)
56+
c.keys = make([]string, 0)
5257
}
5358

5459
// AddKey for appending a new Key to cache
5560
func (c *Cache) AddKey(k string) {
56-
c.Keys = append(c.Keys, k)
61+
c.keys = append(c.keys, k)
5762
}
5863

5964
// GetKeys returns Cache.Keys
6065
func (c *Cache) GetKeys() []string {
61-
return c.Keys
66+
return c.keys
67+
}
68+
69+
// DropV3 removes all keys from the list
70+
func (c *Cache) DropV3() {
71+
c.v3 = make([]interface{}, 0)
72+
}
73+
74+
// V3 returns Cache.V3
75+
func (c *Cache) V3(v ...interface{}) []interface{} {
76+
if len(v) > 0 {
77+
c.v3 = append(c.v3, v[0])
78+
}
79+
return c.v3
80+
}
81+
82+
// C1 can be used to set or get c1. If no argument is passed
83+
// the method returns c1 value. If an argument is passed, the method
84+
// sets c1's value to that of the arguments
85+
func (c *Cache) C1(i ...int) int {
86+
if len(i) > 0 {
87+
c.c1 = i[0]
88+
}
89+
return c.c1
90+
}
91+
92+
// C2 can be used to set or get c2. If no argument is passed
93+
// the method returns c2 value. If an argument is passed, the method
94+
// sets c2's value to that of the arguments
95+
func (c *Cache) C2(i ...int) int {
96+
if len(i) > 0 {
97+
c.c2 = i[0]
98+
}
99+
return c.c2
100+
}
101+
102+
// C3 can be used to set or get c3. If no argument is passed
103+
// the method returns c3 value. If an argument is passed, the method
104+
// sets c3's value to that of the arguments
105+
func (c *Cache) C3(i ...int) int {
106+
if len(i) > 0 {
107+
c.c3 = i[0]
108+
}
109+
return c.c3
110+
}
111+
112+
// V1 can be used to set or get v1. If no argument is passed
113+
// the method returns v1 value. If an argument is passed, the method
114+
// sets v1's value to that of the arguments
115+
func (c *Cache) V1(v ...interface{}) interface{} {
116+
if len(v) > 0 {
117+
c.v1 = v[0]
118+
}
119+
return c.v1
120+
}
121+
122+
// V2 can be used to set or get v2. If no argument is passed
123+
// the method returns v2 value. If an argument is passed, the method
124+
// sets v2's value to that of the arguments
125+
func (c *Cache) V2(v ...interface{}) interface{} {
126+
if len(v) > 0 {
127+
c.v2 = v[0]
128+
}
129+
130+
return c.v2
131+
}
132+
133+
// B can be used to set or get b. If no argument is passed
134+
// the method returns b value. If an argument is passed, the method
135+
// sets b's value to that of the arguments
136+
func (c *Cache) B(b ...[]byte) []byte {
137+
if len(b) > 0 {
138+
c.b = b[0]
139+
}
140+
141+
return c.b
142+
}
143+
144+
// BE expects 2 inputs, a byte array and an error.
145+
// If byte array is not nil, b will be set to the
146+
// value of the new byte array. Error is returned
147+
// with the new/old byte array at the end always
148+
func (c *Cache) BE(b []byte, e error) error {
149+
if b != nil {
150+
c.b = b
151+
}
152+
153+
return e
154+
}
155+
156+
// V1E expects 2 inputs, a byte array and an error.
157+
// If byte array is not nil, b will be set to the
158+
// value of the new byte array. Error is returned
159+
// with the new/old byte array at the end always
160+
func (c *Cache) V1E(v interface{}, e error) error {
161+
if v != nil {
162+
c.v1 = v
163+
}
164+
165+
return e
62166
}

cache/v2/v2.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,28 @@ package v1
22

33
// Cache hosts results from SQL methods
44
type Cache struct {
5-
KeysFound []string
6-
Results interface{}
5+
v1 []string
76
}
87

98
// NewCacheFactory for creating a new Cache
109
func NewCacheFactory() Cache {
1110
cache := Cache{
12-
KeysFound: make([]string, 0),
11+
v1: make([]string, 0),
1312
}
1413
return cache
1514
}
1615

1716
// Clear for clearing the cache
1817
func (c *Cache) Clear() {
19-
c.Results = nil
20-
c.KeysFound = make([]string, 0)
18+
c.v1 = make([]string, 0)
2119
}
2220

2321
// AddKey for appending a new Key to cache
2422
func (c *Cache) AddKey(k string) {
25-
c.KeysFound = append(c.KeysFound, k)
23+
c.v1 = append(c.v1, k)
2624
}
2725

2826
// GetKeys returns Cache.KeysFound
2927
func (c *Cache) GetKeys() []string {
30-
return c.KeysFound
28+
return c.v1
3129
}

db/convert.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ func (a *AssertData) Clear() {
3838

3939
// GetError returns the any error set to AssertData
4040
func (a *AssertData) GetError() error {
41-
return a.cache.GetError()
41+
return a.cache.E()
4242
}
4343

4444
func (a *AssertData) setErr(e ...error) *AssertData {
4545
if len(e) > 0 {
46-
a.cache.E = e[0]
46+
a.cache.E(e[0])
4747
}
4848
return a
4949
}
5050

5151
// Input sets a data source that can be used for assertion
5252
func (a *AssertData) Input(o interface{}) *AssertData {
5353
a.Clear()
54-
a.cache.V1 = o
54+
a.cache.V1(o)
5555
return a
5656
}
5757

@@ -61,7 +61,7 @@ func (a *AssertData) GetString() (string, error) {
6161
return "", a.GetError()
6262
}
6363

64-
s, isString := a.cache.V1.(string)
64+
s, isString := a.cache.V1().(string)
6565
if !isString {
6666
a.setErr(wrapErr(notAType, "string"))
6767
return "", a.GetError()
@@ -76,7 +76,7 @@ func (a *AssertData) GetInt() (int, error) {
7676
return 0, a.GetError()
7777
}
7878

79-
i, isInt := a.cache.V1.(int)
79+
i, isInt := a.cache.V1().(int)
8080
if !isInt {
8181
a.setErr(wrapErr(notAType, "int"))
8282
return 0, a.GetError()
@@ -91,12 +91,12 @@ func (a *AssertData) GetMap() (map[string]string, error) {
9191
return nil, a.GetError()
9292
}
9393

94-
a.cache.B, a.cache.E = yaml.Marshal(a.cache.V1)
94+
a.cache.E(a.cache.BE(yaml.Marshal(a.cache.V1())))
9595
if a.GetError() != nil {
9696
return nil, a.GetError()
9797
}
9898

99-
a.cache.E = yaml.Unmarshal(a.cache.B, &a.d0)
99+
a.cache.E(yaml.Unmarshal(a.cache.B(), &a.d0))
100100
if a.GetError() != nil {
101101
return nil, a.GetError()
102102
}
@@ -109,18 +109,18 @@ func (a *AssertData) GetArray() ([]string, error) {
109109
return nil, a.GetError()
110110
}
111111

112-
_, isArray := a.cache.V1.([]interface{})
112+
_, isArray := a.cache.V1().([]interface{})
113113
if !isArray {
114114
a.setErr(wrapErr(notArrayObj))
115115
return nil, a.GetError()
116116
}
117117

118-
a.cache.B, a.cache.E = yaml.Marshal(a.cache.V1)
118+
a.cache.E(a.cache.BE(yaml.Marshal(a.cache.V1())))
119119
if a.GetError() != nil {
120120
return nil, a.GetError()
121121
}
122122

123-
a.cache.E = yaml.Unmarshal(a.cache.B, &a.s1)
123+
a.cache.E(yaml.Unmarshal(a.cache.B(), &a.s1))
124124
if a.GetError() != nil {
125125
return nil, a.GetError()
126126
}
@@ -135,12 +135,12 @@ func (a *AssertData) Key(k string) *AssertData {
135135
return a
136136
}
137137

138-
_, isMap := a.cache.V1.(map[interface{}]interface{})
138+
_, isMap := a.cache.V1().(map[interface{}]interface{})
139139
if !isMap {
140140
return a.setErr(wrapErr(notAMap))
141141
}
142142

143-
a.cache.V1 = a.cache.V1.(map[interface{}]interface{})[k]
143+
a.cache.V1(a.cache.V1().(map[interface{}]interface{})[k])
144144

145145
return a
146146
}
@@ -151,11 +151,11 @@ func (a *AssertData) Index(i int) *AssertData {
151151
return a
152152
}
153153

154-
_, isArray := a.cache.V1.([]interface{})
154+
_, isArray := a.cache.V1().([]interface{})
155155
if !isArray {
156156
return a.setErr(wrapErr(notArrayObj))
157157
}
158-
a.cache.V1 = a.cache.V1.([]interface{})[i]
158+
a.cache.V1(a.cache.V1().([]interface{})[i])
159159

160160
return a
161161
}

db/sql.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,20 @@ func (s *SQL) get(k string, o interface{}) ([]string, error) {
218218
var key string
219219

220220
s.Clear()
221-
s.Cache.V1, err = copyMap(o)
221+
err = s.Cache.V1E(copyMap(o))
222222
if err != nil {
223223
return nil, wrapErr(err)
224224
}
225225

226226
for {
227-
if _, found := s.getObj(k, s.Cache.V1); !found {
227+
if _, found := s.getObj(k, s.Cache.V1()); !found {
228228
break
229229
}
230230

231231
key = strings.Join(s.Cache.GetKeys(), ".")
232232
s.Query.AddKey(key)
233233

234-
if err := s.delPath(key, s.Cache.V1); err != nil {
234+
if err := s.delPath(key, s.Cache.V1()); err != nil {
235235
return s.Query.GetKeys(), wrapErr(err)
236236
}
237237
s.Cache.DropKeys()
@@ -257,20 +257,20 @@ func (s *SQL) getFirst(k string, o interface{}) (interface{}, error) {
257257
return nil, wrapErr(err)
258258
}
259259

260-
s.Cache.C1 = len(keySlice)
260+
s.Cache.C1(len(keySlice))
261261
if len(keys) == 1 {
262262
path, err := s.getPath(keySlice, o)
263263
return path, wrapErr(err)
264264
}
265265

266266
for i, key := range keys[1:] {
267-
if len(strings.Split(key, ".")) < s.Cache.C1 {
268-
s.Cache.C1 = len(strings.Split(key, "."))
269-
s.Cache.C2 = i + 1
267+
if len(strings.Split(key, ".")) < s.Cache.C1() {
268+
s.Cache.C1(len(strings.Split(key, ".")))
269+
s.Cache.C2(i + 1)
270270
}
271271
}
272272

273-
path, err := s.getPath(strings.Split(keys[s.Cache.C2], "."), o)
273+
path, err := s.getPath(strings.Split(keys[s.Cache.C2()], "."), o)
274274
return path, wrapErr(err)
275275
}
276276

0 commit comments

Comments
 (0)