2
0

Metadata.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /// An item of metadata
  21. private struct MetadataPair {
  22. var key: String
  23. var value: String
  24. init(key: String, value: String) {
  25. self.key = key
  26. self.value = value
  27. }
  28. }
  29. /// Metadata sent with gRPC messages
  30. public class Metadata: CustomStringConvertible, NSCopying {
  31. /// Pointer to underlying C representation
  32. var underlyingArray: UnsafeMutableRawPointer
  33. init(underlyingArray: UnsafeMutableRawPointer) {
  34. self.underlyingArray = underlyingArray
  35. }
  36. public init() {
  37. underlyingArray = cgrpc_metadata_array_create()
  38. }
  39. public init(_ pairs: [[String: String]]) {
  40. underlyingArray = cgrpc_metadata_array_create()
  41. for pair in pairs {
  42. for key in pair.keys {
  43. if let value = pair[key] {
  44. add(key: key, value: value)
  45. }
  46. }
  47. }
  48. }
  49. public init(_ pairs: [String: String]) {
  50. underlyingArray = cgrpc_metadata_array_create()
  51. for key in pairs.keys {
  52. if let value = pairs[key] {
  53. add(key: key, value: value)
  54. }
  55. }
  56. }
  57. deinit {
  58. cgrpc_metadata_array_destroy(underlyingArray)
  59. }
  60. public func count() -> Int {
  61. return cgrpc_metadata_array_get_count(underlyingArray)
  62. }
  63. public func key(_ index: Int) -> String {
  64. if let string = cgrpc_metadata_array_copy_key_at_index(underlyingArray, index) {
  65. defer {
  66. cgrpc_free_copied_string(string)
  67. }
  68. if let key = String(cString: string, encoding: String.Encoding.utf8) {
  69. return key
  70. }
  71. }
  72. return "<binary-metadata-key>"
  73. }
  74. public func value(_ index: Int) -> String {
  75. if let string = cgrpc_metadata_array_copy_value_at_index(underlyingArray, index) {
  76. defer {
  77. cgrpc_free_copied_string(string)
  78. }
  79. if let value = String(cString: string, encoding: String.Encoding.utf8) {
  80. return value
  81. }
  82. }
  83. return "<binary-metadata-value>"
  84. }
  85. public func add(key: String, value: String) {
  86. cgrpc_metadata_array_append_metadata(underlyingArray, key, value)
  87. }
  88. public var description: String {
  89. var result = ""
  90. for i in 0..<count() {
  91. let key = self.key(i)
  92. let value = self.value(i)
  93. result += key + ":" + value + "\n"
  94. }
  95. return result
  96. }
  97. public func copy(with _: NSZone? = nil) -> Any {
  98. let copy = Metadata()
  99. for i in 0..<count() {
  100. copy.add(key: key(i), value: value(i))
  101. }
  102. return copy
  103. }
  104. }