FieldDescriptor.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 description of a field in a protocol buffer message
  35. class FieldDescriptor {
  36. var type : FieldType
  37. var label : FieldLabel
  38. var name : String = ""
  39. var tag : Int = 0
  40. var type_name : String = ""
  41. init(type: Int, name: String, tag: Int, type_name: String, label: Int) {
  42. self.name = name
  43. self.type = FieldType(rawValue:type)!
  44. self.tag = tag
  45. self.type_name = type_name
  46. self.label = FieldLabel(rawValue:label)!
  47. }
  48. init(message: Message) {
  49. name = message.oneField("name")!.string()
  50. type = FieldType(rawValue:message.oneField("type")!.integer())!
  51. tag = message.oneField("number")!.integer()
  52. if let field = message.oneField("type_name") {
  53. type_name = field.string()
  54. }
  55. label = FieldLabel(rawValue:message.oneField("label")!.integer())!
  56. }
  57. func wireType() -> WireType {
  58. switch type {
  59. case .double: return .fixed64
  60. case .float: return .fixed32
  61. case .int64: return .varint
  62. case .uint64: return .varint
  63. case .int32: return .varint
  64. case .fixed64: return .fixed64
  65. case .fixed32: return .fixed32
  66. case .bool: return .varint
  67. case .string: return .lengthDelimited
  68. case .group: return .startGroup
  69. case .message: return .lengthDelimited
  70. case .bytes: return .lengthDelimited
  71. case .uint32: return .varint
  72. case .enumeration: return .varint
  73. case .sfixed32: return .fixed32
  74. case .sfixed64: return .fixed64
  75. case .sint32: return .varint
  76. case .sint64: return .varint
  77. }
  78. }
  79. }