Coverage Report

Created: 2026-03-26 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/swift-protobuf/Sources/SwiftProtobuf/Google_Protobuf_Struct+Extensions.swift
Line
Count
Source
1
// Sources/SwiftProtobuf/Google_Protobuf_Struct+Extensions.swift - Struct extensions
2
//
3
// Copyright (c) 2014 - 2017 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
/// Struct is a well-known message type that can be used to parse or encode
12
/// arbitrary JSON objects without a predefined schema.
13
///
14
// -----------------------------------------------------------------------------
15
16
extension Google_Protobuf_Struct: ExpressibleByDictionaryLiteral {
17
    public typealias Key = String
18
    public typealias Value = Google_Protobuf_Value
19
20
    /// Creates a new `Google_Protobuf_Struct` from a dictionary of string keys to
21
    /// values of type `Google_Protobuf_Value`.
22
0
    public init(dictionaryLiteral: (String, Google_Protobuf_Value)...) {
23
0
        self.init()
24
0
        for (k, v) in dictionaryLiteral {
25
0
            fields[k] = v
26
0
        }
27
0
    }
28
}
29
30
extension Google_Protobuf_Struct: _CustomJSONCodable {
31
10.8k
    internal func encodedJSONString(options: JSONEncodingOptions) throws -> String {
32
10.8k
        var jsonEncoder = JSONEncoder()
33
10.8k
        jsonEncoder.startObject()
34
10.8k
        var mapVisitor = JSONMapEncodingVisitor(encoder: jsonEncoder, options: options)
35
70.7k
        for (k, v) in fields {
36
70.7k
            try mapVisitor.visitSingularStringField(value: k, fieldNumber: 1)
37
70.7k
            try mapVisitor.visitSingularMessageField(value: v, fieldNumber: 2)
38
70.7k
        }
39
10.8k
        mapVisitor.encoder.endObject()
40
10.8k
        return mapVisitor.encoder.stringResult
41
10.8k
    }
42
43
20.8k
    internal mutating func decodeJSON(from decoder: inout JSONDecoder) throws {
44
20.8k
        try decoder.scanner.skipRequiredObjectStart()
45
20.8k
        if decoder.scanner.skipOptionalObjectEnd() {
46
11.5k
            return
47
11.5k
        }
48
141k
        while true {
49
141k
            let key = try decoder.scanner.nextQuotedString()
50
141k
            try decoder.scanner.skipRequiredColon()
51
141k
            var value = Google_Protobuf_Value()
52
141k
            try value.decodeJSON(from: &decoder)
53
140k
            fields[key] = value
54
140k
            if decoder.scanner.skipOptionalObjectEnd() {
55
8.18k
                return
56
132k
            }
57
132k
            try decoder.scanner.skipRequiredComma()
58
131k
        }
59
0
    }
60
}
61
62
extension Google_Protobuf_Struct {
63
    /// Creates a new `Google_Protobuf_Struct` from a dictionary of string keys to
64
    /// values of type `Google_Protobuf_Value`.
65
    ///
66
    /// - Parameter fields: The dictionary from field names to
67
    ///   `Google_Protobuf_Value` messages that should be used to create the
68
    ///   `Struct`.
69
0
    public init(fields: [String: Google_Protobuf_Value]) {
70
0
        self.init()
71
0
        self.fields = fields
72
0
    }
73
74
    /// Accesses the `Google_Protobuf_Value` with the given key for reading and
75
    /// writing.
76
    ///
77
    /// This key-based subscript returns the `Value` for the given key if the key
78
    /// is found in the `Struct`, or nil if the key is not found. If you assign
79
    /// nil as the `Value` for the given key, the `Struct` removes that key and
80
    /// its associated `Value`.
81
    public subscript(key: String) -> Google_Protobuf_Value? {
82
0
        get { fields[key] }
83
0
        set(newValue) { fields[key] = newValue }
84
    }
85
}