Note: If you are reading this on Github, please note that the repo has moved to Gitlab (gitlab.com/metakeule/scaffold) and this is only a mirror.
scaffolding via go templates
go install gitlab.com/metakeule/scaffold@latest
given the following template (file models.templ
)
{
"Models": [
{ "Name": "",
"Fields": [ {"Name": "", "Type": ""} ] }
]
}
>>>models/
{{range .Models}}
>>>{{toLower .Name}}/
>>>model.go
package {{replace .Name "_" "."}}
type {{camelCase1 .Name}} struct {
{{range .Fields}}
{{camelCase1 .Name}} {{.Type}}
{{end}}
}
<<<model.go
<<<{{toLower .Name}}/
{{end}}
<<<models/
and the following json (file models.json
)
{
"Models": [
{
"Name": "person",
"Fields": [
{"Name": "first_name","Type": "string"},
{"Name": "last_name" ,"Type": "string"}
]
},
{
"Name": "address",
"Fields": [
{"Name": "street_no","Type": "string"},
{"Name": "city","Type": "string"}
]
}
]
}
when running the command
scaffold -t=models.templ < models.json
the following directory structure would be build:
models
├── address
│ └── model.go
└── person
└── model.go
where models/address/model.go
contains
package address
type Address struct {
StreetNo string
City string
}
and models/person/model.go
contains
package person
type Person struct {
FirstName string
LastName string
}
To help generating a template from an existing file structure, make sure, you just have one item per collection and then run
scaffold scan --scandir=your/dir
and edit your template as you need.
https://pkg.go.dev/gitlab.com/metakeule/scaffold/lib/scaffold