owpengram-server/internal/store/postgres/media_codec.go
2026-06-04 01:37:39 +08:00

88 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package postgres
import (
"encoding/json"
"telesrv/internal/domain"
)
// 媒体相关 JSON 编解码domain 值对象 ↔ JSONB 列。domain.* 带 json tag可直接 marshal。
// jsonArrayOrEmpty 把切片序列化为 JSONBnil 序列化为 "[]"(列为 NOT NULL DEFAULT '[]')。
func jsonArrayOrEmpty(v any) ([]byte, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
if string(b) == "null" {
return []byte("[]"), nil
}
return b, nil
}
// encodeMessageMedia 把消息媒体快照序列化为 JSONB无媒体序列化为 "{}"。
func encodeMessageMedia(m *domain.MessageMedia) ([]byte, error) {
if m.IsZero() {
return []byte("{}"), nil
}
return json.Marshal(m)
}
// decodeMessageMedia 把消息行的 media JSONB 文本还原为 *MessageMedia空载荷返回 nil。
func decodeMessageMedia(s string) (*domain.MessageMedia, error) {
if s == "" || s == "{}" || s == "null" {
return nil, nil
}
var m domain.MessageMedia
if err := json.Unmarshal([]byte(s), &m); err != nil {
return nil, err
}
if m.IsZero() {
return nil, nil
}
return &m, nil
}
func decodePhotoSizes(s string) ([]domain.PhotoSize, error) {
if s == "" || s == "[]" || s == "null" {
return nil, nil
}
var out []domain.PhotoSize
if err := json.Unmarshal([]byte(s), &out); err != nil {
return nil, err
}
return out, nil
}
func decodeDocumentAttributes(s string) ([]domain.DocumentAttribute, error) {
if s == "" || s == "[]" || s == "null" {
return nil, nil
}
var out []domain.DocumentAttribute
if err := json.Unmarshal([]byte(s), &out); err != nil {
return nil, err
}
return out, nil
}
func decodeInt64Slice(s string) ([]int64, error) {
if s == "" || s == "[]" || s == "null" {
return nil, nil
}
var out []int64
if err := json.Unmarshal([]byte(s), &out); err != nil {
return nil, err
}
return out, nil
}
func decodeStickerPacks(s string) ([]domain.StickerPack, error) {
if s == "" || s == "[]" || s == "null" {
return nil, nil
}
var out []domain.StickerPack
if err := json.Unmarshal([]byte(s), &out); err != nil {
return nil, err
}
return out, nil
}