Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
73 lines (65 loc) · 2.01 KB
/
Copy pathcache.go
File metadata and controls
73 lines (65 loc) · 2.01 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
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
package contexting
import (
"encoding/json"
"fmt"
"os"
)
const currentCacheVersion = 2
type synonymCacheState struct {
Version int `json:"cache_version"`
Entries SynonymResponse `json:"entries"`
Fingerprints map[string]string `json:"fingerprints,omitempty"`
}
func LoadSynonymCache(path string) (SynonymResponse, error) {
state, err := loadSynonymCacheState(path)
return state.Entries, err
}
func loadSynonymCacheState(path string) (synonymCacheState, error) {
state := synonymCacheState{Version: currentCacheVersion, Entries: make(SynonymResponse), Fingerprints: make(map[string]string)}
if path == "" {
return state, nil
}
data, err := os.ReadFile(path)
if os.IsNotExist(err) {
return state, nil
}
if err != nil {
return state, fmt.Errorf("read synonym cache: %w", err)
}
// Decode into a fresh value so a missing version is rejected.
var decoded synonymCacheState
if err := json.Unmarshal(data, &decoded); err != nil {
return state, fmt.Errorf("parse synonym cache: %w; remove the cache file and run ctxt init", err)
}
if decoded.Version != currentCacheVersion {
return state, fmt.Errorf("unsupported synonym cache version %d; remove the cache file and run ctxt init", decoded.Version)
}
state = decoded
if state.Entries == nil {
state.Entries = make(SynonymResponse)
}
if state.Fingerprints == nil {
state.Fingerprints = make(map[string]string)
}
for name, syns := range state.Entries {
state.Entries[name] = sanitizeSynonyms(syns, 16)
}
return state, nil
}
func saveSynonymCacheState(path string, state synonymCacheState) error {
if path == "" {
return nil
}
state.Version = currentCacheVersion
data, err := json.MarshalIndent(state, "", " ")
if err != nil {
return fmt.Errorf("marshal synonym cache: %w", err)
}
return writeFileAtomic(path, append(data, '\n'), 0o600)
}
func SaveSynonymCache(path string, cache SynonymResponse) error {
if cache == nil {
cache = make(SynonymResponse)
}
return saveSynonymCacheState(path, synonymCacheState{Entries: cache})
}