protobuf.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. # https://developers.google.com/protocol-buffers/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. # require mixins before we hook them into the java & c code
  31. require 'google/protobuf/message_exts'
  32. # We define these before requiring the platform-specific modules.
  33. # That way the module init can grab references to these.
  34. module Google
  35. module Protobuf
  36. class Error < StandardError; end
  37. class ParseError < Error; end
  38. class TypeError < ::TypeError; end
  39. end
  40. end
  41. if RUBY_PLATFORM == "java"
  42. require 'json'
  43. require 'google/protobuf_java'
  44. else
  45. begin
  46. require "google/#{RUBY_VERSION.sub(/\.\d+$/, '')}/protobuf_c"
  47. rescue LoadError
  48. require 'google/protobuf_c'
  49. end
  50. module Google
  51. module Protobuf
  52. module Internal
  53. def self.infer_package(names)
  54. # Package is longest common prefix ending in '.', if any.
  55. if not names.empty?
  56. min, max = names.minmax
  57. last_common_dot = nil
  58. min.size.times { |i|
  59. if min[i] != max[i] then break end
  60. if min[i] == ?. then last_common_dot = i end
  61. }
  62. if last_common_dot
  63. return min.slice(0, last_common_dot)
  64. end
  65. end
  66. nil
  67. end
  68. class NestingBuilder
  69. def initialize(msg_names, enum_names)
  70. @to_pos = {nil=>nil}
  71. @msg_children = Hash.new { |hash, key| hash[key] = [] }
  72. @enum_children = Hash.new { |hash, key| hash[key] = [] }
  73. msg_names.each_with_index { |name, idx| @to_pos[name] = idx }
  74. enum_names.each_with_index { |name, idx| @to_pos[name] = idx }
  75. msg_names.each { |name| @msg_children[parent(name)] << name }
  76. enum_names.each { |name| @enum_children[parent(name)] << name }
  77. end
  78. def build(package)
  79. return build_msg(package)
  80. end
  81. private
  82. def build_msg(msg)
  83. return {
  84. :pos => @to_pos[msg],
  85. :msgs => @msg_children[msg].map { |child| build_msg(child) },
  86. :enums => @enum_children[msg].map { |child| @to_pos[child] },
  87. }
  88. end
  89. private
  90. def parent(name)
  91. idx = name.rindex(?.)
  92. if idx
  93. return name.slice(0, idx)
  94. else
  95. return nil
  96. end
  97. end
  98. end
  99. def self.fixup_descriptor(package, msg_names, enum_names)
  100. if package.nil?
  101. package = self.infer_package(msg_names + enum_names)
  102. end
  103. nesting = NestingBuilder.new(msg_names, enum_names).build(package)
  104. return package, nesting
  105. end
  106. end
  107. end
  108. end
  109. end
  110. require 'google/protobuf/repeated_field'
  111. module Google
  112. module Protobuf
  113. def self.encode(msg)
  114. msg.to_proto
  115. end
  116. def self.encode_json(msg, options = {})
  117. msg.to_json(options)
  118. end
  119. def self.decode(klass, proto)
  120. klass.decode(proto)
  121. end
  122. def self.decode_json(klass, json, options = {})
  123. klass.decode_json(json, options)
  124. end
  125. end
  126. end