Coverage Report

Created: 2026-05-16 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/swift-protobuf/Sources/SwiftProtobuf/Google_Protobuf_Wrappers+Extensions.swift
Line
Count
Source
1
// Sources/SwiftProtobuf/Google_Protobuf_Wrappers+Extensions.swift - Well-known wrapper type extensions
2
//
3
// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
4
// Licensed under Apache License v2.0 with Runtime Library Exception
5
//
6
// See LICENSE.txt for license information:
7
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
8
//
9
// -----------------------------------------------------------------------------
10
///
11
/// Extensions to the well-known types in wrapper.proto that customize the JSON
12
/// format of those messages and provide convenience initializers from literals.
13
///
14
// -----------------------------------------------------------------------------
15
16
#if canImport(FoundationEssentials)
17
import FoundationEssentials
18
#else
19
import Foundation
20
#endif
21
22
/// Internal protocol that minimizes the code duplication across the multiple
23
/// wrapper types extended below.
24
protocol ProtobufWrapper {
25
26
    /// The wrapped protobuf type (for example, `ProtobufDouble`).
27
    associatedtype WrappedType: FieldType
28
29
    /// Exposes the generated property to the extensions here.
30
    var value: WrappedType.BaseType { get set }
31
32
    /// Exposes the parameterless initializer to the extensions here.
33
    init()
34
35
    /// Creates a new instance of the wrapper with the given value.
36
    init(_ value: WrappedType.BaseType)
37
}
38
39
extension ProtobufWrapper {
40
96.3k
    mutating func decodeJSON(from decoder: inout JSONDecoder) throws {
41
96.3k
        var v: WrappedType.BaseType?
42
96.3k
        try WrappedType.decodeSingular(value: &v, from: &decoder)
43
95.9k
        value = v ?? WrappedType.proto3DefaultValue
44
95.9k
    }
45
}
46
47
extension Google_Protobuf_DoubleValue:
48
    ProtobufWrapper, ExpressibleByFloatLiteral, _CustomJSONCodable
49
{
50
51
    public typealias WrappedType = ProtobufDouble
52
    public typealias FloatLiteralType = WrappedType.BaseType
53
54
0
    public init(_ value: WrappedType.BaseType) {
55
0
        self.init()
56
0
        self.value = value
57
0
    }
58
59
0
    public init(floatLiteral: FloatLiteralType) {
60
0
        self.init(floatLiteral)
61
0
    }
62
63
328
    func encodedJSONString(options: JSONEncodingOptions) throws -> String {
64
328
        if value.isFinite {
65
328
            // Swift 4.2 and later guarantees that this is accurate
66
328
            // enough to parse back to the exact value on the other end.
67
328
            return value.description
68
328
        } else {
69
0
            // Protobuf-specific handling of NaN and infinities
70
0
            var encoder = JSONEncoder()
71
0
            encoder.putDoubleValue(value: value)
72
0
            return encoder.stringResult
73
0
        }
74
328
    }
75
}
76
77
extension Google_Protobuf_FloatValue:
78
    ProtobufWrapper, ExpressibleByFloatLiteral, _CustomJSONCodable
79
{
80
81
    public typealias WrappedType = ProtobufFloat
82
    public typealias FloatLiteralType = Float
83
84
0
    public init(_ value: WrappedType.BaseType) {
85
0
        self.init()
86
0
        self.value = value
87
0
    }
88
89
0
    public init(floatLiteral: FloatLiteralType) {
90
0
        self.init(floatLiteral)
91
0
    }
92
93
281
    func encodedJSONString(options: JSONEncodingOptions) throws -> String {
94
281
        if value.isFinite {
95
281
            // Swift 4.2 and later guarantees that this is accurate
96
281
            // enough to parse back to the exact value on the other end.
97
281
            return value.description
98
281
        } else {
99
0
            // Protobuf-specific handling of NaN and infinities
100
0
            var encoder = JSONEncoder()
101
0
            encoder.putFloatValue(value: value)
102
0
            return encoder.stringResult
103
0
        }
104
281
    }
105
}
106
107
extension Google_Protobuf_Int64Value:
108
    ProtobufWrapper, ExpressibleByIntegerLiteral, _CustomJSONCodable
109
{
110
111
    public typealias WrappedType = ProtobufInt64
112
    public typealias IntegerLiteralType = WrappedType.BaseType
113
114
0
    public init(_ value: WrappedType.BaseType) {
115
0
        self.init()
116
0
        self.value = value
117
0
    }
118
119
0
    public init(integerLiteral: IntegerLiteralType) {
120
0
        self.init(integerLiteral)
121
0
    }
122
123
1.18k
    func encodedJSONString(options: JSONEncodingOptions) throws -> String {
124
1.18k
        var encoded = value.description
125
1.18k
        if !options.alwaysPrintInt64sAsNumbers {
126
1.02k
            encoded = "\"" + encoded + "\""
127
1.02k
        }
128
1.18k
        return encoded
129
1.18k
    }
130
}
131
132
extension Google_Protobuf_UInt64Value:
133
    ProtobufWrapper, ExpressibleByIntegerLiteral, _CustomJSONCodable
134
{
135
136
    public typealias WrappedType = ProtobufUInt64
137
    public typealias IntegerLiteralType = WrappedType.BaseType
138
139
0
    public init(_ value: WrappedType.BaseType) {
140
0
        self.init()
141
0
        self.value = value
142
0
    }
143
144
0
    public init(integerLiteral: IntegerLiteralType) {
145
0
        self.init(integerLiteral)
146
0
    }
147
148
1.17k
    func encodedJSONString(options: JSONEncodingOptions) throws -> String {
149
1.17k
        var encoded = String(value)
150
1.17k
        if !options.alwaysPrintInt64sAsNumbers {
151
864
            encoded = "\"" + encoded + "\""
152
864
        }
153
1.17k
        return encoded
154
1.17k
    }
155
}
156
157
extension Google_Protobuf_Int32Value:
158
    ProtobufWrapper, ExpressibleByIntegerLiteral, _CustomJSONCodable
159
{
160
161
    public typealias WrappedType = ProtobufInt32
162
    public typealias IntegerLiteralType = WrappedType.BaseType
163
164
0
    public init(_ value: WrappedType.BaseType) {
165
0
        self.init()
166
0
        self.value = value
167
0
    }
168
169
0
    public init(integerLiteral: IntegerLiteralType) {
170
0
        self.init(integerLiteral)
171
0
    }
172
173
2.48k
    func encodedJSONString(options: JSONEncodingOptions) throws -> String {
174
2.48k
        String(value)
175
2.48k
    }
176
}
177
178
extension Google_Protobuf_UInt32Value:
179
    ProtobufWrapper, ExpressibleByIntegerLiteral, _CustomJSONCodable
180
{
181
182
    public typealias WrappedType = ProtobufUInt32
183
    public typealias IntegerLiteralType = WrappedType.BaseType
184
185
0
    public init(_ value: WrappedType.BaseType) {
186
0
        self.init()
187
0
        self.value = value
188
0
    }
189
190
0
    public init(integerLiteral: IntegerLiteralType) {
191
0
        self.init(integerLiteral)
192
0
    }
193
194
325
    func encodedJSONString(options: JSONEncodingOptions) throws -> String {
195
325
        String(value)
196
325
    }
197
}
198
199
extension Google_Protobuf_BoolValue:
200
    ProtobufWrapper, ExpressibleByBooleanLiteral, _CustomJSONCodable
201
{
202
203
    public typealias WrappedType = ProtobufBool
204
    public typealias BooleanLiteralType = Bool
205
206
0
    public init(_ value: WrappedType.BaseType) {
207
0
        self.init()
208
0
        self.value = value
209
0
    }
210
211
0
    public init(booleanLiteral: Bool) {
212
0
        self.init(booleanLiteral)
213
0
    }
214
215
258
    func encodedJSONString(options: JSONEncodingOptions) throws -> String {
216
258
        value ? "true" : "false"
217
258
    }
218
}
219
220
extension Google_Protobuf_StringValue:
221
    ProtobufWrapper, ExpressibleByStringLiteral, _CustomJSONCodable
222
{
223
224
    public typealias WrappedType = ProtobufString
225
    public typealias StringLiteralType = String
226
    public typealias ExtendedGraphemeClusterLiteralType = String
227
    public typealias UnicodeScalarLiteralType = String
228
229
0
    public init(_ value: WrappedType.BaseType) {
230
0
        self.init()
231
0
        self.value = value
232
0
    }
233
234
0
    public init(stringLiteral: String) {
235
0
        self.init(stringLiteral)
236
0
    }
237
238
0
    public init(extendedGraphemeClusterLiteral: String) {
239
0
        self.init(extendedGraphemeClusterLiteral)
240
0
    }
241
242
0
    public init(unicodeScalarLiteral: String) {
243
0
        self.init(unicodeScalarLiteral)
244
0
    }
245
246
62.4k
    func encodedJSONString(options: JSONEncodingOptions) throws -> String {
247
62.4k
        var encoder = JSONEncoder()
248
62.4k
        encoder.putStringValue(value: value)
249
62.4k
        return encoder.stringResult
250
62.4k
    }
251
}
252
253
extension Google_Protobuf_BytesValue: ProtobufWrapper, _CustomJSONCodable {
254
255
    public typealias WrappedType = ProtobufBytes
256
257
0
    public init(_ value: WrappedType.BaseType) {
258
0
        self.init()
259
0
        self.value = value
260
0
    }
261
262
1.23k
    func encodedJSONString(options: JSONEncodingOptions) throws -> String {
263
1.23k
        var encoder = JSONEncoder()
264
1.23k
        encoder.putBytesValue(value: value)
265
1.23k
        return encoder.stringResult
266
1.23k
    }
267
}