Message+AnyAdditions.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Sources/SwiftProtobuf/Message+AnyAdditions.swift - Any-related Message extensions
  2. //
  3. // Copyright (c) 2014 - 2017 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// Extends the `Message` type with `Google_Protobuf_Any`-specific behavior.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. extension Message {
  15. /// Initialize this message from the provided `google.protobuf.Any`
  16. /// well-known type.
  17. ///
  18. /// This corresponds to the `unpack` method in the Google C++ API.
  19. ///
  20. /// If the Any object was decoded from Protobuf Binary or JSON
  21. /// format, then the enclosed field data was stored and is not
  22. /// fully decoded until you unpack the Any object into a message.
  23. /// As such, this method will typically need to perform a full
  24. /// deserialization of the enclosed data and can fail for any
  25. /// reason that deserialization can fail.
  26. ///
  27. /// See `Google_Protobuf_Any.unpackTo()` for more discussion.
  28. ///
  29. /// - Parameter unpackingAny: the message to decode.
  30. /// - Parameter extensions: An `ExtensionMap` used to look up and decode any
  31. /// extensions in this message or messages nested within this message's
  32. /// fields.
  33. /// - Parameter options: The BinaryDecodingOptions to use.
  34. /// - Throws: an instance of ``AnyUnpackError``, ``JSONDecodingError``, or
  35. /// ``BinaryDecodingError`` on failure.
  36. public init(
  37. unpackingAny: Google_Protobuf_Any,
  38. extensions: (any ExtensionMap)? = nil,
  39. options: BinaryDecodingOptions = BinaryDecodingOptions()
  40. ) throws {
  41. self.init()
  42. try unpackingAny._storage.unpackTo(target: &self, extensions: extensions, options: options)
  43. }
  44. }