2020-03-24 13:27:44 -07:00
|
|
|
//
|
|
|
|
|
// JSONDecoder+Properties.swift
|
|
|
|
|
// Harmony
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 10/3/18.
|
|
|
|
|
// Copyright © 2018 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import CoreData
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
public extension CodingUserInfoKey
|
2020-03-24 13:27:44 -07:00
|
|
|
{
|
|
|
|
|
static let managedObjectContext = CodingUserInfoKey(rawValue: "managedObjectContext")!
|
|
|
|
|
static let sourceURL = CodingUserInfoKey(rawValue: "sourceURL")!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public final class JSONDecoder: Foundation.JSONDecoder
|
|
|
|
|
{
|
|
|
|
|
@DecoderItem(key: .managedObjectContext)
|
2020-09-03 16:39:08 -07:00
|
|
|
public var managedObjectContext: NSManagedObjectContext?
|
2020-03-24 13:27:44 -07:00
|
|
|
|
|
|
|
|
@DecoderItem(key: .sourceURL)
|
2020-09-03 16:39:08 -07:00
|
|
|
public var sourceURL: URL?
|
2020-03-24 13:27:44 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
public extension Decoder
|
2020-03-24 13:27:44 -07:00
|
|
|
{
|
|
|
|
|
var managedObjectContext: NSManagedObjectContext? { self.userInfo[.managedObjectContext] as? NSManagedObjectContext }
|
|
|
|
|
var sourceURL: URL? { self.userInfo[.sourceURL] as? URL }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@propertyWrapper
|
2020-09-03 16:39:08 -07:00
|
|
|
public struct DecoderItem<Value>
|
2020-03-24 13:27:44 -07:00
|
|
|
{
|
2020-09-03 16:39:08 -07:00
|
|
|
public let key: CodingUserInfoKey
|
2020-03-24 13:27:44 -07:00
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
public var wrappedValue: Value? {
|
2020-03-24 13:27:44 -07:00
|
|
|
get { fatalError("only works on instance properties of classes") }
|
|
|
|
|
set { fatalError("only works on instance properties of classes") }
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
public init(key: CodingUserInfoKey)
|
2020-03-24 13:27:44 -07:00
|
|
|
{
|
|
|
|
|
self.key = key
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static subscript<OuterSelf: JSONDecoder>(
|
|
|
|
|
_enclosingInstance decoder: OuterSelf,
|
|
|
|
|
wrapped wrappedKeyPath: ReferenceWritableKeyPath<OuterSelf, Value?>,
|
|
|
|
|
storage storageKeyPath: ReferenceWritableKeyPath<OuterSelf, Self>
|
|
|
|
|
) -> Value? {
|
|
|
|
|
get {
|
|
|
|
|
let wrapper = decoder[keyPath: storageKeyPath]
|
|
|
|
|
|
|
|
|
|
let value = decoder.userInfo[wrapper.key] as? Value
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
let wrapper = decoder[keyPath: storageKeyPath]
|
|
|
|
|
decoder.userInfo[wrapper.key] = newValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|