diff --git a/README.md b/README.md index 5618f9e..2da0241 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,10 @@ [![Cocoapods](https://img.shields.io/cocoapods/v/ExCodable.svg)](https://cocoapods.org/pods/ExCodable)
[![LICENSE](https://img.shields.io/github/license/ExCodable/ExCodable.svg)](https://github.com/ExCodable/ExCodable/blob/master/LICENSE) +[![GitHub stars](https://badgen.net/github/stars/ExCodable/ExCodable)](https://github.com/ExCodable/ExCodable/stargazers/) [![@minglq](https://img.shields.io/twitter/url?url=https%3A%2F%2Fgithub.com%2Fiwill%2FExCodable)](https://twitter.com/minglq) -En | [中文](https://iwill.im/ExCodable/) +En | [中文](https://excodable.iwill.im/) ## What's New in ExCodable 1.0 @@ -66,6 +67,8 @@ struct TestExCodable: ExAutoCodable { ## Usage + + ### 1. ExCodable `ExCodable` requires declaring properties with `var` and provide default values. @@ -191,7 +196,7 @@ struct TestStructWithEnum: ExAutoCodable { ```swift struct TestManualEncodeDecode: ExAutoCodable { - @ExCodable("int", encode: { encoder, value in + @ExCodable(encode: { encoder, value in encoder["int"] = value <= 0 ? 0 : value }, decode: { decoder in if let int: Int = decoder["int"], int > 0 { @@ -235,10 +240,9 @@ Custom type conversions for specific properties: ```swift struct TestCustomEncodeDecode: ExAutoCodable { - @ExCodable("int", decode: { decoder in - if let string: String = decoder["string"], - let int = Int(string) { - return int + @ExCodable(decode: { decoder in + if let string: String = decoder["string"] { + return string.count } return 0 }) private(set) @@ -251,9 +255,9 @@ Custom type conversions for specific model: ```swift struct TestCustomTypeConverter: ExAutoCodable { - @ExCodable("doubleFromBool") private(set) + @ExCodable private(set) var doubleFromBool: Double? = nil - @ExCodable("floatFromBool") private(set) + @ExCodable private(set) var floatFromBool: Double? = nil } @@ -355,7 +359,7 @@ ExCodable also supports throw errors: ```swift struct TestNonnullAndThrows: ExAutoCodable { - @ExCodable("int", nonnull: true, throws: true) private(set) + @ExCodable(nonnull: true, throws: true) private(set) var int: Int! = 0 } @@ -394,9 +398,9 @@ class TestSubclass: TestClass { ```swift struct TestStruct: ExAutoCodable, Equatable { - @ExCodable("int") private(set) + @ExCodable private(set) var int: Int = 0 - @ExCodable("string") private(set) + @ExCodable private(set) var string: String? = nil } @@ -408,6 +412,8 @@ let copy = try? dict.decoded() as TestStruct ``` +> See the tests for more examples. + ## Requirements - iOS 12.0+ | tvOS 12.0+ | macOS 11.0+ | watchOS 4.0+ @@ -434,7 +440,9 @@ pod 'ExCodable', '~> 1.0.0' ### 0.x to 1.x -Quickly, but **DEPRECATED**: +When you update to ExCodable 1.0. + +Step 1: Update your code to use the old API - **DEPRECATED** but quick. - Replace protocol `ExCodable` with `ExCodableDEPRECATED`. - Add `static` to func `decodeForTypeConversion(_:codingKey:as:)` of protocol `KeyedDecodingContainerCustomTypeConversion`. @@ -457,7 +465,7 @@ extension TestExCodable: ExCodableDEPRECATED { ``` -Upgrade, SUGGESTED: +Step 2: Upgrade your models to the new API one by one - SUGGESTED: - Replace `protocol` `ExCodable` with `ExAutoCodable`. - Remove initializer `init(from decoder: Decoder) throws`. @@ -478,7 +486,7 @@ struct TestExCodable: ExAutoCodable { ## Stars - Star Chart + Star Chart Hope ExCodable will help you! [Make a star](https://github.com/ExCodable/ExCodable/#repository-container-header) ⭐️ 🤩 diff --git a/Tests/ExCodableTests/ExCodableTests.swift b/Tests/ExCodableTests/ExCodableTests.swift index bee26a4..0626569 100644 --- a/Tests/ExCodableTests/ExCodableTests.swift +++ b/Tests/ExCodableTests/ExCodableTests.swift @@ -11,6 +11,15 @@ import XCTest // @testable import ExCodable +// MARK: ExCodable + +struct TestExCodable: ExAutoCodable, Equatable { + @ExCodable private(set) + var int: Int = 0 + @ExCodable private(set) + var string: String? = nil +} + // MARK: auto codable struct TestAutoCodable: Codable, Equatable { @@ -96,7 +105,7 @@ enum TestEnum: Int, Codable { } struct TestStructWithEnum: ExAutoCodable, Equatable { - @ExCodable("enum") private(set) + @ExCodable private(set) var `enum` = TestEnum.zero } @@ -136,7 +145,7 @@ fileprivate func message(for int: Int) -> String { struct TestCustomEncodeDecode: ExAutoCodable, Equatable { - @ExCodable("int", encode: { encoder, value in + @ExCodable(encode: { encoder, value in encoder["int"] = value <= 0 ? 0 : value }, decode: { decoder in if let int: Int = decoder["int"], int > 0 { @@ -199,9 +208,9 @@ extension TestSubscript: Codable { // MARK: type-conversions struct TestTypeConversion: ExAutoCodable, Equatable { - @ExCodable("intFromString") private(set) + @ExCodable private(set) var intFromString: Int? = nil - @ExCodable("stringFromInt") private(set) + @ExCodable private(set) var stringFromInt: String??? = nil } @@ -263,9 +272,9 @@ extension TestTypeConversions: Encodable, Decodable { // MARK: custom type-conversions struct TestCustomTypeConverter: ExAutoCodable, Equatable { - @ExCodable("doubleFromBool") private(set) + @ExCodable private(set) var doubleFromBool: Double? = nil - @ExCodable("boolFromDouble") private(set) + @ExCodable private(set) var boolFromDouble: Bool? = nil } @@ -374,15 +383,6 @@ struct TestNonnullWithThrows: ExAutoCodable, Equatable { var testThrows: TestThrows! = nil } -// MARK: ExCodable - -struct TestExCodable: ExAutoCodable, Equatable { - @ExCodable("int") private(set) - var int: Int = 0 - @ExCodable("string") private(set) - var string: String? = nil -} - // MARK: - Tests final class ExCodableTests: XCTestCase {