-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
47 lines (42 loc) · 1 KB
/
Copy pathcache.go
File metadata and controls
47 lines (42 loc) · 1 KB
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
package contexting
import (
"encoding/json"
"fmt"
"os"
)
func LoadSynonymCache(path string) (SynonymResponse, error) {
if path == "" {
return make(SynonymResponse), nil
}
bytes, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return make(SynonymResponse), nil
}
return nil, fmt.Errorf("read synonym cache: %w", err)
}
cache := make(SynonymResponse)
if err := json.Unmarshal(bytes, &cache); err != nil {
return nil, fmt.Errorf("parse synonym cache: %w", err)
}
for name, syns := range cache {
cache[name] = sanitizeSynonyms(syns, 16)
}
return cache, nil
}
func SaveSynonymCache(path string, cache SynonymResponse) error {
if path == "" {
return nil
}
if cache == nil {
cache = make(SynonymResponse)
}
bytes, err := json.MarshalIndent(cache, "", " ")
if err != nil {
return fmt.Errorf("marshal synonym cache: %w", err)
}
if err := writeFileAtomic(path, append(bytes, '\n'), 0o644); err != nil {
return fmt.Errorf("write synonym cache: %w", err)
}
return nil
}