Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
swift-urlqueryitem-coder Sources URLQueryItemCoder Encoder Generic Implementation EncodingContainer+SingleValue.swift
4.1 kB · 153 lines
Swift
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154//// EncodingContainer+SingleValue.swift// URLQueryItemCoder//// Created by Kyle Hughes on 1/15/23.//
import Foundation
extension EncodingContainer { internal final class SingleValue { internal let codingPath: [any CodingKey] internal private(set) var storage: Storage? private let configuration: EncodingStrategies // MARK: Internal Initialization internal init(codingPath: [any CodingKey], configuration: EncodingStrategies) { self.codingPath = codingPath self.configuration = configuration storage = nil } // MARK: Private Instance Interface private func preconditionCanEncodeNewValue() { precondition( storage == nil, "A value was already encoded through the single value container." ) } }}
// MARK: - SingleValueEncodingContainer Extension
extension EncodingContainer.SingleValue: SingleValueEncodingContainer { // MARK: Internal Instance Interface internal func encode(_ value: Bool) throws { preconditionCanEncodeNewValue() storage = .primitive(.bool(value)) } internal func encode(_ value: Double) throws { preconditionCanEncodeNewValue() storage = try .primitive( configuration.nonConformingFloatStrategy.encode(value, at: codingPath, using: configuration) ) } internal func encode(_ value: Float) throws { preconditionCanEncodeNewValue() storage = try .primitive( configuration.nonConformingFloatStrategy.encode(value, at: codingPath, using: configuration) ) } internal func encode(_ value: Int) throws { preconditionCanEncodeNewValue() storage = .primitive(.int(value)) } internal func encode(_ value: Int8) throws { preconditionCanEncodeNewValue() storage = .primitive(.int8(value)) } internal func encode(_ value: Int16) throws { preconditionCanEncodeNewValue() storage = .primitive(.int16(value)) } internal func encode(_ value: Int32) throws { preconditionCanEncodeNewValue() storage = .primitive(.int32(value)) } internal func encode(_ value: Int64) throws { preconditionCanEncodeNewValue() storage = .primitive(.int64(value)) } internal func encode(_ value: String) throws { preconditionCanEncodeNewValue() storage = .primitive(.string(value)) } internal func encode(_ value: UInt) throws { preconditionCanEncodeNewValue() storage = .primitive(.uint(value)) } internal func encode(_ value: UInt8) throws { preconditionCanEncodeNewValue() storage = .primitive(.uint8(value)) } internal func encode(_ value: UInt16) throws { preconditionCanEncodeNewValue() storage = .primitive(.uint16(value)) } internal func encode(_ value: UInt32) throws { preconditionCanEncodeNewValue() storage = .primitive(.uint32(value)) } internal func encode(_ value: UInt64) throws { preconditionCanEncodeNewValue() storage = .primitive(.uint64(value)) } internal func encode<T>(_ value: T) throws where T: Encodable { preconditionCanEncodeNewValue() let container = try EncodingContainer.encodeWithSpecialTreatment(value, at: codingPath, using: configuration) storage = .container(container) } internal func encodeNil() throws { preconditionCanEncodeNewValue() storage = .primitive(nil) }}
// MARK: - Container.SingleValue.Storage Definition
extension EncodingContainer.SingleValue { internal enum Storage { case container(EncodingContainer) case primitive(EncodingPrimitiveValue?) }}