mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-11 21:44:25 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62a8e56f1b | ||
|
|
d8df1bdac2 |
@@ -12,6 +12,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"github.com/bitechdev/ResolveSpec/pkg/common"
|
"github.com/bitechdev/ResolveSpec/pkg/common"
|
||||||
"github.com/bitechdev/ResolveSpec/pkg/logger"
|
"github.com/bitechdev/ResolveSpec/pkg/logger"
|
||||||
"github.com/bitechdev/ResolveSpec/pkg/restheadspec"
|
"github.com/bitechdev/ResolveSpec/pkg/restheadspec"
|
||||||
@@ -1099,9 +1101,25 @@ func normalizePostgresValue(value interface{}) interface{} {
|
|||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
// Recursively normalize nested maps
|
// Recursively normalize nested maps
|
||||||
return normalizePostgresTypes(v)
|
return normalizePostgresTypes(v)
|
||||||
|
case string:
|
||||||
|
var jsonObj interface{}
|
||||||
|
if err := json.Unmarshal([]byte(v), &jsonObj); err == nil {
|
||||||
|
// It's valid JSON, return as json.RawMessage so it's not double-encoded
|
||||||
|
return json.RawMessage(v)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
case uuid.UUID:
|
||||||
|
return v.String()
|
||||||
|
case time.Time:
|
||||||
|
return v.Format(time.RFC3339)
|
||||||
|
case bool, int, int8, int16, int32, int64, float32, float64, uint, uint8, uint16, uint32, uint64:
|
||||||
|
return v
|
||||||
default:
|
default:
|
||||||
// For other types (int, float, string, bool, etc.), return as-is
|
// For other types (int, float, bool, etc.), return as-is
|
||||||
|
// Check stringers
|
||||||
|
if str, ok := v.(fmt.Stringer); ok {
|
||||||
|
return str.String()
|
||||||
|
}
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,3 +47,20 @@ func ExtractTableNameOnly(fullName string) string {
|
|||||||
|
|
||||||
return fullName[startIndex:]
|
return fullName[startIndex:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetPointerElement returns the element type if the provided reflect.Type is a pointer.
|
||||||
|
// If the type is a slice of pointers, it returns the element type of the pointer within the slice.
|
||||||
|
// If neither condition is met, it returns the original type.
|
||||||
|
func GetPointerElement(v reflect.Type) reflect.Type {
|
||||||
|
if v.Kind() == reflect.Ptr {
|
||||||
|
return v.Elem()
|
||||||
|
}
|
||||||
|
if v.Kind() == reflect.Slice && v.Elem().Kind() == reflect.Ptr {
|
||||||
|
subElem := v.Elem()
|
||||||
|
if subElem.Elem().Kind() == reflect.Ptr {
|
||||||
|
return subElem.Elem().Elem()
|
||||||
|
}
|
||||||
|
return v.Elem()
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|||||||
@@ -702,7 +702,7 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, url
|
|||||||
pkName := reflection.GetPrimaryKeyName(model)
|
pkName := reflection.GetPrimaryKeyName(model)
|
||||||
|
|
||||||
// First, read the existing record from the database
|
// First, read the existing record from the database
|
||||||
existingRecord := reflect.New(reflect.TypeOf(model).Elem()).Interface()
|
existingRecord := reflect.New(reflection.GetPointerElement(reflect.TypeOf(model))).Interface()
|
||||||
selectQuery := h.db.NewSelect().Model(existingRecord)
|
selectQuery := h.db.NewSelect().Model(existingRecord)
|
||||||
|
|
||||||
// Apply conditions to select
|
// Apply conditions to select
|
||||||
@@ -850,7 +850,7 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, url
|
|||||||
for _, item := range updates {
|
for _, item := range updates {
|
||||||
if itemID, ok := item["id"]; ok {
|
if itemID, ok := item["id"]; ok {
|
||||||
// First, read the existing record
|
// First, read the existing record
|
||||||
existingRecord := reflect.New(reflect.TypeOf(model).Elem()).Interface()
|
existingRecord := reflect.New(reflection.GetPointerElement(reflect.TypeOf(model))).Interface()
|
||||||
selectQuery := tx.NewSelect().Model(existingRecord).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), itemID)
|
selectQuery := tx.NewSelect().Model(existingRecord).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), itemID)
|
||||||
if err := selectQuery.ScanModel(ctx); err != nil {
|
if err := selectQuery.ScanModel(ctx); err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
@@ -958,7 +958,7 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, url
|
|||||||
if itemMap, ok := item.(map[string]interface{}); ok {
|
if itemMap, ok := item.(map[string]interface{}); ok {
|
||||||
if itemID, ok := itemMap["id"]; ok {
|
if itemID, ok := itemMap["id"]; ok {
|
||||||
// First, read the existing record
|
// First, read the existing record
|
||||||
existingRecord := reflect.New(reflect.TypeOf(model).Elem()).Interface()
|
existingRecord := reflect.New(reflection.GetPointerElement(reflect.TypeOf(model))).Interface()
|
||||||
selectQuery := tx.NewSelect().Model(existingRecord).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), itemID)
|
selectQuery := tx.NewSelect().Model(existingRecord).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), itemID)
|
||||||
if err := selectQuery.ScanModel(ctx); err != nil {
|
if err := selectQuery.ScanModel(ctx); err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
|
|||||||
@@ -1240,7 +1240,7 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, id
|
|||||||
txNestedProcessor := common.NewNestedCUDProcessor(tx, h.registry, h)
|
txNestedProcessor := common.NewNestedCUDProcessor(tx, h.registry, h)
|
||||||
|
|
||||||
// First, read the existing record from the database
|
// First, read the existing record from the database
|
||||||
existingRecord := reflect.New(reflect.TypeOf(model).Elem()).Interface()
|
existingRecord := reflect.New(reflection.GetPointerElement(reflect.TypeOf(model))).Interface()
|
||||||
selectQuery := tx.NewSelect().Model(existingRecord).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), targetID)
|
selectQuery := tx.NewSelect().Model(existingRecord).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), targetID)
|
||||||
if err := selectQuery.ScanModel(ctx); err != nil {
|
if err := selectQuery.ScanModel(ctx); err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
@@ -1294,9 +1294,7 @@ func (h *Handler) handleUpdate(ctx context.Context, w common.ResponseWriter, id
|
|||||||
// Populate model instance from dataMap to preserve custom types (like SqlJSONB)
|
// Populate model instance from dataMap to preserve custom types (like SqlJSONB)
|
||||||
// Get the type of the model, handling both pointer and non-pointer types
|
// Get the type of the model, handling both pointer and non-pointer types
|
||||||
modelType := reflect.TypeOf(model)
|
modelType := reflect.TypeOf(model)
|
||||||
if modelType.Kind() == reflect.Ptr {
|
modelType = reflection.GetPointerElement(modelType)
|
||||||
modelType = modelType.Elem()
|
|
||||||
}
|
|
||||||
modelInstance := reflect.New(modelType).Interface()
|
modelInstance := reflect.New(modelType).Interface()
|
||||||
if err := reflection.MapToStruct(dataMap, modelInstance); err != nil {
|
if err := reflection.MapToStruct(dataMap, modelInstance); err != nil {
|
||||||
return fmt.Errorf("failed to populate model from data: %w", err)
|
return fmt.Errorf("failed to populate model from data: %w", err)
|
||||||
@@ -1600,9 +1598,7 @@ func (h *Handler) handleDelete(ctx context.Context, w common.ResponseWriter, id
|
|||||||
|
|
||||||
// First, fetch the record that will be deleted
|
// First, fetch the record that will be deleted
|
||||||
modelType := reflect.TypeOf(model)
|
modelType := reflect.TypeOf(model)
|
||||||
if modelType.Kind() == reflect.Ptr {
|
modelType = reflection.GetPointerElement(modelType)
|
||||||
modelType = modelType.Elem()
|
|
||||||
}
|
|
||||||
recordToDelete := reflect.New(modelType).Interface()
|
recordToDelete := reflect.New(modelType).Interface()
|
||||||
|
|
||||||
selectQuery := h.db.NewSelect().Model(recordToDelete).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), id)
|
selectQuery := h.db.NewSelect().Model(recordToDelete).Where(fmt.Sprintf("%s = ?", common.QuoteIdent(pkName)), id)
|
||||||
|
|||||||
Reference in New Issue
Block a user