-
Notifications
You must be signed in to change notification settings - Fork 14
/
json_file.go
141 lines (113 loc) · 3.3 KB
/
json_file.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
package kv
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"sync"
"github.com/brexhq/substation/config"
_config "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
"github.com/brexhq/substation/internal/file"
"github.com/tidwall/gjson"
)
// errJSONFileInvalid is returned when the file contains invalid JSON.
var errJSONFileInvalid = fmt.Errorf("invalid JSON")
// kvJSONFile is a read-only key-value store that is derived from a file containing
// an object and stored in memory.
type kvJSONFile struct {
// File contains the location of the text file. This can be either a path on local
// disk, an HTTP(S) URL, or an AWS S3 URL.
File string `json:"file"`
// IsLines indicates that the file is a JSON Lines file. The first non-null value
// is returned when a key is found.
IsLines bool `json:"is_lines"`
mu *sync.Mutex
object []byte
}
// Create a new JSON file KV store.
func newKVJSONFile(cfg config.Config) (*kvJSONFile, error) {
var store kvJSONFile
if err := _config.Decode(cfg.Settings, &store); err != nil {
return nil, err
}
store.mu = new(sync.Mutex)
if store.File == "" {
return nil, fmt.Errorf("kv: json: options %+v: %v", &store, errors.ErrMissingRequiredOption)
}
return &store, nil
}
func (store *kvJSONFile) String() string {
return toString(store)
}
// Get retrieves a value from the store.
func (store *kvJSONFile) Get(ctx context.Context, key string) (interface{}, error) {
store.mu.Lock()
defer store.mu.Unlock()
// JSON Lines files are queried as an array and the first non-null value is returned.
// See https://github.com/tidwall/gjson#json-lines for more information.
if store.IsLines && !strings.HasPrefix(key, "..#.") {
key = "..#." + key
res := gjson.GetBytes(store.object, key)
for _, v := range res.Array() {
if v.Exists() {
return v.Value(), nil
}
}
return nil, nil
}
res := gjson.GetBytes(store.object, key)
if !res.Exists() {
return nil, nil
}
return res.Value(), nil
}
// Set is unused because this is a read-only store.
func (store *kvJSONFile) Set(ctx context.Context, key string, val interface{}) error {
return errSetNotSupported
}
// SetWithTTL is unused because this is a read-only store.
func (store *kvJSONFile) SetWithTTL(ctx context.Context, key string, val interface{}, ttl int64) error {
return errSetNotSupported
}
// IsEnabled returns true if the store is ready for use.
func (store *kvJSONFile) IsEnabled() bool {
store.mu.Lock()
defer store.mu.Unlock()
return store.object != nil
}
// Setup creates the store by reading the text file into memory.
func (store *kvJSONFile) Setup(ctx context.Context) error {
store.mu.Lock()
defer store.mu.Unlock()
// avoids unnecessary setup
if store.object != nil {
return nil
}
path, err := file.Get(ctx, store.File)
defer os.Remove(path)
if err != nil {
return fmt.Errorf("kv: json_file: %v", err)
}
buf, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("kv: json_file: %v", err)
}
if !json.Valid(buf) {
return fmt.Errorf("kv: json_file: %v", errJSONFileInvalid)
}
store.object = buf
return nil
}
// Closes the store.
func (store *kvJSONFile) Close() error {
store.mu.Lock()
defer store.mu.Unlock()
// avoids unnecessary closing
if store.object == nil {
return nil
}
store.object = nil
return nil
}