Files
gochat/internal/model/json_helpers.go
T
2026-06-04 15:44:48 +08:00

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)
}