Skip to content

CaptureContext/swift-sharing-extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swift-sharing-extensions [beta]

SwiftPM 6.2 Platforms @capture_context

Extensions for swift-sharing package

Products

  • SharingKeys

    Provides SharedReaderKey and Shared initializers structural key declarations

  • SharingKeysCore

    Provides helpers for structural key declarations

Usage

SharingKeysCore

Declaration

Extend AppStorageKeys or InMemoryStorageKeys and specify available domains.

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

extension StorageKeyBuilder<InMemoryStorageKeys> {
	var firstDomain: Subdomain<InMemoryStorageKeys.FirstDomain> { subdomain() }
	var otherDomain: Subdomain<InMemoryStorageKeys.OtherDomain> { 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<InMemoryStorageKeys.FirstDomain> {
	var childDomain: Subdomain<Domain.ChildDomain> { subdomain() }
  
  /// Final key will be "first_domain-some_entry"
	var someEntry: Entry { entry() }
}

extension StorageKeyBuilder<InMemoryStorageKeys.FirstDomain.ChildDomain> {
  /// Final key will be "first_domain-child_domain-some_entry"
  /// Strict entries are type-checked at call site
  var someProtectedEntry: Entry.Strict<Int> { entry() }
}

extension StorageKeyBuilder<InMemoryStorageKeys.OtherDomain> {
  /// 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<Domain.ChildDomain1> { 
    // 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<InMemoryStorageKeys.OtherDomain.ChildDomain1> {
  /// 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 for formatting

Access your shared state in one of two ways

import Sharing
import SharingKeys

@Shared(\.inMemory.keyPath.toEntry)
var value: Int = 0
@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" 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.

.package(
  url: "[email protected]:capturecontext/swift-sharing-extensions.git", 
  .upToNextMinor(from: "0.0.1")
)

or via HTTPS

.package(
  url: "https://github.com:capturecontext/swift-sharing-extensions.git", 
  .upToNextMinor(from: "0.0.1")
)

Do not forget about target dependencies:

.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

.product(
  name: "SharingKeysCore", 
  package: "swift-sharing-extensions"
)

License

This library is released under the MIT license. See LICENSE for details.