-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
1 parent
73db255
commit 6750ed8
Showing
7 changed files
with
298 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Protocol Buffers - Google's data interchange format | ||
// Copyright 2024 Google Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd | ||
|
||
/// Swift specific additions to simplify usage. | ||
extension GPBUnknownField { | ||
|
||
/// The value of the field in a type-safe manner. | ||
public enum Value: Equatable { | ||
case varint(UInt64) | ||
case fixed32(UInt32) | ||
case fixed64(UInt64) | ||
case lengthDelimited(Data) // length prefixed | ||
case group(GPBUnknownFields) // tag delimited | ||
} | ||
|
||
/// The value of the field in a type-safe manner. | ||
/// | ||
/// - Note: This is only valid for non-legacy fields. | ||
public var value: Value { | ||
switch type { | ||
case .varint: | ||
return .varint(varint) | ||
case .fixed32: | ||
return .fixed32(fixed32) | ||
case .fixed64: | ||
return .fixed64(fixed64) | ||
case .lengthDelimited: | ||
return .lengthDelimited(lengthDelimited) | ||
case .group: | ||
return .group(group) | ||
case .legacy: | ||
fatalError("`value` not valid for Legacy fields.") | ||
@unknown default: | ||
fatalError("Internal error: Unknown field type: \(type)") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Protocol Buffers - Google's data interchange format | ||
// Copyright 2024 Google Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd | ||
|
||
/// Swift specific additions to simplify usage. | ||
extension GPBUnknownFields { | ||
|
||
/// Fetches the first varint for the given field number. | ||
public func firstVarint(_ fieldNumber: Int32) -> UInt64? { | ||
var value: UInt64 = 0 | ||
guard getFirst(fieldNumber, varint: &value) else { return nil } | ||
return value | ||
} | ||
|
||
/// Fetches the first fixed32 for the given field number. | ||
public func firstFixed32(_ fieldNumber: Int32) -> UInt32? { | ||
var value: UInt32 = 0 | ||
guard getFirst(fieldNumber, fixed32: &value) else { return nil } | ||
return value | ||
} | ||
|
||
/// Fetches the first fixed64 for the given field number. | ||
public func firstFixed64(_ fieldNumber: Int32) -> UInt64? { | ||
var value: UInt64 = 0 | ||
guard getFirst(fieldNumber, fixed64: &value) else { return nil } | ||
return value | ||
} | ||
|
||
} | ||
|
||
/// Map the `NSFastEnumeration` support to a Swift `Sequence`. | ||
extension GPBUnknownFields: Sequence { | ||
public typealias Element = GPBUnknownField | ||
|
||
public struct Iterator: IteratorProtocol { | ||
var iter: NSFastEnumerationIterator | ||
|
||
init(_ fields: NSFastEnumeration) { | ||
self.iter = NSFastEnumerationIterator(fields) | ||
} | ||
|
||
public mutating func next() -> GPBUnknownField? { | ||
return iter.next() as? GPBUnknownField | ||
} | ||
} | ||
|
||
public func makeIterator() -> Iterator { | ||
return Iterator(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.