aboutsummaryrefslogtreecommitdiffstats
path: root/worker/jmap/cache/gob.go
blob: 8b153c50e94332886c4f69d7de2e13f9a9cbba66 (plain) (blame)
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
package cache

import (
	"bytes"
	"encoding/gob"

	"git.sr.ht/~rockorager/go-jmap/mail/email"
	"git.sr.ht/~rockorager/go-jmap/mail/mailbox"
	"git.sr.ht/~rockorager/go-jmap/mail/thread"
)

type jmapObject interface {
	*email.Email |
		*thread.Thread |
		*email.QueryResponse |
		*mailbox.Mailbox |
		*FolderContents |
		*IDList
}

func marshal[T jmapObject](obj T) ([]byte, error) {
	buf := bytes.NewBuffer(nil)
	encoder := gob.NewEncoder(buf)
	err := encoder.Encode(obj)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

func unmarshal[T jmapObject](data []byte, obj T) error {
	buf := bytes.NewBuffer(data)
	decoder := gob.NewDecoder(buf)
	return decoder.Decode(obj)
}