- Use an intermediate struct for (un)marshaling dummyObject
- Remove dummyObject.SkipSave as it would always be set to "false"
(i.e., persisted to disk).
- Minor cleanups in handling locks and some unused vars
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -21,8 +21,6 @@ func newCache(ds store.Store) *cache {
|
| 21 | 21 |
} |
| 22 | 22 |
|
| 23 | 23 |
func (c *cache) kmap(kvObject KVObject) (kvMap, error) {
|
| 24 |
- var err error |
|
| 25 |
- |
|
| 26 | 24 |
c.mu.Lock() |
| 27 | 25 |
keyPrefix := Key(kvObject.KeyPrefix()...) |
| 28 | 26 |
kmap, ok := c.kmm[keyPrefix] |
| ... | ... |
@@ -52,8 +50,7 @@ func (c *cache) kmap(kvObject KVObject) (kvMap, error) {
|
| 52 | 52 |
} |
| 53 | 53 |
|
| 54 | 54 |
dstO := kvObject.New() |
| 55 |
- err = dstO.SetValue(kvPair.Value) |
|
| 56 |
- if err != nil {
|
|
| 55 |
+ if err := dstO.SetValue(kvPair.Value); err != nil {
|
|
| 57 | 56 |
return nil, err |
| 58 | 57 |
} |
| 59 | 58 |
|
| ... | ... |
@@ -116,19 +113,18 @@ func (c *cache) del(kvObject KVObject, atomic bool) error {
|
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
c.mu.Lock() |
| 119 |
+ defer c.mu.Unlock() |
|
| 119 | 120 |
// If atomic is true, cache needs to maintain its own index |
| 120 | 121 |
// for atomicity and del needs to be atomic. |
| 121 | 122 |
if atomic {
|
| 122 | 123 |
if prev, ok := kmap[Key(kvObject.Key()...)]; ok {
|
| 123 | 124 |
if prev.Index() != kvObject.Index() {
|
| 124 |
- c.mu.Unlock() |
|
| 125 | 125 |
return ErrKeyModified |
| 126 | 126 |
} |
| 127 | 127 |
} |
| 128 | 128 |
} |
| 129 | 129 |
|
| 130 | 130 |
delete(kmap, Key(kvObject.Key()...)) |
| 131 |
- c.mu.Unlock() |
|
| 132 | 131 |
return nil |
| 133 | 132 |
} |
| 134 | 133 |
|
| ... | ... |
@@ -77,7 +77,6 @@ type dummyObject struct {
|
| 77 | 77 |
ID string |
| 78 | 78 |
DBIndex uint64 |
| 79 | 79 |
DBExists bool |
| 80 |
- SkipSave bool |
|
| 81 | 80 |
ReturnValue bool |
| 82 | 81 |
} |
| 83 | 82 |
|
| ... | ... |
@@ -119,27 +118,34 @@ func (n *dummyObject) Exists() bool {
|
| 119 | 119 |
} |
| 120 | 120 |
|
| 121 | 121 |
func (n *dummyObject) Skip() bool {
|
| 122 |
- return n.SkipSave |
|
| 122 |
+ return false |
|
| 123 |
+} |
|
| 124 |
+ |
|
| 125 |
+type tmpStruct struct {
|
|
| 126 |
+ Name string `json:"name"` |
|
| 127 |
+ NetworkType string `json:"networkType"` |
|
| 128 |
+ EnableIPv6 bool `json:"enableIPv6"` |
|
| 129 |
+ Generic options.Generic `json:"generic"` |
|
| 123 | 130 |
} |
| 124 | 131 |
|
| 125 | 132 |
func (n *dummyObject) MarshalJSON() ([]byte, error) {
|
| 126 |
- return json.Marshal(map[string]any{
|
|
| 127 |
- "name": n.Name, |
|
| 128 |
- "networkType": n.NetworkType, |
|
| 129 |
- "enableIPv6": n.EnableIPv6, |
|
| 130 |
- "generic": n.Generic, |
|
| 133 |
+ return json.Marshal(tmpStruct{
|
|
| 134 |
+ Name: n.Name, |
|
| 135 |
+ NetworkType: n.NetworkType, |
|
| 136 |
+ EnableIPv6: n.EnableIPv6, |
|
| 137 |
+ Generic: n.Generic, |
|
| 131 | 138 |
}) |
| 132 | 139 |
} |
| 133 | 140 |
|
| 134 | 141 |
func (n *dummyObject) UnmarshalJSON(b []byte) error {
|
| 135 |
- var netMap map[string]any |
|
| 142 |
+ var netMap tmpStruct |
|
| 136 | 143 |
if err := json.Unmarshal(b, &netMap); err != nil {
|
| 137 | 144 |
return err |
| 138 | 145 |
} |
| 139 |
- n.Name = netMap["name"].(string) |
|
| 140 |
- n.NetworkType = netMap["networkType"].(string) |
|
| 141 |
- n.EnableIPv6 = netMap["enableIPv6"].(bool) |
|
| 142 |
- n.Generic = netMap["generic"].(map[string]any) |
|
| 146 |
+ n.Name = netMap.Name |
|
| 147 |
+ n.NetworkType = netMap.NetworkType |
|
| 148 |
+ n.EnableIPv6 = netMap.EnableIPv6 |
|
| 149 |
+ n.Generic = netMap.Generic |
|
| 143 | 150 |
return nil |
| 144 | 151 |
} |
| 145 | 152 |
|
| ... | ... |
@@ -199,22 +205,23 @@ func (r *recStruct) Skip() bool {
|
| 199 | 199 |
} |
| 200 | 200 |
|
| 201 | 201 |
func dummyKVObject(id string, retValue bool) *dummyObject {
|
| 202 |
- cDict := map[string]string{
|
|
| 203 |
- "foo": "bar", |
|
| 204 |
- "hello": "world", |
|
| 205 |
- } |
|
| 206 | 202 |
return &dummyObject{
|
| 207 | 203 |
Name: "testNw", |
| 208 | 204 |
NetworkType: "bridge", |
| 209 | 205 |
EnableIPv6: true, |
| 210 |
- Rec: &recStruct{Name: "gen", Field1: 5, Dict: cDict},
|
|
| 206 |
+ Rec: &recStruct{Name: "gen", Field1: 5, Dict: map[string]string{
|
|
| 207 |
+ "foo": "bar", |
|
| 208 |
+ "hello": "world", |
|
| 209 |
+ }}, |
|
| 211 | 210 |
ID: id, |
| 212 | 211 |
DBIndex: 0, |
| 213 | 212 |
ReturnValue: retValue, |
| 214 | 213 |
DBExists: false, |
| 215 |
- SkipSave: false, |
|
| 216 |
- Generic: map[string]any{
|
|
| 217 |
- "label1": &recStruct{Name: "value1", Field1: 1, Dict: cDict},
|
|
| 214 |
+ Generic: options.Generic{
|
|
| 215 |
+ "label1": &recStruct{Name: "value1", Field1: 1, Dict: map[string]string{
|
|
| 216 |
+ "foo": "bar", |
|
| 217 |
+ "hello": "world", |
|
| 218 |
+ }}, |
|
| 218 | 219 |
"label2": "subnet=10.1.1.0/16", |
| 219 | 220 |
}, |
| 220 | 221 |
} |