Skip to content

Commit

Permalink
added starter target
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Mar 25, 2024
1 parent b9d0800 commit f453ff3
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
"revision" : "32e8d724467f8fe623624570367e3d50c5638e46",
"version" : "1.5.2"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax",
"state" : {
"revision" : "fa8f95c2d536d6620cc2f504ebe8a6167c9fc2dd",
"version" : "510.0.1"
}
}
],
"version" : 2
Expand Down
15 changes: 12 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// swift-tools-version: 5.8
// swift-tools-version: 5.9

// swiftlint:disable explicit_acl explicit_top_level_acl

import PackageDescription
import CompilerPluginSupport

// let swiftSettings: [SwiftSetting] = [
// .enableUpcomingFeature("BareSlashRegexLiterals"),
Expand All @@ -16,17 +17,25 @@ import PackageDescription

let package = Package(
name: "StealthyStash",
platforms: [.macOS(.v12), .iOS(.v14), .watchOS(.v7), .tvOS(.v14)],
platforms: [.macOS(.v12), .iOS(.v14), .watchOS(.v7), .tvOS(.v14), .macCatalyst(.v14), .visionOS(.v1)],
products: [
.library(
name: "StealthyStash",
targets: ["StealthyStash"]
)
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0")
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-syntax", from: "510.0.0")
],
targets: [
.macro(
name: "StealthyStashMacros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
]
),
.target(
name: "StealthyStash",
dependencies: [
Expand Down
122 changes: 121 additions & 1 deletion Samples/Demo/App/Keychain/CompositeCredentials.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,127 @@
import StealthyStash

struct CompositeCredentials: StealthyModel {
typealias QueryBuilder = CompositeCredentialsQueryBuilder
//typealias QueryBuilder = CompositeCredentialsQueryBuilder

struct QueryBuilder: ModelQueryBuilder {
static func updates(
from previousItem: CompositeCredentials,
to newItem: CompositeCredentials
) -> [StealthyPropertyUpdate] {
let newPasswordData = newItem.password.flatMap {
$0.data(using: .utf8)
}.map {
InternetPasswordItem(account: newItem.userName, data: $0)
}

let oldPasswordData = previousItem.password.flatMap {
$0.data(using: .utf8)
}.map {
InternetPasswordItem(account: previousItem.userName, data: $0)
}

let previousTokenData = previousItem.token.flatMap {
$0.data(using: .utf8)
}.map {
GenericPasswordItem(account: previousItem.userName, data: $0)
}

let newTokenData = newItem.token.flatMap {
$0.data(using: .utf8)
}.map {
GenericPasswordItem(account: newItem.userName, data: $0)
}

let passwordUpdate = StealthyPropertyUpdate(
previousProperty: oldPasswordData,
newProperty: newPasswordData
)
let tokenUpdate = StealthyPropertyUpdate(
previousProperty: previousTokenData,
newProperty: newTokenData
)
return [passwordUpdate, tokenUpdate]
}

static func properties(
from model: CompositeCredentials,
for _: ModelOperation
) -> [AnyStealthyProperty] {
let passwordData = model.password.flatMap {
$0.data(using: .utf8)
}

let passwordProperty: AnyStealthyProperty = .init(
property: InternetPasswordItem(
account: model.userName,
data: passwordData ?? .init()
)
)

let tokenData = model.token.flatMap {
$0.data(using: .utf8)
}

let tokenProperty: AnyStealthyProperty = .init(
property: GenericPasswordItem(
account: model.userName,
data: tokenData ?? .init()
)
)

return [passwordProperty, tokenProperty]
}

static func queries(from _: Void) -> [String: Query] {
[
"password": TypeQuery(type: .internet),
"token": TypeQuery(type: .generic)
]
}

static func model(
from properties: [String: [AnyStealthyProperty]]
) throws -> CompositeCredentials? {
for internet in properties["password"] ?? [] {
for generic in properties["token"] ?? [] {
if internet.account == generic.account {
return .init(
userName: internet.account,
password: internet.dataString,
token: generic.dataString
)
}
}
}
let properties = properties.values.flatMap { $0 }.enumerated().sorted { lhs, rhs in
if lhs.element.propertyType == rhs.element.propertyType {
return lhs.offset < rhs.offset
} else {
return lhs.element.propertyType == .internet
}
}.map(\.element)

guard let username = properties.map(\.account).first else {
return nil
}
let password = properties
.first { $0.propertyType == .internet }?
.data
let token = properties.first {
$0.propertyType == .generic && $0.account == username
}?.data

return CompositeCredentials(
userName: username,
password: password?.string(),
token: token?.string()
)
}

typealias QueryType = Void

typealias StealthyModelType = CompositeCredentials
}

internal init(userName: String, password: String?, token: String?) {
self.userName = userName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import StealthyStash

@available(*, deprecated, message: "Use internal builder.")
struct CompositeCredentialsQueryBuilder: ModelQueryBuilder {
static func updates(
from previousItem: CompositeCredentials,
Expand Down
12 changes: 12 additions & 0 deletions Sources/StealthyStashMacros/CompositeModelMacro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// File.swift
//
//
// Created by Leo Dion on 3/25/24.
//

import SwiftSyntax
import SwiftSyntaxMacros

//@attached(member, names: named(_$backingData), named(persistentBackingData), named(schemaMetadata), named(init), named(_$observationRegistrar), named(_SwiftDataNoType)) @attached(memberAttribute) @attached(extension, conformances: Observable, PersistentModel) macro Model()

0 comments on commit f453ff3

Please sign in to comment.