main.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // read and write protos without generated code
  35. // 1. READING
  36. // - load a FileDescriptorSet describing a message to be read
  37. // - read that message into a standard container
  38. // - access message fields using methods of that container
  39. // 2. WRITING
  40. // - load a FileDescriptorSet describing a proto to be written
  41. // - use a standard container to add and set message fields
  42. // - use the container to write the message
  43. // applications
  44. // - dynamic proto inspection
  45. // - testing
  46. // - quick app development without requiring code generation
  47. // From https://developers.google.com/protocol-buffers/docs/techniques#self-description:
  48. // "All that said, the reason that this functionality is not included in the Protocol
  49. // Buffer library is because we have never had a use for it inside Google"
  50. // where am I?
  51. func whereami() {
  52. let fileManager = FileManager.default
  53. if (!fileManager.fileExists(atPath:"Samples")) {
  54. print("\nThis tool should be run from the project directory that contains \"Samples\".")
  55. print("\nSet the working directory in Xcode with:")
  56. print(" \"Edit Scheme...\">\"Run\">\"Options\">\"Working Directory\".")
  57. let path = fileManager.currentDirectoryPath
  58. print("\nCurrent directory:")
  59. print(" " + path + "\n")
  60. exit(0)
  61. }
  62. }
  63. whereami()
  64. // This generates a data description of FileDescriptorSets that can be used to read them
  65. func regenerate() {
  66. if let descriptorProto = NSData(contentsOfFile:"Samples/descriptor.out") {
  67. if let descriptorMessage = FileDescriptorSet().readMessage(name:"FileDescriptorSet",
  68. proto:descriptorProto as Data) {
  69. let builder = CodeBuilder(descriptorMessage)
  70. do {
  71. try builder.string().write(toFile:"_FileDescriptor.swift",
  72. atomically:false,
  73. encoding: String.Encoding.utf8)
  74. } catch let err as NSError {
  75. print(err)
  76. }
  77. descriptorMessage.display()
  78. }
  79. }
  80. }
  81. regenerate()
  82. func stickynote_reader() {
  83. if let fileDescriptorSetProto = NSData(contentsOfFile:"Samples/stickynote.out") {
  84. // load a FileDescriptorSet that includes a descriptor for the message to be read
  85. let fileDescriptorSet = FileDescriptorSet(proto:fileDescriptorSetProto as Data)
  86. // load a proto with the specified message descriptor
  87. if let messageProto = NSData(contentsOfFile: "Samples/StickyNoteRequest.bin") {
  88. if let message = fileDescriptorSet.readMessage(name:"StickyNoteRequest",
  89. proto:messageProto as Data) {
  90. // display the message
  91. message.display()
  92. message.forOneField(name:"message") {(field) in print(field.string())}
  93. }
  94. }
  95. }
  96. }
  97. stickynote_reader()
  98. func sample_reader() {
  99. if let fileDescriptorSetProto = NSData(contentsOfFile:"Samples/sample.out") {
  100. // load a FileDescriptorSet that includes a descriptor for the message to be read
  101. let fileDescriptorSet = FileDescriptorSet(proto:fileDescriptorSetProto as Data)
  102. // load a proto with the specified message descriptor
  103. if let messageProto = NSData(contentsOfFile:"Samples/SampleMessage.bin") {
  104. if let message = fileDescriptorSet.readMessage(name:"SampleMessage",
  105. proto:messageProto as Data) {
  106. // display the message
  107. message.display()
  108. message.forOneField(name:"text") {(field) in print(field.string())}
  109. }
  110. }
  111. }
  112. }
  113. sample_reader()
  114. func sample_writer() {
  115. if let fileDescriptorSetProto = NSData(contentsOfFile:"Samples/sample.out") {
  116. // load a FileDescriptorSet that includes a descriptor for the message to be created
  117. let fileDescriptorSet = FileDescriptorSet(proto:fileDescriptorSetProto as Data)
  118. // construct an internal representation of the message
  119. if let message = fileDescriptorSet.createMessage(name:"SampleMessage") {
  120. message.addField(name:"d") {(field) in field.setDouble(-12.34)}
  121. message.addField(name:"f") {(field) in field.setFloat(-56.78)}
  122. message.addField(name:"i64") {(field) in field.setInt(123)}
  123. message.addField(name:"ui64") {(field) in field.setInt(123456)}
  124. message.addField(name:"i32") {(field) in field.setInt(123)}
  125. message.addField(name:"f64") {(field) in field.setInt(123)}
  126. message.addField(name:"f32") {(field) in field.setInt(123)}
  127. message.addField(name:"b") {(field) in field.setBool(true)}
  128. message.addField(name:"text") {(field) in field.setString("hello, world")}
  129. message.addField(name:"message") {(field) in
  130. let innerMessage = fileDescriptorSet.createMessage(name:"SampleMessage")!
  131. innerMessage.addField(name:"text") {(field) in field.setString("inner message")}
  132. innerMessage.addField(name:"i32") {(field) in field.setInt(54321)}
  133. innerMessage.addField(name:"message") {(field) in
  134. let innermostMessage = fileDescriptorSet.createMessage(name:"SampleMessage")!
  135. innermostMessage.addField(name:"text") {(field) in field.setString("innermost message")}
  136. innermostMessage.addField(name:"i32") {(field) in field.setInt(12345)}
  137. field.setMessage(innermostMessage)
  138. }
  139. field.setMessage(innerMessage)
  140. }
  141. message.addField(name:"data") {(field) in
  142. let data = "ABCDEFG 123".data(using: .utf8)!
  143. field.setData(data)
  144. }
  145. message.addField(name:"ui32") {(field) in field.setInt(123456)}
  146. message.addField(name:"sf32") {(field) in field.setInt(123456)}
  147. message.addField(name:"sf64") {(field) in field.setInt(123456)}
  148. message.addField(name:"si32") {(field) in field.setInt(-123)}
  149. message.addField(name:"si64") {(field) in field.setInt(-123)}
  150. message.display()
  151. // write the message as a protocol buffer
  152. let data = message.serialize()
  153. NSData(data:data).write(toFile: "SampleRequest.out", atomically: false)
  154. // re-read it
  155. if let message = fileDescriptorSet.readMessage(name:"SampleMessage",
  156. proto:data) {
  157. // display the message
  158. print("REDISPLAY")
  159. message.display()
  160. message.forOneField(name:"text") {(field) in print(field.string())}
  161. }
  162. }
  163. }
  164. }
  165. sample_writer()