Possible to create structs with fields/types defined in an external JSON file?

I’m developing an application that initially will be talking to a Mysql database but will also need to connect to Postgres and possible Oracle. I’m using Gorm to provide abstraction and facilitate easily changing the database type w/o updating the code.

I was curious in Go if it was possible to define a type struct (a Gorm table model) by specifying the struct fields in an external JSON or config file of some sort?

Example:

{
  "tables": [
   {
    "name": "mytable",
    "constraints": [],
    "indexes": []
    "columns": [
      "id":  {"type": "varchar", "size": 20, "options": { "autoincr": true, "not null": true },
      ...
    ]
   }
 ],
}

Take something like that and at runtime somehow create a Gorm object.

type MyTable struct {
  gorm.Model // I can actually do without using this Gorm builtin structure if this is an issue
  Id     string   `gorm:"type:varchar(40);not null"`
}

Thanks for any help

1 Like

You can create a script to read your JSON and generate Go.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.