杨锴
2025-03-11 90dc3329d1973fda691e357cf4523d5c7c67fa1d
Pods/Alamofire/Source/Features/URLEncodedFormEncoder.swift
@@ -72,10 +72,10 @@
        /// - Returns:   The encoded key.
        func encode(_ key: String, atIndex index: Int) -> String {
            switch self {
            case .brackets: return "\(key)[]"
            case .noBrackets: return key
            case .indexInBrackets: return "\(key)[\(index)]"
            case let .custom(encoding): return encoding(key, index)
            case .brackets: "\(key)[]"
            case .noBrackets: key
            case .indexInBrackets: "\(key)[\(index)]"
            case let .custom(encoding): encoding(key, index)
            }
        }
    }
@@ -94,8 +94,8 @@
        /// - Returns:         The encoded `String`.
        func encode(_ value: Bool) -> String {
            switch self {
            case .numeric: return value ? "1" : "0"
            case .literal: return value ? "true" : "false"
            case .numeric: value ? "1" : "0"
            case .literal: value ? "true" : "false"
            }
        }
    }
@@ -117,9 +117,9 @@
        ///                   `Encodable` implementation.
        func encode(_ data: Data) throws -> String? {
            switch self {
            case .deferredToData: return nil
            case .base64: return data.base64EncodedString()
            case let .custom(encoding): return try encoding(data)
            case .deferredToData: nil
            case .base64: data.base64EncodedString()
            case let .custom(encoding): try encoding(data)
            }
        }
    }
@@ -127,11 +127,11 @@
    /// Encoding to use for `Date` values.
    public enum DateEncoding {
        /// ISO8601 and RFC3339 formatter.
        private static let iso8601Formatter: ISO8601DateFormatter = {
        private static let iso8601Formatter = Protected<ISO8601DateFormatter>({
            let formatter = ISO8601DateFormatter()
            formatter.formatOptions = .withInternetDateTime
            return formatter
        }()
        }())
        /// Defers encoding to the `Date` type. This is the default encoding.
        case deferredToDate
@@ -155,17 +155,17 @@
        func encode(_ date: Date) throws -> String? {
            switch self {
            case .deferredToDate:
                return nil
                nil
            case .secondsSince1970:
                return String(date.timeIntervalSince1970)
                String(date.timeIntervalSince1970)
            case .millisecondsSince1970:
                return String(date.timeIntervalSince1970 * 1000.0)
                String(date.timeIntervalSince1970 * 1000.0)
            case .iso8601:
                return DateEncoding.iso8601Formatter.string(from: date)
                DateEncoding.iso8601Formatter.read { $0.string(from: date) }
            case let .formatted(formatter):
                return formatter.string(from: date)
                formatter.string(from: date)
            case let .custom(closure):
                return try closure(date)
                try closure(date)
            }
        }
    }
@@ -213,13 +213,13 @@
        func encode(_ key: String) -> String {
            switch self {
            case .useDefaultKeys: return key
            case .convertToSnakeCase: return convertToSnakeCase(key)
            case .convertToKebabCase: return convertToKebabCase(key)
            case .capitalized: return String(key.prefix(1).uppercased() + key.dropFirst())
            case .uppercased: return key.uppercased()
            case .lowercased: return key.lowercased()
            case let .custom(encoding): return encoding(key)
            case .useDefaultKeys: key
            case .convertToSnakeCase: convertToSnakeCase(key)
            case .convertToKebabCase: convertToKebabCase(key)
            case .capitalized: String(key.prefix(1).uppercased() + key.dropFirst())
            case .uppercased: key.uppercased()
            case .lowercased: key.lowercased()
            case let .custom(encoding): encoding(key)
            }
        }
@@ -295,18 +295,18 @@
    ///
    /// This encoding affects how the `parent`, `child`, `grandchild` path is encoded. Brackets are used by default.
    /// e.g. `parent[child][grandchild]=value`.
    public struct KeyPathEncoding {
    public struct KeyPathEncoding: Sendable {
        /// Encodes key paths by wrapping each component in brackets. e.g. `parent[child][grandchild]`.
        public static let brackets = KeyPathEncoding { "[\($0)]" }
        /// Encodes key paths by separating each component with dots. e.g. `parent.child.grandchild`.
        public static let dots = KeyPathEncoding { ".\($0)" }
        private let encoding: (_ subkey: String) -> String
        private let encoding: @Sendable (_ subkey: String) -> String
        /// Creates an instance with the encoding closure called for each sub-key in a key path.
        ///
        /// - Parameter encoding: Closure used to perform the encoding.
        public init(encoding: @escaping (_ subkey: String) -> String) {
        public init(encoding: @escaping @Sendable (_ subkey: String) -> String) {
            self.encoding = encoding
        }
@@ -316,7 +316,7 @@
    }
    /// Encoding to use for `nil` values.
    public struct NilEncoding {
    public struct NilEncoding: Sendable {
        /// Encodes `nil` by dropping the entire key / value pair.
        public static let dropKey = NilEncoding { nil }
        /// Encodes `nil` by dropping only the value. e.g. `value1=one&nilValue=&value2=two`.
@@ -324,12 +324,12 @@
        /// Encodes `nil` as `null`.
        public static let null = NilEncoding { "null" }
        private let encoding: () -> String?
        private let encoding: @Sendable () -> String?
        /// Creates an instance with the encoding closure called for `nil` values.
        ///
        /// - Parameter encoding: Closure used to perform the encoding.
        public init(encoding: @escaping () -> String?) {
        public init(encoding: @escaping @Sendable () -> String?) {
            self.encoding = encoding
        }
@@ -352,8 +352,8 @@
        /// - Returns:          The encoded `String`.
        func encode(_ string: String) -> String {
            switch self {
            case .percentEscaped: return string.replacingOccurrences(of: " ", with: "%20")
            case .plusReplaced: return string.replacingOccurrences(of: " ", with: "+")
            case .percentEscaped: string.replacingOccurrences(of: " ", with: "%20")
            case .plusReplaced: string.replacingOccurrences(of: " ", with: "+")
            }
        }
    }
@@ -366,7 +366,7 @@
        var localizedDescription: String {
            switch self {
            case let .invalidRootObject(object):
                return "URLEncodedFormEncoder requires keyed root object. Received \(object) instead."
                "URLEncodedFormEncoder requires keyed root object. Received \(object) instead."
            }
        }
    }
@@ -431,7 +431,7 @@
        self.allowedCharacters = allowedCharacters
    }
    func encode(_ value: Encodable) throws -> URLEncodedFormComponent {
    func encode(_ value: any Encodable) throws -> URLEncodedFormComponent {
        let context = URLEncodedFormContext(.object([]))
        let encoder = _URLEncodedFormEncoder(context: context,
                                             boolEncoding: boolEncoding,
@@ -449,7 +449,7 @@
    ///
    /// - Returns:         The encoded `String`.
    /// - Throws:          An `Error` or `EncodingError` instance if encoding fails.
    public func encode(_ value: Encodable) throws -> String {
    public func encode(_ value: any Encodable) throws -> String {
        let component: URLEncodedFormComponent = try encode(value)
        guard case let .object(object) = component else {
@@ -475,7 +475,7 @@
    /// - Returns:         The encoded `Data`.
    ///
    /// - Throws:          An `Error` or `EncodingError` instance if encoding fails.
    public func encode(_ value: Encodable) throws -> Data {
    public func encode(_ value: any Encodable) throws -> Data {
        let string: String = try encode(value)
        return Data(string.utf8)
@@ -483,7 +483,7 @@
}
final class _URLEncodedFormEncoder {
    var codingPath: [CodingKey]
    var codingPath: [any CodingKey]
    // Returns an empty dictionary, as this encoder doesn't support userInfo.
    var userInfo: [CodingUserInfoKey: Any] { [:] }
@@ -495,7 +495,7 @@
    private let nilEncoding: URLEncodedFormEncoder.NilEncoding
    init(context: URLEncodedFormContext,
         codingPath: [CodingKey] = [],
         codingPath: [any CodingKey] = [],
         boolEncoding: URLEncodedFormEncoder.BoolEncoding,
         dataEncoding: URLEncodedFormEncoder.DataEncoding,
         dateEncoding: URLEncodedFormEncoder.DateEncoding,
@@ -520,7 +520,7 @@
        return KeyedEncodingContainer(container)
    }
    func unkeyedContainer() -> UnkeyedEncodingContainer {
    func unkeyedContainer() -> any UnkeyedEncodingContainer {
        _URLEncodedFormEncoder.UnkeyedContainer(context: context,
                                                codingPath: codingPath,
                                                boolEncoding: boolEncoding,
@@ -529,7 +529,7 @@
                                                nilEncoding: nilEncoding)
    }
    func singleValueContainer() -> SingleValueEncodingContainer {
    func singleValueContainer() -> any SingleValueEncodingContainer {
        _URLEncodedFormEncoder.SingleValueContainer(context: context,
                                                    codingPath: codingPath,
                                                    boolEncoding: boolEncoding,
@@ -557,16 +557,16 @@
    /// Converts self to an `[URLEncodedFormData]` or returns `nil` if not convertible.
    var array: [URLEncodedFormComponent]? {
        switch self {
        case let .array(array): return array
        default: return nil
        case let .array(array): array
        default: nil
        }
    }
    /// Converts self to an `Object` or returns `nil` if not convertible.
    var object: Object? {
        switch self {
        case let .object(object): return object
        default: return nil
        case let .object(object): object
        default: nil
        }
    }
@@ -577,12 +577,12 @@
    /// - parameters:
    ///     - value: Value of `Self` to set at the supplied path.
    ///     - path: `CodingKey` path to update with the supplied value.
    public mutating func set(to value: URLEncodedFormComponent, at path: [CodingKey]) {
    public mutating func set(to value: URLEncodedFormComponent, at path: [any CodingKey]) {
        set(&self, to: value, at: path)
    }
    /// Recursive backing method to `set(to:at:)`.
    private func set(_ context: inout URLEncodedFormComponent, to value: URLEncodedFormComponent, at path: [CodingKey]) {
    private func set(_ context: inout URLEncodedFormComponent, to value: URLEncodedFormComponent, at path: [any CodingKey]) {
        guard !path.isEmpty else {
            context = value
            return
@@ -660,7 +660,7 @@
extension _URLEncodedFormEncoder {
    final class KeyedContainer<Key> where Key: CodingKey {
        var codingPath: [CodingKey]
        var codingPath: [any CodingKey]
        private let context: URLEncodedFormContext
        private let boolEncoding: URLEncodedFormEncoder.BoolEncoding
@@ -669,7 +669,7 @@
        private let nilEncoding: URLEncodedFormEncoder.NilEncoding
        init(context: URLEncodedFormContext,
             codingPath: [CodingKey],
             codingPath: [any CodingKey],
             boolEncoding: URLEncodedFormEncoder.BoolEncoding,
             dataEncoding: URLEncodedFormEncoder.DataEncoding,
             dateEncoding: URLEncodedFormEncoder.DateEncoding,
@@ -682,7 +682,7 @@
            self.nilEncoding = nilEncoding
        }
        private func nestedCodingPath(for key: CodingKey) -> [CodingKey] {
        private func nestedCodingPath(for key: any CodingKey) -> [any CodingKey] {
            codingPath + [key]
        }
    }
@@ -768,7 +768,7 @@
        try container.encode(value)
    }
    func nestedSingleValueEncoder(for key: Key) -> SingleValueEncodingContainer {
    func nestedSingleValueEncoder(for key: Key) -> any SingleValueEncodingContainer {
        let container = _URLEncodedFormEncoder.SingleValueContainer(context: context,
                                                                    codingPath: nestedCodingPath(for: key),
                                                                    boolEncoding: boolEncoding,
@@ -779,7 +779,7 @@
        return container
    }
    func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
    func nestedUnkeyedContainer(forKey key: Key) -> any UnkeyedEncodingContainer {
        let container = _URLEncodedFormEncoder.UnkeyedContainer(context: context,
                                                                codingPath: nestedCodingPath(for: key),
                                                                boolEncoding: boolEncoding,
@@ -801,7 +801,7 @@
        return KeyedEncodingContainer(container)
    }
    func superEncoder() -> Encoder {
    func superEncoder() -> any Encoder {
        _URLEncodedFormEncoder(context: context,
                               codingPath: codingPath,
                               boolEncoding: boolEncoding,
@@ -810,7 +810,7 @@
                               nilEncoding: nilEncoding)
    }
    func superEncoder(forKey key: Key) -> Encoder {
    func superEncoder(forKey key: Key) -> any Encoder {
        _URLEncodedFormEncoder(context: context,
                               codingPath: nestedCodingPath(for: key),
                               boolEncoding: boolEncoding,
@@ -822,7 +822,7 @@
extension _URLEncodedFormEncoder {
    final class SingleValueContainer {
        var codingPath: [CodingKey]
        var codingPath: [any CodingKey]
        private var canEncodeNewValue = true
@@ -833,7 +833,7 @@
        private let nilEncoding: URLEncodedFormEncoder.NilEncoding
        init(context: URLEncodedFormContext,
             codingPath: [CodingKey],
             codingPath: [any CodingKey],
             boolEncoding: URLEncodedFormEncoder.BoolEncoding,
             dataEncoding: URLEncodedFormEncoder.DataEncoding,
             dateEncoding: URLEncodedFormEncoder.DateEncoding,
@@ -966,10 +966,10 @@
extension _URLEncodedFormEncoder {
    final class UnkeyedContainer {
        var codingPath: [CodingKey]
        var codingPath: [any CodingKey]
        var count = 0
        var nestedCodingPath: [CodingKey] {
        var nestedCodingPath: [any CodingKey] {
            codingPath + [AnyCodingKey(intValue: count)!]
        }
@@ -980,7 +980,7 @@
        private let nilEncoding: URLEncodedFormEncoder.NilEncoding
        init(context: URLEncodedFormContext,
             codingPath: [CodingKey],
             codingPath: [any CodingKey],
             boolEncoding: URLEncodedFormEncoder.BoolEncoding,
             dataEncoding: URLEncodedFormEncoder.DataEncoding,
             dateEncoding: URLEncodedFormEncoder.DateEncoding,
@@ -1007,7 +1007,7 @@
        try container.encode(value)
    }
    func nestedSingleValueContainer() -> SingleValueEncodingContainer {
    func nestedSingleValueContainer() -> any SingleValueEncodingContainer {
        defer { count += 1 }
        return _URLEncodedFormEncoder.SingleValueContainer(context: context,
@@ -1030,7 +1030,7 @@
        return KeyedEncodingContainer(container)
    }
    func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
    func nestedUnkeyedContainer() -> any UnkeyedEncodingContainer {
        defer { count += 1 }
        return _URLEncodedFormEncoder.UnkeyedContainer(context: context,
@@ -1041,7 +1041,7 @@
                                                       nilEncoding: nilEncoding)
    }
    func superEncoder() -> Encoder {
    func superEncoder() -> any Encoder {
        defer { count += 1 }
        return _URLEncodedFormEncoder(context: context,
@@ -1088,9 +1088,9 @@
    func serialize(_ component: URLEncodedFormComponent, forKey key: String) -> String {
        switch component {
        case let .string(string): return "\(escape(keyEncoding.encode(key)))=\(escape(string))"
        case let .array(array): return serialize(array, forKey: key)
        case let .object(object): return serialize(object, forKey: key)
        case let .string(string): "\(escape(keyEncoding.encode(key)))=\(escape(string))"
        case let .array(array): serialize(array, forKey: key)
        case let .object(object): serialize(object, forKey: key)
        }
    }