forked from bitly/go-simplejson
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjfile.go
177 lines (156 loc) · 4.43 KB
/
jfile.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package jpath
import (
"errors"
"os"
"sync"
)
var (
// ErrSpecificNode is for when retrieving a node does not return a specific key/value, but perhaps a map
ErrSpecificNode = errors.New("could not find a specific node that matched the given path")
)
// JFile represents a JSON file and contains the filename and root node
type JFile struct {
filename string
rootnode *Node
rw *sync.RWMutex
pretty bool // Indent JSON output prettily
}
// NewFile will read the given filename and return a JFile struct
func NewFile(filename string) (*JFile, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
js, err := New(data)
if err != nil {
return nil, err
}
rw := &sync.RWMutex{}
return &JFile{filename, js, rw, true}, nil
}
// GetFilename returns the current filename
func (jf *JFile) GetFilename() string {
return jf.filename
}
// SetPretty can be used for setting the "pretty" flag to true, for indenting
// all JSON output. Set to true by default.
func (jf *JFile) SetPretty(pretty bool) {
jf.pretty = pretty
}
// SetRW allows a different mutex to be used when writing the JSON documents to file
func (jf *JFile) SetRW(rw *sync.RWMutex) {
jf.rw = rw
}
// GetNode tries to find the JSON node that corresponds to the given JSON path
func (jf *JFile) GetNode(JSONpath string) (*Node, error) {
node, _, err := jf.rootnode.GetNodes(JSONpath)
if node == NilNode {
return NilNode, errors.New("nil node")
}
return node, err
}
// GetString tries to find the string that corresponds to the given JSON path
func (jf *JFile) GetString(JSONpath string) (string, error) {
node, err := jf.GetNode(JSONpath)
if err != nil {
return "", err
}
return node.String(), nil
}
// SetString will change the value of the key that the given JSON path points to
func (jf *JFile) SetString(JSONpath, value string) error {
_, parentNode, err := jf.rootnode.GetNodes(JSONpath)
if err != nil {
return err
}
m, ok := parentNode.CheckMap()
if !ok {
return errors.New("Parent is not a map: " + JSONpath)
}
// Set the string
m[lastpart(JSONpath)] = value
newdata, err := jf.rootnode.PrettyJSON()
if err != nil {
return err
}
return jf.Write(newdata)
}
// Write writes the current JSON data to the file
func (jf *JFile) Write(data []byte) error {
jf.rw.Lock()
defer jf.rw.Unlock()
return os.WriteFile(jf.filename, data, 0666)
}
// AddJSON adds JSON data at the given JSON path. If pretty is true, the JSON is indented.
func (jf *JFile) AddJSON(JSONpath string, JSONdata []byte) error {
if err := jf.rootnode.AddJSON(JSONpath, JSONdata); err != nil {
return err
}
// Use the correct JSON function, depending on the pretty parameter
JSON := jf.rootnode.JSON
if jf.pretty {
JSON = jf.rootnode.PrettyJSON
}
data, err := JSON()
if err != nil {
return err
}
return jf.Write(data)
}
// DelKey removes a key from the map that the JSON path leads to.
// Returns ErrKeyNotFound if the key is not found.
func (jf *JFile) DelKey(JSONpath string) error {
err := jf.rootnode.DelKey(JSONpath)
if err != nil {
return err
}
// Use the correct JSON function, depending on the pretty parameter
JSON := jf.rootnode.JSON
if jf.pretty {
JSON = jf.rootnode.PrettyJSON
}
data, err := JSON()
if err != nil {
return err
}
return jf.Write(data)
}
// JSON returns the current JSON data, as prettily formatted JSON
func (jf *JFile) JSON() ([]byte, error) {
return jf.rootnode.PrettyJSON()
}
// SetString sets a value to the given JSON file at the given JSON path
func SetString(filename, JSONpath, value string) error {
jf, err := NewFile(filename)
if err != nil {
return err
}
return jf.SetString(JSONpath, value)
}
// AddJSON adds JSON data to the given JSON file at the given JSON path
func AddJSON(filename, JSONpath string, JSONdata []byte, pretty bool) error {
jf, err := NewFile(filename)
if err != nil {
return err
}
jf.SetPretty(pretty)
return jf.AddJSON(JSONpath, JSONdata)
}
// GetString will find the string that corresponds to the given JSON path,
// given a filename and a simple JSON path expression.
func GetString(filename, JSONpath string) (string, error) {
jf, err := NewFile(filename)
if err != nil {
return "", err
}
return jf.GetString(JSONpath)
}
// DelKey removes a key from a map in a JSON file, given a JSON path,
// where the last element of the path is the key to be removed.
func DelKey(filename, JSONpath string) error {
jf, err := NewFile(filename)
if err != nil {
return err
}
return jf.DelKey(JSONpath)
}