Skip to content

Commit 4dc9cde

Browse files
committed
Re-enable type replacements based on nullability
1 parent 48e1b84 commit 4dc9cde

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

gen/gen_test.go

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ func TestProcessTypeReplacements(t *testing.T) {
1111
{
1212
Columns: []drivers.Column{
1313
{
14-
Name: "id",
15-
Type: "int",
16-
DBType: "serial",
17-
Default: "some db nonsense",
14+
Name: "id",
15+
Type: "int",
16+
DBType: "serial",
17+
Default: "some db nonsense",
18+
Nullable: false,
1819
},
1920
{
20-
Name: "name",
21-
Type: "null.String",
22-
DBType: "serial",
23-
Default: "some db nonsense",
21+
Name: "name",
22+
Type: "null.String",
23+
DBType: "serial",
24+
Default: "some db nonsense",
25+
Nullable: true,
2426
},
2527
{
2628
Name: "domain",
@@ -29,41 +31,48 @@ func TestProcessTypeReplacements(t *testing.T) {
2931
DomainName: "domain name",
3032
},
3133
{
32-
Name: "by_named",
33-
Type: "int",
34-
DBType: "numeric",
35-
Default: "some db nonsense",
34+
Name: "by_named",
35+
Type: "int",
36+
DBType: "numeric",
37+
Default: "some db nonsense",
38+
Nullable: false,
3639
},
3740
{
38-
Name: "by_comment",
39-
Type: "string",
40-
DBType: "text",
41-
Default: "some db nonsense",
42-
Comment: "xid",
41+
Name: "by_comment",
42+
Type: "string",
43+
DBType: "text",
44+
Default: "some db nonsense",
45+
Nullable: false,
46+
Comment: "xid",
4347
},
4448
},
4549
},
4650
{
4751
Key: "named_table",
4852
Columns: []drivers.Column{
4953
{
50-
Name: "id",
51-
Type: "int",
52-
DBType: "serial",
53-
Default: "some db nonsense",
54+
Name: "id",
55+
Type: "int",
56+
DBType: "serial",
57+
Default: "some db nonsense",
58+
Nullable: false,
5459
},
5560
{
56-
Name: "by_comment",
57-
Type: "string",
58-
DBType: "text",
59-
Default: "some db nonsense",
60-
Comment: "xid",
61+
Name: "by_comment",
62+
Type: "string",
63+
DBType: "text",
64+
Default: "some db nonsense",
65+
Nullable: false,
66+
Comment: "xid",
6167
},
6268
},
6369
},
6470
}
6571

6672
types := map[string]drivers.Type{
73+
"excellent.Type": {
74+
Imports: []string{`"rock.com/excellent"`},
75+
},
6776
"excellent.NamedType": {
6877
Imports: []string{`"rock.com/excellent-name"`},
6978
},
@@ -82,6 +91,12 @@ func TestProcessTypeReplacements(t *testing.T) {
8291
}
8392

8493
replacements := []Replace{
94+
{
95+
Match: drivers.Column{
96+
DBType: "serial",
97+
},
98+
Replace: "excellent.Type",
99+
},
85100
{
86101
Tables: []string{"named_table"},
87102
Match: drivers.Column{
@@ -91,7 +106,8 @@ func TestProcessTypeReplacements(t *testing.T) {
91106
},
92107
{
93108
Match: drivers.Column{
94-
Type: "null.String",
109+
Type: "null.String",
110+
Nullable: true,
95111
},
96112
Replace: "int",
97113
},
@@ -117,7 +133,7 @@ func TestProcessTypeReplacements(t *testing.T) {
117133

118134
processTypeReplacements(types, replacements, tables)
119135

120-
if typ := tables[0].Columns[0].Type; typ != "int" {
136+
if typ := tables[0].Columns[0].Type; typ != "excellent.Type" {
121137
t.Error("type was wrong:", typ)
122138
}
123139

gen/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func matchColumn(c, m drivers.Column) bool {
9898
if m.Generated != c.Generated {
9999
return false
100100
}
101+
if m.Nullable != c.Nullable {
102+
return false
103+
}
101104

102105
return true
103106
}

0 commit comments

Comments
 (0)