Skip to content

Commit b393d65

Browse files
authored
internal/dinosql: Override columns with array type (#184)
1 parent 7a2dd70 commit b393d65

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

internal/dinosql/gen.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,12 @@ func (r Result) Structs() []GoStruct {
467467
}
468468

469469
func (r Result) goType(col core.Column) string {
470+
// package overrides have a higher precedence
471+
for _, oride := range append(r.Settings.Overrides, r.packageSettings.Overrides...) {
472+
if oride.Column != "" && oride.columnName == col.Name && oride.table == col.Table {
473+
return oride.goTypeName
474+
}
475+
}
470476
typ := r.goInnerType(col)
471477
if col.IsArray {
472478
return "[]" + typ
@@ -483,9 +489,6 @@ func (r Result) goInnerType(col core.Column) string {
483489
if oride.PostgresType != "" && oride.PostgresType == columnType && oride.Null != notNull {
484490
return oride.goTypeName
485491
}
486-
if oride.Column != "" && oride.columnName == col.Name && oride.table == col.Table {
487-
return oride.goTypeName
488-
}
489492
}
490493

491494
switch columnType {

internal/dinosql/gen_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ func TestColumnsToStruct(t *testing.T) {
4040
DataType: "text",
4141
NotNull: true,
4242
},
43+
{
44+
Name: "languages",
45+
DataType: "text",
46+
IsArray: true,
47+
},
4348
}
4449

4550
// all of the columns are on the 'foo' table
@@ -55,8 +60,16 @@ func TestColumnsToStruct(t *testing.T) {
5560
Column: "foo.retyped",
5661
}
5762
o.Parse()
63+
64+
// set up column-based array override test
65+
oa := Override{
66+
GoType: "github.com/lib/pq.StringArray",
67+
Column: "foo.languages",
68+
}
69+
oa.Parse()
70+
5871
r.packageSettings = PackageSettings{
59-
Overrides: []Override{o},
72+
Overrides: []Override{o, oa},
6073
}
6174

6275
actual := r.columnsToStruct("Foo", cols)
@@ -69,6 +82,7 @@ func TestColumnsToStruct(t *testing.T) {
6982
{Name: "Tags", Type: "[]string", Tags: map[string]string{"json:": "tags"}},
7083
{Name: "ByteSeq", Type: "[]byte", Tags: map[string]string{"json:": "byte_seq"}},
7184
{Name: "Retyped", Type: "pkg.CustomType", Tags: map[string]string{"json:": "retyped"}},
85+
{Name: "Languages", Type: "pq.StringArray", Tags: map[string]string{"json:": "languages"}},
7286
},
7387
}
7488
if diff := cmp.Diff(expected, actual); diff != "" {

0 commit comments

Comments
 (0)