KeyPathValue

0.1.0

🔑 Structure for assigning values using keypath
p-x9/KeyPathValue

What's New

v0.1.0

2024-04-01T14:25:40Z

What's Changed

  • implemented Initializer using PartialKeyPath by @p-x9 in #1
  • unit test by @p-x9 in #3
  • returns whether the value was successfully applied or not by @p-x9 in #4

New Contributors

  • @p-x9 made their first contribution in #1

Full Changelog: 0.0.1...0.1.0

KeyPathValue

Structure for assigning values using keypath

Example

ReferenceWritableKeyPathValueApplier

Suppose we have the following model.

struct CALayerModel {
    var bounds: CGRect?
    var position: CGPoint?
    var frame: CGRect?

    var backgroundColor: CGColor?

    var cornerRadius: CGFloat?

    var borderWidth: CGFloat?
    var borderColor: CGColor?
}

Define propertyMap using ReferenceWritableKeyPathValueApplier

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)
    ]

Can be assigned to an object using propertyMap.

extension CALayerModel {
    public func applyProperties(to target: CALayer) {
        Self.propertyMap.forEach { keyPath, applier in
            let value = self[keyPath: keyPath]
            applier.apply(value, target)
        }
    }
}

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Fri Apr 12 2024 16:19:53 GMT-0900 (Hawaii-Aleutian Daylight Time)