Metadata.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if SWIFT_PACKAGE
  17. import CgRPC
  18. #endif
  19. import Foundation // for String.Encoding
  20. /// Metadata sent with gRPC messages
  21. public class Metadata: CustomStringConvertible {
  22. /// Pointer to underlying C representation
  23. let underlyingArray: UnsafeMutableRawPointer
  24. init(underlyingArray: UnsafeMutableRawPointer) {
  25. self.underlyingArray = underlyingArray
  26. }
  27. public init() {
  28. underlyingArray = cgrpc_metadata_array_create()
  29. }
  30. public init(_ pairs: [String: String]) {
  31. underlyingArray = cgrpc_metadata_array_create()
  32. for (key, value) in pairs {
  33. add(key: key, value: value)
  34. }
  35. }
  36. deinit {
  37. cgrpc_metadata_array_destroy(underlyingArray)
  38. }
  39. public func count() -> Int {
  40. return cgrpc_metadata_array_get_count(underlyingArray)
  41. }
  42. // Returns `nil` for non-UTF8 metadata key strings.
  43. public func key(_ index: Int) -> String? {
  44. // We actually know that this method will never return nil,
  45. // so we can forcibly unwrap the result. (Also below.)
  46. let keyData = cgrpc_metadata_array_copy_key_at_index(underlyingArray, index)!
  47. defer { cgrpc_free_copied_string(keyData) }
  48. return String(cString: keyData, encoding: String.Encoding.utf8)
  49. }
  50. // Returns `nil` for non-UTF8 metadata value strings.
  51. public func value(_ index: Int) -> String? {
  52. // We actually know that this method will never return nil,
  53. // so we can forcibly unwrap the result. (Also below.)
  54. let valueData = cgrpc_metadata_array_copy_value_at_index(underlyingArray, index)!
  55. defer { cgrpc_free_copied_string(valueData) }
  56. return String(cString: valueData, encoding: String.Encoding.utf8)
  57. }
  58. public func add(key: String, value: String) {
  59. cgrpc_metadata_array_append_metadata(underlyingArray, key, value)
  60. }
  61. public var description: String {
  62. var result = ""
  63. for i in 0..<count() {
  64. let key = self.key(i)
  65. let value = self.value(i)
  66. result += (key ?? "(nil)") + ":" + (value ?? "(nil)") + "\n"
  67. }
  68. return result
  69. }
  70. public func copy() -> Metadata {
  71. let copy = Metadata()
  72. for index in 0..<count() {
  73. let keyData = cgrpc_metadata_array_copy_key_at_index(underlyingArray, index)!
  74. defer { cgrpc_free_copied_string(keyData) }
  75. let valueData = cgrpc_metadata_array_copy_value_at_index(underlyingArray, index)!
  76. defer { cgrpc_free_copied_string(valueData) }
  77. cgrpc_metadata_array_append_metadata(copy.underlyingArray, keyData, valueData)
  78. }
  79. return copy
  80. }
  81. }