Refactor boolean value handling in fmtRow function

- Simplified the conversion of 'readable' and 'built_in' fields to boolean values by utilizing a new toBool function, enhancing code readability and maintainability.
- Removed debug SQL print statements from the Update method to clean up the output and improve performance.
This commit is contained in:
Max 2025-07-15 18:37:42 +08:00
parent bd95ac0604
commit cb9606681e
2 changed files with 25 additions and 14 deletions

View file

@ -41,18 +41,10 @@ func fmtRow(row map[string]interface{}) map[string]interface{} {
// Convert boolean values
if row["readable"] != nil {
if row["readable"].(int64) == 1 {
row["readable"] = true
} else {
row["readable"] = false
}
row["readable"] = toBool(row["readable"])
}
if row["built_in"] != nil {
if row["built_in"].(int64) == 1 {
row["built_in"] = true
} else {
row["built_in"] = false
}
row["built_in"] = toBool(row["built_in"])
}
return row
}
@ -345,10 +337,6 @@ func (db *DB) Update(options *types.UpdateOptions) error {
data["updated_at"] = time.Now()
data["mtime"] = time.Now()
// Debug SQL
fmt.Printf("Update SQL: %v\n", data)
fmt.Printf("Update ID: %v\n", rows[0]["id"])
err = m.Update(rows[0]["id"], data)
if err != nil {
return err

23
dsl/io/utils.go Normal file
View file

@ -0,0 +1,23 @@
package io
// toBool converts various types to boolean
func toBool(v interface{}) bool {
if v == nil {
return false
}
switch val := v.(type) {
case bool:
return val
case int:
return val == 1
case int64:
return val == 1
case float64:
return val == 1
case string:
return val == "1" || val == "true"
default:
return false
}
}