Skip to content

Instantly share code, notes, and snippets.

@qnighy
Last active April 22, 2020 08:07
Show Gist options
  • Save qnighy/099a39e70e66d8b402b193d4b0471aee to your computer and use it in GitHub Desktop.
Save qnighy/099a39e70e66d8b402b193d4b0471aee to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
#
# generated_code.rb is in the same directory as this test.
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
old_gc = GC.stress
GC.stress = 0x01 | 0x04
require 'generated_code_pb'
require 'generated_code_proto2_pb'
GC.stress = old_gc
require 'test/unit'
class GCTest < Test::Unit::TestCase
def get_msg_proto3
A::B::C::TestMessage.new(
:optional_int32 => 1,
:optional_int64 => 1,
:optional_uint32 => 1,
:optional_uint64 => 1,
:optional_bool => true,
:optional_double => 1.0,
:optional_float => 1.0,
:optional_string => "a",
:optional_bytes => "b",
:optional_enum => A::B::C::TestEnum::A,
:optional_msg => A::B::C::TestMessage.new(),
:repeated_int32 => [1],
:repeated_int64 => [1],
:repeated_uint32 => [1],
:repeated_uint64 => [1],
:repeated_bool => [true],
:repeated_double => [1.0],
:repeated_float => [1.0],
:repeated_string => ["a"],
:repeated_bytes => ["b"],
:repeated_enum => [A::B::C::TestEnum::A],
:repeated_msg => [A::B::C::TestMessage.new()],
:map_int32_string => {1 => "a"},
:map_int64_string => {1 => "a"},
:map_uint32_string => {1 => "a"},
:map_uint64_string => {1 => "a"},
:map_bool_string => {true => "a"},
:map_string_string => {"a" => "a"},
:map_string_msg => {"a" => A::B::C::TestMessage.new()},
:map_string_int32 => {"a" => 1},
:map_string_bool => {"a" => true},
)
end
def get_msg_proto2
A::B::Proto2::TestMessage.new(
:optional_int32 => 1,
:optional_int64 => 1,
:optional_uint32 => 1,
:optional_uint64 => 1,
:optional_bool => true,
:optional_double => 1.0,
:optional_float => 1.0,
:optional_string => "a",
:optional_bytes => "b",
:optional_enum => A::B::Proto2::TestEnum::A,
:optional_msg => A::B::Proto2::TestMessage.new(),
:repeated_int32 => [1],
:repeated_int64 => [1],
:repeated_uint32 => [1],
:repeated_uint64 => [1],
:repeated_bool => [true],
:repeated_double => [1.0],
:repeated_float => [1.0],
:repeated_string => ["a"],
:repeated_bytes => ["b"],
:repeated_enum => [A::B::Proto2::TestEnum::A],
:repeated_msg => [A::B::Proto2::TestMessage.new()],
:required_int32 => 1,
:required_int64 => 1,
:required_uint32 => 1,
:required_uint64 => 1,
:required_bool => true,
:required_double => 1.0,
:required_float => 1.0,
:required_string => "a",
:required_bytes => "b",
:required_enum => A::B::Proto2::TestEnum::A,
:required_msg => A::B::Proto2::TestMessage.new(),
)
end
def test_generated_msg
old_gc = GC.stress
GC.stress = 0x01 | 0x04
from = get_msg_proto3
data = A::B::C::TestMessage.encode(from)
to = A::B::C::TestMessage.decode(data)
from = get_msg_proto2
data = A::B::Proto2::TestMessage.encode(from)
to = A::B::Proto2::TestMessage.decode(data)
GC.stress = old_gc
puts "passed"
end
end
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: tests/generated_code.proto
require 'google/protobuf'
Google::Protobuf::DescriptorPool.generated_pool.build do
add_file("tests/generated_code.proto", :syntax => :proto3) do
add_message "a.b.c.TestMessage" do
optional :optional_int32, :int32, 1
optional :optional_int64, :int64, 2
optional :optional_uint32, :uint32, 3
optional :optional_uint64, :uint64, 4
optional :optional_bool, :bool, 5
optional :optional_double, :double, 6
optional :optional_float, :float, 7
optional :optional_string, :string, 8
optional :optional_bytes, :bytes, 9
optional :optional_enum, :enum, 10, "a.b.c.TestEnum"
optional :optional_msg, :message, 11, "a.b.c.TestMessage"
repeated :repeated_int32, :int32, 21
repeated :repeated_int64, :int64, 22
repeated :repeated_uint32, :uint32, 23
repeated :repeated_uint64, :uint64, 24
repeated :repeated_bool, :bool, 25
repeated :repeated_double, :double, 26
repeated :repeated_float, :float, 27
repeated :repeated_string, :string, 28
repeated :repeated_bytes, :bytes, 29
repeated :repeated_enum, :enum, 30, "a.b.c.TestEnum"
repeated :repeated_msg, :message, 31, "a.b.c.TestMessage"
map :map_int32_string, :int32, :string, 61
map :map_int64_string, :int64, :string, 62
map :map_uint32_string, :uint32, :string, 63
map :map_uint64_string, :uint64, :string, 64
map :map_bool_string, :bool, :string, 65
map :map_string_string, :string, :string, 66
map :map_string_msg, :string, :message, 67, "a.b.c.TestMessage"
map :map_string_enum, :string, :enum, 68, "a.b.c.TestEnum"
map :map_string_int32, :string, :int32, 69
map :map_string_bool, :string, :bool, 70
optional :nested_message, :message, 80, "a.b.c.TestMessage.NestedMessage"
oneof :my_oneof do
optional :oneof_int32, :int32, 41
optional :oneof_int64, :int64, 42
optional :oneof_uint32, :uint32, 43
optional :oneof_uint64, :uint64, 44
optional :oneof_bool, :bool, 45
optional :oneof_double, :double, 46
optional :oneof_float, :float, 47
optional :oneof_string, :string, 48
optional :oneof_bytes, :bytes, 49
optional :oneof_enum, :enum, 50, "a.b.c.TestEnum"
optional :oneof_msg, :message, 51, "a.b.c.TestMessage"
end
end
add_message "a.b.c.TestMessage.NestedMessage" do
optional :foo, :int32, 1
end
add_message "a.b.c.testLowercaseNested" do
end
add_message "a.b.c.testLowercaseNested.lowercase" do
end
add_message "a.b.c.TestUnknown" do
optional :optional_unknown, :message, 11, "a.b.c.TestUnknown"
repeated :repeated_unknown, :message, 31, "a.b.c.TestUnknown"
map :map_unknown, :string, :message, 67, "a.b.c.TestUnknown"
optional :unknown_field, :int32, 89
oneof :my_oneof do
optional :oneof_unknown, :message, 51, "a.b.c.TestUnknown"
end
end
add_enum "a.b.c.TestEnum" do
value :Default, 0
value :A, 1
value :B, 2
value :C, 3
end
end
end
module A
module B
module C
TestMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("a.b.c.TestMessage").msgclass
TestMessage::NestedMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("a.b.c.TestMessage.NestedMessage").msgclass
TestLowercaseNested = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("a.b.c.testLowercaseNested").msgclass
TestLowercaseNested::Lowercase = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("a.b.c.testLowercaseNested.lowercase").msgclass
TestUnknown = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("a.b.c.TestUnknown").msgclass
TestEnum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("a.b.c.TestEnum").enummodule
end
end
end
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: tests/generated_code_proto2.proto
require 'google/protobuf'
Google::Protobuf::DescriptorPool.generated_pool.build do
add_file("tests/generated_code_proto2.proto", :syntax => :proto2) do
add_message "a.b.proto2.TestMessage" do
optional :optional_int32, :int32, 1
optional :optional_int64, :int64, 2
optional :optional_uint32, :uint32, 3
optional :optional_uint64, :uint64, 4
optional :optional_bool, :bool, 5
optional :optional_double, :double, 6
optional :optional_float, :float, 7
optional :optional_string, :string, 8
optional :optional_bytes, :bytes, 9
optional :optional_enum, :enum, 10, "a.b.proto2.TestEnum"
optional :optional_msg, :message, 11, "a.b.proto2.TestMessage"
repeated :repeated_int32, :int32, 21
repeated :repeated_int64, :int64, 22
repeated :repeated_uint32, :uint32, 23
repeated :repeated_uint64, :uint64, 24
repeated :repeated_bool, :bool, 25
repeated :repeated_double, :double, 26
repeated :repeated_float, :float, 27
repeated :repeated_string, :string, 28
repeated :repeated_bytes, :bytes, 29
repeated :repeated_enum, :enum, 30, "a.b.proto2.TestEnum"
repeated :repeated_msg, :message, 31, "a.b.proto2.TestMessage"
required :required_int32, :int32, 41
required :required_int64, :int64, 42
required :required_uint32, :uint32, 43
required :required_uint64, :uint64, 44
required :required_bool, :bool, 45
required :required_double, :double, 46
required :required_float, :float, 47
required :required_string, :string, 48
required :required_bytes, :bytes, 49
required :required_enum, :enum, 50, "a.b.proto2.TestEnum"
required :required_msg, :message, 51, "a.b.proto2.TestMessage"
optional :nested_message, :message, 80, "a.b.proto2.TestMessage.NestedMessage"
oneof :my_oneof do
optional :oneof_int32, :int32, 61
optional :oneof_int64, :int64, 62
optional :oneof_uint32, :uint32, 63
optional :oneof_uint64, :uint64, 64
optional :oneof_bool, :bool, 65
optional :oneof_double, :double, 66
optional :oneof_float, :float, 67
optional :oneof_string, :string, 68
optional :oneof_bytes, :bytes, 69
optional :oneof_enum, :enum, 70, "a.b.proto2.TestEnum"
optional :oneof_msg, :message, 71, "a.b.proto2.TestMessage"
end
end
add_message "a.b.proto2.TestMessage.NestedMessage" do
optional :foo, :int32, 1
end
add_message "a.b.proto2.TestUnknown" do
optional :optional_unknown, :message, 11, "a.b.proto2.TestUnknown"
repeated :repeated_unknown, :message, 31, "a.b.proto2.TestUnknown"
optional :unknown_field, :int32, 89
oneof :my_oneof do
optional :oneof_unknown, :message, 51, "a.b.proto2.TestUnknown"
end
end
add_enum "a.b.proto2.TestEnum" do
value :Default, 0
value :A, 1
value :B, 2
value :C, 3
end
end
end
module A
module B
module Proto2
TestMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("a.b.proto2.TestMessage").msgclass
TestMessage::NestedMessage = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("a.b.proto2.TestMessage.NestedMessage").msgclass
TestUnknown = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("a.b.proto2.TestUnknown").msgclass
TestEnum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("a.b.proto2.TestEnum").enummodule
end
end
end
# Protocol Buffers - Google's data interchange format
# Copyright 2008 Google Inc. All rights reserved.
# https://developers.google.com/protocol-buffers/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# require mixins before we hook them into the java & c code
require 'google/protobuf/message_exts'
# We define these before requiring the platform-specific modules.
# That way the module init can grab references to these.
module Google
module Protobuf
class Error < StandardError; end
class ParseError < Error; end
class TypeError < ::TypeError; end
end
end
if RUBY_PLATFORM == "java"
require 'json'
require 'google/protobuf_java'
else
begin
require "google/#{RUBY_VERSION.sub(/\.\d+$/, '')}/protobuf_c"
rescue LoadError
require 'google/protobuf_c'
end
module Google
module Protobuf
module Internal
def self.infer_package(names)
# Package is longest common prefix ending in '.', if any.
if not names.empty?
min, max = names.minmax
last_common_dot = nil
min.size.times { |i|
if min[i] != max[i] then break end
if min[i] == ?. then last_common_dot = i end
}
if last_common_dot
return min.slice(0, last_common_dot)
end
end
nil
end
class NestingBuilder
def initialize(msg_names, enum_names)
@to_pos = {nil=>nil}
@msg_children = Hash.new { |hash, key| hash[key] = [] }
@enum_children = Hash.new { |hash, key| hash[key] = [] }
msg_names.each_with_index { |name, idx| @to_pos[name] = idx }
enum_names.each_with_index { |name, idx| @to_pos[name] = idx }
msg_names.each { |name| @msg_children[parent(name)] << name }
enum_names.each { |name| @enum_children[parent(name)] << name }
end
def build(package)
return build_msg(package)
end
private
def build_msg(msg)
return {
:pos => @to_pos[msg],
:msgs => @msg_children[msg].map { |child| build_msg(child) },
:enums => @enum_children[msg].map { |child| @to_pos[child] },
}
end
private
def parent(name)
idx = name.rindex(?.)
if idx
return name.slice(0, idx)
else
return nil
end
end
end
def self.fixup_descriptor(package, msg_names, enum_names)
if package.nil?
package = self.infer_package(msg_names + enum_names)
end
nesting = NestingBuilder.new(msg_names, enum_names).build(package)
return package, nesting
end
end
end
end
end
require 'google/protobuf/repeated_field'
module Google
module Protobuf
def self.encode(msg)
msg.to_proto
end
def self.encode_json(msg, options = {})
msg.to_json(options)
end
def self.decode(klass, proto)
klass.decode(proto)
end
def self.decode_json(klass, json, options = {})
klass.decode_json(json, options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment