forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
149 lines (129 loc) · 3.83 KB
/
node.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cdata
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"sync"
"github.com/intelsdi-x/snap/core/ctypes"
"github.com/intelsdi-x/snap/pkg/ctree"
)
// Represents a set of configuration data
type ConfigDataNode struct {
mutex *sync.Mutex
table map[string]ctypes.ConfigValue
}
// GobEcode encodes a ConfigDataNode in go binary format
func (c *ConfigDataNode) GobEncode() ([]byte, error) {
w := new(bytes.Buffer)
encoder := gob.NewEncoder(w)
if err := encoder.Encode(&c.table); err != nil {
return nil, err
}
return w.Bytes(), nil
}
// GobDecode decodes a GOB into a ConfigDataNode
func (c *ConfigDataNode) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
c.mutex = new(sync.Mutex)
decoder := gob.NewDecoder(r)
return decoder.Decode(&c.table)
}
// MarshalJSON marshals a ConfigDataNode into JSON
func (c *ConfigDataNode) MarshalJSON() ([]byte, error) {
return json.Marshal(c.table)
}
// UnmarshalJSON unmarshals JSON into a ConfigDataNode
func (c *ConfigDataNode) UnmarshalJSON(data []byte) error {
t := map[string]interface{}{}
c.table = map[string]ctypes.ConfigValue{}
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
if err := dec.Decode(&t); err != nil {
return err
}
for k, i := range t {
switch t := i.(type) {
case string:
c.table[k] = ctypes.ConfigValueStr{Value: t}
case bool:
c.table[k] = ctypes.ConfigValueBool{Value: t}
case json.Number:
if v, err := t.Int64(); err == nil {
c.table[k] = ctypes.ConfigValueInt{Value: int(v)}
continue
}
if v, err := t.Float64(); err == nil {
c.table[k] = ctypes.ConfigValueFloat{Value: v}
continue
}
default:
return fmt.Errorf("Error Unmarshalling JSON ConfigDataNode. Key: %v Type: %v is unsupported.", k, t)
}
}
c.mutex = new(sync.Mutex)
return nil
}
// Returns a new and empty node.
func NewNode() *ConfigDataNode {
return &ConfigDataNode{
mutex: new(sync.Mutex),
table: make(map[string]ctypes.ConfigValue),
}
}
func FromTable(table map[string]ctypes.ConfigValue) *ConfigDataNode {
return &ConfigDataNode{
mutex: new(sync.Mutex),
table: table,
}
}
// Returns the table of configuration items [key(string) / value(core.ConfigValue)].
func (c *ConfigDataNode) Table() map[string]ctypes.ConfigValue {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.table
}
// Adds an item to the ConfigDataNode.
func (c *ConfigDataNode) AddItem(k string, v ctypes.ConfigValue) {
// And empty is a noop
if k == "" {
return
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.table[k] = v
}
// Merges a ConfigDataNode on top of this one (overwriting items where it occurs).
func (c ConfigDataNode) Merge(n ctree.Node) ctree.Node {
// Because Add only allows the ConfigDataNode type we
// are safe to convert ctree.Node interface to ConfigDataNode
cd := n.(*ConfigDataNode)
t := cd.Table()
// For the table in the passed ConfigDataNode(converted) add each item to
// this ConfigDataNode overwritting where needed.
for k, v := range t {
c.AddItem(k, v)
}
// Return modified version of ConfigDataNode(as ctree.Node)
return c
}
// Deletes a field in ConfigDataNode. If the field does not exist Delete is
// considered a no-op
func (c ConfigDataNode) DeleteItem(k string) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.table, k)
}