diff options
Diffstat (limited to 'internal/item/store.go')
| -rw-r--r-- | internal/item/store.go | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/internal/item/store.go b/internal/item/store.go index e50e6e9..12aa729 100644 --- a/internal/item/store.go +++ b/internal/item/store.go @@ -2,6 +2,7 @@ package item import ( "fmt" + "log" "os" "path/filepath" "sync" @@ -44,6 +45,7 @@ func (s *ItemStore) Load(id string) (*ItemDef, error) { if err := yaml.Unmarshal(data, &def); err != nil { return nil, fmt.Errorf("parse item %s: %w", id, err) } + warnDeclaredIDMismatch("item", path, id, def.ID) def.ID = id s.cache[id] = &def return &def, nil @@ -63,6 +65,7 @@ func (s *ItemStore) LoadAll() ([]*ItemDef, error) { if err := yaml.Unmarshal(data, &def); err != nil { return fmt.Errorf("parse item %s: %w", id, err) } + warnDeclaredIDMismatch("item", path, id, def.ID) def.ID = id s.cache[id] = &def defs = append(defs, &def) @@ -71,6 +74,17 @@ func (s *ItemStore) LoadAll() ([]*ItemDef, error) { return defs, err } +// warnDeclaredIDMismatch logs when a data file declares an id: that disagrees +// with its filename. IDs come from the filename (def.ID = id above), so the +// declared value is silently ignored at runtime; without this warning an author +// sees no sign that the id they wrote is dead. +func warnDeclaredIDMismatch(kind, path, filenameID, declaredID string) { + if declaredID != "" && declaredID != filenameID { + log.Printf("%s %s: file declares id %q but is loaded as %q; the filename is authoritative and the declared id is ignored", + kind, path, declaredID, filenameID) + } +} + // PathIndex returns a copy of the id→path index, safe to iterate concurrently. func (s *ItemStore) PathIndex() map[string]string { s.mu.Lock() |
