https://github.com/capturecontext/swift-sharing-extensions
Extensions for swift-sharing package
https://github.com/capturecontext/swift-sharing-extensions
shared-state structural-keys swift swift-sharing user-defaults user-defaults-manager userdefaults
Last synced: 6 months ago
JSON representation
Extensions for swift-sharing package
- Host: GitHub
- URL: https://github.com/capturecontext/swift-sharing-extensions
- Owner: CaptureContext
- License: mit
- Created: 2025-07-28T12:52:52.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-07-28T12:57:44.000Z (7 months ago)
- Last Synced: 2025-08-10T11:51:13.010Z (6 months ago)
- Topics: shared-state, structural-keys, swift, swift-sharing, user-defaults, user-defaults-manager, userdefaults
- Language: Swift
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swift-sharing-extensions [beta]
[](https://swift.org/download/)  [](https://twitter.com/capture_context)
Extensions for [`swift-sharing`](https://github.com/pointfreeco/swift-sharing) package
## Products
- **[SharingKeys](./Sources/SharingKeys)**
Provides `SharedReaderKey` and `Shared` initializers structural key declarations
- **[SharingKeysCore](./Sources/SharingKeysCore)**
Provides helpers for structural key declarations
## Usage
### SharingKeysCore
##### Declaration
Extend `AppStorageKeys` or `InMemoryStorageKeys` and specify available domains.
```swift
import SharingKeysCore
extension InMemoryStorageKeys {
enum FirstDomain: Sendable {
enum ChildDomain: Sendable {}
}
enum OtherDomain: Sendable {
enum ChildDomain1: Sendable {}
enum ChildDomain2: Sendable {}
}
}
```
Specify accessors by extending `StorageKeyBuilder` type
```swift
extension StorageKeyBuilder {
var firstDomain: Subdomain { subdomain() }
var otherDomain: Subdomain { subdomain() }
/// Final key will be "first_domain-some_entry"
/// And will have the same value as `InMemoryStorageKeys[\.firstDomain.someEntry]`
var rawEntry: Entry { "first_domain-some_entry" }
}
extension StorageKeyBuilder {
var childDomain: Subdomain { subdomain() }
/// Final key will be "first_domain-some_entry"
var someEntry: Entry { entry() }
}
extension StorageKeyBuilder {
/// Final key will be "first_domain-child_domain-some_entry"
/// Strict entries are type-checked at call site
var someProtectedEntry: Entry.Strict { entry() }
}
extension StorageKeyBuilder {
/// Final key will be "stringLiteral key"
var customEntry: Entry { "stringLiteral key" }
/// Final key will be "other_domain###FORMATTEDENTRY"
var formattedEntry: Entry { entry(format: .upper, separator: "###") }
/// Final key will be "other_domain###KEY"
var formattedEntry2: Entry { entry("key", format: .upper, separator: "###") }
var randomPropertyName: Subdomain {
// Subdomains can be modified just as entries, however subdomains are not
// ExpressibleByStringLiteral and must be created with a `subdomain` method
subdomain("weCould fix The name_here", format: .camel, separator: "---")
}
}
extension StorageKeyBuilder {
/// Final key will be "other_domain---weCouldFixTheNameHere-value"
var value: Entry { entry() }
}
// ...
```
> [!NOTE]
> As you might have noticed
>
> - Default formatting for keys is snake_casing components and joining them with `"-"`
> - String literal keys bypass the formatting
> - Custom format and leading separator can be specified for each component
>
> This package uses [`swift-casification`](https://github.com/capturecontext/swift-casification) for formatting
Access your shared state in one of two ways
```swift
import Sharing
import SharingKeys
@Shared(\.inMemory.keyPath.toEntry)
var value: Int = 0
```
```swift
@Shared(.inMemory(\.keyPath.toEntry))
var value: Int = 0
```
## Installation
### Basic
You can add swift-sharing-extensions to an Xcode project by adding it as a package dependency.
1. From the **File** menu, select **Swift Packages › Add Package Dependency…**
2. Enter [`"https://github.com/capturecontext/swift-sharing-extensions"`](https://github.com/capturecontext/swift-sharing-extensions) into the package repository URL text field
3. Choose products you need to link them to your project.
### Recommended
If you use SwiftPM for your project structure, add DeclarativeConfiguration to your package file.
```swift
.package(
url: "git@github.com:capturecontext/swift-sharing-extensions.git",
.upToNextMinor(from: "0.0.1")
)
```
or via HTTPS
```swift
.package(
url: "https://github.com:capturecontext/swift-sharing-extensions.git",
.upToNextMinor(from: "0.0.1")
)
```
Do not forget about target dependencies:
```swift
.product(
name: "SharingKeys",
package: "swift-sharing-extensions"
)
```
or for the targets that don't need to read/write values to Shared storage but only to declare storage keys
```swift
.product(
name: "SharingKeysCore",
package: "swift-sharing-extensions"
)
```
## License
This library is released under the MIT license. See [LICENSE](./LICENSE) for details.