chore: update goctl interface{} to any (#2819)
* chore: update goctl interface{} to any
* chore: update goctl interface{} to any
This commit is contained in:
@@ -71,7 +71,7 @@ func (m *defaultStudentModel) Insert(data Student) (sql.Result, error) {
|
||||
func (m *defaultStudentModel) FindOne(id int64) (*Student, error) {
|
||||
studentIdKey := fmt.Sprintf("%s%v", cacheStudentIdPrefix, id)
|
||||
var resp Student
|
||||
err := m.QueryRow(&resp, studentIdKey, func(conn sqlx.SqlConn, v interface{}) error {
|
||||
err := m.QueryRow(&resp, studentIdKey, func(conn sqlx.SqlConn, v any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", studentRows, m.table)
|
||||
return conn.QueryRow(v, query, id)
|
||||
})
|
||||
@@ -88,7 +88,7 @@ func (m *defaultStudentModel) FindOne(id int64) (*Student, error) {
|
||||
func (m *defaultStudentModel) FindOneByClassName(class, name string) (*Student, error) {
|
||||
studentClassNameKey := fmt.Sprintf("%s%v%v", cacheStudentClassNamePrefix, class, name)
|
||||
var resp Student
|
||||
err := m.QueryRowIndex(&resp, studentClassNameKey, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
err := m.QueryRowIndex(&resp, studentClassNameKey, m.formatPrimary, func(conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `class` = ? and `name` = ? limit 1", studentRows, m.table)
|
||||
if err := conn.QueryRow(&resp, query, class, name); err != nil {
|
||||
return nil, err
|
||||
@@ -124,11 +124,11 @@ func (m *defaultStudentModel) Delete(id int64, className, studentName string) er
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultStudentModel) formatPrimary(primary interface{}) string {
|
||||
func (m *defaultStudentModel) formatPrimary(primary any) string {
|
||||
return fmt.Sprintf("%s%v", cacheStudentIdPrefix, primary)
|
||||
}
|
||||
|
||||
func (m *defaultStudentModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
func (m *defaultStudentModel) queryPrimary(conn sqlx.SqlConn, v, primary any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", studentRows, m.table)
|
||||
return conn.QueryRow(v, query, primary)
|
||||
}
|
||||
|
||||
@@ -27,13 +27,13 @@ type rowsScanner interface {
|
||||
Columns() ([]string, error)
|
||||
Err() error
|
||||
Next() bool
|
||||
Scan(v ...interface{}) error
|
||||
Scan(v ...any) error
|
||||
}
|
||||
|
||||
func getTaggedFieldValueMap(v reflect.Value) (map[string]interface{}, error) {
|
||||
func getTaggedFieldValueMap(v reflect.Value) (map[string]any, error) {
|
||||
rt := mapping.Deref(v.Type())
|
||||
size := rt.NumField()
|
||||
result := make(map[string]interface{}, size)
|
||||
result := make(map[string]any, size)
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
key := parseTagName(rt.Field(i))
|
||||
@@ -63,7 +63,7 @@ func getTaggedFieldValueMap(v reflect.Value) (map[string]interface{}, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func mapStructFieldsIntoSlice(v reflect.Value, columns []string, strict bool) ([]interface{}, error) {
|
||||
func mapStructFieldsIntoSlice(v reflect.Value, columns []string, strict bool) ([]any, error) {
|
||||
fields := unwrapFields(v)
|
||||
if strict && len(columns) < len(fields) {
|
||||
return nil, ErrNotMatchDestination
|
||||
@@ -74,7 +74,7 @@ func mapStructFieldsIntoSlice(v reflect.Value, columns []string, strict bool) ([
|
||||
return nil, err
|
||||
}
|
||||
|
||||
values := make([]interface{}, len(columns))
|
||||
values := make([]any, len(columns))
|
||||
if len(taggedMap) == 0 {
|
||||
for i := 0; i < len(values); i++ {
|
||||
valueField := fields[i]
|
||||
@@ -100,7 +100,7 @@ func mapStructFieldsIntoSlice(v reflect.Value, columns []string, strict bool) ([
|
||||
if tagged, ok := taggedMap[column]; ok {
|
||||
values[i] = tagged
|
||||
} else {
|
||||
var anonymous interface{}
|
||||
var anonymous any
|
||||
values[i] = &anonymous
|
||||
}
|
||||
}
|
||||
@@ -119,7 +119,7 @@ func parseTagName(field reflect.StructField) string {
|
||||
return options[0]
|
||||
}
|
||||
|
||||
func unmarshalRow(v interface{}, scanner rowsScanner, strict bool) error {
|
||||
func unmarshalRow(v any, scanner rowsScanner, strict bool) error {
|
||||
if !scanner.Next() {
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
@@ -161,7 +161,7 @@ func unmarshalRow(v interface{}, scanner rowsScanner, strict bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
func unmarshalRows(v interface{}, scanner rowsScanner, strict bool) error {
|
||||
func unmarshalRows(v any, scanner rowsScanner, strict bool) error {
|
||||
rv := reflect.ValueOf(v)
|
||||
if err := mapping.ValidatePtr(&rv); err != nil {
|
||||
return err
|
||||
@@ -181,7 +181,7 @@ func unmarshalRows(v interface{}, scanner rowsScanner, strict bool) error {
|
||||
rve.Set(reflect.Append(rve, reflect.Indirect(item)))
|
||||
}
|
||||
}
|
||||
fillFn := func(value interface{}) error {
|
||||
fillFn := func(value any) error {
|
||||
if rve.CanSet() {
|
||||
if err := scanner.Scan(value); err != nil {
|
||||
return err
|
||||
|
||||
@@ -26,12 +26,12 @@ func NewMockConn(db *sql.DB) *MockConn {
|
||||
}
|
||||
|
||||
// Exec executes sql and returns the result
|
||||
func (conn *MockConn) Exec(query string, args ...interface{}) (sql.Result, error) {
|
||||
func (conn *MockConn) Exec(query string, args ...any) (sql.Result, error) {
|
||||
return exec(conn.db, query, args...)
|
||||
}
|
||||
|
||||
// ExecCtx executes sql and returns the result
|
||||
func (conn *MockConn) ExecCtx(_ context.Context, query string, args ...interface{}) (sql.Result, error) {
|
||||
func (conn *MockConn) ExecCtx(_ context.Context, query string, args ...any) (sql.Result, error) {
|
||||
return exec(conn.db, query, args...)
|
||||
}
|
||||
|
||||
@@ -47,50 +47,50 @@ func (conn *MockConn) PrepareCtx(_ context.Context, query string) (sqlx.StmtSess
|
||||
}
|
||||
|
||||
// QueryRow executes sql and returns a query row
|
||||
func (conn *MockConn) QueryRow(v interface{}, q string, args ...interface{}) error {
|
||||
func (conn *MockConn) QueryRow(v any, q string, args ...any) error {
|
||||
return query(conn.db, func(rows *sql.Rows) error {
|
||||
return unmarshalRow(v, rows, true)
|
||||
}, q, args...)
|
||||
}
|
||||
|
||||
// QueryRowCtx executes sql and returns a query row
|
||||
func (conn *MockConn) QueryRowCtx(_ context.Context, v interface{}, query string, args ...interface{}) error {
|
||||
func (conn *MockConn) QueryRowCtx(_ context.Context, v any, query string, args ...any) error {
|
||||
return conn.QueryRow(v, query, args...)
|
||||
}
|
||||
|
||||
// QueryRowPartial executes sql and returns a partial query row
|
||||
func (conn *MockConn) QueryRowPartial(v interface{}, q string, args ...interface{}) error {
|
||||
func (conn *MockConn) QueryRowPartial(v any, q string, args ...any) error {
|
||||
return query(conn.db, func(rows *sql.Rows) error {
|
||||
return unmarshalRow(v, rows, false)
|
||||
}, q, args...)
|
||||
}
|
||||
|
||||
// QueryRowPartialCtx executes sql and returns a partial query row
|
||||
func (conn *MockConn) QueryRowPartialCtx(_ context.Context, v interface{}, query string, args ...interface{}) error {
|
||||
func (conn *MockConn) QueryRowPartialCtx(_ context.Context, v any, query string, args ...any) error {
|
||||
return conn.QueryRowPartial(v, query, args...)
|
||||
}
|
||||
|
||||
// QueryRows executes sql and returns query rows
|
||||
func (conn *MockConn) QueryRows(v interface{}, q string, args ...interface{}) error {
|
||||
func (conn *MockConn) QueryRows(v any, q string, args ...any) error {
|
||||
return query(conn.db, func(rows *sql.Rows) error {
|
||||
return unmarshalRows(v, rows, true)
|
||||
}, q, args...)
|
||||
}
|
||||
|
||||
// QueryRowsCtx executes sql and returns query rows
|
||||
func (conn *MockConn) QueryRowsCtx(_ context.Context, v interface{}, query string, args ...interface{}) error {
|
||||
func (conn *MockConn) QueryRowsCtx(_ context.Context, v any, query string, args ...any) error {
|
||||
return conn.QueryRows(v, query, args...)
|
||||
}
|
||||
|
||||
// QueryRowsPartial executes sql and returns partial query rows
|
||||
func (conn *MockConn) QueryRowsPartial(v interface{}, q string, args ...interface{}) error {
|
||||
func (conn *MockConn) QueryRowsPartial(v any, q string, args ...any) error {
|
||||
return query(conn.db, func(rows *sql.Rows) error {
|
||||
return unmarshalRows(v, rows, false)
|
||||
}, q, args...)
|
||||
}
|
||||
|
||||
// QueryRowsPartialCtx executes sql and returns partial query rows
|
||||
func (conn *MockConn) QueryRowsPartialCtx(_ context.Context, v interface{}, query string, args ...interface{}) error {
|
||||
func (conn *MockConn) QueryRowsPartialCtx(_ context.Context, v any, query string, args ...any) error {
|
||||
return conn.QueryRowsPartial(v, query, args...)
|
||||
}
|
||||
|
||||
@@ -113,50 +113,50 @@ func (s statement) Close() error {
|
||||
return s.stmt.Close()
|
||||
}
|
||||
|
||||
func (s statement) Exec(args ...interface{}) (sql.Result, error) {
|
||||
func (s statement) Exec(args ...any) (sql.Result, error) {
|
||||
return execStmt(s.stmt, args...)
|
||||
}
|
||||
|
||||
func (s statement) ExecCtx(_ context.Context, args ...interface{}) (sql.Result, error) {
|
||||
func (s statement) ExecCtx(_ context.Context, args ...any) (sql.Result, error) {
|
||||
return s.Exec(args...)
|
||||
}
|
||||
|
||||
func (s statement) QueryRow(v interface{}, args ...interface{}) error {
|
||||
func (s statement) QueryRow(v any, args ...any) error {
|
||||
return queryStmt(s.stmt, func(rows *sql.Rows) error {
|
||||
return unmarshalRow(v, rows, true)
|
||||
}, args...)
|
||||
}
|
||||
|
||||
func (s statement) QueryRowCtx(_ context.Context, v interface{}, args ...interface{}) error {
|
||||
func (s statement) QueryRowCtx(_ context.Context, v any, args ...any) error {
|
||||
return s.QueryRow(v, args...)
|
||||
}
|
||||
|
||||
func (s statement) QueryRowPartial(v interface{}, args ...interface{}) error {
|
||||
func (s statement) QueryRowPartial(v any, args ...any) error {
|
||||
return queryStmt(s.stmt, func(rows *sql.Rows) error {
|
||||
return unmarshalRow(v, rows, false)
|
||||
}, args...)
|
||||
}
|
||||
|
||||
func (s statement) QueryRowPartialCtx(_ context.Context, v interface{}, args ...interface{}) error {
|
||||
func (s statement) QueryRowPartialCtx(_ context.Context, v any, args ...any) error {
|
||||
return s.QueryRowPartial(v, args...)
|
||||
}
|
||||
|
||||
func (s statement) QueryRows(v interface{}, args ...interface{}) error {
|
||||
func (s statement) QueryRows(v any, args ...any) error {
|
||||
return queryStmt(s.stmt, func(rows *sql.Rows) error {
|
||||
return unmarshalRows(v, rows, true)
|
||||
}, args...)
|
||||
}
|
||||
|
||||
func (s statement) QueryRowsCtx(_ context.Context, v interface{}, args ...interface{}) error {
|
||||
func (s statement) QueryRowsCtx(_ context.Context, v any, args ...any) error {
|
||||
return s.QueryRows(v, args...)
|
||||
}
|
||||
|
||||
func (s statement) QueryRowsPartial(v interface{}, args ...interface{}) error {
|
||||
func (s statement) QueryRowsPartial(v any, args ...any) error {
|
||||
return queryStmt(s.stmt, func(rows *sql.Rows) error {
|
||||
return unmarshalRows(v, rows, false)
|
||||
}, args...)
|
||||
}
|
||||
|
||||
func (s statement) QueryRowsPartialCtx(_ context.Context, v interface{}, args ...interface{}) error {
|
||||
func (s statement) QueryRowsPartialCtx(_ context.Context, v any, args ...any) error {
|
||||
return s.QueryRowsPartial(v, args...)
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
const slowThreshold = time.Millisecond * 500
|
||||
|
||||
func exec(db *sql.DB, q string, args ...interface{}) (sql.Result, error) {
|
||||
func exec(db *sql.DB, q string, args ...any) (sql.Result, error) {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -48,7 +48,7 @@ func exec(db *sql.DB, q string, args ...interface{}) (sql.Result, error) {
|
||||
return result, err
|
||||
}
|
||||
|
||||
func execStmt(conn *sql.Stmt, args ...interface{}) (sql.Result, error) {
|
||||
func execStmt(conn *sql.Stmt, args ...any) (sql.Result, error) {
|
||||
stmt := fmt.Sprint(args...)
|
||||
startTime := timex.Now()
|
||||
result, err := conn.Exec(args...)
|
||||
@@ -65,7 +65,7 @@ func execStmt(conn *sql.Stmt, args ...interface{}) (sql.Result, error) {
|
||||
return result, err
|
||||
}
|
||||
|
||||
func query(db *sql.DB, scanner func(*sql.Rows) error, q string, args ...interface{}) error {
|
||||
func query(db *sql.DB, scanner func(*sql.Rows) error, q string, args ...any) error {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -102,7 +102,7 @@ func query(db *sql.DB, scanner func(*sql.Rows) error, q string, args ...interfac
|
||||
return scanner(rows)
|
||||
}
|
||||
|
||||
func queryStmt(conn *sql.Stmt, scanner func(*sql.Rows) error, args ...interface{}) error {
|
||||
func queryStmt(conn *sql.Stmt, scanner func(*sql.Rows) error, args ...any) error {
|
||||
stmt := fmt.Sprint(args...)
|
||||
startTime := timex.Now()
|
||||
rows, err := conn.Query(args...)
|
||||
|
||||
@@ -41,7 +41,7 @@ func escape(input string) string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func format(query string, args ...interface{}) (string, error) {
|
||||
func format(query string, args ...any) (string, error) {
|
||||
numArgs := len(args)
|
||||
if numArgs == 0 {
|
||||
return query, nil
|
||||
|
||||
Reference in New Issue
Block a user