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

Can use AnyCodable through PropertyWrapper #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions AnyCodable.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
8D746F0121F2F2A900C0A07C /* MockCodableIfPresentObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D746EFC21F2F2A900C0A07C /* MockCodableIfPresentObject.swift */; };
8D746F0221F2F2A900C0A07C /* MockCodableObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D746EFD21F2F2A900C0A07C /* MockCodableObject.swift */; };
8D746F0321F2F2A900C0A07C /* DecodingContainer+AnyCollectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D746EFE21F2F2A900C0A07C /* DecodingContainer+AnyCollectionTests.swift */; };
C931FCEF255533A9008474BB /* PropertyWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C931FCEE255533A9008474BB /* PropertyWrapper.swift */; };
C931FCF7255537E7008474BB /* PropertyWrapper+Decode.swift in Sources */ = {isa = PBXBuildFile; fileRef = C931FCF6255537E7008474BB /* PropertyWrapper+Decode.swift */; };
C931FCFD25553867008474BB /* MockCodableObjectWithPropertyWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C931FCFC25553867008474BB /* MockCodableObjectWithPropertyWrapper.swift */; };
C931FD0925553BD7008474BB /* PropertyWrapper+Encode.swift in Sources */ = {isa = PBXBuildFile; fileRef = C931FD0825553BD7008474BB /* PropertyWrapper+Encode.swift */; };
C931FD0F25553C7E008474BB /* MockCodableIfPresentObjectWithPropertyWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C931FD0E25553C7E008474BB /* MockCodableIfPresentObjectWithPropertyWrapper.swift */; };
C931FD1325553D17008474BB /* PropertyWrapperIfPresent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C931FD1225553D17008474BB /* PropertyWrapperIfPresent.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -41,6 +47,12 @@
8D746EFC21F2F2A900C0A07C /* MockCodableIfPresentObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockCodableIfPresentObject.swift; sourceTree = "<group>"; };
8D746EFD21F2F2A900C0A07C /* MockCodableObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockCodableObject.swift; sourceTree = "<group>"; };
8D746EFE21F2F2A900C0A07C /* DecodingContainer+AnyCollectionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "DecodingContainer+AnyCollectionTests.swift"; sourceTree = "<group>"; };
C931FCEE255533A9008474BB /* PropertyWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PropertyWrapper.swift; sourceTree = "<group>"; };
C931FCF6255537E7008474BB /* PropertyWrapper+Decode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PropertyWrapper+Decode.swift"; sourceTree = "<group>"; };
C931FCFC25553867008474BB /* MockCodableObjectWithPropertyWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockCodableObjectWithPropertyWrapper.swift; sourceTree = "<group>"; };
C931FD0825553BD7008474BB /* PropertyWrapper+Encode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PropertyWrapper+Encode.swift"; sourceTree = "<group>"; };
C931FD0E25553C7E008474BB /* MockCodableIfPresentObjectWithPropertyWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockCodableIfPresentObjectWithPropertyWrapper.swift; sourceTree = "<group>"; };
C931FD1225553D17008474BB /* PropertyWrapperIfPresent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PropertyWrapperIfPresent.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -86,6 +98,8 @@
8D746EDF21F2F28C00C0A07C /* AnyCodable.h */,
8D746EF721F2F29B00C0A07C /* DecodingContainer+AnyCollection.swift */,
8D746EF621F2F29B00C0A07C /* EncodingContainer+AnyCollection.swift */,
C931FCEE255533A9008474BB /* PropertyWrapper.swift */,
C931FD1225553D17008474BB /* PropertyWrapperIfPresent.swift */,
8D746EE021F2F28C00C0A07C /* Info.plist */,
);
path = AnyCodable;
Expand All @@ -97,8 +111,12 @@
8D746EFA21F2F2A900C0A07C /* AnyCodingKeyTests.swift */,
8D746EFE21F2F2A900C0A07C /* DecodingContainer+AnyCollectionTests.swift */,
8D746EFB21F2F2A900C0A07C /* EncodingContainer+AnyCollectionTests.swift */,
C931FCF6255537E7008474BB /* PropertyWrapper+Decode.swift */,
C931FD0825553BD7008474BB /* PropertyWrapper+Encode.swift */,
8D746EFC21F2F2A900C0A07C /* MockCodableIfPresentObject.swift */,
8D746EFD21F2F2A900C0A07C /* MockCodableObject.swift */,
C931FD0E25553C7E008474BB /* MockCodableIfPresentObjectWithPropertyWrapper.swift */,
C931FCFC25553867008474BB /* MockCodableObjectWithPropertyWrapper.swift */,
8D746EEC21F2F28D00C0A07C /* Info.plist */,
);
path = AnyCodableTests;
Expand Down Expand Up @@ -214,7 +232,9 @@
buildActionMask = 2147483647;
files = (
8D746EF821F2F29C00C0A07C /* EncodingContainer+AnyCollection.swift in Sources */,
C931FCEF255533A9008474BB /* PropertyWrapper.swift in Sources */,
8D746EF921F2F29C00C0A07C /* DecodingContainer+AnyCollection.swift in Sources */,
C931FD1325553D17008474BB /* PropertyWrapperIfPresent.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -224,8 +244,12 @@
files = (
8D746F0221F2F2A900C0A07C /* MockCodableObject.swift in Sources */,
8D746F0121F2F2A900C0A07C /* MockCodableIfPresentObject.swift in Sources */,
C931FCFD25553867008474BB /* MockCodableObjectWithPropertyWrapper.swift in Sources */,
C931FCF7255537E7008474BB /* PropertyWrapper+Decode.swift in Sources */,
C931FD0F25553C7E008474BB /* MockCodableIfPresentObjectWithPropertyWrapper.swift in Sources */,
8D746EFF21F2F2A900C0A07C /* AnyCodingKeyTests.swift in Sources */,
8D746F0021F2F2A900C0A07C /* EncodingContainer+AnyCollectionTests.swift in Sources */,
C931FD0925553BD7008474BB /* PropertyWrapper+Encode.swift in Sources */,
8D746F0321F2F2A900C0A07C /* DecodingContainer+AnyCollectionTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
4 changes: 2 additions & 2 deletions AnyCodable/DecodingContainer+AnyCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extension KeyedDecodingContainer {
}
}

private extension KeyedDecodingContainer {
internal extension KeyedDecodingContainer {
func decode(_ type: [String: Any].Type) throws -> [String: Any] {
var dictionary: [String: Any] = [:]
for key in allKeys {
Expand All @@ -120,7 +120,7 @@ private extension KeyedDecodingContainer {
}
}

private extension UnkeyedDecodingContainer {
internal extension UnkeyedDecodingContainer {
mutating func decode(_ type: [Any].Type) throws -> [Any] {
var elements: [Any] = []
while !isAtEnd {
Expand Down
4 changes: 2 additions & 2 deletions AnyCodable/EncodingContainer+AnyCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extension KeyedEncodingContainer {
}
}

private extension KeyedEncodingContainer where K == AnyCodingKey {
internal extension KeyedEncodingContainer where K == AnyCodingKey {
mutating func encode(_ value: [String: Any]) throws {
for (k, v) in value {
let key = AnyCodingKey(stringValue: k)!
Expand All @@ -89,7 +89,7 @@ private extension KeyedEncodingContainer where K == AnyCodingKey {
}
}

private extension UnkeyedEncodingContainer {
internal extension UnkeyedEncodingContainer {
/// Encodes the given value.
///
/// - parameter value: The value to encode.
Expand Down
47 changes: 47 additions & 0 deletions AnyCodable/PropertyWrapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// PropertyWrapper.swift
// AnyCodable
//
// Created by 이병찬 on 2020/11/06.
// Copyright © 2020 levantAJ. All rights reserved.
//

import Foundation

@propertyWrapper
public struct CodedAnyArray: Codable {
public var wrappedValue: [Any]

public init(wrappedValue: [Any]) {
self.wrappedValue = wrappedValue
}

public init(from decoder: Decoder) throws {
var values = try decoder.unkeyedContainer()
self.wrappedValue = try values.decode([Any].self)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(wrappedValue)
}
}

@propertyWrapper
public struct CodedAnyDictonary: Codable {
public var wrappedValue: [String: Any]

public init(wrappedValue: [String: Any]) {
self.wrappedValue = wrappedValue
}

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: AnyCodingKey.self)
self.wrappedValue = try values.decode([String: Any].self)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encode(wrappedValue)
}
}
63 changes: 63 additions & 0 deletions AnyCodable/PropertyWrapperIfPresent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// PropertyWrapperIfPresent.swift
// AnyCodable
//
// Created by 이병찬 on 2020/11/06.
// Copyright © 2020 levantAJ. All rights reserved.
//

import Foundation

@propertyWrapper
public struct CodedIfPresentAnyArray: Codable {
public var wrappedValue: [Any]?

public init(wrappedValue: [Any]?) {
self.wrappedValue = wrappedValue
}

public init(from decoder: Decoder) throws {
if var values = try? decoder.unkeyedContainer() {
self.wrappedValue = try values.decode([Any].self)
} else {
self.wrappedValue = nil
}
}

public func encode(to encoder: Encoder) throws {
if let value = wrappedValue {
var container = encoder.unkeyedContainer()
try container.encode(value)
} else {
var container = encoder.singleValueContainer()
try container.encodeNil()
}
}
}

@propertyWrapper
public struct CodedIfPresentAnyDictonary: Codable {
public var wrappedValue: [String: Any]?

public init(wrappedValue: [String: Any]?) {
self.wrappedValue = wrappedValue
}

public init(from decoder: Decoder) throws {
if let values = try? decoder.container(keyedBy: AnyCodingKey.self) {
self.wrappedValue = try values.decode([String: Any].self)
} else {
self.wrappedValue = nil
}
}

public func encode(to encoder: Encoder) throws {
if let value = wrappedValue {
var container = encoder.container(keyedBy: AnyCodingKey.self)
try container.encode(value)
} else {
var container = encoder.singleValueContainer()
try container.encodeNil()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// MockCodableIfPresentObjectWithPropertyWrapper.swift
// AnyCodableTests
//
// Created by 이병찬 on 2020/11/06.
// Copyright © 2020 levantAJ. All rights reserved.
//

import Foundation
@testable import AnyCodable

struct MockCodableIfPresentObjectWithPropertyWrapper: Codable {
@CodedIfPresentAnyDictonary var dict: [String: Any]?
@CodedIfPresentAnyArray var array: [Any]?

init(dict: [String: Any]?, array: [Any]?) {
self.dict = dict
self.array = array
}
}
20 changes: 20 additions & 0 deletions AnyCodableTests/MockCodableObjectWithPropertyWrapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// MockCodableObjectWithPropertyWrapper.swift
// AnyCodableTests
//
// Created by 이병찬 on 2020/11/06.
// Copyright © 2020 levantAJ. All rights reserved.
//

import Foundation
@testable import AnyCodable

struct MockCodableObjectWithPropertyWrapper: Codable {
@CodedAnyDictonary private(set) var dict: [String: Any]
@CodedAnyArray private(set) var array: [Any]

init(dict: [String: Any], array: [Any]) {
self.dict = dict
self.array = array
}
}
Loading