Skip to content

Commit 6750ed8

Browse files
thomasvlcopybara-github
authored andcommitted
[ObjC] Add Swift helpers for GPBUnknownFields/GPBUnknownField.
`GPBUnknownFields` additions: - Provide `Optional` based apis for the `getFirst*` apis. - Map the `NSFastEnumeration` over as a `Sequence` to support looping over the fields. `GPBUnknownField` addition: - Add an `enum` with associated values to provide a more type-safe way for inspection. PiperOrigin-RevId: 649090744
1 parent 73db255 commit 6750ed8

File tree

7 files changed

+298
-26
lines changed

7 files changed

+298
-26
lines changed

Protobuf.podspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Pod::Spec.new do |s|
1818
s.source = { :git => 'https://github.com/protocolbuffers/protobuf.git',
1919
:tag => "v#{s.version}" }
2020

21-
s.source_files = 'objectivec/*.{h,m}',
21+
s.source_files = 'objectivec/*.{h,m,swift}',
2222
'objectivec/google/protobuf/Any.pbobjc.h',
2323
'objectivec/google/protobuf/Api.pbobjc.h',
2424
'objectivec/google/protobuf/Duration.pbobjc.h',
@@ -33,6 +33,9 @@ Pod::Spec.new do |s|
3333
# left out, as it's an umbrella implementation file.
3434
s.exclude_files = 'objectivec/GPBProtocolBuffers.m'
3535

36+
# Now that there is a Swift source file, set a version.
37+
s.swift_version = '5.0'
38+
3639
s.resource_bundle = {
3740
"Protobuf_Privacy" => "PrivacyInfo.xcprivacy"
3841
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2024 Google Inc. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
/// Swift specific additions to simplify usage.
9+
extension GPBUnknownField {
10+
11+
/// The value of the field in a type-safe manner.
12+
public enum Value: Equatable {
13+
case varint(UInt64)
14+
case fixed32(UInt32)
15+
case fixed64(UInt64)
16+
case lengthDelimited(Data) // length prefixed
17+
case group(GPBUnknownFields) // tag delimited
18+
}
19+
20+
/// The value of the field in a type-safe manner.
21+
///
22+
/// - Note: This is only valid for non-legacy fields.
23+
public var value: Value {
24+
switch type {
25+
case .varint:
26+
return .varint(varint)
27+
case .fixed32:
28+
return .fixed32(fixed32)
29+
case .fixed64:
30+
return .fixed64(fixed64)
31+
case .lengthDelimited:
32+
return .lengthDelimited(lengthDelimited)
33+
case .group:
34+
return .group(group)
35+
case .legacy:
36+
fatalError("`value` not valid for Legacy fields.")
37+
@unknown default:
38+
fatalError("Internal error: Unknown field type: \(type)")
39+
}
40+
}
41+
42+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2024 Google Inc. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
/// Swift specific additions to simplify usage.
9+
extension GPBUnknownFields {
10+
11+
/// Fetches the first varint for the given field number.
12+
public func firstVarint(_ fieldNumber: Int32) -> UInt64? {
13+
var value: UInt64 = 0
14+
guard getFirst(fieldNumber, varint: &value) else { return nil }
15+
return value
16+
}
17+
18+
/// Fetches the first fixed32 for the given field number.
19+
public func firstFixed32(_ fieldNumber: Int32) -> UInt32? {
20+
var value: UInt32 = 0
21+
guard getFirst(fieldNumber, fixed32: &value) else { return nil }
22+
return value
23+
}
24+
25+
/// Fetches the first fixed64 for the given field number.
26+
public func firstFixed64(_ fieldNumber: Int32) -> UInt64? {
27+
var value: UInt64 = 0
28+
guard getFirst(fieldNumber, fixed64: &value) else { return nil }
29+
return value
30+
}
31+
32+
}
33+
34+
/// Map the `NSFastEnumeration` support to a Swift `Sequence`.
35+
extension GPBUnknownFields: Sequence {
36+
public typealias Element = GPBUnknownField
37+
38+
public struct Iterator: IteratorProtocol {
39+
var iter: NSFastEnumerationIterator
40+
41+
init(_ fields: NSFastEnumeration) {
42+
self.iter = NSFastEnumerationIterator(fields)
43+
}
44+
45+
public mutating func next() -> GPBUnknownField? {
46+
return iter.next() as? GPBUnknownField
47+
}
48+
}
49+
50+
public func makeIterator() -> Iterator {
51+
return Iterator(self)
52+
}
53+
}

objectivec/ProtocolBuffers_OSX.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
F43ADD312C2F2B91005312E5 /* GPBUnknownFields.h in Headers */ = {isa = PBXBuildFile; fileRef = F43ADD2F2C2F2B91005312E5 /* GPBUnknownFields.h */; };
7575
F43ADD332C2F2BAD005312E5 /* GPBUnknownFieldsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = F43ADD322C2F2BAD005312E5 /* GPBUnknownFieldsTest.m */; };
7676
F43ADD432C2F381F005312E5 /* GPBCompileTest25.m in Sources */ = {isa = PBXBuildFile; fileRef = F43ADD422C2F381F005312E5 /* GPBCompileTest25.m */; };
77+
F43ADD502C333D6C005312E5 /* GPBUnknownFields+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F43ADD4F2C333D6B005312E5 /* GPBUnknownFields+Additions.swift */; };
78+
F43ADD562C345CED005312E5 /* GPBUnknownField+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F43ADD552C345CED005312E5 /* GPBUnknownField+Additions.swift */; };
7779
F43C88D0191D77FC009E917D /* text_format_unittest_data.txt in Resources */ = {isa = PBXBuildFile; fileRef = F43C88CF191D77FC009E917D /* text_format_unittest_data.txt */; };
7880
F4487C4D1A9F8E0200531423 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
7981
F4487C521A9F8E4D00531423 /* GPBProtocolBuffers.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BCF338814ED799900BC5317 /* GPBProtocolBuffers.m */; };
@@ -214,6 +216,8 @@
214216
F43ADD2F2C2F2B91005312E5 /* GPBUnknownFields.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBUnknownFields.h; sourceTree = "<group>"; };
215217
F43ADD322C2F2BAD005312E5 /* GPBUnknownFieldsTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUnknownFieldsTest.m; sourceTree = "<group>"; };
216218
F43ADD422C2F381F005312E5 /* GPBCompileTest25.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest25.m; sourceTree = "<group>"; };
219+
F43ADD4F2C333D6B005312E5 /* GPBUnknownFields+Additions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "GPBUnknownFields+Additions.swift"; sourceTree = "<group>"; };
220+
F43ADD552C345CED005312E5 /* GPBUnknownField+Additions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "GPBUnknownField+Additions.swift"; sourceTree = "<group>"; };
217221
F43C88CF191D77FC009E917D /* text_format_unittest_data.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = text_format_unittest_data.txt; sourceTree = "<group>"; };
218222
F4411BE71AF12FD700324B4A /* GPBArray_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBArray_PackagePrivate.h; sourceTree = "<group>"; };
219223
F4487C511A9F8E0200531423 /* libTestSingleSourceBuild.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libTestSingleSourceBuild.a; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -417,8 +421,10 @@
417421
F4B6B8AF1A9CC98000892426 /* GPBUnknownField_PackagePrivate.h */,
418422
7461B4AE0F94F99000A0C422 /* GPBUnknownField.h */,
419423
7461B4AF0F94F99000A0C422 /* GPBUnknownField.m */,
424+
F43ADD552C345CED005312E5 /* GPBUnknownField+Additions.swift */,
420425
F43ADD2F2C2F2B91005312E5 /* GPBUnknownFields.h */,
421426
F43ADD2E2C2F2B91005312E5 /* GPBUnknownFields.m */,
427+
F43ADD4F2C333D6B005312E5 /* GPBUnknownFields+Additions.swift */,
422428
F4B6B8B21A9CCBDA00892426 /* GPBUnknownFieldSet_PackagePrivate.h */,
423429
7461B4E10F94F99000A0C422 /* GPBUnknownFieldSet.h */,
424430
7461B4E20F94F99000A0C422 /* GPBUnknownFieldSet.m */,
@@ -668,6 +674,9 @@
668674
LastTestingUpgradeCheck = 0600;
669675
LastUpgradeCheck = 1400;
670676
TargetAttributes = {
677+
7461B52D0F94FAF800A0C422 = {
678+
LastSwiftMigration = 1530;
679+
};
671680
8BBEA4A5147C727100C4ADB7 = {
672681
LastSwiftMigration = "";
673682
TestTargetID = 8B9A5EA41831993600A9D33B;
@@ -719,6 +728,7 @@
719728
files = (
720729
F47CF93123D9006000C7B24C /* GPBType.pbobjc.m in Sources */,
721730
7461B53C0F94FB4E00A0C422 /* GPBCodedInputStream.m in Sources */,
731+
F43ADD502C333D6C005312E5 /* GPBUnknownFields+Additions.swift in Sources */,
722732
F47CF93223D9006000C7B24C /* GPBWrappers.pbobjc.m in Sources */,
723733
F47CF94123D902D500C7B24C /* GPBEmpty.pbobjc.m in Sources */,
724734
F43ADD302C2F2B91005312E5 /* GPBUnknownFields.m in Sources */,
@@ -737,6 +747,7 @@
737747
8B79657B14992E3F002FFBFC /* GPBRootObject.m in Sources */,
738748
F47CF93523D9006000C7B24C /* GPBTimestamp.pbobjc.m in Sources */,
739749
8B96157414C8C38C00A2AC0B /* GPBDescriptor.m in Sources */,
750+
F43ADD562C345CED005312E5 /* GPBUnknownField+Additions.swift in Sources */,
740751
F45C69CC16DFD08D0081955B /* GPBExtensionInternals.m in Sources */,
741752
F47CF92B23D9006000C7B24C /* GPBStruct.pbobjc.m in Sources */,
742753
F47CF94323D902D500C7B24C /* GPBAny.pbobjc.m in Sources */,
@@ -834,28 +845,36 @@
834845
7461B52F0F94FAFA00A0C422 /* Debug */ = {
835846
isa = XCBuildConfiguration;
836847
buildSettings = {
848+
CLANG_ENABLE_MODULES = YES;
837849
CLANG_ENABLE_OBJC_WEAK = YES;
838850
COMBINE_HIDPI_IMAGES = YES;
839851
DEAD_CODE_STRIPPING = YES;
840852
PRODUCT_NAME = ProtocolBuffers;
853+
SWIFT_OBJC_BRIDGING_HEADER = GPBProtocolBuffers.h;
854+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
855+
SWIFT_VERSION = 5.0;
841856
USER_HEADER_SEARCH_PATHS = "$(SRCROOT)";
842857
};
843858
name = Debug;
844859
};
845860
7461B5300F94FAFA00A0C422 /* Release */ = {
846861
isa = XCBuildConfiguration;
847862
buildSettings = {
863+
CLANG_ENABLE_MODULES = YES;
848864
CLANG_ENABLE_OBJC_WEAK = YES;
849865
COMBINE_HIDPI_IMAGES = YES;
850866
DEAD_CODE_STRIPPING = YES;
851867
PRODUCT_NAME = ProtocolBuffers;
868+
SWIFT_OBJC_BRIDGING_HEADER = GPBProtocolBuffers.h;
869+
SWIFT_VERSION = 5.0;
852870
USER_HEADER_SEARCH_PATHS = "$(SRCROOT)";
853871
};
854872
name = Release;
855873
};
856874
8BBEA4A7147C727100C4ADB7 /* Debug */ = {
857875
isa = XCBuildConfiguration;
858876
buildSettings = {
877+
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
859878
CLANG_ENABLE_MODULES = YES;
860879
CLANG_ENABLE_OBJC_WEAK = YES;
861880
COMBINE_HIDPI_IMAGES = YES;
@@ -888,6 +907,7 @@
888907
8BBEA4A8147C727100C4ADB7 /* Release */ = {
889908
isa = XCBuildConfiguration;
890909
buildSettings = {
910+
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
891911
CLANG_ENABLE_MODULES = YES;
892912
CLANG_ENABLE_OBJC_WEAK = YES;
893913
COMBINE_HIDPI_IMAGES = YES;

objectivec/ProtocolBuffers_iOS.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
F43ADD352C2F2CE9005312E5 /* GPBUnknownFieldsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = F43ADD342C2F2CE9005312E5 /* GPBUnknownFieldsTest.m */; };
7474
F43ADD412C2F2D60005312E5 /* GPBUnknownFields.m in Sources */ = {isa = PBXBuildFile; fileRef = F43ADD362C2F2D06005312E5 /* GPBUnknownFields.m */; };
7575
F43ADD472C2F387A005312E5 /* GPBCompileTest25.m in Sources */ = {isa = PBXBuildFile; fileRef = F43ADD462C2F387A005312E5 /* GPBCompileTest25.m */; };
76+
F43ADD522C333E58005312E5 /* GPBUnknownFields+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F43ADD512C333E58005312E5 /* GPBUnknownFields+Additions.swift */; };
77+
F43ADD582C345D0D005312E5 /* GPBUnknownField+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F43ADD572C345D0D005312E5 /* GPBUnknownField+Additions.swift */; };
7678
F43C88D0191D77FC009E917D /* text_format_unittest_data.txt in Resources */ = {isa = PBXBuildFile; fileRef = F43C88CF191D77FC009E917D /* text_format_unittest_data.txt */; };
7779
F4487C6A1A9F8F8100531423 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
7880
F4487C6F1A9F8FFF00531423 /* GPBProtocolBuffers.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BCF338814ED799900BC5317 /* GPBProtocolBuffers.m */; };
@@ -214,6 +216,8 @@
214216
F43ADD362C2F2D06005312E5 /* GPBUnknownFields.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBUnknownFields.m; sourceTree = "<group>"; };
215217
F43ADD372C2F2D06005312E5 /* GPBUnknownFields.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GPBUnknownFields.h; sourceTree = "<group>"; };
216218
F43ADD462C2F387A005312E5 /* GPBCompileTest25.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GPBCompileTest25.m; sourceTree = "<group>"; };
219+
F43ADD512C333E58005312E5 /* GPBUnknownFields+Additions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "GPBUnknownFields+Additions.swift"; sourceTree = "<group>"; };
220+
F43ADD572C345D0D005312E5 /* GPBUnknownField+Additions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "GPBUnknownField+Additions.swift"; sourceTree = "<group>"; };
217221
F43C88CF191D77FC009E917D /* text_format_unittest_data.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = text_format_unittest_data.txt; sourceTree = "<group>"; };
218222
F4411BE81AF1301700324B4A /* GPBArray_PackagePrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPBArray_PackagePrivate.h; sourceTree = "<group>"; };
219223
F4487C6E1A9F8F8100531423 /* libTestSingleSourceBuild.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libTestSingleSourceBuild.a; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -422,8 +426,10 @@
422426
F4B6B8B01A9CC99500892426 /* GPBUnknownField_PackagePrivate.h */,
423427
7461B4AE0F94F99000A0C422 /* GPBUnknownField.h */,
424428
7461B4AF0F94F99000A0C422 /* GPBUnknownField.m */,
429+
F43ADD572C345D0D005312E5 /* GPBUnknownField+Additions.swift */,
425430
F43ADD372C2F2D06005312E5 /* GPBUnknownFields.h */,
426431
F43ADD362C2F2D06005312E5 /* GPBUnknownFields.m */,
432+
F43ADD512C333E58005312E5 /* GPBUnknownFields+Additions.swift */,
427433
F4B6B8B11A9CCBBB00892426 /* GPBUnknownFieldSet_PackagePrivate.h */,
428434
7461B4E10F94F99000A0C422 /* GPBUnknownFieldSet.h */,
429435
7461B4E20F94F99000A0C422 /* GPBUnknownFieldSet.m */,
@@ -673,6 +679,9 @@
673679
LastTestingUpgradeCheck = 0600;
674680
LastUpgradeCheck = 1400;
675681
TargetAttributes = {
682+
7461B52D0F94FAF800A0C422 = {
683+
LastSwiftMigration = 1530;
684+
};
676685
8BBEA4A5147C727100C4ADB7 = {
677686
LastSwiftMigration = 0940;
678687
TestTargetID = 8B9A5EA41831993600A9D33B;
@@ -724,6 +733,7 @@
724733
files = (
725734
7461B53C0F94FB4E00A0C422 /* GPBCodedInputStream.m in Sources */,
726735
F47CF96D23D903C600C7B24C /* GPBAny.pbobjc.m in Sources */,
736+
F43ADD522C333E58005312E5 /* GPBUnknownFields+Additions.swift in Sources */,
727737
F47CF96623D903C600C7B24C /* GPBSourceContext.pbobjc.m in Sources */,
728738
F47CF96C23D903C600C7B24C /* GPBType.pbobjc.m in Sources */,
729739
F43ADD412C2F2D60005312E5 /* GPBUnknownFields.m in Sources */,
@@ -742,6 +752,7 @@
742752
F47CF96523D903C600C7B24C /* GPBApi.pbobjc.m in Sources */,
743753
F4353D271ABB156F005A6198 /* GPBDictionary.m in Sources */,
744754
8B79657B14992E3F002FFBFC /* GPBRootObject.m in Sources */,
755+
F43ADD582C345D0D005312E5 /* GPBUnknownField+Additions.swift in Sources */,
745756
F47CF96223D903C600C7B24C /* GPBFieldMask.pbobjc.m in Sources */,
746757
8B96157414C8C38C00A2AC0B /* GPBDescriptor.m in Sources */,
747758
F45C69CC16DFD08D0081955B /* GPBExtensionInternals.m in Sources */,
@@ -839,9 +850,13 @@
839850
7461B52F0F94FAFA00A0C422 /* Debug */ = {
840851
isa = XCBuildConfiguration;
841852
buildSettings = {
853+
CLANG_ENABLE_MODULES = YES;
842854
CLANG_ENABLE_OBJC_WEAK = YES;
843855
PRODUCT_NAME = ProtocolBuffers;
844856
SKIP_INSTALL = YES;
857+
SWIFT_OBJC_BRIDGING_HEADER = GPBProtocolBuffers.h;
858+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
859+
SWIFT_VERSION = 5.0;
845860
TARGETED_DEVICE_FAMILY = "1,2";
846861
USER_HEADER_SEARCH_PATHS = "$(SRCROOT)";
847862
};
@@ -850,9 +865,12 @@
850865
7461B5300F94FAFA00A0C422 /* Release */ = {
851866
isa = XCBuildConfiguration;
852867
buildSettings = {
868+
CLANG_ENABLE_MODULES = YES;
853869
CLANG_ENABLE_OBJC_WEAK = YES;
854870
PRODUCT_NAME = ProtocolBuffers;
855871
SKIP_INSTALL = YES;
872+
SWIFT_OBJC_BRIDGING_HEADER = GPBProtocolBuffers.h;
873+
SWIFT_VERSION = 5.0;
856874
TARGETED_DEVICE_FAMILY = "1,2";
857875
USER_HEADER_SEARCH_PATHS = "$(SRCROOT)";
858876
};
@@ -861,6 +879,7 @@
861879
8BBEA4A7147C727100C4ADB7 /* Debug */ = {
862880
isa = XCBuildConfiguration;
863881
buildSettings = {
882+
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
864883
CLANG_ENABLE_MODULES = YES;
865884
CLANG_ENABLE_OBJC_WEAK = YES;
866885
FRAMEWORK_SEARCH_PATHS = (
@@ -899,6 +918,7 @@
899918
8BBEA4A8147C727100C4ADB7 /* Release */ = {
900919
isa = XCBuildConfiguration;
901920
buildSettings = {
921+
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
902922
CLANG_ENABLE_MODULES = YES;
903923
CLANG_ENABLE_OBJC_WEAK = YES;
904924
FRAMEWORK_SEARCH_PATHS = (

0 commit comments

Comments
 (0)