66 "os"
77)
88
9+ const currentCacheVersion = 2
10+
911type synonymCacheState struct {
1012 Version int `json:"cache_version"`
1113 Entries SynonymResponse `json:"entries"`
@@ -18,7 +20,7 @@ func LoadSynonymCache(path string) (SynonymResponse, error) {
1820}
1921
2022func loadSynonymCacheState (path string ) (synonymCacheState , error ) {
21- state := synonymCacheState {Entries : make (SynonymResponse ), Fingerprints : make (map [string ]string )}
23+ state := synonymCacheState {Version : currentCacheVersion , Entries : make (SynonymResponse ), Fingerprints : make (map [string ]string )}
2224 if path == "" {
2325 return state , nil
2426 }
@@ -29,20 +31,16 @@ func loadSynonymCacheState(path string) (synonymCacheState, error) {
2931 if err != nil {
3032 return state , fmt .Errorf ("read synonym cache: %w" , err )
3133 }
32- var fields map [string ]json.RawMessage
33- if err := json .Unmarshal (data , & fields ); err != nil {
34- return state , fmt .Errorf ("parse synonym cache: %w" , err )
34+ // Decode into a fresh value so a missing version is rejected.
35+ var decoded synonymCacheState
36+ if err := json .Unmarshal (data , & decoded ); err != nil {
37+ return state , fmt .Errorf ("parse synonym cache: %w; remove the cache file and run ctxt init" , err )
3538 }
36- if version , ok := fields ["cache_version" ]; ok && len (version ) > 0 && version [0 ] != '[' {
37- if err := json .Unmarshal (data , & state ); err != nil {
38- return state , fmt .Errorf ("parse synonym cache: %w" , err )
39- }
40- if state .Version != 2 {
41- return state , fmt .Errorf ("unsupported synonym cache version %d" , state .Version )
42- }
43- } else if err := json .Unmarshal (data , & state .Entries ); err != nil {
44- return state , fmt .Errorf ("parse synonym cache: %w" , err )
39+ if decoded .Version != currentCacheVersion {
40+ return state , fmt .Errorf ("unsupported synonym cache version %d; remove the cache file and run ctxt init" , decoded .Version )
4541 }
42+ state = decoded
43+
4644 if state .Entries == nil {
4745 state .Entries = make (SynonymResponse )
4846 }
@@ -59,7 +57,7 @@ func saveSynonymCacheState(path string, state synonymCacheState) error {
5957 if path == "" {
6058 return nil
6159 }
62- state .Version = 2
60+ state .Version = currentCacheVersion
6361 data , err := json .MarshalIndent (state , "" , " " )
6462 if err != nil {
6563 return fmt .Errorf ("marshal synonym cache: %w" , err )
@@ -68,18 +66,8 @@ func saveSynonymCacheState(path string, state synonymCacheState) error {
6866}
6967
7068func SaveSynonymCache (path string , cache SynonymResponse ) error {
71- if path == "" {
72- return nil
73- }
7469 if cache == nil {
7570 cache = make (SynonymResponse )
7671 }
77- bytes , err := json .MarshalIndent (cache , "" , " " )
78- if err != nil {
79- return fmt .Errorf ("marshal synonym cache: %w" , err )
80- }
81- if err := writeFileAtomic (path , append (bytes , '\n' ), 0o600 ); err != nil {
82- return fmt .Errorf ("write synonym cache: %w" , err )
83- }
84- return nil
72+ return saveSynonymCacheState (path , synonymCacheState {Entries : cache })
8573}
0 commit comments