Swiftpack.co - 0xLeif/OnionStash as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
0xLeif/OnionStash
Store Onions 🧅
.package(url: "https://github.com/0xLeif/OnionStash.git", from: "0.2.0")

OnionStash

Layered Data

Onion Layers

  • ID
  • Optional: Type
  • Optional: Meta
  • Value

Layerable

public protocol Layerable {
  func idLayer() -> String
  func typeLayer() -> String?
  func metaLayer() -> [String: String]?
  func valueLayer() -> Data
}

GitHub

link
Stars: 1
Last commit: 3 hours ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

OnionBank 🧅 🏦
3 hours ago

Added

  • OnionBank 🧅 🏦
  • Logging
  • OnionStashable protocol
  • Fun typealiases
  • OnionBank Tests

Examples

Layerable Data
struct User: Layerable, Codable, Equatable {
  var id = UUID().uuidString
  
  var name: String
  var isRegistered: Bool
  
  func idLayer() -> String {
    id
  }
  
  func typeLayer() -> String? {
    "\(User.self).\(isRegistered ? "Registered" : "Anon")"
  }
  
  func metaLayer() -> [String : String]? {
    ["tag": (name == "Anon") ? "0" : "1"]
  }
  
  func valueLayer() -> Data {
    name.data(using: .utf8)!
  }
}
Onion Stash
var stash = OnionStash<User>()

let someUser = User(name: "Leif", isRegistered: true)

stash.add(value: someUser)
stash.add(value: someUser)
stash.add(value: someUser)

XCTAssertEqual(stash.onions.count, 1)

XCTAssertNotNil(stash.onion(forID: someUser.id))

stash.add(
  values: (0 ..< 99999)
    .map { i in User(name: "Anon", isRegistered: i.isMultiple(of: 2)) }
)

XCTAssertEqual(stash.onions.count, 100000)
XCTAssertEqual(stash.onions(forType: "User.Registered").count, 50001)
Onion Bank
var bank = OnionBank(
    stashableOnionTypes: OnionStash<A>.self, OnionStash<B>.self
)

XCTAssertEqual(bank.all.count, 2)

bank.add(value: A(value: "First Bank Value"))

try! bank.save()

var loadedBank = OnionBank(
stashableOnionTypes: OnionStash<A>.self, OnionStash<B>.self
)

dump(loadedBank)

XCTAssertEqual((loadedBank.all[0] as? OnionStash<A>)?.onionSet.count, 1)

try! bank.deleteAll()

XCTAssertEqual(bank.all.count, 2)
XCTAssertEqual((bank.all[0] as? OnionStash<A>)?.onionSet.count, 0)

try! loadedBank.load()

XCTAssertEqual(loadedBank.all.count, 2)
XCTAssertEqual((loadedBank.all[0] as? OnionStash<A>)?.onionSet.count, 0)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics