Swiftpack.co - p-x9/IndirectlyCodable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by p-x9.
p-x9/IndirectlyCodable 0.0.1
indirectly `Codable` protocol
⭐️ 0
🕓 3 weeks ago
.package(url: "https://github.com/p-x9/IndirectlyCodable.git", from: "0.0.1")

IndirectlyCodable

Protocol for indirect Codable(Encodable, Decodable) conformance

Document

IndirectlyCodable

Target

Direct Codable conforming type aliases. Must conform to ObjectConvertiblyCodable described below.

typealias Target = CALayerModel

codable()

Get a Codable object.

codableTypeName

When making a class conform to this protocol, it is not possible to create additional aliases to the Target type in the inherited class. Therefore, you must use this property to get the class with NSClassFromString.

@objc
public class var codableTypeName: String {
    String(reflecting: Target.self)
}

ObjectConvertiblyCodable

Target

Alias of the type to be represented

typealias Target = CALayer

applyProperties to Target

Apply the value of a property of confidence to the target object. Using this library named p-x9/KeyPathValue, you can create a propertyMap as follows.

let propertyMap: [PartialKeyPath<CALayerModel>: ReferenceWritableKeyPathValueApplier<CALayer>] = [
        \.bounds: .init(\.bounds),
         \.position: .init(\.position),
         \.frame: .init(\.frame),
         \.backgroundColor: .init(\.backgroundColor),
         \.cornerRadius: .init(\.cornerRadius),
         \.borderWidth: .init(\.borderWidth),
         \.borderColor: .init(\.borderColor)
    ]

func applyProperties(to target: CALayer) {
    Self.propertyMap.forEach { keyPath, applier in
        var value = self[keyPath: keyPath]
        if let convertible = value as? (any ObjectConvertiblyCodable),
            let converted = convertible.converted() {
            value = converted
        }
        applier.apply(value, target)
    }
}

reverseApplyProperties with Target

Apply values from the target object to its own properties.

GitHub

link
Stars: 0
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

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