diff --git a/Sources/protoc-gen-swift/ExtensionSetGenerator.swift b/Sources/protoc-gen-swift/ExtensionSetGenerator.swift index 09ff06717..afeaf56db 100644 --- a/Sources/protoc-gen-swift/ExtensionSetGenerator.swift +++ b/Sources/protoc-gen-swift/ExtensionSetGenerator.swift @@ -109,21 +109,24 @@ class ExtensionSetGenerator { p.outdent() p.print("}\n") - p.print( - "/// Returns true if extension `\(swiftFullExtensionName)`\n/// has been explicitly set.\n", - "\(visibility)var \(extensionNames.has): Bool {\n") - p.indent() - p.print("return hasExtensionValue(ext: \(swiftFullExtensionName))\n") - p.outdent() - p.print("}\n") + // Repeated extension fields can use .isEmpty and clear by setting to the empty list. + if fieldDescriptor.label != .repeated { + p.print( + "/// Returns true if extension `\(swiftFullExtensionName)`\n/// has been explicitly set.\n", + "\(visibility)var \(extensionNames.has): Bool {\n") + p.indent() + p.print("return hasExtensionValue(ext: \(swiftFullExtensionName))\n") + p.outdent() + p.print("}\n") - p.print( - "/// Clears the value of extension `\(swiftFullExtensionName)`.\n/// Subsequent reads from it will return its default value.\n", - "\(visibility)mutating func \(extensionNames.clear)() {\n") - p.indent() - p.print("clearExtensionValue(ext: \(swiftFullExtensionName))\n") - p.outdent() - p.print("}\n") + p.print( + "/// Clears the value of extension `\(swiftFullExtensionName)`.\n/// Subsequent reads from it will return its default value.\n", + "\(visibility)mutating func \(extensionNames.clear)() {\n") + p.indent() + p.print("clearExtensionValue(ext: \(swiftFullExtensionName))\n") + p.outdent() + p.print("}\n") + } } } diff --git a/Tests/SwiftProtobufTests/Test_Extensions.swift b/Tests/SwiftProtobufTests/Test_Extensions.swift index 9c85e84f9..ed08182a5 100644 --- a/Tests/SwiftProtobufTests/Test_Extensions.swift +++ b/Tests/SwiftProtobufTests/Test_Extensions.swift @@ -166,9 +166,9 @@ class Test_Extensions: XCTestCase, PBTestHelpers { XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.repeated_int32_extension]: 7\n[protobuf_unittest.repeated_int32_extension]: 9\n") - XCTAssertTrue(m.hasProtobufUnittest_repeatedInt32Extension) + XCTAssertFalse(m.ProtobufUnittest_repeatedInt32Extension.isEmpty) m.ProtobufUnittest_repeatedInt32Extension = [] - XCTAssertFalse(m.hasProtobufUnittest_repeatedInt32Extension) + XCTAssertTrue(m.ProtobufUnittest_repeatedInt32Extension.isEmpty) } func test_defaultInt32Extension() throws { @@ -252,9 +252,9 @@ class Test_Extensions: XCTestCase, PBTestHelpers { XCTFail("Decoding into unextended message failed for \(coded)") } - XCTAssertTrue(m.hasRepeatedExtensionGroup) + XCTAssertFalse(m.repeatedExtensionGroup.isEmpty) m.repeatedExtensionGroup = [] - XCTAssertFalse(m.hasRepeatedExtensionGroup) + XCTAssertTrue(m.repeatedExtensionGroup.isEmpty) } func test_MessageNoStorageClass() {