-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathmsgpack.go
More file actions
41 lines (34 loc) · 1.4 KB
/
Copy pathmsgpack.go
File metadata and controls
41 lines (34 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Package encoding provides centralized serialization/deserialization for Marmot.
// ALL msgpack operations MUST go through this package to ensure consistent behavior.
//
// Thread Safety: Marshal and Unmarshal are safe for concurrent use.
//
// Type Preservation: When decoding into interface{}, msgpack strings decode as
// Go strings (not []byte). This is critical for SQLite which treats BLOB and TEXT
// as different types for PRIMARY KEY comparison.
package encoding
import (
"bytes"
"github.com/vmihailenco/msgpack/v5"
)
// Marshal encodes a value to msgpack format.
func Marshal(v interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
if err := enc.Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Unmarshal decodes msgpack data using loose interface decoding.
// When decoding into interface{}, strings are preserved as Go strings (not []byte).
// This is essential for SQLite type matching.
func Unmarshal(data []byte, v interface{}) error {
dec := msgpack.NewDecoder(bytes.NewReader(data))
// UseLooseInterfaceDecoding converts []byte to string when decoding into interface{}
// This is CRITICAL for SQLite which treats BLOB and TEXT as different types
// for PRIMARY KEY comparison. Without this, INSERT OR REPLACE fails to find
// existing rows because the PK value type doesn't match.
dec.UseLooseInterfaceDecoding(true)
return dec.Decode(v)
}