package server import ( "encoding/json" "net/http" "github.com/gin-gonic/gin" "gorm.io/datatypes" ) // marshalJSON converts a map to datatypes.JSON for storage. Returns nil on // nil input so empty detail columns stay NULL-ish rather than "null". func marshalJSON(m map[string]any) datatypes.JSON { if m == nil { return nil } b, err := json.Marshal(m) if err != nil { return datatypes.JSON([]byte("{}")) } return b } // bind parses the JSON request body into out and writes a 400 on failure. // Returns false (and aborts) when the body is invalid so callers can early-return. func bind(c *gin.Context, out any) bool { if err := c.ShouldBindJSON(out); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效: " + err.Error()}) return false } return true } // jsonRaw marshals v to datatypes.JSON for storage in a JSON column. func jsonRaw(v any) datatypes.JSON { b, err := json.Marshal(v) if err != nil { return datatypes.JSON([]byte("null")) } return b } // jsonUnmarshal decodes a datatypes.JSON value into out. func jsonUnmarshal(raw datatypes.JSON, out any) error { if len(raw) == 0 { return nil } return json.Unmarshal(raw, out) }