datastore/internal/api/converter.go
Luís Murta 86036afa4e
API server online and DB connection established
Introduces the api-server/main.go, which creates the echo server,
connects it to the implemented handlers and runs it.

Fix the SQL query to retrieved transactions w/ categories.

Issues #6 and #7
2024-03-23 16:51:07 +00:00

31 lines
460 B
Go

package api
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
}