$ oct generate gorm --help
Option | Env. Variable | Description |
---|---|---|
-i , --input |
OCTOPUS_INPUT |
Octopus schema file to read |
-o , --output |
OCTOPUS_OUTPUT |
Target file or directory |
-a , --pointerAssociation |
OCTOPUS_POINTER_ASSOCIATION |
Use pointer type on associated field if flag is set |
-e , --embed |
OCTOPUS_EMBED |
Define embedded struct. Format: <structName>:<columnName1>[,<columnName2>]... |
-g , --groups |
OCTOPUS_GROUPS |
Table groups to generate. Set multiple groups with comma( , ) separated. |
-k , --package |
OCTOPUS_PACKAGE |
Source package name |
-p , --prefix |
OCTOPUS_PREFIX |
Model struct name prefix. Format: <group1>:<prefix1>[,<group2>:<prefix2>]... Example: group1:prefix1,group2:prefix2 |
-r , --removePrefix |
OCTOPUS_REMOVE_PREFIX |
Prefixes to remove from model struct name. Set multiple prefixes with comma( , ) separated. |
-u , --uniqueNameSuffix |
OCTOPUS_UNIQUE_NAME_SUFFIX |
Unique constraint name suffix |
- Matches column name only.
- Default embedded struct is:
gorm.Model
:id
,created_at
,updated_at
,deleted_at
- See gorm.Model
- To disable default
gorm.Model
, use--embed gorm.Model:
option.
$ oct generate gorm \
--input examples/user.json \
--output output/user.go \
--package model
Generated *.go
file:
package model
import (
"gopkg.in/guregu/null.v4"
)
type UserGroup struct {
ID int64 `gorm:"primary_key;auto_increment"`
Name string `gorm:"type:varchar(40);unique;not null"`
}
func (c *UserGroup) TableName() string { return "group" }
type User struct {
ID int64 `gorm:"primary_key;auto_increment"`
Name string `gorm:"type:varchar(40);unique;not null"`
GroupID null.Int
UserGroup UserGroup `gorm:"foreignKey:GroupID;references:ID"`
}
func (c *User) TableName() string { return "user" }
Assume that you have defined a custom embedded struct IdName
:
type IdName struct {
ID int64 `gorm:"primary_key;auto_increment"`
Name string `gorm:"type:varchar(40);unique;not null"`
}
$ oct generate gorm \
--input examples/user.json \
--output output/user.go \
--package model \
--embed IdName:id,name
Generated *.go
file:
package model
import (
"gopkg.in/guregu/null.v4"
)
type UserGroup struct {
IdName
}
func (c *UserGroup) TableName() string { return "group" }
type User struct {
IdName
GroupID null.Int
UserGroup UserGroup `gorm:"foreignKey:GroupID;references:ID"`
}
func (c *User) TableName() string { return "user" }