-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgo_type.go
92 lines (84 loc) · 2.34 KB
/
go_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package golang
import (
"strings"
"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
"github.com/sqlc-dev/plugin-sdk-go/sdk"
"github.com/sqlc-dev/plugin-sdk-go/plugin"
)
func addExtraGoStructTags(tags map[string]string, req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) {
for _, override := range options.Overrides {
oride := override.ShimOverride
if oride.GoType.StructTags == nil {
continue
}
if !override.Matches(col.Table, req.Catalog.DefaultSchema) {
// Different table.
continue
}
cname := col.Name
if col.OriginalName != "" {
cname = col.OriginalName
}
if !sdk.MatchString(oride.ColumnName, cname) {
// Different column.
continue
}
// Add the extra tags.
for k, v := range oride.GoType.StructTags {
tags[k] = v
}
}
}
func goType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
// Check if the column's type has been overridden
for _, override := range options.Overrides {
oride := override.ShimOverride
if oride.GoType.TypeName == "" {
continue
}
cname := col.Name
if col.OriginalName != "" {
cname = col.OriginalName
}
sameTable := override.Matches(col.Table, req.Catalog.DefaultSchema)
if oride.Column != "" && sdk.MatchString(oride.ColumnName, cname) && sameTable {
if col.IsSqlcSlice {
return "[]" + oride.GoType.TypeName
}
return oride.GoType.TypeName
}
}
typ := goInnerType(req, options, col)
if col.IsSqlcSlice {
return "[]" + typ
}
if col.IsArray {
return strings.Repeat("[]", int(col.ArrayDims)) + typ
}
return typ
}
func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray
// package overrides have a higher precedence
for _, override := range options.Overrides {
oride := override.ShimOverride
if oride.GoType.TypeName == "" {
continue
}
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != notNull && oride.Unsigned == col.Unsigned {
return oride.GoType.TypeName
}
}
// TODO: Extend the engine interface to handle types
switch req.Settings.Engine {
case "mysql":
return mysqlType(req, options, col)
case "postgresql":
return postgresType(req, options, col)
case "sqlite":
return sqliteType(req, options, col)
default:
return "interface{}"
}
}