Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
)
|
|
|
|
func (m *Memory) GetRoleDataset(_ context.Context, tenantID string) (*RoleDataset, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
revs := m.roleSets[tenantID]
|
|
if len(revs) == 0 {
|
|
return nil, ErrNotFound
|
|
}
|
|
cp := revs[len(revs)-1]
|
|
return &cp, nil
|
|
}
|
|
|
|
func (m *Memory) SaveRoleDataset(_ context.Context, w RoleDatasetWrite) (*RoleDataset, bool, error) {
|
|
if !json.Valid(w.Data) || w.CatalogueVersion == "" || w.By == "" {
|
|
return nil, false, ErrInvalidInput
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.tenants[w.TenantID]; !ok {
|
|
return nil, false, ErrNotFound
|
|
}
|
|
revs := m.roleSets[w.TenantID]
|
|
current := len(revs)
|
|
if w.ExpectedRevision != current {
|
|
return nil, false, &RevisionConflict{Current: current}
|
|
}
|
|
if current > 0 {
|
|
cur := revs[current-1]
|
|
if cur.CatalogueVersion == w.CatalogueVersion && sameJSON(cur.Data, w.Data) {
|
|
return &cur, false, nil
|
|
}
|
|
}
|
|
next := RoleDataset{
|
|
TenantID: w.TenantID, Data: append(json.RawMessage(nil), w.Data...), CatalogueVersion: w.CatalogueVersion,
|
|
Revision: current + 1, UpdatedBy: w.By, UpdatedAt: w.At.UTC(),
|
|
}
|
|
m.roleSets[w.TenantID] = append(revs, next)
|
|
cp := next
|
|
return &cp, true, nil
|
|
}
|
|
|
|
func (m *Memory) GetRoleDatasetRevision(_ context.Context, tenantID string, revision int) (*RoleDataset, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
revs := m.roleSets[tenantID]
|
|
if revision < 1 || revision > len(revs) {
|
|
return nil, ErrNotFound
|
|
}
|
|
cp := revs[revision-1]
|
|
return &cp, nil
|
|
}
|