From be2ff409a1e5369d0a8b45257e4dbbd646165db1 Mon Sep 17 00:00:00 2001 From: Corsider Date: Fri, 5 Jul 2024 15:18:43 +0300 Subject: [PATCH 1/6] fixed incorrect scanning --- mapper.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/mapper.go b/mapper.go index cf06103..6cf2aad 100644 --- a/mapper.go +++ b/mapper.go @@ -215,7 +215,26 @@ func constructor(size, offset uintptr, next ctorFunc) ctorFunc { } func scannable(typ reflect.Type) bool { - return typ == timeReflectType || typ.Implements(scannerReflectType) + /* + var a **sql.NullFloat64 + println(scannable(reflect.TypeOf(a))) -> true + var b *sql.NullFloat64 + println(scannable(reflect.TypeOf(b))) -> true + var c sql.NullFloat64 + println(scannable(reflect.TypeOf(c))) -> true + */ + for typ.Kind() == reflect.Ptr { // going by ptr's + typ = typ.Elem() + } + + // checking type + if typ == timeReflectType || typ.Implements(scannerReflectType) { + return true + } + + // checking ptr + ptrType := reflect.PtrTo(typ) + return ptrType.Implements(scannerReflectType) } const ( From 2f82910f825742d423ac3d23c034101a8eeaab4c Mon Sep 17 00:00:00 2001 From: Corsider Date: Thu, 11 Jul 2024 13:08:36 +0300 Subject: [PATCH 2/6] switched project to .mod, refactored scanning --- go.mod | 5 +++++ go.sum | 2 ++ init_test.go | 2 +- mapper.go | 26 +++-------------------- mapper_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++ naming.go | 2 +- row_test.go | 2 +- rows_test.go | 2 +- select_test.go | 2 +- wrap_go19_test.go | 3 ++- wrap_test.go | 2 +- 11 files changed, 72 insertions(+), 30 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8dc997e --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module sqle + +go 1.22.0 + +require github.com/mattn/go-sqlite3 v1.14.22 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e8d092a --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/init_test.go b/init_test.go index 79bbbbb..aa12ddb 100644 --- a/init_test.go +++ b/init_test.go @@ -21,7 +21,7 @@ import ( "os" "sync/atomic" - "github.com/lazada/sqle/testdata" + "sqle/testdata" ) var ( diff --git a/mapper.go b/mapper.go index 6cf2aad..1e17585 100644 --- a/mapper.go +++ b/mapper.go @@ -19,11 +19,10 @@ import ( "database/sql/driver" "errors" "reflect" + "sqle/embed" "sync" "time" "unsafe" - - "github.com/lazada/sqle/embed" ) type ctorFunc func(unsafe.Pointer) unsafe.Pointer @@ -149,7 +148,7 @@ func (m *Mapper) inspect(parent *structMap, offset uintptr, typ reflect.Type) *s if fieldtyp = field.Type; fieldtyp.Kind() == reflect.Ptr { fieldtyp, isptr = fieldtyp.Elem(), ptrMask } - if fieldtyp.Kind() == reflect.Struct && !scannable(fieldtyp) { + if fieldtyp.Kind() == reflect.Struct && !scannable(fieldtyp) && !scannable(reflect.PointerTo(fieldtyp)) { if s = m.inspect(smap, field.Offset|isptr, fieldtyp); s != nil { smap.aliases = append(smap.aliases, s.aliases...) smap.fields = append(smap.fields, s.fields...) @@ -215,26 +214,7 @@ func constructor(size, offset uintptr, next ctorFunc) ctorFunc { } func scannable(typ reflect.Type) bool { - /* - var a **sql.NullFloat64 - println(scannable(reflect.TypeOf(a))) -> true - var b *sql.NullFloat64 - println(scannable(reflect.TypeOf(b))) -> true - var c sql.NullFloat64 - println(scannable(reflect.TypeOf(c))) -> true - */ - for typ.Kind() == reflect.Ptr { // going by ptr's - typ = typ.Elem() - } - - // checking type - if typ == timeReflectType || typ.Implements(scannerReflectType) { - return true - } - - // checking ptr - ptrType := reflect.PtrTo(typ) - return ptrType.Implements(scannerReflectType) + return typ == timeReflectType || typ.Implements(scannerReflectType) } const ( diff --git a/mapper_test.go b/mapper_test.go index 92257bf..428dbd8 100644 --- a/mapper_test.go +++ b/mapper_test.go @@ -13,3 +13,57 @@ // limitations under the License. package sqle + +import ( + "database/sql" + "reflect" + "testing" + "time" +) + +type testStruct struct { + ID int + Name string + CreatedAt time.Time + UpdatedAt *time.Time + Price *sql.NullFloat64 + Price2 sql.NullFloat64 +} + +func TestInspect(t *testing.T) { + mapper := NewMapper("sql", nil) + typ := reflect.TypeOf(&testStruct{}).Elem() + + smap := mapper.inspect(nil, 0, typ) + + expectedAliases := []string{"ID", "Name", "CreatedAt", "UpdatedAt", "Price", "Price2"} + if !reflect.DeepEqual(smap.aliases, expectedAliases) { + t.Errorf("Expected aliases %v, but got %v", expectedAliases, smap.aliases) + } + + expectedFieldsCount := len(expectedAliases) + if len(smap.fields) != expectedFieldsCount { + t.Errorf("Expected %d fields, but got %d", expectedFieldsCount, len(smap.fields)) + } + + for i, field := range smap.fields { + expectedOffset := typ.Field(i).Offset + if field.offset != expectedOffset { + t.Errorf("Expected offset %d for field %s, but got %d", expectedOffset, expectedAliases[i], field.offset) + } + } + + expectedTypes := []reflect.Type{ + reflect.TypeOf(int(0)), + reflect.TypeOf(""), + reflect.TypeOf(time.Time{}), + reflect.TypeOf(&time.Time{}), + reflect.TypeOf(&sql.NullFloat64{}), + reflect.TypeOf(sql.NullFloat64{}), + } + for i, field := range smap.fields { + if field.typ != expectedTypes[i] { + t.Errorf("Expected type %v for field %s, but got %v", expectedTypes[i], expectedAliases[i], field.typ) + } + } +} diff --git a/naming.go b/naming.go index 6044284..68f3e45 100644 --- a/naming.go +++ b/naming.go @@ -18,7 +18,7 @@ import ( "strings" "sync" - "github.com/lazada/sqle/strcase" + "sqle/strcase" ) type NamingConvention interface { diff --git a/row_test.go b/row_test.go index 115e004..b27b2d2 100644 --- a/row_test.go +++ b/row_test.go @@ -19,7 +19,7 @@ import ( "testing" "time" - "github.com/lazada/sqle/testdata" + "sqle/testdata" ) func TestRow_ScanMap(t *testing.T) { diff --git a/rows_test.go b/rows_test.go index 416b233..20ce71f 100644 --- a/rows_test.go +++ b/rows_test.go @@ -18,7 +18,7 @@ import ( "testing" "time" - "github.com/lazada/sqle/testdata" + "sqle/testdata" ) func TestRows_ScanMap(t *testing.T) { diff --git a/select_test.go b/select_test.go index 0d5041f..e3dbaa7 100644 --- a/select_test.go +++ b/select_test.go @@ -18,7 +18,7 @@ import ( "testing" "time" - "github.com/lazada/sqle/testdata" + "sqle/testdata" ) func selectStruct(b *testing.B, limit int) { diff --git a/wrap_go19_test.go b/wrap_go19_test.go index 2cf070a..73854b6 100644 --- a/wrap_go19_test.go +++ b/wrap_go19_test.go @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build go1.9 // +build go1.9 package sqle @@ -20,7 +21,7 @@ import ( "context" "testing" - "github.com/lazada/sqle/testdata" + "sqle/testdata" ) func TestWrap_Conn(t *testing.T) { diff --git a/wrap_test.go b/wrap_test.go index 9bc12c4..311c080 100644 --- a/wrap_test.go +++ b/wrap_test.go @@ -18,7 +18,7 @@ import ( "context" "testing" - "github.com/lazada/sqle/testdata" + "sqle/testdata" ) func TestWrap_DB(t *testing.T) { From 1595fe575bc1f63cc5f16d1aa37bf6c14e5a0879 Mon Sep 17 00:00:00 2001 From: Corsider Date: Thu, 11 Jul 2024 13:32:29 +0300 Subject: [PATCH 3/6] changed module name --- go.mod | 2 +- init_test.go | 2 +- mapper.go | 2 +- naming.go | 2 +- row_test.go | 2 +- rows_test.go | 2 +- select_test.go | 2 +- wrap_go19_test.go | 2 +- wrap_test.go | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 8dc997e..6736004 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module sqle +module github.com/lazada/sqle go 1.22.0 diff --git a/init_test.go b/init_test.go index aa12ddb..79bbbbb 100644 --- a/init_test.go +++ b/init_test.go @@ -21,7 +21,7 @@ import ( "os" "sync/atomic" - "sqle/testdata" + "github.com/lazada/sqle/testdata" ) var ( diff --git a/mapper.go b/mapper.go index 1e17585..12d3d02 100644 --- a/mapper.go +++ b/mapper.go @@ -18,8 +18,8 @@ import ( "database/sql" "database/sql/driver" "errors" + "github.com/lazada/sqle/embed" "reflect" - "sqle/embed" "sync" "time" "unsafe" diff --git a/naming.go b/naming.go index 68f3e45..6044284 100644 --- a/naming.go +++ b/naming.go @@ -18,7 +18,7 @@ import ( "strings" "sync" - "sqle/strcase" + "github.com/lazada/sqle/strcase" ) type NamingConvention interface { diff --git a/row_test.go b/row_test.go index b27b2d2..115e004 100644 --- a/row_test.go +++ b/row_test.go @@ -19,7 +19,7 @@ import ( "testing" "time" - "sqle/testdata" + "github.com/lazada/sqle/testdata" ) func TestRow_ScanMap(t *testing.T) { diff --git a/rows_test.go b/rows_test.go index 20ce71f..416b233 100644 --- a/rows_test.go +++ b/rows_test.go @@ -18,7 +18,7 @@ import ( "testing" "time" - "sqle/testdata" + "github.com/lazada/sqle/testdata" ) func TestRows_ScanMap(t *testing.T) { diff --git a/select_test.go b/select_test.go index e3dbaa7..0d5041f 100644 --- a/select_test.go +++ b/select_test.go @@ -18,7 +18,7 @@ import ( "testing" "time" - "sqle/testdata" + "github.com/lazada/sqle/testdata" ) func selectStruct(b *testing.B, limit int) { diff --git a/wrap_go19_test.go b/wrap_go19_test.go index 73854b6..7fcee9c 100644 --- a/wrap_go19_test.go +++ b/wrap_go19_test.go @@ -21,7 +21,7 @@ import ( "context" "testing" - "sqle/testdata" + "github.com/lazada/sqle/testdata" ) func TestWrap_Conn(t *testing.T) { diff --git a/wrap_test.go b/wrap_test.go index 311c080..9bc12c4 100644 --- a/wrap_test.go +++ b/wrap_test.go @@ -18,7 +18,7 @@ import ( "context" "testing" - "sqle/testdata" + "github.com/lazada/sqle/testdata" ) func TestWrap_DB(t *testing.T) { From 7ba16de25623219b610112e407e8f382f3b3f734 Mon Sep 17 00:00:00 2001 From: Corsider Date: Thu, 11 Jul 2024 13:37:46 +0300 Subject: [PATCH 4/6] decreased go version --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 6736004..267a43f 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/lazada/sqle -go 1.22.0 +go 1.8.0 require github.com/mattn/go-sqlite3 v1.14.22 From d4ec08c336ebb266d46f28ac3bd45fdefb42174f Mon Sep 17 00:00:00 2001 From: Corsider Date: Thu, 11 Jul 2024 17:38:11 +0300 Subject: [PATCH 5/6] removed .mod --- go.mod | 5 ----- go.sum | 2 -- 2 files changed, 7 deletions(-) delete mode 100644 go.mod delete mode 100644 go.sum diff --git a/go.mod b/go.mod deleted file mode 100644 index 267a43f..0000000 --- a/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module github.com/lazada/sqle - -go 1.8.0 - -require github.com/mattn/go-sqlite3 v1.14.22 diff --git a/go.sum b/go.sum deleted file mode 100644 index e8d092a..0000000 --- a/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= -github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= From 1045ae6d7660a30302de992ce27b3ab8a736de85 Mon Sep 17 00:00:00 2001 From: Egor Barsukov <51398473+Corsider@users.noreply.github.com> Date: Thu, 11 Jul 2024 18:00:43 +0300 Subject: [PATCH 6/6] fixed build tag --- wrap_go19_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/wrap_go19_test.go b/wrap_go19_test.go index 7fcee9c..2cf070a 100644 --- a/wrap_go19_test.go +++ b/wrap_go19_test.go @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build go1.9 // +build go1.9 package sqle