24 lines
560 B
Go
24 lines
560 B
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"gorm.io/datatypes"
|
|
)
|
|
|
|
// JSONMap is a convenience type for JSON map fields used in request DTOs.
|
|
// Maps to datatypes.JSON for GORM storage.
|
|
type JSONMap map[string]interface{}
|
|
|
|
// ToDatatypesJSON converts a JSONMap to datatypes.JSON for GORM storage.
|
|
// Returns nil if the input is nil or empty.
|
|
func ToDatatypesJSON(m *JSONMap) datatypes.JSON {
|
|
if m == nil || len(*m) == 0 {
|
|
return datatypes.JSON("{}")
|
|
}
|
|
b, err := json.Marshal(*m)
|
|
if err != nil {
|
|
return datatypes.JSON("{}")
|
|
}
|
|
return datatypes.JSON(b)
|
|
} |