package notify

import (
	"bytes"
	"encoding/gob"
)

func Register(data interface{}) {
	gob.Register(data)
}

func RegisterName(name string, data interface{}) {
	gob.RegisterName(name, data)
}

func RegisterAll(data []interface{}) {
	for _, v := range data {
		gob.Register(v)
	}
}

func RegisterNames(data map[string]interface{}) {
	for k, v := range data {
		gob.RegisterName(k, v)
	}
}

func encode(src interface{}) ([]byte, error) {
	var buf bytes.Buffer
	enc := gob.NewEncoder(&buf)
	err := enc.Encode(&src)
	return buf.Bytes(), err
}

func Encode(src interface{}) ([]byte, error) {
	return encode(src)
}

func Decode(src []byte) (interface{}, error) {
	dec := gob.NewDecoder(bytes.NewReader(src))
	var dst interface{}
	err := dec.Decode(&dst)
	return dst, err
}
