Improve model-by-ref handling
This commit is contained in:
@@ -116,13 +116,18 @@ func (a *issueHelper) mergeWith(b *issueHelper) {
|
||||
}
|
||||
|
||||
// Op check utils
|
||||
|
||||
func (svc *service) canOpData(connectionID, modelID uint64) (err error) {
|
||||
if svc.hasConnectionIssues(connectionID) {
|
||||
return errRecordOpProblematicConnection(connectionID)
|
||||
func (svc *service) canOpData(ref ModelRef) (err error) {
|
||||
if svc.hasConnectionIssues(ref.ConnectionID) {
|
||||
return errRecordOpProblematicConnection(ref.ConnectionID)
|
||||
}
|
||||
if svc.hasModelIssues(modelID) {
|
||||
return errRecordOpProblematicModel(modelID)
|
||||
|
||||
var mod = svc.FindModelByRef(ref)
|
||||
if mod == nil {
|
||||
return errModelNotFound(ref.ResourceID)
|
||||
}
|
||||
|
||||
if svc.hasModelIssues(mod.ResourceID) {
|
||||
return errRecordOpProblematicModel(mod.ResourceID)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -110,9 +110,15 @@ func (mm ModelSet) FindByResourceID(resourceID uint64) *Model {
|
||||
|
||||
func (mm ModelSet) FindByResourceIdent(resourceType, resourceIdent string) *Model {
|
||||
for _, m := range mm {
|
||||
if m.ResourceType == resourceType && m.Resource == resourceIdent {
|
||||
return m
|
||||
if m.ResourceType != resourceType {
|
||||
continue
|
||||
}
|
||||
|
||||
if m.Resource != resourceIdent {
|
||||
continue
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -383,7 +383,7 @@ func (svc *service) RemoveConnection(ctx context.Context, ID uint64) (err error)
|
||||
|
||||
// Create stores new data (create data entry)
|
||||
func (svc *service) Create(ctx context.Context, mf ModelRef, operations OperationSet, rr ...ValueGetter) (err error) {
|
||||
if err = svc.canOpData(mf.ConnectionID, mf.ResourceID); err != nil {
|
||||
if err = svc.canOpData(mf); err != nil {
|
||||
return fmt.Errorf("cannot create data entry: %w", err)
|
||||
}
|
||||
|
||||
@@ -396,7 +396,7 @@ func (svc *service) Create(ctx context.Context, mf ModelRef, operations Operatio
|
||||
}
|
||||
|
||||
func (svc *service) Update(ctx context.Context, mf ModelRef, operations OperationSet, rr ...ValueGetter) (err error) {
|
||||
if err = svc.canOpData(mf.ConnectionID, mf.ResourceID); err != nil {
|
||||
if err = svc.canOpData(mf); err != nil {
|
||||
return fmt.Errorf("cannot update data entry: %w", err)
|
||||
}
|
||||
|
||||
@@ -415,7 +415,7 @@ func (svc *service) Update(ctx context.Context, mf ModelRef, operations Operatio
|
||||
}
|
||||
|
||||
func (svc *service) Search(ctx context.Context, mf ModelRef, operations OperationSet, f filter.Filter) (iter Iterator, err error) {
|
||||
if err = svc.canOpData(mf.ConnectionID, mf.ResourceID); err != nil {
|
||||
if err = svc.canOpData(mf); err != nil {
|
||||
err = fmt.Errorf("cannot search data entry: %w", err)
|
||||
return
|
||||
}
|
||||
@@ -430,7 +430,7 @@ func (svc *service) Search(ctx context.Context, mf ModelRef, operations Operatio
|
||||
}
|
||||
|
||||
func (svc *service) Lookup(ctx context.Context, mf ModelRef, operations OperationSet, lookup ValueGetter, dst ValueSetter) (err error) {
|
||||
if err = svc.canOpData(mf.ConnectionID, mf.ResourceID); err != nil {
|
||||
if err = svc.canOpData(mf); err != nil {
|
||||
return fmt.Errorf("cannot lookup data entry: %w", err)
|
||||
}
|
||||
|
||||
@@ -442,7 +442,7 @@ func (svc *service) Lookup(ctx context.Context, mf ModelRef, operations Operatio
|
||||
}
|
||||
|
||||
func (svc *service) Delete(ctx context.Context, mf ModelRef, operations OperationSet, vv ...ValueGetter) (err error) {
|
||||
if err = svc.canOpData(mf.ConnectionID, mf.ResourceID); err != nil {
|
||||
if err = svc.canOpData(mf); err != nil {
|
||||
return fmt.Errorf("cannot delete data entry: %w", err)
|
||||
}
|
||||
|
||||
@@ -460,7 +460,7 @@ func (svc *service) Delete(ctx context.Context, mf ModelRef, operations Operatio
|
||||
}
|
||||
|
||||
func (svc *service) Truncate(ctx context.Context, mf ModelRef, operations OperationSet) (err error) {
|
||||
if err = svc.canOpData(mf.ConnectionID, mf.ResourceID); err != nil {
|
||||
if err = svc.canOpData(mf); err != nil {
|
||||
return fmt.Errorf("cannot truncate data entry: %w", err)
|
||||
}
|
||||
|
||||
@@ -820,6 +820,19 @@ func (svc *service) FindModelByResourceIdent(connectionID uint64, resourceType,
|
||||
return svc.models[connectionID].FindByResourceIdent(resourceType, resourceIdent)
|
||||
}
|
||||
|
||||
func (svc *service) FindModelByRef(ref ModelRef) *Model {
|
||||
connectionID := ref.ConnectionID
|
||||
if connectionID == 0 {
|
||||
connectionID = svc.defConnID
|
||||
}
|
||||
|
||||
if ref.ResourceID > 0 {
|
||||
return svc.models[connectionID].FindByResourceID(ref.ResourceID)
|
||||
}
|
||||
|
||||
return svc.models[connectionID].FindByResourceIdent(ref.ResourceType, ref.Resource)
|
||||
}
|
||||
|
||||
func (svc *service) FindModelByIdent(connectionID uint64, ident string) *Model {
|
||||
if connectionID == 0 {
|
||||
connectionID = svc.defConnID
|
||||
|
||||
Reference in New Issue
Block a user