/
api_keyvalue.go
108 lines (88 loc) · 2.55 KB
/
api_keyvalue.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
/*
* Copyright 2014-2015 Jason Woods.
*
* 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 admin
import (
"bytes"
"encoding/json"
"net/url"
"sort"
)
// APIKeyValue represents a set of data
type APIKeyValue struct {
entryMap map[string]APIEncodable
}
// Get always returns nil for an APIKeyValue as it is not navigatable
func (d *APIKeyValue) Get(string) (APINavigatable, error) {
return nil, nil
}
// Call an API
func (d *APIKeyValue) Call(params url.Values) (string, error) {
return "", ErrNotImplemented
}
// SetEntry sets a new data entry
func (d *APIKeyValue) SetEntry(key string, entry APIEncodable) {
if d.entryMap == nil {
d.entryMap = make(map[string]APIEncodable)
}
d.entryMap[key] = entry
}
// RemoveEntry removes a data entry
func (d *APIKeyValue) RemoveEntry(key string, entry APIEncodable) {
if _, ok := d.entryMap[key]; !ok {
return
}
delete(d.entryMap, key)
}
// MarshalJSON returns the APIKeyValue data in JSON form
func (d *APIKeyValue) MarshalJSON() ([]byte, error) {
return json.Marshal(d.entryMap)
}
// HumanReadable returns the APIKeyValue as a string
func (d *APIKeyValue) HumanReadable(indent string) ([]byte, error) {
if d.entryMap == nil || len(d.entryMap) == 0 {
return []byte("none"), nil
}
var result bytes.Buffer
newIndent := indent + APIIndentation
mapOrder := make([]string, 0, len(d.entryMap))
for key := range d.entryMap {
mapOrder = append(mapOrder, key)
}
sort.Strings(mapOrder)
for _, key := range mapOrder {
entry := d.entryMap[key]
subResult, err := entry.HumanReadable(newIndent)
if err != nil {
return nil, err
}
result.WriteString(indent)
result.WriteString(key)
if bytes.IndexRune(subResult, '\n') != -1 {
result.WriteString(":\n")
result.Write(subResult)
continue
}
result.WriteString(": ")
result.Write(subResult)
result.WriteString("\n")
}
return result.Bytes(), nil
}
// Update ensures the data we have is up to date - should be overriden by users
// if required to keep the contents up to date on each request
func (d *APIKeyValue) Update() error {
return nil
}