Skip to content

Commit

Permalink
Don't generate has/clear for repeated extension fields.
Browse files Browse the repository at this point in the history
The presence can be checked with `isEmpty`on the Array, and they can be cleared
by assigning to `[]`.

Fixes apple#944
Helps with apple#1204
  • Loading branch information
thomasvl committed Mar 7, 2022
1 parent 63e341f commit 123c538
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
31 changes: 17 additions & 14 deletions Sources/protoc-gen-swift/ExtensionSetGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftProtobufTests/Test_Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 123c538

Please sign in to comment.