CodeBuilder.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 code generator that builds a structure describing the contents of the FileDescriptorSet proto
  35. public class CodeBuilder {
  36. private var code : String = ""
  37. public func string() -> String {
  38. return code
  39. }
  40. /// the initializer builds a code representation from a message
  41. public init(_ message: Message) {
  42. code = ""
  43. code += "import Foundation\n"
  44. code += "\n"
  45. code += "var _FileDescriptor : [[String:Any]] = [\n"
  46. message.forEachField(["file","message_type"]) {(field) in
  47. generateProtoDescription(field:field)
  48. }
  49. code += "];\n"
  50. }
  51. /// generate code for a dictionary literal describing a proto
  52. private func generateProtoDescription(field: Field) {
  53. field.message().forEachField("name") {(field) in
  54. code += " [\"name\": \"\(field.string())\",\n"
  55. code += " \"fields\": [\n"
  56. }
  57. field.message().forEachField("field") {(field) in
  58. var name : String?
  59. var number : Int?
  60. var type : Int = 0
  61. var label : Int = 0
  62. var typeName : String? = ""
  63. for field in field.message().fields {
  64. if field.name() == "name" {
  65. name = field.string()
  66. } else if field.name() == "number" {
  67. number = field.integer()
  68. } else if field.name() == "type_name" {
  69. typeName = field.string()
  70. } else if field.name() == "type" {
  71. type = field.integer()
  72. } else if field.name() == "label" {
  73. label = field.integer()
  74. }
  75. }
  76. if let name = name,
  77. let number = number,
  78. let typeName = typeName {
  79. code += " [\"number\":\(number), \"name\":\"\(name)\", \"label\":\(label), \"type\":\(type), \"type_name\":\"\(typeName)\"],\n"
  80. }
  81. }
  82. code += " ]],\n"
  83. field.message().forEachField("nested_type") {(field) in
  84. generateProtoDescription(field:field)
  85. }
  86. }
  87. }