From 9c809e526a3b1157b86e9bcb3fe4d026576ed39e Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Thu, 5 Sep 2019 16:13:25 +0200 Subject: [PATCH] check for empty Properties of update --- routes/simulator/simulator_validators.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/routes/simulator/simulator_validators.go b/routes/simulator/simulator_validators.go index 2a4fa91..db9dc0c 100644 --- a/routes/simulator/simulator_validators.go +++ b/routes/simulator/simulator_validators.go @@ -1,7 +1,9 @@ package simulator import ( + "encoding/json" "github.com/jinzhu/gorm/dialects/postgres" + "github.com/nsf/jsondiff" "gopkg.in/go-playground/validator.v9" ) @@ -75,8 +77,17 @@ func (r *updateSimulatorRequest) updatedSimulator(oldSimulator Simulator) (Simul if r.State != "" { s.State = r.State } - // TODO check for empty properties? - s.Properties = r.Properties + + // only update props if not empty + var emptyJson postgres.Jsonb + // Serialize empty json and params + emptyJson_ser, _ := json.Marshal(emptyJson) + startParams_ser, _ := json.Marshal(r.Properties) + opts := jsondiff.DefaultConsoleOptions() + diff, _ := jsondiff.Compare(emptyJson_ser, startParams_ser, &opts) + if diff.String() != "FullMatch" { + s.Properties = r.Properties + } return s, nil }