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
|
package object
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
type ItemStore struct {
dataDir string
cache map[string]*ItemDef
}
func NewItemStore(dataDir string) *ItemStore {
return &ItemStore{
dataDir: dataDir,
cache: make(map[string]*ItemDef),
}
}
func (s *ItemStore) Load(id string) (*ItemDef, error) {
if def, ok := s.cache[id]; ok {
return def, nil
}
path := filepath.Join(s.dataDir, "items", id+".yaml")
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read item %s: %w", id, err)
}
var def ItemDef
if err := yaml.Unmarshal(data, &def); err != nil {
return nil, fmt.Errorf("parse item %s: %w", id, err)
}
s.cache[id] = &def
return &def, nil
}
|