Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade framework to use Swift's implementation of KeyPath -> Strong conversion #8

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
478 changes: 0 additions & 478 deletions JSONPatch.xcodeproj/project.pbxproj

This file was deleted.

This file was deleted.

This file was deleted.

100 changes: 0 additions & 100 deletions JSONPatch.xcodeproj/xcshareddata/xcschemes/JSONPatch.xcscheme

This file was deleted.

22 changes: 0 additions & 22 deletions JSONPatch/Info.plist

This file was deleted.

19 changes: 0 additions & 19 deletions JSONPatch/JSONPatch.h

This file was deleted.

20 changes: 0 additions & 20 deletions JSONPatch/KeyPath+StringConversion.swift

This file was deleted.

22 changes: 0 additions & 22 deletions JSONPatchTests/Info.plist

This file was deleted.

33 changes: 0 additions & 33 deletions PRJSONPatch.podspec

This file was deleted.

15 changes: 7 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
// swift-tools-version:5.0
// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "JSONPatch",
platforms: [
.iOS(.v8), .macOS(.v10_13)
.iOS(.v16), .macOS(.v13),
],
products: [
.library(
name: "JSONPatch",
targets: ["JSONPatch"]),
targets: ["JSONPatch"])
],
targets: [
.target(
name: "JSONPatch",
dependencies: [],
path: "JSONPatch"),
name: "JSONPatch"),
.testTarget(
name: "JSONPatchTests",
dependencies: ["JSONPatch"],
path: "JSONPatchTests"),
dependencies: ["JSONPatch"]
),
]
)
48 changes: 2 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,14 @@
# JSONPatch
[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/PRJSONPatch.svg)](https://img.shields.io/cocoapods/v/PRJSONPatch.svg)
[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)

A Swift μ-framework for creating [RFC6902](https://tools.ietf.org/html/rfc6902) compliant [JSON patch](http://jsonpatch.com) objects

## Requirements

- iOS 8.0+ / macOS 10.13+
- Swift 4.2+
- iOS 16.4+ / macOS 13.3+

### IMPORTANT!

The framework relies on the Objective C runtime for converting keypaths into strings, and so it is crucial that the properties the keypaths point to are representable in Objective C. To achieve this, you will have to add the `@objc` annotation at each variable declaration.

```swift
class Patch: NSObject {
@objc var baz: String!
@objc var foo: String!
@objc var hello: [String]!
}
```

Alternatively you can also use the `@objcMembers` annotation at the class level if all class members are representible in Objective C

```swift
@objcMembers class Patch: NSObject {
...
}
```
The `Package.swift` description declares requirements from iOS 16 and macOS 13, but in reality the requirement is actually iOS 16.4 and macOS 13.3. This is because the framework now relies on Swift's implementation of converting keypath's to strings, and that API is only available from iOS 16.4 and macOS 13.3.

## Installation
### Carthage

To integrate JSONPatch into your Xcode project using [Carthage](https://github.com/Carthage/Carthage), specify it in your `Cartfile`:

```ogdl
github "peterringset/JSONPatch" ~> 2.0
```

### CocoaPods

To integrate JSONPatch into your Xcode project using [CocoaPods](https://cocoapods.org), specify it in your `Podfile`:

```ruby
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
pod 'PRJSONPatch', '~> 2.0'
end
```

### Swift package manger
To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter `https://github.com/peterringset/JSONPatch`.

Expand Down
File renamed without changes.
17 changes: 9 additions & 8 deletions JSONPatch/JSONPatch.swift → Sources/JSONPatch/JSONPatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import Foundation

public struct JSONPatch<Root: NSObject> {
@available(iOS 16.4, macOS 13.3, *)
public struct JSONPatch<Root> {
typealias StringKeyPath = String

enum Operation {
Expand All @@ -28,31 +28,32 @@ public struct JSONPatch<Root: NSObject> {
}

public static func add<V: Encodable>(_ keyPath: KeyPath<Root, V>, value: V) -> Self {
return .init(operation: .add(keyPath: String(describing: keyPath), value: AnyEncodable(value)))
return .init(operation: .add(keyPath: keyPath.path, value: AnyEncodable(value)))
}

public static func remove<V>(_ keyPath: KeyPath<Root, V>) -> Self {
return .init(operation: .remove(keyPath: String(describing: keyPath)))
return .init(operation: .remove(keyPath: keyPath.path))
}

public static func replace<V: Encodable>(_ keyPath: KeyPath<Root, V>, value: V) -> Self {
return .init(operation: .replace(keyPath: String(describing: keyPath), value: AnyEncodable(value)))
return .init(operation: .replace(keyPath: keyPath.path, value: AnyEncodable(value)))
}

public static func move<V>(from: KeyPath<Root, V>, to: KeyPath<Root, V>) -> Self {
return .init(operation: .move(from: String(describing: from), to: String(describing: to)))
return .init(operation: .move(from: from.path, to: to.path))
}

public static func copy<V>(from: KeyPath<Root, V>, to: KeyPath<Root, V>) -> Self {
return .init(operation: .copy(from: String(describing: from), to: String(describing: to)))
return .init(operation: .copy(from: from.path, to: to.path))
}

public static func test<V: Encodable>(_ keyPath: KeyPath<Root, V>, value: V) -> Self {
return .init(operation: .test(keyPath: String(describing: keyPath), value: AnyEncodable(value)))
return .init(operation: .test(keyPath: keyPath.path, value: AnyEncodable(value)))
}

}

@available(iOS 16.4, macOS 13.3, *)
extension JSONPatch: Encodable {

private enum CodingKeys: String, CodingKey {
Expand Down
Loading