38 lines
978 B
Go
38 lines
978 B
Go
package opportunity
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
type Snapshot struct {
|
|
Version string `json:"version"`
|
|
GeneratedAt string `json:"generated_at"`
|
|
Occupations map[string]Evidence `json:"occupations"`
|
|
}
|
|
|
|
// Load reads the latest complete snapshot. A missing cache is an expected
|
|
// state: NextPath can still provide interest matches without inventing labor
|
|
// evidence.
|
|
func Load(path string) (Snapshot, error) {
|
|
if path == "" {
|
|
return Snapshot{}, nil
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if os.IsNotExist(err) {
|
|
return Snapshot{}, nil
|
|
}
|
|
if err != nil {
|
|
return Snapshot{}, fmt.Errorf("read opportunity cache: %w", err)
|
|
}
|
|
var snapshot Snapshot
|
|
if err := json.Unmarshal(data, &snapshot); err != nil {
|
|
return Snapshot{}, fmt.Errorf("decode opportunity cache: %w", err)
|
|
}
|
|
if snapshot.Version == "" || len(snapshot.Occupations) == 0 {
|
|
return Snapshot{}, fmt.Errorf("opportunity cache is empty")
|
|
}
|
|
return snapshot, nil
|
|
}
|