GPBUnknownField+Additions.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. /// Swift specific additions to simplify usage.
  8. extension GPBUnknownField {
  9. /// The value of the field in a type-safe manner.
  10. public enum Value: Equatable {
  11. case varint(UInt64)
  12. case fixed32(UInt32)
  13. case fixed64(UInt64)
  14. case lengthDelimited(Data) // length prefixed
  15. case group(GPBUnknownFields) // tag delimited
  16. }
  17. /// The value of the field in a type-safe manner.
  18. ///
  19. /// - Note: This is only valid for non-legacy fields.
  20. public var value: Value {
  21. switch type {
  22. case .varint:
  23. return .varint(varint)
  24. case .fixed32:
  25. return .fixed32(fixed32)
  26. case .fixed64:
  27. return .fixed64(fixed64)
  28. case .lengthDelimited:
  29. return .lengthDelimited(lengthDelimited)
  30. case .group:
  31. return .group(group)
  32. case .legacy:
  33. fatalError("`value` not valid for Legacy fields.")
  34. @unknown default:
  35. fatalError("Internal error: Unknown field type: \(type)")
  36. }
  37. }
  38. }