mijia/modelq

Make it easier to generate sqlx code

Closed this issue · 3 comments

To quote the README:

The generated models rely on the modelq/gmq package, I am not sure if this would be OK, or could this be changable and plugable, no idea so far.

I think different users have different preferences. What I think woul be nice, is if it was easier to support generating sqlx code (with a custom template is OK). sqlx uses a "db" tag rather than a "json" tag by default.

With that in mind, it would be great if the default struct template stated something like:

var modelStruct string = `type {{.Name}} struct {
    {{range .Fields}}{{.Name}} {{.Type}} `json:"{{.FieldMeta}}"`{{if .Comment}} // {{.Comment}}{{end}}
    {{end}}
}

instead of

var modelStruct string = `type {{.Name}} struct {
    {{range .Fields}}{{.Name}} {{.Type}} {{.JsonMeta}}{{if .Comment}} // {{.Comment}}{{end}}
    {{end}}
}

That way it would be easier/less hacky to make a "db" tag for sqlx in place of your default JSON tag;-)

mijia commented

@smyrman Yeah, modelq supports the customized template for the generated code, in some project we are using gmq and sqlx together, so we have a template like this

{{ define "struct" }}
type {{.Name}} struct {
    {{range .Fields}}{{.Name}} {{.Type}} `json:"{{.ColumnName}}",db:"{{.ColumnName}}"`{{if .Comment}} // {{.Comment}}{{end}}
    {{end}}
}
{{ end }}

You could refer to https://github.com/mijia/modelq/blob/master/examples/custom.tmpl , define your own code template parts or some of them to override the default ones.

thx. I guess this issue can be resolved.

mijia commented

:)