Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON tags for input structs as well #90

Merged
merged 7 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions example/author/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions example/composite/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions example/erp/order/customer.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions example/inline_param_count/inline0/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions example/inline_param_count/inline1/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions example/inline_param_count/inline2/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 29 additions & 8 deletions internal/codegen/golang/templated_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/jschaf/pggen/internal/ast"
"github.com/jschaf/pggen/internal/codegen/golang/gotype"
"github.com/jschaf/pggen/internal/pginfer"
"strconv"
"strings"
)
Expand Down Expand Up @@ -48,6 +49,7 @@ type TemplatedParam struct {
LowerName string // name of the param in lowerCamelCase, like 'firstName' from pggen.arg('first_name')
QualType string // package-qualified Go type to use for this param
Type gotype.Type
RawName pginfer.InputParam
}

type TemplatedColumn struct {
Expand Down Expand Up @@ -96,14 +98,26 @@ func (tq TemplatedQuery) EmitParams() string {
return sb.String()
}

func getLongestInput(inputs []TemplatedParam) int {
max := 0
for _, in := range inputs {
if len(in.UpperName) > max {
max = len(in.UpperName)
// getLongestInput returns the length of the longest name and type name in all
// columns. Useful for struct definition alignment.
func getLongestInput(inputs []TemplatedParam) (int, int) {
nameLen := 0
for _, out := range inputs {
if len(out.UpperName) > nameLen {
nameLen = len(out.UpperName)
farazfazli marked this conversation as resolved.
Show resolved Hide resolved
}
}
nameLen++ // 1 space to separate name from type

typeLen := 0
for _, out := range inputs {
if len(out.QualType) > typeLen {
typeLen = len(out.QualType)
}
}
return max
typeLen++ // 1 space to separate type from struct tags.

return nameLen, typeLen
}

// EmitParamStruct emits the struct definition for query params if needed.
Expand All @@ -115,12 +129,19 @@ func (tq TemplatedQuery) EmitParamStruct() string {
sb.WriteString("\n\ntype ")
sb.WriteString(tq.Name)
sb.WriteString("Params struct {\n")
typeCol := getLongestInput(tq.Inputs) + 1 // 1 space
maxNameLen, maxTypeLen := getLongestInput(tq.Inputs)
for _, out := range tq.Inputs {
// Name
sb.WriteString("\t")
sb.WriteString(out.UpperName)
sb.WriteString(strings.Repeat(" ", typeCol-len(out.UpperName)))
// Type
farazfazli marked this conversation as resolved.
Show resolved Hide resolved
sb.WriteString(strings.Repeat(" ", maxNameLen-len(out.UpperName)))
sb.WriteString(out.QualType)
// JSON struct tag
sb.WriteString(strings.Repeat(" ", maxTypeLen-len(out.QualType)))
sb.WriteString("`json:")
sb.WriteString(strconv.Quote(out.RawName.PgName))
sb.WriteString("`")
sb.WriteRune('\n')
}
sb.WriteString("}")
Expand Down
1 change: 1 addition & 0 deletions internal/codegen/golang/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (tm Templater) templateFile(file codegen.QueryFile, isLeader bool) (Templat
LowerName: tm.chooseLowerName(input.PgName, "unnamedParam", i, len(query.Inputs)),
QualType: gotype.QualifyType(goType, pkgPath),
Type: goType,
RawName: query.Inputs[i],
}
ds := FindInputDeclarers(goType).ListAll()
declarers.AddAll(ds...)
Expand Down
Loading