Skip to content

Commit

Permalink
Recognize array and hstore options in SQL tag
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 5, 2018
1 parent a0f6ccc commit 3ddf462
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ var _ = Describe("Time", func() {

var _ = Describe("array model", func() {
type value struct {
Values []int16 `pg:",array"`
Values []int16 `sql:",array"`
}

var db *pg.DB
Expand Down
4 changes: 2 additions & 2 deletions example_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
func ExampleDB_Model_postgresArrayStructTag() {
type Item struct {
Id int64
Emails []string `pg:",array"` // marshalled as PostgreSQL array
Numbers [][]int `pg:",array"` // marshalled as PostgreSQL array
Emails []string `sql:",array"` // marshalled as PostgreSQL array
Numbers [][]int `sql:",array"` // marshalled as PostgreSQL array
}

_, err := pgdb.Exec(`CREATE TEMP TABLE items (id serial, emails text[], numbers int[][])`)
Expand Down
2 changes: 1 addition & 1 deletion example_hstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func ExampleDB_Model_hstoreStructTag() {
type Item struct {
Id int64
Attrs map[string]string `pg:",hstore"` // marshalled as PostgreSQL hstore
Attrs map[string]string `sql:",hstore"` // marshalled as PostgreSQL hstore
}

_, err := pgdb.Exec(`CREATE TEMP TABLE items (id serial, attrs hstore)`)
Expand Down
8 changes: 7 additions & 1 deletion orm/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ func (t *Table) newField(f reflect.StructField, index []int) *Field {
}

pgTag := parseTag(f.Tag.Get("pg"))
if _, ok := pgTag.Options["array"]; ok {

if _, ok := sqlTag.Options["array"]; ok {
field.SetFlag(ArrayFlag)
} else if _, ok := pgTag.Options["array"]; ok {
field.SetFlag(ArrayFlag)
}

Expand All @@ -367,6 +370,9 @@ func (t *Table) newField(f reflect.StructField, index []int) *Field {
} else if field.HasFlag(ArrayFlag) {
field.append = types.ArrayAppender(f.Type)
field.scan = types.ArrayScanner(f.Type)
} else if _, ok := sqlTag.Options["hstore"]; ok {
field.append = types.HstoreAppender(f.Type)
field.scan = types.HstoreScanner(f.Type)
} else if _, ok := pgTag.Options["hstore"]; ok {
field.append = types.HstoreAppender(f.Type)
field.scan = types.HstoreScanner(f.Type)
Expand Down
4 changes: 2 additions & 2 deletions orm/table_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type CreateTableModel struct {
NullInt64 sql.NullInt64
NullString sql.NullString
Slice []int
SliceArray []int `pg:",array"`
SliceArray []int `sql:",array"`
Map map[int]int
MapHstore map[int]int `pg:",hstore"`
MapHstore map[int]int `sql:",hstore"`
Struct struct{}
StructPtr *struct{}
Unique int `sql:",unique"`
Expand Down
4 changes: 2 additions & 2 deletions pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func InMulti(values ...interface{}) types.ValueAppender {
//
// For struct fields you can use array tag:
//
// Emails []string `pg:",array"`
// Emails []string `sql:",array"`
func Array(v interface{}) *types.Array {
return types.NewArray(v)
}
Expand All @@ -79,7 +79,7 @@ func Array(v interface{}) *types.Array {
//
// For struct fields you can use hstore tag:
//
// Attrs map[string]string `pg:",hstore"`
// Attrs map[string]string `sql:",hstore"`
func Hstore(v interface{}) *types.Hstore {
return types.NewHstore(v)
}
Expand Down

0 comments on commit 3ddf462

Please sign in to comment.