mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
dialect/sql: match column scan with sql tag
Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/209 Reviewed By: alexsn Differential Revision: D18757390 fbshipit-source-id: 534a8404a06487d90f1f4f10b1f93c774bac2e97
This commit is contained in:
committed by
Facebook Github Bot
parent
1a73b8cf55
commit
80dab06a57
@@ -101,7 +101,9 @@ func scanStruct(typ reflect.Type, columns []string) (*rowScan, error) {
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
f := typ.Field(i)
|
||||
name := strings.ToLower(f.Name)
|
||||
if tag, ok := f.Tag.Lookup("json"); ok {
|
||||
if tag, ok := f.Tag.Lookup("sql"); ok {
|
||||
name = tag
|
||||
} else if tag, ok := f.Tag.Lookup("json"); ok {
|
||||
name = strings.Split(tag, ",")[0]
|
||||
}
|
||||
names[name] = i
|
||||
|
||||
@@ -71,6 +71,20 @@ func TestScanSlice(t *testing.T) {
|
||||
require.Equal(t, "bar", v4[1].Name)
|
||||
require.Equal(t, 1, v4[0].Count)
|
||||
require.Equal(t, 2, v4[1].Count)
|
||||
|
||||
rows = &mockRows{
|
||||
columns: []string{"nick_name", "COUNT(*)"},
|
||||
values: [][]interface{}{{"foo", 1}, {"bar", 2}},
|
||||
}
|
||||
var v5 []*struct {
|
||||
Count int
|
||||
Name string `json:"name" sql:"nick_name"`
|
||||
}
|
||||
require.NoError(t, ScanSlice(rows, &v5))
|
||||
require.Equal(t, "foo", v5[0].Name)
|
||||
require.Equal(t, "bar", v5[1].Name)
|
||||
require.Equal(t, 1, v5[0].Count)
|
||||
require.Equal(t, 2, v5[1].Count)
|
||||
}
|
||||
|
||||
type mockRows struct {
|
||||
|
||||
Reference in New Issue
Block a user