Coverage Report

Created: 2025-08-11 06:15

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