I am currently exploring with GORM and have the following struct:
type Datasource struct {
gorm.Model
Inputs []string `gorm: serializer:json`
Outputs []string `gorm: serializer:json`
Parameters map[string]interface{} `gorm: serializer:json`
}
I do initialise my connection with the following func:
func ConnectDatabase() {
database, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("Failed to connect to database!")
}
err = database.AutoMigrate(&Datasource{})
if err != nil {
return
}
inputs := []string{"input_1", "input_2", "input_3"}
outputs := []string{"output_1"}
parameters := map[string]interface{}{"host": "here.it.is", "port": 8090}
database.Create(&Datasource{Inputs: inputs, Outputs: outputs, Parameters: parameters})
DB = database
}
but get the following error when executing AutoMigrate
[error] unsupported data type: &[]
Shall I use datatypes
?
Thanks
In fact the code above is almost working. GORM
supports JSON natively but you have to proper annotate your code :-)
type Datasource struct {
gorm.Model
Inputs []string `gorm:"serializer:json"`
Outputs []string `gorm:"serializer:json"`
Parameters map[string]interface{} `gorm:"serializer:json"`
}
See this link for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With