Skip to content

Commit

Permalink
remove Literals, directly @CodableWrapper only support optional value
Browse files Browse the repository at this point in the history
  • Loading branch information
PAN committed Jun 11, 2021
1 parent 4966f6c commit 3d75f09
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 75 deletions.
2 changes: 1 addition & 1 deletion CodableWrapper.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |spec|
spec.name = "CodableWrapper"
spec.version = "0.1.1"
spec.version = "0.1.2"
spec.requires_arc = true
spec.summary = "Codable + PropertyWrapper"
spec.description = "Codable + PropertyWrapper = ☕"
Expand Down
10 changes: 10 additions & 0 deletions CodableWrapper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
4066815224F375C000562E47 /* DefaultTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4066814E24F375C000562E47 /* DefaultTest.swift */; };
4066815324F375C000562E47 /* FeatureExampleTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4066814F24F375C000562E47 /* FeatureExampleTest.swift */; };
4066815424F375C000562E47 /* BuiltInTransfromsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4066815024F375C000562E47 /* BuiltInTransfromsTest.swift */; };
40C744AD26732F410099FC20 /* CodableWrapper.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 40D7C4D824C05415004522F7 /* CodableWrapper.framework */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -51,6 +52,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
40C744AD26732F410099FC20 /* CodableWrapper.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -105,12 +107,20 @@
path = Tests/CodableWrapperTests;
sourceTree = "<group>";
};
40C744AC26732F410099FC20 /* Frameworks */ = {
isa = PBXGroup;
children = (
);
name = Frameworks;
sourceTree = "<group>";
};
40D7C4BF24C053F3004522F7 = {
isa = PBXGroup;
children = (
4066812824F375AB00562E47 /* CodableWrapper */,
4066814D24F375C000562E47 /* CodableWrapperTests */,
40D7C4CB24C05401004522F7 /* Products */,
40C744AC26732F410099FC20 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down
17 changes: 9 additions & 8 deletions Sources/CodableWrapper/CodableWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public final class CodableWrapper<Value>: Codable {
}
}

init(construct: Construct) {
unsafeCreated = false
self.construct = construct
@available(*, unavailable, message: "directly `@CodableWrapper` only support optional value")
public init() {
fatalError()
}

init(storedValue: Value) {
init(construct: Construct) {
unsafeCreated = false
self.storedValue = storedValue
self.construct = construct
}

fileprivate init(unsafed: ()) {
Expand Down Expand Up @@ -141,9 +141,10 @@ public extension KeyedDecodingContainer {

// Try parse bridged type at first
if let bridge = Value.self as? _BuiltInBridgeType.Type,
let dictionary = _containerDictionary(),
let json = dictionary[key.stringValue],
let bridged = bridge._transform(from: json) as? Value {
let dictionary = _containerDictionary(),
let json = dictionary[key.stringValue],
let bridged = bridge._transform(from: json) as? Value
{
wrapper.storedValue = bridged
}
wrapper.decoderInjetion = injection
Expand Down
12 changes: 6 additions & 6 deletions Sources/CodableWrapper/CodableWrapperInit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

import Foundation

extension CodableWrapper where Value: Codable {
public convenience init(codingKeys: [String] = [], defaultValue: Value) {
public extension CodableWrapper where Value: Codable {
convenience init(codingKeys: [String] = [], defaultValue: Value) {
let construct = Construct(codingKeys: codingKeys, fromNull: { defaultValue }, fromJSON: { _ in .default }, toJSON: { _ in .default })
self.init(construct: construct)
}

public convenience init<Wrapped>(codingKeys: [String] = []) where Value == Wrapped? {
let construct = Construct(codingKeys: codingKeys, fromNull: { Optional<Wrapped>.none }, fromJSON: { _ in .default }, toJSON: { _ in .default })
convenience init<Wrapped>(codingKeys: [String] = []) where Value == Wrapped? {
let construct = Construct(codingKeys: codingKeys, fromNull: { Wrapped?.none }, fromJSON: { _ in .default }, toJSON: { _ in .default })
self.init(construct: construct)
}
}

extension CodableWrapper {
public convenience init<T: TransformType>(codingKeys: [String] = [], transformer: T) where T.Object == Value {
public extension CodableWrapper {
convenience init<T: TransformType>(codingKeys: [String] = [], transformer: T) where T.Object == Value {
let construct = Construct(codingKeys: codingKeys, fromNull: transformer.fromNull, fromJSON: transformer.fromJSON, toJSON: transformer.toJSON)
self.init(construct: construct)
}
Expand Down
60 changes: 0 additions & 60 deletions Sources/CodableWrapper/Literals.swift

This file was deleted.

0 comments on commit 3d75f09

Please sign in to comment.