-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.go
More file actions
77 lines (65 loc) · 1.32 KB
/
Copy pathdata.go
File metadata and controls
77 lines (65 loc) · 1.32 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// data.go
//
// Created by Frederic DELBOS - fred@hyperboloide.com on Feb 8 2015.
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
//
package sprocess
import (
"encoding/json"
"errors"
"sync"
)
type Data struct {
sync.RWMutex
data map[string]interface{}
}
func NewData() *Data {
return &Data{
data: make(map[string]interface{}),
}
}
func NewDataFrom(o map[string]interface{}) *Data {
return &Data{
data: o,
}
}
func (d *Data) Get(key string) (interface{}, error) {
d.RLock()
defer d.RUnlock()
v, exists := d.data[key]
if exists == false {
return nil, errors.New("Data '" + key + "' not found")
}
return v, nil
}
func (d *Data) Export() map[string]interface{} {
d.RLock()
defer d.RUnlock()
copy := make(map[string]interface{})
for k, v := range d.data {
copy[k] = v
}
return copy
}
func (d *Data) Set(key string, value interface{}) {
d.Lock()
defer d.Unlock()
d.data[key] = value
}
func (d *Data) Filter() ([]byte, error) {
d.RLock()
defer d.RUnlock()
copy := map[string]interface{}{
"size": d.data["size"],
"identifier": d.data["identifier"],
"filename": d.data["filename"],
}
return json.Marshal(copy)
}
func (d *Data) ToJson() ([]byte, error) {
d.RLock()
defer d.RUnlock()
return json.Marshal(d.data)
}