Skip to content

Commit c9461e8

Browse files
committed
internal/codegen/golang: simplify template tag condition
Rather than modeling when .Tag will be empty, check directly whether .Tag is empty. This simplifies the template and reduces the number of places that must be touched when adding new sources of struct tags.
1 parent b2f6f07 commit c9461e8

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

internal/codegen/golang/templates/pgx/batchCode.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ type {{.MethodName}}BatchResults struct {
1212

1313
{{if .Arg.EmitStruct}}
1414
type {{.Arg.Type}} struct { {{- range .Arg.Struct.Fields}}
15-
{{.Name}} {{.Type}} {{if or ($.EmitJSONTags) ($.EmitDBTags)}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
15+
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
1616
{{- end}}
1717
}
1818
{{end}}
1919

2020
{{if .Ret.EmitStruct}}
2121
type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
22-
{{.Name}} {{.Type}} {{if or ($.EmitJSONTags) ($.EmitDBTags)}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
22+
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
2323
{{- end}}
2424
}
2525
{{end}}

internal/codegen/golang/templates/pgx/queryCode.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
1010
{{if ne (hasPrefix .Cmd ":batch") true}}
1111
{{if .Arg.EmitStruct}}
1212
type {{.Arg.Type}} struct { {{- range .Arg.Struct.Fields}}
13-
{{.Name}} {{.Type}} {{if or ($.EmitJSONTags) ($.EmitDBTags)}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
13+
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
1414
{{- end}}
1515
}
1616
{{end}}
1717

1818
{{if .Ret.EmitStruct}}
1919
type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
20-
{{.Name}} {{.Type}} {{if or ($.EmitJSONTags) ($.EmitDBTags)}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
20+
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
2121
{{- end}}
2222
}
2323
{{end}}

internal/codegen/golang/templates/stdlib/queryCode.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const {{.ConstantName}} = {{$.Q}}-- name: {{.MethodName}} {{.Cmd}}
77

88
{{if .Arg.EmitStruct}}
99
type {{.Arg.Type}} struct { {{- range .Arg.UniqueFields}}
10-
{{.Name}} {{.Type}} {{if or ($.EmitJSONTags) ($.EmitDBTags)}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
10+
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
1111
{{- end}}
1212
}
1313
{{end}}
1414

1515
{{if .Ret.EmitStruct}}
1616
type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
17-
{{.Name}} {{.Type}} {{if or ($.EmitJSONTags) ($.EmitDBTags)}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
17+
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
1818
{{- end}}
1919
}
2020
{{end}}

internal/codegen/golang/templates/template.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ type {{.Name}} struct { {{- range .Fields}}
9494
{{- if .Comment}}
9595
{{comment .Comment}}{{else}}
9696
{{- end}}
97-
{{.Name}} {{.Type}} {{if or ($.EmitJSONTags) ($.EmitDBTags)}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
97+
{{.Name}} {{.Type}} {{if .Tag}}{{$.Q}}{{.Tag}}{{$.Q}}{{end}}
9898
{{- end}}
9999
}
100100
{{end}}

0 commit comments

Comments
 (0)