Skip to content

Commit 4c93fb9

Browse files
author
Joji Mekkatt
committed
Fix #6
1 parent 504759c commit 4c93fb9

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

generators/templates.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ func Create{{ initialCap .Name }}(obj *{{ initialCap .Name }}) error {
611611
return errors.New("Invalid object type")
612612
}
613613
614+
saveObj := obj
615+
614616
// Check if object already exists
615617
if collections.{{ .Name }}s[obj.Key] != nil {
616618
// Perform Update callback
@@ -619,6 +621,9 @@ func Create{{ initialCap .Name }}(obj *{{ initialCap .Name }}) error {
619621
log.Errorf("{{ initialCap .Name }}Update retruned error for: %+v. Err: %v", obj, err)
620622
return err
621623
}
624+
625+
// save the original object after update
626+
saveObj = collections.{{ .Name }}s[obj.Key]
622627
} else {
623628
// save it in cache
624629
collections.{{ .Name }}s[obj.Key] = obj
@@ -633,9 +638,9 @@ func Create{{ initialCap .Name }}(obj *{{ initialCap .Name }}) error {
633638
}
634639
635640
// Write it to modeldb
636-
err = obj.Write()
641+
err = saveObj.Write()
637642
if err != nil {
638-
log.Errorf("Error saving {{ .Name }} %s to db. Err: %v", obj.Key, err)
643+
log.Errorf("Error saving {{ .Name }} %s to db. Err: %v", saveObj.Key, err)
639644
return err
640645
}
641646

generators/templates/routeFunc.tmpl

+7-2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ func Create{{ initialCap .Name }}(obj *{{ initialCap .Name }}) error {
139139
return errors.New("Invalid object type")
140140
}
141141

142+
saveObj := obj
143+
142144
// Check if object already exists
143145
if collections.{{ .Name }}s[obj.Key] != nil {
144146
// Perform Update callback
@@ -147,6 +149,9 @@ func Create{{ initialCap .Name }}(obj *{{ initialCap .Name }}) error {
147149
log.Errorf("{{ initialCap .Name }}Update retruned error for: %+v. Err: %v", obj, err)
148150
return err
149151
}
152+
153+
// save the original object after update
154+
saveObj = collections.{{ .Name }}s[obj.Key]
150155
} else {
151156
// save it in cache
152157
collections.{{ .Name }}s[obj.Key] = obj
@@ -161,9 +166,9 @@ func Create{{ initialCap .Name }}(obj *{{ initialCap .Name }}) error {
161166
}
162167

163168
// Write it to modeldb
164-
err = obj.Write()
169+
err = saveObj.Write()
165170
if err != nil {
166-
log.Errorf("Error saving {{ .Name }} %s to db. Err: %v", obj.Key, err)
171+
log.Errorf("Error saving {{ .Name }} %s to db. Err: %v", saveObj.Key, err)
167172
return err
168173
}
169174

testdata/one/output.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ func CreateTenant(obj *Tenant) error {
243243
return errors.New("Invalid object type")
244244
}
245245

246+
saveObj := obj
247+
246248
// Check if object already exists
247249
if collections.tenants[obj.Key] != nil {
248250
// Perform Update callback
@@ -251,6 +253,9 @@ func CreateTenant(obj *Tenant) error {
251253
log.Errorf("TenantUpdate retruned error for: %+v. Err: %v", obj, err)
252254
return err
253255
}
256+
257+
// save the original object after update
258+
saveObj = collections.tenants[obj.Key]
254259
} else {
255260
// save it in cache
256261
collections.tenants[obj.Key] = obj
@@ -265,9 +270,9 @@ func CreateTenant(obj *Tenant) error {
265270
}
266271

267272
// Write it to modeldb
268-
err = obj.Write()
273+
err = saveObj.Write()
269274
if err != nil {
270-
log.Errorf("Error saving tenant %s to db. Err: %v", obj.Key, err)
275+
log.Errorf("Error saving tenant %s to db. Err: %v", saveObj.Key, err)
271276
return err
272277
}
273278

@@ -477,6 +482,8 @@ func CreateNetwork(obj *Network) error {
477482
return errors.New("Invalid object type")
478483
}
479484

485+
saveObj := obj
486+
480487
// Check if object already exists
481488
if collections.networks[obj.Key] != nil {
482489
// Perform Update callback
@@ -485,6 +492,9 @@ func CreateNetwork(obj *Network) error {
485492
log.Errorf("NetworkUpdate retruned error for: %+v. Err: %v", obj, err)
486493
return err
487494
}
495+
496+
// save the original object after update
497+
saveObj = collections.networks[obj.Key]
488498
} else {
489499
// save it in cache
490500
collections.networks[obj.Key] = obj
@@ -499,9 +509,9 @@ func CreateNetwork(obj *Network) error {
499509
}
500510

501511
// Write it to modeldb
502-
err = obj.Write()
512+
err = saveObj.Write()
503513
if err != nil {
504-
log.Errorf("Error saving network %s to db. Err: %v", obj.Key, err)
514+
log.Errorf("Error saving network %s to db. Err: %v", saveObj.Key, err)
505515
return err
506516
}
507517

0 commit comments

Comments
 (0)