Field.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. import Foundation
  34. /// A representation of a protocol buffer field that can be used to read and build protobufs
  35. public class Field {
  36. private var descriptor: FieldDescriptor
  37. var value: Any!
  38. init(descriptor: FieldDescriptor, value: Any) {
  39. self.descriptor = descriptor
  40. self.value = value
  41. }
  42. init(descriptor: FieldDescriptor) {
  43. self.descriptor = descriptor
  44. }
  45. /// display a field for testing and debugging
  46. func display(indent: String) {
  47. let type = self.descriptor.type
  48. var line = indent
  49. line += String(describing:self.descriptor.label) + " "
  50. line += String(describing:type)
  51. if ((type == .message) || (type == .enumeration)) {
  52. line += ":" + self.descriptor.type_name
  53. }
  54. line += " "
  55. line += self.descriptor.name
  56. line += " = "
  57. if let value = value as? Int {
  58. line += "\(value)"
  59. print(line)
  60. } else if let value = value as? Int32 {
  61. line += "\(value)"
  62. print(line)
  63. } else if let value = value as? Int64 {
  64. line += "\(value)"
  65. print(line)
  66. } else if let value = value as? Bool {
  67. line += "\(value)"
  68. print(line)
  69. } else if let value = value as? String {
  70. line += "\(value)"
  71. print(line)
  72. } else if let message = value as? Message {
  73. line += " {"
  74. print(line)
  75. for field in message.fields {
  76. field.display(indent:indent + " ")
  77. }
  78. print("\(indent)}")
  79. } else if let value = value as? Double {
  80. line += "\(value)"
  81. print(line)
  82. } else if let value = value as? Float {
  83. line += "\(value)"
  84. print(line)
  85. } else if let value = value as? Data {
  86. line += "\(value)"
  87. print(line)
  88. } else {
  89. line += "???"
  90. print(line)
  91. }
  92. }
  93. public func name() -> String {
  94. return descriptor.name
  95. }
  96. public func tag() -> Int {
  97. return descriptor.tag
  98. }
  99. public func wireType() -> WireType {
  100. return descriptor.wireType()
  101. }
  102. public func type() -> FieldType {
  103. return descriptor.type
  104. }
  105. public func message() -> Message {
  106. return value as! Message
  107. }
  108. public func data() -> Data {
  109. return value as! Data
  110. }
  111. public func string() -> String {
  112. return value as! String
  113. }
  114. public func integer() -> Int {
  115. if let value = value as? Int {
  116. return value
  117. } else if let value = value as? Int32 {
  118. return Int(value)
  119. } else if let value = value as? Int64 {
  120. return Int(value)
  121. } else {
  122. return 0
  123. }
  124. }
  125. public func bool() -> Bool {
  126. if let value = value as? Bool {
  127. return value
  128. } else if let value = value as? Int {
  129. return value != 0
  130. } else {
  131. return false
  132. }
  133. }
  134. public func double() -> Double {
  135. return value as! Double
  136. }
  137. public func float() -> Float {
  138. return value as! Float
  139. }
  140. public func setString(_ value:String) {
  141. self.value = value
  142. }
  143. public func setMessage(_ value:Message) {
  144. self.value = value
  145. }
  146. public func setData(_ value:Data) {
  147. self.value = value
  148. }
  149. public func setInt(_ value:Int) {
  150. self.value = value
  151. }
  152. public func setDouble(_ value:Double) {
  153. self.value = value
  154. }
  155. public func setFloat(_ value:Float) {
  156. self.value = value
  157. }
  158. public func setBool(_ value:Bool) {
  159. self.value = value
  160. }
  161. }