this repo has no description
1//
2// EncodingContainer+SingleValue.swift
3// URLQueryItemCoder
4//
5// Created by Kyle Hughes on 1/15/23.
6//
7
8import Foundation
9
10extension EncodingContainer {
11 internal final class SingleValue {
12 internal let codingPath: [any CodingKey]
13
14 internal private(set) var storage: Storage?
15
16 private let configuration: EncodingStrategies
17
18 // MARK: Internal Initialization
19
20 internal init(codingPath: [any CodingKey], configuration: EncodingStrategies) {
21 self.codingPath = codingPath
22 self.configuration = configuration
23
24 storage = nil
25 }
26
27 // MARK: Private Instance Interface
28
29 private func preconditionCanEncodeNewValue() {
30 precondition(
31 storage == nil,
32 "A value was already encoded through the single value container."
33 )
34 }
35 }
36}
37
38// MARK: - SingleValueEncodingContainer Extension
39
40extension EncodingContainer.SingleValue: SingleValueEncodingContainer {
41 // MARK: Internal Instance Interface
42
43 internal func encode(_ value: Bool) throws {
44 preconditionCanEncodeNewValue()
45
46 storage = .primitive(.bool(value))
47 }
48
49 internal func encode(_ value: Double) throws {
50 preconditionCanEncodeNewValue()
51
52 storage = try .primitive(
53 configuration.nonConformingFloatStrategy.encode(value, at: codingPath, using: configuration)
54 )
55 }
56
57 internal func encode(_ value: Float) throws {
58 preconditionCanEncodeNewValue()
59
60 storage = try .primitive(
61 configuration.nonConformingFloatStrategy.encode(value, at: codingPath, using: configuration)
62 )
63 }
64
65 internal func encode(_ value: Int) throws {
66 preconditionCanEncodeNewValue()
67
68 storage = .primitive(.int(value))
69 }
70
71 internal func encode(_ value: Int8) throws {
72 preconditionCanEncodeNewValue()
73
74 storage = .primitive(.int8(value))
75 }
76
77 internal func encode(_ value: Int16) throws {
78 preconditionCanEncodeNewValue()
79
80 storage = .primitive(.int16(value))
81 }
82
83 internal func encode(_ value: Int32) throws {
84 preconditionCanEncodeNewValue()
85
86 storage = .primitive(.int32(value))
87 }
88
89 internal func encode(_ value: Int64) throws {
90 preconditionCanEncodeNewValue()
91
92 storage = .primitive(.int64(value))
93 }
94
95 internal func encode(_ value: String) throws {
96 preconditionCanEncodeNewValue()
97
98 storage = .primitive(.string(value))
99 }
100
101 internal func encode(_ value: UInt) throws {
102 preconditionCanEncodeNewValue()
103
104 storage = .primitive(.uint(value))
105 }
106
107 internal func encode(_ value: UInt8) throws {
108 preconditionCanEncodeNewValue()
109
110 storage = .primitive(.uint8(value))
111 }
112
113 internal func encode(_ value: UInt16) throws {
114 preconditionCanEncodeNewValue()
115
116 storage = .primitive(.uint16(value))
117 }
118
119 internal func encode(_ value: UInt32) throws {
120 preconditionCanEncodeNewValue()
121
122 storage = .primitive(.uint32(value))
123 }
124
125 internal func encode(_ value: UInt64) throws {
126 preconditionCanEncodeNewValue()
127
128 storage = .primitive(.uint64(value))
129 }
130
131 internal func encode<T>(_ value: T) throws where T: Encodable {
132 preconditionCanEncodeNewValue()
133
134 let container = try EncodingContainer.encodeWithSpecialTreatment(value, at: codingPath, using: configuration)
135
136 storage = .container(container)
137 }
138
139 internal func encodeNil() throws {
140 preconditionCanEncodeNewValue()
141
142 storage = .primitive(nil)
143 }
144}
145
146// MARK: - Container.SingleValue.Storage Definition
147
148extension EncodingContainer.SingleValue {
149 internal enum Storage {
150 case container(EncodingContainer)
151 case primitive(EncodingPrimitiveValue?)
152 }
153}