aboutsummaryrefslogblamecommitdiffstats
path: root/worker/jmap/cache/gob.go
blob: 589bd954d464a4a56ed927dea8871b7bb111a5e9 (plain) (tree)


































                                                        
package cache

import (
	"bytes"
	"encoding/gob"

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

type jmapObject interface {
	*jmap.Session |
		*email.Email |
		*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)
}