-
Notifications
You must be signed in to change notification settings - Fork 17
/
item.go
111 lines (94 loc) · 2.58 KB
/
item.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
// Kodex (Community Edition - CE) - Privacy & Security Engineering Platform
// Copyright (C) 2019-2024 KIProtect GmbH (HRB 208395B) - Germany
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package kodex
import (
"encoding/json"
"fmt"
)
type Item struct {
d map[string]interface{}
}
func MakeItem(d map[string]interface{}) *Item {
item := new(Item)
item.d = d
return item
}
func (f *Item) Keys() []string {
keys := make([]string, 0)
for key := range f.d {
keys = append(keys, key)
}
return keys
}
func (f *Item) Values() []interface{} {
values := make([]interface{}, 0)
for _, value := range f.d {
values = append(values, value)
}
return values
}
func (f *Item) Delete(key string) {
delete(f.d, key)
}
func (f *Item) Get(key string) (interface{}, bool) {
v, ok := f.d[key]
return v, ok
}
func (f *Item) All() map[string]interface{} {
return f.d
}
func (f *Item) Set(key string, value interface{}) {
f.d[key] = value
}
func (f *Item) Serialize(format string) ([]byte, error) {
switch format {
case "json":
return f.SerializeJSON()
default:
return nil, fmt.Errorf("Unknown format: %s", format)
}
}
func (f *Item) SerializeJSON() ([]byte, error) {
return json.Marshal(f.d)
}
func (f *Item) MarshalJSON() ([]byte, error) {
return f.SerializeJSON()
}
// Validates the format of a list of items
type IsItem struct{}
func (i IsItem) Validate(value interface{}, values map[string]interface{}) (interface{}, error) {
strMap, ok := value.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected a string map")
}
return MakeItem(strMap), nil
}
type IsItems struct{}
func (i IsItems) Validate(value interface{}, values map[string]interface{}) (interface{}, error) {
list, ok := value.([]interface{})
if !ok {
return nil, fmt.Errorf("expected a list")
}
itemList := make([]*Item, len(list))
for i, listValue := range list {
item, ok := listValue.(*Item)
if !ok {
return nil, fmt.Errorf("not an item")
}
itemList[i] = item
}
return itemList, nil
}