Metadata.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. self.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. return String(cString:cgrpc_metadata_array_get_key_at_index(underlyingArray, index),
  65. encoding:String.Encoding.utf8)!;
  66. }
  67. public func value(_ index: Int) -> (String) {
  68. return String(cString:cgrpc_metadata_array_get_value_at_index(underlyingArray, index),
  69. encoding:String.Encoding.utf8)!;
  70. }
  71. public func add(key:String, value:String) {
  72. cgrpc_metadata_array_append_metadata(underlyingArray, key, value)
  73. }
  74. public var description : String {
  75. var result = ""
  76. for i in 0..<count() {
  77. let key = self.key(i)
  78. let value = self.value(i)
  79. result += key + ":" + value + "\n"
  80. }
  81. return result
  82. }
  83. public func copy(with zone: NSZone? = nil) -> Any {
  84. let copy = Metadata()
  85. for i in 0..<count() {
  86. copy.add(key:self.key(i), value:self.value(i))
  87. }
  88. return copy
  89. }
  90. }