Add docs comments to struct query

This commit is contained in:
2025-03-05 21:30:40 +01:00
parent 3aa8c838e8
commit 52aa406c6d

View File

@@ -18,6 +18,7 @@ type DBField struct {
encrypted bool encrypted bool
} }
// StructTagsToFields converts a struct to a slice of DBField instances
func StructTagsToFields(s any) ([]DBField, error) { func StructTagsToFields(s any) ([]DBField, error) {
v := reflect.ValueOf(s) v := reflect.ValueOf(s)
@@ -106,6 +107,7 @@ func toSnakeCase(s string) string {
return res return res
} }
// InsertStruct creates an INSERT query from a struct
func (q *Query) InsertStruct(s any, table string) (*Query, error) { func (q *Query) InsertStruct(s any, table string) (*Query, error) {
fields, err := StructTagsToFields(s) fields, err := StructTagsToFields(s)
if err != nil { if err != nil {
@@ -142,6 +144,7 @@ func (q *Query) InsertStruct(s any, table string) (*Query, error) {
return q, nil return q, nil
} }
// UpdateStruct creates an UPDATE query from a struct
func (q *Query) UpdateStruct(s any, table string) (*Query, error) { func (q *Query) UpdateStruct(s any, table string) (*Query, error) {
fields, err := StructTagsToFields(s) fields, err := StructTagsToFields(s)
if err != nil { if err != nil {
@@ -171,6 +174,7 @@ func (q *Query) UpdateStruct(s any, table string) (*Query, error) {
return q, nil return q, nil
} }
// SelectStruct creates a SELECT query from a struct
func (q *Query) SelectStruct(s any, table string) (*Query, error) { func (q *Query) SelectStruct(s any, table string) (*Query, error) {
fields, err := StructTagsToFields(s) fields, err := StructTagsToFields(s)
if err != nil { if err != nil {
@@ -188,7 +192,7 @@ func (q *Query) SelectStruct(s any, table string) (*Query, error) {
// Scanner is an interface that both sql.Row and sql.Rows satisfy // Scanner is an interface that both sql.Row and sql.Rows satisfy
type Scanner interface { type Scanner interface {
Scan(dest ...interface{}) error Scan(dest ...any) error
} }
// scanStructInstance is an internal function that handles the scanning logic for a single instance // scanStructInstance is an internal function that handles the scanning logic for a single instance
@@ -198,7 +202,7 @@ func (db *database) scanStructInstance(destVal reflect.Value, scanner Scanner) e
return fmt.Errorf("failed to extract struct fields: %w", err) return fmt.Errorf("failed to extract struct fields: %w", err)
} }
scanDest := make([]interface{}, len(fields)) scanDest := make([]any, len(fields))
var fieldsToDecrypt []string var fieldsToDecrypt []string
nullStringIndexes := make(map[int]reflect.Value) nullStringIndexes := make(map[int]reflect.Value)