new: added new Meta field to Endpoint objects (closes #66)

This commit is contained in:
evilsocket 2018-02-22 18:41:04 +01:00
commit 02d414107e
3 changed files with 48 additions and 3 deletions

44
network/meta.go Normal file
View file

@ -0,0 +1,44 @@
package network
import (
"encoding/json"
"sync"
)
type Meta struct {
sync.Mutex
m map[string]interface{}
}
// we want to protect concurrent access to the Meta
// object so the m field needs to be unexported, this
// is to have it in JSON regardless.
type metaJSON struct {
Values map[string]interface{} `json:"values"`
}
func NewMeta() *Meta {
return &Meta{
m: make(map[string]interface{}),
}
}
func (m *Meta) MarshalJSON() ([]byte, error) {
return json.Marshal(metaJSON{Values: m.m})
}
func (m *Meta) Set(name string, value interface{}) {
m.Lock()
defer m.Unlock()
m.m[name] = value
}
func (m *Meta) Get(name string) interface{} {
m.Lock()
defer m.Unlock()
if v, found := m.m[name]; found == true {
return v
}
return ""
}