Skip to content

Conversation

@zamderax
Copy link
Contributor

@zamderax zamderax commented Oct 15, 2025

Summary

Add async/await overloads for MemoryAdapter, FileAdapter, and Enforcer while keeping the original EventLoopFuture-based APIs intact. Methods are split into +AsyncAwait.swift extensions and availability-gated to match Package.swift platforms.

Changes

MemoryAdapter

  • Moved async/await overloads to Sources/Casbin/Adapter/MemoryAdapter+AsyncAwait.swift.
  • Fixed removePolicies to be atomic (all-or-nothing). Mirrored in DefaultModel.removePolicies.
  • Corrected savePolicy to use explicit section headers ("p"/"g").
  • loadPolicy/loadFilteredPolicy now append only rule fields (no ptype) and fixed filter indexing.

FileAdapter

  • Added async/await overloads in Sources/Casbin/Adapter/FileAdapter+AsyncAwait.swift that bridge existing futures via continuations.
  • Availability-gated with @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *).

Enforcer

  • Added async/await overloads in Sources/Casbin/Enforcer+AsyncAwait.swift for common operations:
    • loadPolicy, loadFilteredPolicy, savePolicy, clearPolicy, setModel, setAdapter
    • enforce(_:)
    • policy and grouping management (addPolicy, addPolicies, removePolicy, removePolicies, removeFilteredPolicy, and grouping counterparts)
  • Overloads bridge to existing APIs using continuations and maintain behavior.

Tests

  • Re-enabled async removePolicies test and updated expectations (model policies don’t include ptype).
  • swift test passes locally (XCTest + Swift Testing).

Rationale

  • Split pattern improves readability and isolates concurrency concerns.
  • @available maintains backward compatibility with the current platforms defined in Package.swift.

zamderax and others added 8 commits October 15, 2025 23:48
- Update Package.swift with minimum platform requirements (macOS 10.15, iOS 13, etc.)
- Add async/await overloads for all 9 MemoryAdapter methods:
  - loadPolicy(m:)
  - loadFilteredPolicy(m:f:)
  - savePolicy(m:)
  - clearPolicy()
  - addPolicy(sec:ptype:rule:)
  - addPolicies(sec:ptype:rules:)
  - removePolicy(sec:ptype:rule:)
  - removePolicies(sec:ptype:rules:)
  - removeFilteredPolicy(sec:ptype:fieldIndex:fieldValues:)

Original EventLoopFuture-based methods remain for backward compatibility.
Async/await versions provide cleaner, more maintainable concurrency code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Add 7 test cases covering all async/await methods:
- ✅ async addPolicy and loadPolicy
- ✅ async addPolicies and savePolicy
- ✅ async removePolicy
- ⏭️  async removePolicies (skipped - pre-existing bug in implementation)
- ✅ async clearPolicy
- ✅ async removeFilteredPolicy
- ✅ async loadFilteredPolicy

Note: removePolicies test disabled due to pre-existing bug where logic
checks `if policy.contains(rule)` instead of `if !policy.contains(rule)`.
This bug exists in both EventLoopFuture and async/await implementations.

6/7 tests pass, verifying async/await methods work identically to
EventLoopFuture versions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
…emoryAdapter load/save with model

- Make removePolicies atomic: return true only if all rules exist; otherwise do not remove any
- Fix inverted existence check and remove early return
- Mirror semantics in DefaultModel.removePolicies
- MemoryAdapter.savePolicy: write explicit section ('p'/'g') instead of iterating ptype characters
- MemoryAdapter.loadPolicy/loadFilteredPolicy: drop [sec, ptype] headers; append only rule fields
- Fix loadFilteredPolicy indexing to match dropped headers
- Tests: re-enable async removePolicies test; update expectations to no-ptype rules
@hsluoyz hsluoyz merged commit 099f9c5 into casbin:master Oct 16, 2025
4 checks passed
@github-actions
Copy link

🎉 This PR is included in version 1.15.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

zamderax added a commit to wendylabsinc/SwiftCasbin that referenced this pull request Oct 16, 2025
- Add Config+AsyncAwait.swift with async/await overloads for static factory methods
- Add DefaultModel+AsyncAwait.swift with async/await overloads for static factory methods
- Both follow the same pattern as PR casbin#53 with @available annotations and continuation-based bridging
- Methods bridge existing EventLoopFuture-based APIs to async/await

Note: Tests have a pre-existing hang issue on this branch unrelated to these changes.
The code compiles successfully and follows the established async/await patterns.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants