add json tags to all models

This commit is contained in:
Sonja Happ 2019-07-31 09:37:12 +02:00
parent e6c8fff397
commit 05a70f80f6

View file

@ -1,83 +1,95 @@
package common package common
import ( import (
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/dialects/postgres" "github.com/jinzhu/gorm/dialects/postgres"
"time"
) )
// Fields shared by all models (same as gorm.model but including proper json tags)
type commonModelFields struct {
// ID of the model (primary key in DB)
ID uint `gorm:"primary_key",json:"id"`
// Time the model object is created
CreatedAt time.Time `json:"-"`
// Time of the last update of the model object
UpdatedAt time.Time `json:"-"`
// Time the model is deleted
DeletedAt *time.Time `json:"-"`
}
// User data model // User data model
type User struct { type User struct {
gorm.Model commonModelFields
// Username of user // Username of user
Username string `gorm:"unique;not null"` Username string `gorm:"unique;not null",json:"username"`
// Password of user // Password of user
Password string `gorm:"not null"` Password string `gorm:"not null",json:"-"`
// Mail of user // Mail of user
Mail string `gorm:"default:''"` Mail string `gorm:"default:''",json:"mail"`
// Role of user // Role of user
Role string `gorm:"default:'user'"` Role string `gorm:"default:'user'",json:"role"`
// Scenarios to which user has access // Scenarios to which user has access
Scenarios []*Scenario `gorm:"many2many:user_scenarios"` Scenarios []*Scenario `gorm:"many2many:user_scenarios",json:"-"`
} }
// Scenario data model // Scenario data model
type Scenario struct { type Scenario struct {
gorm.Model commonModelFields
// Name of scenario // Name of scenario
Name string `gorm:"not null"` Name string `gorm:"not null",json:"name"`
// Running state of scenario // Running state of scenario
Running bool `gorm:"default:false"` Running bool `gorm:"default:false",json:"running"`
// Start parameters of scenario as JSON // Start parameters of scenario as JSON
StartParameters postgres.Jsonb StartParameters postgres.Jsonb `json:"startParameters"`
// Users that have access to the scenario // Users that have access to the scenario
Users []*User `gorm:"not null;many2many:user_scenarios"` Users []*User `gorm:"not null;many2many:user_scenarios",json:"-"`
// SimulationModels that belong to the scenario // SimulationModels that belong to the scenario
SimulationModels []SimulationModel `gorm:"foreignkey:ScenarioID"` SimulationModels []SimulationModel `gorm:"foreignkey:ScenarioID",json:"-"`
// Dashboards that belong to the Scenario // Dashboards that belong to the Scenario
Dashboards []Dashboard `gorm:"foreignkey:ScenarioID"` Dashboards []Dashboard `gorm:"foreignkey:ScenarioID",json:"-"`
} }
// SimulationModel data model // SimulationModel data model
type SimulationModel struct { type SimulationModel struct {
gorm.Model commonModelFields
// Name of simulation model // Name of simulation model
Name string `gorm:"not null"` Name string `gorm:"not null",json:"name"`
// Number of output signals // Number of output signals
OutputLength int `gorm:"default:1"` OutputLength int `gorm:"default:1",json:"outputLength"`
// Number of input signals // Number of input signals
InputLength int `gorm:"default:1"` InputLength int `gorm:"default:1",json:"inputLength"`
// Start parameters of simulation model as JSON // Start parameters of simulation model as JSON
StartParameters postgres.Jsonb StartParameters postgres.Jsonb `json:"startParameters"`
// ID of Scenario to which simulation model belongs // ID of Scenario to which simulation model belongs
ScenarioID uint ScenarioID uint `json:"scenarioID"`
// ID of simulator associated with simulation model // ID of simulator associated with simulation model
SimulatorID uint SimulatorID uint `json:"simulatorID"`
// Mapping of output signals of the simulation model, order of signals is important // Mapping of output signals of the simulation model, order of signals is important
OutputMapping []Signal `gorm:"foreignkey:SimulationModelID"` OutputMapping []Signal `gorm:"foreignkey:SimulationModelID",json:"-"`
// Mapping of input signals of the simulation model, order of signals is important // Mapping of input signals of the simulation model, order of signals is important
InputMapping []Signal `gorm:"foreignkey:SimulationModelID"` InputMapping []Signal `gorm:"foreignkey:SimulationModelID",json:"-"`
// Files of simulation model (can be CIM and other simulation model file formats) // Files of simulation model (can be CIM and other simulation model file formats)
Files []File `gorm:"foreignkey:SimulationModelID"` Files []File `gorm:"foreignkey:SimulationModelID",json:"-"`
} }
// Signal data model // Signal data model
type Signal struct { type Signal struct {
gorm.Model commonModelFields
// Name of Signal // Name of Signal
Name string Name string `json:"name"`
// Unit of Signal // Unit of Signal
Unit string Unit string `json:"unit"`
// Index of the Signal in the mapping // Index of the Signal in the mapping
Index uint Index uint `json:"index"`
// Direction of the signal (in or out) // Direction of the signal (in or out)
Direction string Direction string `json:"direction"`
// ID of simulation model // ID of simulation model
SimulationModelID uint SimulationModelID uint `json:"simulationModelID"`
} }
// Simulator data model // Simulator data model
type Simulator struct { type Simulator struct {
gorm.Model commonModelFields
// UUID of the simulator // UUID of the simulator
UUID string `gorm:"not null",json:"uuid"` UUID string `gorm:"not null",json:"uuid"`
// Host if the simulator // Host if the simulator
@ -95,74 +107,74 @@ type Simulator struct {
// Raw properties of simulator as JSON string // Raw properties of simulator as JSON string
RawProperties postgres.Jsonb `json:"rawProperties"` RawProperties postgres.Jsonb `json:"rawProperties"`
// SimulationModels in which the simulator is used // SimulationModels in which the simulator is used
SimulationModels []SimulationModel `gorm:"foreignkey:SimulatorID"` SimulationModels []SimulationModel `gorm:"foreignkey:SimulatorID",json:"-"`
} }
// Dashboard data model // Dashboard data model
type Dashboard struct { type Dashboard struct {
gorm.Model commonModelFields
// Name of dashboard // Name of dashboard
Name string `gorm:"not null"` Name string `gorm:"not null",json:"name"`
// Grid of dashboard // Grid of dashboard
Grid int `gorm:"default:15"` Grid int `gorm:"default:15",json:"grid"`
// ID of scenario to which dashboard belongs // ID of scenario to which dashboard belongs
ScenarioID uint ScenarioID uint `json:"scenarioID"`
// Widgets that belong to dashboard // Widgets that belong to dashboard
Widgets []Widget `gorm:"foreignkey:DashboardID"` Widgets []Widget `gorm:"foreignkey:DashboardID",json:"-"`
} }
// Widget data model // Widget data model
type Widget struct { type Widget struct {
gorm.Model commonModelFields
// Name of widget // Name of widget
Name string `gorm:"not null"` Name string `gorm:"not null",json:"name"`
// Type of widget // Type of widget
Type string `gorm:"not null"` Type string `gorm:"not null",json:"type"`
// Width of widget // Width of widget
Width uint `gorm:"not null"` Width uint `gorm:"not null",json:"width"`
// Height of widget // Height of widget
Height uint `gorm:"not null"` Height uint `gorm:"not null",json:"height"`
// Minimal width of widget // Minimal width of widget
MinWidth uint `gorm:"not null"` MinWidth uint `gorm:"not null",json:"minWidth"`
// Minimal height of widget // Minimal height of widget
MinHeight uint `gorm:"not null"` MinHeight uint `gorm:"not null",json:"minHeight"`
// X position of widget // X position of widget
X int `gorm:"not null"` X int `gorm:"not null",json:"x"`
// Y position of widget // Y position of widget
Y int `gorm:"not null"` Y int `gorm:"not null",json:"y"`
// Z position of widget // Z position of widget
Z int `gorm:"not null"` Z int `gorm:"not null",json:"z"`
// Locked state of widget // Locked state of widget
IsLocked bool `gorm:"default:false"` IsLocked bool `gorm:"default:false",json:"isLocked"`
// Custom properties of widget as JSON string // Custom properties of widget as JSON string
CustomProperties postgres.Jsonb CustomProperties postgres.Jsonb `json:"customProperties"`
// ID of dashboard to which widget belongs // ID of dashboard to which widget belongs
DashboardID uint DashboardID uint `json:"dashboardID"`
// Files that belong to widget (for example images) // Files that belong to widget (for example images)
Files []File `gorm:"foreignkey:WidgetID"` Files []File `gorm:"foreignkey:WidgetID",json:"-"`
} }
// File data model // File data model
type File struct { type File struct {
gorm.Model commonModelFields
// Name of file // Name of file
Name string `gorm:"not null"` Name string `gorm:"not null",json:"name"`
// Type of file (MIME type) // Type of file (MIME type)
Type string Type string `json:"type"`
// Size of file (in byte) // Size of file (in byte)
Size uint Size uint `json:"size"`
// Height of image (only needed in case of image) // Height of image (only needed in case of image)
ImageHeight uint ImageHeight uint `json:"imageHeight"`
// Width of image (only needed in case of image) // Width of image (only needed in case of image)
ImageWidth uint ImageWidth uint `json:"imageWidth"`
// Last modification time of file // Last modification time of file
Date string Date string `json:"date"`
// ID of model to which file belongs // ID of model to which file belongs
SimulationModelID uint SimulationModelID uint `json:"simulationModelID"`
// ID of widget to which file belongs // ID of widget to which file belongs
WidgetID uint WidgetID uint `json:"widgetID"`
// File itself // File itself
FileData []byte `gorm:"column:FileData"` FileData []byte `gorm:"column:FileData",json:"-"`
} }
// Credentials type (not for DB) // Credentials type (not for DB)