mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00

- move amqp endpoint implementation to amqp package - improve code coverage of simulator testing - remove some unnecessary code from package implementation
93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package simulator
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/jinzhu/gorm/dialects/postgres"
|
|
"github.com/nsf/jsondiff"
|
|
"gopkg.in/go-playground/validator.v9"
|
|
)
|
|
|
|
var validate *validator.Validate
|
|
|
|
type validNewSimulator struct {
|
|
UUID string `form:"UUID" validate:"required"`
|
|
Host string `form:"Host" validate:"required"`
|
|
Modeltype string `form:"Modeltype" validate:"required"`
|
|
Properties postgres.Jsonb `form:"Properties" validate:"required"`
|
|
State string `form:"State"`
|
|
}
|
|
|
|
type validUpdatedSimulator struct {
|
|
UUID string `form:"UUID" validate:"omitempty"`
|
|
Host string `form:"Host" validate:"omitempty"`
|
|
Modeltype string `form:"Modeltype" validate:"omitempty"`
|
|
Properties postgres.Jsonb `form:"Properties" validate:"omitempty"`
|
|
State string `form:"State" validate:"omitempty"`
|
|
}
|
|
|
|
type addSimulatorRequest struct {
|
|
validNewSimulator `json:"simulator"`
|
|
}
|
|
|
|
type updateSimulatorRequest struct {
|
|
validUpdatedSimulator `json:"simulator"`
|
|
}
|
|
|
|
func (r *addSimulatorRequest) validate() error {
|
|
validate = validator.New()
|
|
errs := validate.Struct(r)
|
|
return errs
|
|
}
|
|
|
|
func (r *validUpdatedSimulator) validate() error {
|
|
validate = validator.New()
|
|
errs := validate.Struct(r)
|
|
return errs
|
|
}
|
|
|
|
func (r *addSimulatorRequest) createSimulator() Simulator {
|
|
var s Simulator
|
|
|
|
s.UUID = r.UUID
|
|
s.Host = r.Host
|
|
s.Modeltype = r.Modeltype
|
|
s.Properties = r.Properties
|
|
if r.State != "" {
|
|
s.State = r.State
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (r *updateSimulatorRequest) updatedSimulator(oldSimulator Simulator) Simulator {
|
|
// Use the old Simulator as a basis for the updated Simulator `s`
|
|
s := oldSimulator
|
|
|
|
if r.UUID != "" {
|
|
s.UUID = r.UUID
|
|
}
|
|
|
|
if r.Host != "" {
|
|
s.Host = r.Host
|
|
}
|
|
|
|
if r.Modeltype != "" {
|
|
s.Modeltype = r.Modeltype
|
|
}
|
|
|
|
if r.State != "" {
|
|
s.State = r.State
|
|
}
|
|
|
|
// 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
|
|
}
|