diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-07-03 04:11:38 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-07-03 04:36:59 +0900 |
| commit | 087959e81bcd52104676ccacedd605e6491b4376 (patch) | |
| tree | 11cd6eccbfd9278f6c99d33191b320bb4e728888 /internal/app/state/join.go | |
| parent | e6967b8bbb5ceec3abbd002f52cff1167a969e9e (diff) | |
app: remove split implementation
It is completely nonsensical and highly error-prone to have multiple implementations of this in the same build. This should be switched at compile time instead therefore the split packages are pointless.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/app/state/join.go')
| -rw-r--r-- | internal/app/state/join.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/internal/app/state/join.go b/internal/app/state/join.go new file mode 100644 index 00000000..2b4011fe --- /dev/null +++ b/internal/app/state/join.go @@ -0,0 +1,60 @@ +package state + +import ( + "errors" + "maps" +) + +var ( + ErrDuplicate = errors.New("store contains duplicates") +) + +/* +Joiner is the interface that wraps the Join method. + +The Join function uses Joiner if available. +*/ +type Joiner interface{ Join() (Entries, error) } + +// Join returns joined state entries of all active aids. +func Join(s Store) (Entries, error) { + if j, ok := s.(Joiner); ok { + return j.Join() + } + + var ( + aids []int + entries = make(Entries) + + el int + res Entries + loadErr error + ) + + if ln, err := s.List(); err != nil { + return nil, err + } else { + aids = ln + } + + for _, aid := range aids { + if _, err := s.Do(aid, func(c Cursor) { + res, loadErr = c.Load() + }); err != nil { + return nil, err + } + + if loadErr != nil { + return nil, loadErr + } + + // save expected length + el = len(entries) + len(res) + maps.Copy(entries, res) + if len(entries) != el { + return nil, ErrDuplicate + } + } + + return entries, nil +} |
