Swap direct access to the DB on the API server with an data abstraction layer. Implement each API type converter separately and revert changes to the auto-generated server implementation types. Add error return to DAL methods. Implement `Transactions`. Add tools.go with oapi-codegen and mockgen. https://www.jvt.me/posts/2022/06/15/go-tools-dependency-management/ Update go packages. Issues #5, #12
31 lines
460 B
Go
31 lines
460 B
Go
package dal
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
"reflect"
|
|
)
|
|
|
|
func convert[T any](rows *sql.Rows) []T {
|
|
var ans []T
|
|
for rows.Next() {
|
|
var r T
|
|
s := reflect.ValueOf(&r).Elem()
|
|
|
|
numCols := s.NumField()
|
|
columns := make([]interface{}, numCols)
|
|
|
|
for i := 0; i < numCols; i++ {
|
|
field := s.Field(i)
|
|
columns[i] = field.Addr().Interface()
|
|
}
|
|
|
|
if err := rows.Scan(columns...); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
ans = append(ans, r)
|
|
}
|
|
return ans
|
|
}
|