mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
Implement scan struct
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -8,11 +9,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type DBField struct {
|
type DBField struct {
|
||||||
Name string
|
Name string
|
||||||
Value any
|
Value any
|
||||||
Type reflect.Type
|
Type reflect.Type
|
||||||
useDefault bool
|
OriginalName string
|
||||||
encrypted bool
|
useDefault bool
|
||||||
|
encrypted bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func StructTagsToFields(s any) ([]DBField, error) {
|
func StructTagsToFields(s any) ([]DBField, error) {
|
||||||
@@ -71,11 +73,12 @@ func StructTagsToFields(s any) ([]DBField, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fields = append(fields, DBField{
|
fields = append(fields, DBField{
|
||||||
Name: tag,
|
Name: tag,
|
||||||
Value: v.Field(i).Interface(),
|
Value: v.Field(i).Interface(),
|
||||||
Type: f.Type,
|
Type: f.Type,
|
||||||
useDefault: useDefault,
|
OriginalName: f.Name,
|
||||||
encrypted: encrypted,
|
useDefault: useDefault,
|
||||||
|
encrypted: encrypted,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return fields, nil
|
return fields, nil
|
||||||
@@ -172,3 +175,64 @@ func (q *Query) UpdateStruct(s any, table string, where []string, args []any) (*
|
|||||||
|
|
||||||
return q, nil
|
return q, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *database) ScanStruct(row *sql.Row, dest any) error {
|
||||||
|
// Get the fields of the destination struct
|
||||||
|
fields, err := StructTagsToFields(dest)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to extract struct fields: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a slice of pointers to hold the scan destinations
|
||||||
|
scanDest := make([]interface{}, len(fields))
|
||||||
|
destVal := reflect.ValueOf(dest).Elem()
|
||||||
|
|
||||||
|
var fieldsToDecrypt []string
|
||||||
|
nullStringIndexes := make(map[int]reflect.Value)
|
||||||
|
|
||||||
|
for i, field := range fields {
|
||||||
|
// Find the field in the struct
|
||||||
|
structField := destVal.FieldByName(field.OriginalName)
|
||||||
|
if !structField.IsValid() {
|
||||||
|
return fmt.Errorf("struct field %s not found", field.OriginalName)
|
||||||
|
}
|
||||||
|
|
||||||
|
if field.encrypted {
|
||||||
|
fieldsToDecrypt = append(fieldsToDecrypt, field.OriginalName)
|
||||||
|
}
|
||||||
|
|
||||||
|
if structField.Kind() == reflect.String {
|
||||||
|
nullStringIndexes[i] = structField
|
||||||
|
|
||||||
|
var ns sql.NullString
|
||||||
|
scanDest[i] = &ns
|
||||||
|
} else {
|
||||||
|
scanDest[i] = structField.Addr().Interface()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan the row into the destination pointers
|
||||||
|
if err := row.Scan(scanDest...); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set null strings to nil if they are null
|
||||||
|
for i, field := range nullStringIndexes {
|
||||||
|
ns := scanDest[i].(*sql.NullString)
|
||||||
|
if ns.Valid {
|
||||||
|
field.SetString(ns.String)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt encrypted fields
|
||||||
|
for _, fieldName := range fieldsToDecrypt {
|
||||||
|
field := destVal.FieldByName(fieldName)
|
||||||
|
decValue, err := db.secretsService.Decrypt(field.Interface().(string))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
field.SetString(decValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user