diff --git a/CodableFirebase.xcodeproj/project.pbxproj b/CodableFirebase.xcodeproj/project.pbxproj index bdcb01b..44ce1a2 100644 --- a/CodableFirebase.xcodeproj/project.pbxproj +++ b/CodableFirebase.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 09D19A4B218D64F900A862A3 /* DecodeStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09D19A4A218D64F900A862A3 /* DecodeStrategy.swift */; }; + 09D19A4D218D650000A862A3 /* EncodeStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09D19A4C218D650000A862A3 /* EncodeStrategy.swift */; }; CE7DD3711F9CFA81000225C5 /* CodableFirebase.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE7DD3671F9CFA81000225C5 /* CodableFirebase.framework */; }; CE7DD3781F9CFA81000225C5 /* CodableFirebase.h in Headers */ = {isa = PBXBuildFile; fileRef = CE7DD36A1F9CFA81000225C5 /* CodableFirebase.h */; settings = {ATTRIBUTES = (Public, ); }; }; CE7DD3831F9D04AE000225C5 /* FirestoreEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE7DD3811F9D04AE000225C5 /* FirestoreEncoder.swift */; }; @@ -31,6 +33,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 09D19A4A218D64F900A862A3 /* DecodeStrategy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecodeStrategy.swift; sourceTree = ""; }; + 09D19A4C218D650000A862A3 /* EncodeStrategy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncodeStrategy.swift; sourceTree = ""; }; CE7DD3671F9CFA81000225C5 /* CodableFirebase.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CodableFirebase.framework; sourceTree = BUILT_PRODUCTS_DIR; }; CE7DD36A1F9CFA81000225C5 /* CodableFirebase.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CodableFirebase.h; sourceTree = ""; }; CE7DD36B1F9CFA81000225C5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -92,6 +96,8 @@ CE7DD3811F9D04AE000225C5 /* FirestoreEncoder.swift */, CEFDBF891FF3E24200745EBE /* FirebaseDecoder.swift */, CEFDBF8B1FF3E3CB00745EBE /* FirebaseEncoder.swift */, + 09D19A4A218D64F900A862A3 /* DecodeStrategy.swift */, + 09D19A4C218D650000A862A3 /* EncodeStrategy.swift */, CEFDBF851FF3B56200745EBE /* Decoder.swift */, CEFDBF811FF3B35B00745EBE /* Encoder.swift */, CE7DD36B1F9CFA81000225C5 /* Info.plist */, @@ -222,8 +228,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 09D19A4D218D650000A862A3 /* EncodeStrategy.swift in Sources */, CE7DD3831F9D04AE000225C5 /* FirestoreEncoder.swift in Sources */, CEFDBF8C1FF3E3CB00745EBE /* FirebaseEncoder.swift in Sources */, + 09D19A4B218D64F900A862A3 /* DecodeStrategy.swift in Sources */, CEFDBF821FF3B35B00745EBE /* Encoder.swift in Sources */, CEFDBF861FF3B56200745EBE /* Decoder.swift in Sources */, CE7DD3841F9D04AE000225C5 /* FirestoreDecoder.swift in Sources */, diff --git a/CodableFirebase/DecodeStrategy.swift b/CodableFirebase/DecodeStrategy.swift new file mode 100644 index 0000000..a91be47 --- /dev/null +++ b/CodableFirebase/DecodeStrategy.swift @@ -0,0 +1,43 @@ +// +// DecodeStrategy.swift +// CodableFirebase +// +// Created by Zitao Xiong on 11/3/18. +// Copyright © 2018 ViolentOctopus. All rights reserved. +// + +import Foundation + +/// The strategy to use for decoding `Date` values. +public enum DateDecodingStrategy { + /// Defer to `Date` for decoding. This is the default strategy. + case deferredToDate + + /// Decode the `Date` as a UNIX timestamp from a JSON number. + case secondsSince1970 + + /// Decode the `Date` as UNIX millisecond timestamp from a JSON number. + case millisecondsSince1970 + + /// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format). + @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) + case iso8601 + + /// Decode the `Date` as a string parsed by the given formatter. + case formatted(DateFormatter) + + /// Decode the `Date` as a custom value decoded by the given closure. + case custom((_ decoder: Decoder) throws -> Date) +} + +/// The strategy to use for decoding `Data` values. +public enum DataDecodingStrategy { + /// Defer to `Data` for decoding. + case deferredToData + + /// Decode the `Data` from a Base64-encoded string. This is the default strategy. + case base64 + + /// Decode the `Data` as a custom value decoded by the given closure. + case custom((_ decoder: Decoder) throws -> Data) +} diff --git a/CodableFirebase/Decoder.swift b/CodableFirebase/Decoder.swift index df4ca1f..27db3ee 100644 --- a/CodableFirebase/Decoder.swift +++ b/CodableFirebase/Decoder.swift @@ -11,8 +11,8 @@ import Foundation class _FirebaseDecoder : Decoder { /// Options set on the top-level encoder to pass down the decoding hierarchy. struct _Options { - let dateDecodingStrategy: FirebaseDecoder.DateDecodingStrategy? - let dataDecodingStrategy: FirebaseDecoder.DataDecodingStrategy? + let dateDecodingStrategy: DateDecodingStrategy? + let dataDecodingStrategy: DataDecodingStrategy? let skipFirestoreTypes: Bool let userInfo: [CodingUserInfoKey : Any] } diff --git a/CodableFirebase/EncodeStrategy.swift b/CodableFirebase/EncodeStrategy.swift new file mode 100644 index 0000000..bca95c9 --- /dev/null +++ b/CodableFirebase/EncodeStrategy.swift @@ -0,0 +1,47 @@ +// +// EncodeStrategy.swift +// CodableFirebase +// +// Created by Zitao Xiong on 11/3/18. +// Copyright © 2018 ViolentOctopus. All rights reserved. +// + +import Foundation + +/// The strategy to use for encoding `Date` values. +public enum DateEncodingStrategy { + /// Defer to `Date` for choosing an encoding. This is the default strategy. + case deferredToDate + + /// Encode the `Date` as a UNIX timestamp (as a JSON number). + case secondsSince1970 + + /// Encode the `Date` as UNIX millisecond timestamp (as a JSON number). + case millisecondsSince1970 + + /// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format). + @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) + case iso8601 + + /// Encode the `Date` as a string formatted by the given formatter. + case formatted(DateFormatter) + + /// Encode the `Date` as a custom value encoded by the given closure. + /// + /// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place. + case custom((Date, Encoder) throws -> Void) +} + +/// The strategy to use for encoding `Data` values. +public enum DataEncodingStrategy { + /// Defer to `Data` for choosing an encoding. + case deferredToData + + /// Encoded the `Data` as a Base64-encoded string. This is the default strategy. + case base64 + + /// Encode the `Data` as a custom value encoded by the given closure. + /// + /// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place. + case custom((Data, Encoder) throws -> Void) +} diff --git a/CodableFirebase/Encoder.swift b/CodableFirebase/Encoder.swift index 69fe0ef..a5f061b 100644 --- a/CodableFirebase/Encoder.swift +++ b/CodableFirebase/Encoder.swift @@ -11,8 +11,8 @@ import Foundation class _FirebaseEncoder : Encoder { /// Options set on the top-level encoder to pass down the encoding hierarchy. struct _Options { - let dateEncodingStrategy: FirebaseEncoder.DateEncodingStrategy? - let dataEncodingStrategy: FirebaseEncoder.DataEncodingStrategy? + let dateEncodingStrategy: DateEncodingStrategy? + let dataEncodingStrategy: DataEncodingStrategy? let skipFirestoreTypes: Bool let userInfo: [CodingUserInfoKey : Any] } diff --git a/CodableFirebase/FirebaseDecoder.swift b/CodableFirebase/FirebaseDecoder.swift index a6a20f0..c130531 100644 --- a/CodableFirebase/FirebaseDecoder.swift +++ b/CodableFirebase/FirebaseDecoder.swift @@ -9,40 +9,6 @@ import Foundation open class FirebaseDecoder { - /// The strategy to use for decoding `Date` values. - public enum DateDecodingStrategy { - /// Defer to `Date` for decoding. This is the default strategy. - case deferredToDate - - /// Decode the `Date` as a UNIX timestamp from a JSON number. - case secondsSince1970 - - /// Decode the `Date` as UNIX millisecond timestamp from a JSON number. - case millisecondsSince1970 - - /// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format). - @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) - case iso8601 - - /// Decode the `Date` as a string parsed by the given formatter. - case formatted(DateFormatter) - - /// Decode the `Date` as a custom value decoded by the given closure. - case custom((_ decoder: Decoder) throws -> Date) - } - - /// The strategy to use for decoding `Data` values. - public enum DataDecodingStrategy { - /// Defer to `Data` for decoding. - case deferredToData - - /// Decode the `Data` from a Base64-encoded string. This is the default strategy. - case base64 - - /// Decode the `Data` as a custom value decoded by the given closure. - case custom((_ decoder: Decoder) throws -> Data) - } - public init() {} open var userInfo: [CodingUserInfoKey : Any] = [:] diff --git a/CodableFirebase/FirebaseEncoder.swift b/CodableFirebase/FirebaseEncoder.swift index 49f79fc..269e922 100644 --- a/CodableFirebase/FirebaseEncoder.swift +++ b/CodableFirebase/FirebaseEncoder.swift @@ -9,44 +9,6 @@ import Foundation open class FirebaseEncoder { - /// The strategy to use for encoding `Date` values. - public enum DateEncodingStrategy { - /// Defer to `Date` for choosing an encoding. This is the default strategy. - case deferredToDate - - /// Encode the `Date` as a UNIX timestamp (as a JSON number). - case secondsSince1970 - - /// Encode the `Date` as UNIX millisecond timestamp (as a JSON number). - case millisecondsSince1970 - - /// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format). - @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) - case iso8601 - - /// Encode the `Date` as a string formatted by the given formatter. - case formatted(DateFormatter) - - /// Encode the `Date` as a custom value encoded by the given closure. - /// - /// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place. - case custom((Date, Encoder) throws -> Void) - } - - /// The strategy to use for encoding `Data` values. - public enum DataEncodingStrategy { - /// Defer to `Data` for choosing an encoding. - case deferredToData - - /// Encoded the `Data` as a Base64-encoded string. This is the default strategy. - case base64 - - /// Encode the `Data` as a custom value encoded by the given closure. - /// - /// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place. - case custom((Data, Encoder) throws -> Void) - } - public init() {} open var userInfo: [CodingUserInfoKey : Any] = [:]