protobuf.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. min, max = names.minmax
  56. last_common_dot = nil
  57. min.size.times { |i|
  58. if min[i] != max[i] then break end
  59. if min[i] == ?. then last_common_dot = i end
  60. }
  61. if last_common_dot
  62. return min.slice(0, last_common_dot)
  63. end
  64. end
  65. class NestingBuilder
  66. def initialize(msg_names, enum_names)
  67. @to_pos = {nil=>nil}
  68. @msg_children = Hash.new { |hash, key| hash[key] = [] }
  69. @enum_children = Hash.new { |hash, key| hash[key] = [] }
  70. msg_names.each_with_index { |name, idx| @to_pos[name] = idx }
  71. enum_names.each_with_index { |name, idx| @to_pos[name] = idx }
  72. msg_names.each { |name| @msg_children[parent(name)] << name }
  73. enum_names.each { |name| @enum_children[parent(name)] << name }
  74. end
  75. def build(package)
  76. return build_msg(package)
  77. end
  78. private
  79. def build_msg(msg)
  80. return {
  81. :pos => @to_pos[msg],
  82. :msgs => @msg_children[msg].map { |child| build_msg(child) },
  83. :enums => @enum_children[msg].map { |child| @to_pos[child] },
  84. }
  85. end
  86. private
  87. def parent(name)
  88. idx = name.rindex(?.)
  89. if idx
  90. return name.slice(0, idx)
  91. else
  92. return nil
  93. end
  94. end
  95. end
  96. def self.fixup_descriptor(package, msg_names, enum_names)
  97. if package.nil?
  98. package = self.infer_package(msg_names + enum_names)
  99. end
  100. nesting = NestingBuilder.new(msg_names, enum_names).build(package)
  101. return package, nesting
  102. end
  103. end
  104. end
  105. end
  106. end
  107. require 'google/protobuf/repeated_field'
  108. module Google
  109. module Protobuf
  110. def self.encode(msg)
  111. msg.to_proto
  112. end
  113. def self.encode_json(msg, options = {})
  114. msg.to_json(options)
  115. end
  116. def self.decode(klass, proto)
  117. klass.decode(proto)
  118. end
  119. def self.decode_json(klass, json, options = {})
  120. klass.decode_json(json, options)
  121. end
  122. end
  123. end