Skip to content

internal/dinosql: Override columns with array type #184

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

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 6 additions & 3 deletions internal/dinosql/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ func (r Result) Structs() []GoStruct {
}

func (r Result) goType(col core.Column) string {
// package overrides have a higher precedence
for _, oride := range append(r.Settings.Overrides, r.packageSettings.Overrides...) {
if oride.Column != "" && oride.columnName == col.Name && oride.table == col.Table {
return oride.goTypeName
}
}
typ := r.goInnerType(col)
if col.IsArray {
return "[]" + typ
Expand All @@ -483,9 +489,6 @@ func (r Result) goInnerType(col core.Column) string {
if oride.PostgresType != "" && oride.PostgresType == columnType && oride.Null != notNull {
return oride.goTypeName
}
if oride.Column != "" && oride.columnName == col.Name && oride.table == col.Table {
return oride.goTypeName
}
}

switch columnType {
Expand Down
16 changes: 15 additions & 1 deletion internal/dinosql/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func TestColumnsToStruct(t *testing.T) {
DataType: "text",
NotNull: true,
},
{
Name: "languages",
DataType: "text",
IsArray: true,
},
}

// all of the columns are on the 'foo' table
Expand All @@ -55,8 +60,16 @@ func TestColumnsToStruct(t *testing.T) {
Column: "foo.retyped",
}
o.Parse()

// set up column-based array override test
oa := Override{
GoType: "github.com/lib/pq.StringArray",
Column: "foo.languages",
}
oa.Parse()

r.packageSettings = PackageSettings{
Overrides: []Override{o},
Overrides: []Override{o, oa},
}

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