Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
roanutil committed Oct 20, 2023
1 parent 77707d0 commit 8663820
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ let package = Package(
targets: [
.target(
name: "Secret",
swiftSettings: settings
swiftSettings: swiftSettings
),
.testTarget(
name: "SecretTests",
dependencies: ["Secret"],
swiftSettings: settings
swiftSettings: swiftSettings
),
]
)

let settings: [SwiftSetting] = [
.enableExperimentalFeature("StrictConcurrency"),
let swiftSettings: [SwiftSetting] = [
.enableUpcomingFeature("BareSlashRegexLiterals"),
.enableUpcomingFeature("ConciseMagicFile"),
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("ForwardTrailingClosures"),
.enableUpcomingFeature("ImplicitOpenExistentials"),
.enableUpcomingFeature("StrictConcurrency"),
]
75 changes: 75 additions & 0 deletions Sources/Secret/Hashed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,78 @@ extension Hashed: Identifiable where T: Identifiable {
/// passthrough ``wrappedValue``s ``Identifiable`` conformance.
public var id: T.ID { wrappedValue.id }
}

extension Hashed: ExpressibleByUnicodeScalarLiteral where T: ExpressibleByUnicodeScalarLiteral {
public init(unicodeScalarLiteral value: T.UnicodeScalarLiteralType) {
self.init(T(unicodeScalarLiteral: value))
}
}

extension Hashed: ExpressibleByExtendedGraphemeClusterLiteral where T: ExpressibleByExtendedGraphemeClusterLiteral {
public init(extendedGraphemeClusterLiteral value: T.ExtendedGraphemeClusterLiteralType) {
self.init(T(extendedGraphemeClusterLiteral: value))
}
}

extension Hashed: ExpressibleByStringLiteral where T: ExpressibleByStringLiteral {
public init(stringLiteral value: T.StringLiteralType) {
self.init(T(stringLiteral: value))
}
}

extension Hashed: ExpressibleByStringInterpolation where T: ExpressibleByStringInterpolation {
public init(stringInterpolation: T.StringInterpolation) {
self.init(T(stringInterpolation: stringInterpolation))
}
}

extension Hashed: ExpressibleByNilLiteral where T: ExpressibleByNilLiteral {
public init(nilLiteral: ()) {
self.init(T(nilLiteral: nilLiteral))
}
}

extension Hashed: ExpressibleByFloatLiteral where T: ExpressibleByFloatLiteral {
public init(floatLiteral value: T.FloatLiteralType) {
self.init(T(floatLiteral: value))
}
}

extension Hashed: ExpressibleByIntegerLiteral where T: ExpressibleByIntegerLiteral {
public init(integerLiteral value: T.IntegerLiteralType) {
self.init(T(integerLiteral: value))
}
}

extension Hashed: ExpressibleByDictionaryLiteral where T: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (T.Key, T.Value)...) {
let transform = unsafeBitCast(
T.init(dictionaryLiteral:) as ((T.Key, T.Value)...) -> T,
to: (([(T.Key, T.Value)]) -> T).self
)
self.init(transform(elements))
}
}

extension Hashed: LosslessStringConvertible where T: LosslessStringConvertible {
public init?(_ description: String) {
guard let wrapped = T(description) else {
return nil
}
self.init(wrapped)
}
}

extension Hashed: AdditiveArithmetic where T: AdditiveArithmetic {
public static var zero: Hashed<T> {
self.init(T.zero)
}

public static func + (lhs: Hashed<T>, rhs: Hashed<T>) -> Hashed<T> {
self.init(lhs.wrappedValue + rhs.wrappedValue)
}

public static func - (lhs: Hashed<T>, rhs: Hashed<T>) -> Hashed<T> {
self.init(lhs.wrappedValue - rhs.wrappedValue)
}
}
75 changes: 75 additions & 0 deletions Sources/Secret/Redacted.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,78 @@ extension Redacted: Sendable where T: Sendable {}
extension Redacted: Identifiable where T: Identifiable {
public var id: T.ID { wrappedValue.id }
}

extension Redacted: ExpressibleByUnicodeScalarLiteral where T: ExpressibleByUnicodeScalarLiteral {
public init(unicodeScalarLiteral value: T.UnicodeScalarLiteralType) {
self.init(T(unicodeScalarLiteral: value))
}
}

extension Redacted: ExpressibleByExtendedGraphemeClusterLiteral where T: ExpressibleByExtendedGraphemeClusterLiteral {
public init(extendedGraphemeClusterLiteral value: T.ExtendedGraphemeClusterLiteralType) {
self.init(T(extendedGraphemeClusterLiteral: value))
}
}

extension Redacted: ExpressibleByStringLiteral where T: ExpressibleByStringLiteral {
public init(stringLiteral value: T.StringLiteralType) {
self.init(T(stringLiteral: value))
}
}

extension Redacted: ExpressibleByStringInterpolation where T: ExpressibleByStringInterpolation {
public init(stringInterpolation: T.StringInterpolation) {
self.init(T(stringInterpolation: stringInterpolation))
}
}

extension Redacted: ExpressibleByNilLiteral where T: ExpressibleByNilLiteral {
public init(nilLiteral: ()) {
self.init(T(nilLiteral: nilLiteral))
}
}

extension Redacted: ExpressibleByFloatLiteral where T: ExpressibleByFloatLiteral {
public init(floatLiteral value: T.FloatLiteralType) {
self.init(T(floatLiteral: value))
}
}

extension Redacted: ExpressibleByIntegerLiteral where T: ExpressibleByIntegerLiteral {
public init(integerLiteral value: T.IntegerLiteralType) {
self.init(T(integerLiteral: value))
}
}

extension Redacted: ExpressibleByDictionaryLiteral where T: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (T.Key, T.Value)...) {
let transform = unsafeBitCast(
T.init(dictionaryLiteral:) as ((T.Key, T.Value)...) -> T,
to: (([(T.Key, T.Value)]) -> T).self
)
self.init(transform(elements))
}
}

extension Redacted: LosslessStringConvertible where T: LosslessStringConvertible {
public init?(_ description: String) {
guard let wrapped = T(description) else {
return nil
}
self.init(wrapped)
}
}

extension Redacted: AdditiveArithmetic where T: AdditiveArithmetic {
public static var zero: Redacted<T> {
self.init(T.zero)
}

public static func + (lhs: Redacted<T>, rhs: Redacted<T>) -> Redacted<T> {
self.init(lhs.wrappedValue + rhs.wrappedValue)
}

public static func - (lhs: Redacted<T>, rhs: Redacted<T>) -> Redacted<T> {
self.init(lhs.wrappedValue - rhs.wrappedValue)
}
}
2 changes: 2 additions & 0 deletions Tests/SecretTests/HashedDebugDescriptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ final class HashedDebugDescriptionTests: XCTestCase {
)
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testSinglePropertyActor() throws {
let value = Hashed(SinglePropertyActor())
XCTAssertEqual(value.debugDescription, "SinglePropertyActor: \(value.wrappedValue.hashValue.description)")
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testMultiPropertyActor() throws {
let value = Hashed(MultiPropertyActor())
XCTAssertEqual(value.debugDescription, "MultiPropertyActor: \(value.wrappedValue.hashValue.description)")
Expand Down
2 changes: 2 additions & 0 deletions Tests/SecretTests/HashedDescriptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ final class HashedDescriptionTests: XCTestCase {
XCTAssertEqual(value.description, value.wrappedValue.hashValue.description)
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testSinglePropertyActor() throws {
let value = Hashed(SinglePropertyActor())
XCTAssertEqual(value.description, value.wrappedValue.hashValue.description)
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testMultiPropertyActor() throws {
let value = Hashed(MultiPropertyActor())
XCTAssertEqual(value.description, value.wrappedValue.hashValue.description)
Expand Down
2 changes: 2 additions & 0 deletions Tests/SecretTests/HashedReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ final class HashedReflectionTests: XCTestCase {
XCTAssertEqual(value.wrappedValue.hashValue, hash)
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testSinglePropertyActor() async throws {
let value = Hashed(SinglePropertyStruct())
let mirror = Mirror(reflecting: value)
Expand All @@ -200,6 +201,7 @@ final class HashedReflectionTests: XCTestCase {
XCTAssertEqual(value.wrappedValue.hashValue, hash)
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testMultiPropertyActor() throws {
let value = Hashed(SinglePropertyStruct())
let mirror = Mirror(reflecting: value)
Expand Down
2 changes: 2 additions & 0 deletions Tests/SecretTests/Internal/Fixtures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ enum MultiCaseAssociatedValueEnum: Hashable {
case secretB(String)
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
actor SinglePropertyActor: Hashable {
var value: String = "SECRET_VALUE"

Expand All @@ -87,6 +88,7 @@ actor SinglePropertyActor: Hashable {
}
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
actor MultiPropertyActor: Hashable {
var valueA: String = "SECRET_VALUE_A"
let valueB: Bool = false
Expand Down
2 changes: 2 additions & 0 deletions Tests/SecretTests/RedactedDebugDescriptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ final class RedactedDebugDescriptionTests: XCTestCase {
XCTAssertEqual(value.debugDescription, "MultiCaseAssociatedValueEnum: REDACTED")
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testSinglePropertyActor() throws {
let value = Redacted(SinglePropertyActor())
XCTAssertEqual(value.debugDescription, "SinglePropertyActor: REDACTED")
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testMultiPropertyActor() throws {
let value = Redacted(MultiPropertyActor())
XCTAssertEqual(value.debugDescription, "MultiPropertyActor: REDACTED")
Expand Down
2 changes: 2 additions & 0 deletions Tests/SecretTests/RedactedDescriptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ final class RedactedDescriptionTests: XCTestCase {
XCTAssertEqual(value.description, "REDACTED")
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testSinglePropertyActor() throws {
let value = Redacted(SinglePropertyActor())
XCTAssertEqual(value.description, "REDACTED")
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testMultiPropertyActor() throws {
let value = Redacted(MultiPropertyActor())
XCTAssertEqual(value.description, "REDACTED")
Expand Down
2 changes: 2 additions & 0 deletions Tests/SecretTests/RedactedReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ final class RedactedReflectionTests: XCTestCase {
}
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testSinglePropertyActor() throws {
let value = Redacted(SinglePropertyActor())
let mirror = Mirror(reflecting: value)
Expand All @@ -180,6 +181,7 @@ final class RedactedReflectionTests: XCTestCase {
}
}

@available(iOS 13, tvOS 13, macOS 10.15, watchOS 6, *)
func testMultiPropertyActor() throws {
let value = Redacted(MultiPropertyActor())
let mirror = Mirror(reflecting: value)
Expand Down

0 comments on commit 8663820

Please sign in to comment.