protobuf 2.8.0.beta9 → 2.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +0 -1
- data/{UPGRADING.md → CHANGES.md} +20 -12
- data/LICENSE.txt +14 -0
- data/README.md +60 -74
- data/bin/protoc-gen-ruby +26 -0
- data/bin/rprotoc +4 -58
- data/lib/protobuf.rb +15 -13
- data/lib/protobuf/code_generator.rb +41 -0
- data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +51 -0
- data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +248 -0
- data/lib/protobuf/field/base_field.rb +0 -1
- data/lib/protobuf/field/bytes_field.rb +1 -1
- data/lib/protobuf/field/message_field.rb +2 -4
- data/lib/protobuf/field/varint_field.rb +4 -7
- data/lib/protobuf/generators/base.rb +71 -0
- data/lib/protobuf/generators/enum_generator.rb +31 -0
- data/lib/protobuf/generators/extension_generator.rb +28 -0
- data/lib/protobuf/generators/field_generator.rb +132 -0
- data/lib/protobuf/generators/file_generator.rb +140 -0
- data/lib/protobuf/generators/group_generator.rb +113 -0
- data/lib/protobuf/generators/message_generator.rb +99 -0
- data/lib/protobuf/generators/printable.rb +161 -0
- data/lib/protobuf/generators/service_generator.rb +27 -0
- data/lib/protobuf/lifecycle.rb +7 -3
- data/lib/protobuf/message.rb +51 -34
- data/lib/protobuf/rpc/connectors/common.rb +4 -6
- data/lib/protobuf/rpc/server.rb +1 -1
- data/lib/protobuf/rpc/servers/zmq/server.rb +2 -2
- data/lib/protobuf/rpc/servers/zmq/worker.rb +2 -2
- data/lib/protobuf/rpc/service.rb +3 -3
- data/lib/protobuf/rpc/service_directory.rb +24 -12
- data/lib/protobuf/version.rb +1 -1
- data/proto/google/protobuf/compiler/plugin.proto +147 -0
- data/proto/google/protobuf/descriptor.proto +620 -0
- data/protobuf.gemspec +12 -21
- data/spec/bin/protoc-gen-ruby_spec.rb +18 -0
- data/spec/data/data.bin +3 -0
- data/{test/data/unk.png → spec/data/types.bin} +0 -0
- data/spec/encoding/all_types_spec.rb +91 -0
- data/spec/encoding/extreme_values_spec.rb +0 -0
- data/spec/lib/protobuf/code_generator_spec.rb +60 -0
- data/spec/lib/protobuf/generators/base_spec.rb +87 -0
- data/spec/lib/protobuf/generators/enum_generator_spec.rb +45 -0
- data/spec/lib/protobuf/generators/extension_generator_spec.rb +43 -0
- data/spec/lib/protobuf/generators/field_generator_spec.rb +99 -0
- data/spec/lib/protobuf/generators/file_generator_spec.rb +29 -0
- data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
- data/spec/lib/protobuf/generators/service_generator_spec.rb +43 -0
- data/spec/lib/protobuf/lifecycle_spec.rb +31 -1
- data/spec/lib/protobuf/logger_spec.rb +5 -0
- data/spec/lib/protobuf/message_spec.rb +22 -8
- data/spec/lib/protobuf/rpc/connectors/common_spec.rb +1 -1
- data/spec/lib/protobuf/rpc/service_directory_spec.rb +45 -7
- data/spec/lib/protobuf/rpc/service_spec.rb +3 -3
- data/spec/spec_helper.rb +15 -16
- data/spec/support/test/all_types.data.bin +0 -0
- data/spec/support/test/all_types.data.txt +119 -0
- data/spec/support/test/defaults.pb.rb +25 -0
- data/spec/support/test/defaults.proto +9 -0
- data/spec/support/test/enum.pb.rb +10 -8
- data/spec/support/test/extended.pb.rb +5 -3
- data/spec/support/test/extreme_values.data.bin +0 -0
- data/spec/support/test/google_unittest.pb.rb +543 -0
- data/spec/support/test/google_unittest.proto +713 -0
- data/spec/support/test/google_unittest_import.pb.rb +37 -0
- data/{ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h → spec/support/test/google_unittest_import.proto} +27 -17
- data/spec/support/test/google_unittest_import_public.pb.rb +8 -0
- data/{ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h → spec/support/test/google_unittest_import_public.proto} +5 -31
- data/spec/support/test/multi_field_extensions.pb.rb +29 -7
- data/spec/support/test/multi_field_extensions.proto +12 -2
- data/spec/support/test/resource.pb.rb +20 -16
- metadata +149 -236
- data/examples/addressbook.pb.rb +0 -55
- data/examples/addressbook.proto +0 -24
- data/examples/reading_a_message.rb +0 -32
- data/examples/writing_a_message.rb +0 -46
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +0 -142
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +0 -318
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +0 -99
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +0 -103
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +0 -85
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +0 -167
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +0 -98
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +0 -72
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +0 -159
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +0 -170
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +0 -102
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +0 -103
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +0 -118
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +0 -104
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +0 -2721
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +0 -303
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +0 -84
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +0 -121
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +0 -77
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +0 -108
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +0 -101
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +0 -72
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +0 -213
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +0 -109
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +0 -134
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +0 -121
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +0 -113
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +0 -120
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +0 -113
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +0 -64
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +0 -434
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +0 -73
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +0 -790
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +0 -156
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +0 -108
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +0 -93
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +0 -1367
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +0 -5223
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +0 -366
- data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +0 -136
- data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +0 -904
- data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +0 -424
- data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +0 -82
- data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +0 -1102
- data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +0 -207
- data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +0 -136
- data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +0 -313
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +0 -238
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +0 -357
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +0 -340
- data/ext/protobuf-2.4.1/src/google/protobuf/message.h +0 -692
- data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +0 -239
- data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +0 -64
- data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +0 -80
- data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +0 -1295
- data/ext/protobuf-2.4.1/src/google/protobuf/service.h +0 -291
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +0 -1211
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +0 -220
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +0 -119
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +0 -123
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +0 -121
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +0 -457
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +0 -170
- data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +0 -174
- data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +0 -101
- data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +0 -83
- data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +0 -98
- data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +0 -285
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +0 -11915
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +0 -2895
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +0 -211
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +0 -56
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +0 -188
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +0 -151
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +0 -4752
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +0 -150
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +0 -816
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +0 -197
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +0 -403
- data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +0 -268
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +0 -304
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +0 -620
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +0 -774
- data/ext/ruby_generator/Makefile +0 -10
- data/ext/ruby_generator/RubyGenerator.cpp +0 -544
- data/ext/ruby_generator/RubyGenerator.h +0 -206
- data/ext/ruby_generator/extconf.rb +0 -35
- data/test/data/data_source.py +0 -14
- data/test/data/types_source.py +0 -22
- data/test/proto/addressbook.pb.rb +0 -66
- data/test/proto/addressbook.proto +0 -33
- data/test/proto/addressbook_base.pb.rb +0 -58
- data/test/proto/addressbook_base.proto +0 -26
- data/test/proto/addressbook_ext.pb.rb +0 -20
- data/test/proto/addressbook_ext.proto +0 -6
- data/test/proto/collision.pb.rb +0 -17
- data/test/proto/collision.proto +0 -5
- data/test/proto/ext_collision.pb.rb +0 -24
- data/test/proto/ext_collision.proto +0 -8
- data/test/proto/ext_range.pb.rb +0 -22
- data/test/proto/ext_range.proto +0 -7
- data/test/proto/float_default.proto +0 -10
- data/test/proto/lowercase.pb.rb +0 -30
- data/test/proto/lowercase.proto +0 -9
- data/test/proto/merge.pb.rb +0 -39
- data/test/proto/merge.proto +0 -15
- data/test/proto/nested.pb.rb +0 -30
- data/test/proto/nested.proto +0 -9
- data/test/proto/optional_field.pb.rb +0 -35
- data/test/proto/optional_field.proto +0 -12
- data/test/proto/packed.pb.rb +0 -22
- data/test/proto/packed.proto +0 -6
- data/test/proto/rpc.proto +0 -6
- data/test/proto/types.pb.rb +0 -84
- data/test/proto/types.proto +0 -37
- data/test/test_addressbook.rb +0 -56
- data/test/test_enum_value.rb +0 -41
- data/test/test_extension.rb +0 -36
- data/test/test_lowercase.rb +0 -11
- data/test/test_message.rb +0 -128
- data/test/test_optional_field.rb +0 -103
- data/test/test_packed_field.rb +0 -40
- data/test/test_parse.rb +0 -15
- data/test/test_repeated_types.rb +0 -132
- data/test/test_serialize.rb +0 -61
- data/test/test_standard_message.rb +0 -96
- data/test/test_types.rb +0 -226
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'protobuf/generators/base'
|
2
|
+
require 'protobuf/generators/group_generator'
|
3
|
+
|
4
|
+
module Protobuf
|
5
|
+
module Generators
|
6
|
+
class MessageGenerator < Base
|
7
|
+
|
8
|
+
def initialize(descriptor, indent_level, options = {})
|
9
|
+
super
|
10
|
+
@only_declarations = options.fetch(:declaration) { false }
|
11
|
+
@extension_fields = options.fetch(:extension_fields) { {} }
|
12
|
+
end
|
13
|
+
|
14
|
+
def compile
|
15
|
+
run_once(:compile) do
|
16
|
+
if @only_declarations
|
17
|
+
compile_declaration
|
18
|
+
else
|
19
|
+
compile_message
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def compile_declaration
|
25
|
+
run_once(:compile_declaration) do
|
26
|
+
if is_printable?
|
27
|
+
print_class(descriptor.name, :message) do
|
28
|
+
group = GroupGenerator.new(current_indent)
|
29
|
+
group.add_enums(descriptor.enum_type, :namespace => type_namespace)
|
30
|
+
group.add_message_declarations(descriptor.nested_type)
|
31
|
+
print group.to_s
|
32
|
+
end
|
33
|
+
else
|
34
|
+
print_class(descriptor.name, :message)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def compile_message
|
40
|
+
run_once(:compile_message) do
|
41
|
+
if is_printable?
|
42
|
+
print_class(descriptor.name, nil) do
|
43
|
+
group = GroupGenerator.new(current_indent)
|
44
|
+
group.add_messages(descriptor.nested_type, :extension_fields => @extension_fields, :namespace => type_namespace)
|
45
|
+
group.add_message_fields(descriptor.field)
|
46
|
+
self.class.validate_tags(fully_qualified_type_namespace, descriptor.field.map(&:number))
|
47
|
+
|
48
|
+
group.add_comment(:extension_range, 'Extension Fields')
|
49
|
+
group.add_extension_ranges(descriptor.extension_range) do |extension_range|
|
50
|
+
"extensions #{extension_range.start}...#{extension_range.end}"
|
51
|
+
end
|
52
|
+
|
53
|
+
group.add_extension_fields(message_extension_fields)
|
54
|
+
|
55
|
+
group.order = [ :message, :field, :extension_range, :extension_field ]
|
56
|
+
print group.to_s
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def has_extensions?
|
65
|
+
! message_extension_fields.empty?
|
66
|
+
end
|
67
|
+
|
68
|
+
def has_fields?
|
69
|
+
descriptor.field.count > 0
|
70
|
+
end
|
71
|
+
|
72
|
+
def has_nested_enums?
|
73
|
+
descriptor.enum_type.count > 0
|
74
|
+
end
|
75
|
+
|
76
|
+
def has_nested_messages?
|
77
|
+
descriptor.nested_type.count > 0
|
78
|
+
end
|
79
|
+
|
80
|
+
def has_nested_types?
|
81
|
+
has_nested_enums? || has_nested_messages?
|
82
|
+
end
|
83
|
+
|
84
|
+
def is_printable?
|
85
|
+
if @only_declarations
|
86
|
+
has_nested_types?
|
87
|
+
else
|
88
|
+
has_fields? || has_nested_messages? || has_extensions?
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def message_extension_fields
|
93
|
+
@extension_fields.fetch(fully_qualified_type_namespace) { [] }
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
@@ -0,0 +1,161 @@
|
|
1
|
+
module Protobuf
|
2
|
+
module Generators
|
3
|
+
module Printable
|
4
|
+
|
5
|
+
PARENT_CLASS_MESSAGE = "::Protobuf::Message".freeze
|
6
|
+
PARENT_CLASS_ENUM = "::Protobuf::Enum".freeze
|
7
|
+
PARENT_CLASS_SERVICE = "::Protobuf::Rpc::Service".freeze
|
8
|
+
|
9
|
+
# Initialize the printer.
|
10
|
+
# Must be called by any class/module that includes the Printable module.
|
11
|
+
#
|
12
|
+
def init_printer(indent_level)
|
13
|
+
@io = ::StringIO.new
|
14
|
+
@_indent_level = indent_level.to_i || 0
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
# Print a one-line comment.
|
20
|
+
#
|
21
|
+
def comment(message)
|
22
|
+
puts "# #{message}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def current_indent
|
26
|
+
@_indent_level
|
27
|
+
end
|
28
|
+
|
29
|
+
# Print a "header" comment.
|
30
|
+
#
|
31
|
+
# header("Lorem ipsum dolor")
|
32
|
+
# ##
|
33
|
+
# # Lorem ipsum dolor
|
34
|
+
# #
|
35
|
+
def header(message)
|
36
|
+
puts
|
37
|
+
puts "##"
|
38
|
+
puts "# #{message}"
|
39
|
+
puts "#"
|
40
|
+
end
|
41
|
+
|
42
|
+
# Increase the indent level. An outdent will only occur if given a block
|
43
|
+
# (after the block is finished).
|
44
|
+
#
|
45
|
+
def indent
|
46
|
+
@_indent_level += 1
|
47
|
+
yield
|
48
|
+
outdent
|
49
|
+
end
|
50
|
+
|
51
|
+
# Take a string and upcase the first character of each namespace.
|
52
|
+
# Due to the nature of varying standards about how class/modules are named
|
53
|
+
# (e.g. CamelCase, Underscore_Case, SCREAMING_SNAKE_CASE), we only want
|
54
|
+
# to capitalize the first character to ensure ruby will treat the value
|
55
|
+
# as a constant. Otherwise we do not attempt to change the
|
56
|
+
# token's definition.
|
57
|
+
#
|
58
|
+
# modulize("foo.bar.Baz") -> "::Foo::Bar::Baz"
|
59
|
+
# modulize("foo.bar.baz") -> "::Foo::Bar::Baz"
|
60
|
+
# modulize("foo.bar.BAZ") -> "::Foo::Bar::BAZ"
|
61
|
+
#
|
62
|
+
def modulize(name)
|
63
|
+
name = name.gsub(/\./, '::')
|
64
|
+
name = name.gsub(/(^(?:::)?[a-z]|::[a-z])/) { |match| match.upcase }
|
65
|
+
name
|
66
|
+
end
|
67
|
+
|
68
|
+
# Decrease the indent level. Cannot be negative.
|
69
|
+
#
|
70
|
+
def outdent
|
71
|
+
@_indent_level -= 1 unless @_indent_level == 0
|
72
|
+
end
|
73
|
+
|
74
|
+
# Return the parent class for a given type.
|
75
|
+
# Valid types are :message, :enum, and :service, otherwise an error
|
76
|
+
# will be thrown.
|
77
|
+
#
|
78
|
+
def parent_class(type)
|
79
|
+
case type
|
80
|
+
when :message then
|
81
|
+
PARENT_CLASS_MESSAGE
|
82
|
+
when :enum then
|
83
|
+
PARENT_CLASS_ENUM
|
84
|
+
when :service then
|
85
|
+
PARENT_CLASS_SERVICE
|
86
|
+
else
|
87
|
+
raise "Unknown parent class type #{type}: #{caller[0..5].join("\n")}"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Print a class or module block, indicated by type.
|
92
|
+
# If a class, can be given a parent class to inherit from.
|
93
|
+
# If a block is given, call the block from within an indent block.
|
94
|
+
# Otherwise, end the block on the same line.
|
95
|
+
#
|
96
|
+
def print_block(name, parent_klass, type, &block)
|
97
|
+
name = modulize(name)
|
98
|
+
block_def = "#{type} #{name}"
|
99
|
+
block_def += " < #{parent_class(parent_klass)}" if parent_klass
|
100
|
+
|
101
|
+
if block_given?
|
102
|
+
puts block_def
|
103
|
+
indent { block.call }
|
104
|
+
puts "end"
|
105
|
+
puts
|
106
|
+
else
|
107
|
+
block_def += "; end"
|
108
|
+
puts block_def
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Use print_block to print a class, with optional parent class
|
113
|
+
# to inherit from. Accepts a block for use with print_block.
|
114
|
+
#
|
115
|
+
def print_class(name, parent_klass, &block)
|
116
|
+
print_block(name, parent_klass, :class, &block)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Use print_block to print a module.
|
120
|
+
# Accepts a block for use with print_block.
|
121
|
+
#
|
122
|
+
def print_module(name, &block)
|
123
|
+
print_block(name, nil, :module, &block)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Print a file require.
|
127
|
+
#
|
128
|
+
# print_require('foo/bar/baz') -> "require 'foo/bar/baz'"
|
129
|
+
#
|
130
|
+
def print_require(file)
|
131
|
+
puts "require '#{file}'"
|
132
|
+
end
|
133
|
+
|
134
|
+
# Puts the given message prefixed by the indent level.
|
135
|
+
# If no message is given print a newline.
|
136
|
+
#
|
137
|
+
def puts(message = nil)
|
138
|
+
if message
|
139
|
+
@io.puts((" " * @_indent_level) + message)
|
140
|
+
else
|
141
|
+
@io.puts
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# Print the given message raw, no indent.
|
146
|
+
#
|
147
|
+
def print(contents)
|
148
|
+
@io.print(contents)
|
149
|
+
end
|
150
|
+
|
151
|
+
# Returns the contents of the underlying StringIO object.
|
152
|
+
#
|
153
|
+
def print_contents
|
154
|
+
@io.rewind
|
155
|
+
@io.read
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'protobuf/generators/base'
|
2
|
+
|
3
|
+
module Protobuf
|
4
|
+
module Generators
|
5
|
+
class ServiceGenerator < Base
|
6
|
+
|
7
|
+
def compile
|
8
|
+
run_once(:compile) do
|
9
|
+
print_class(descriptor.name, :service) do
|
10
|
+
descriptor.method.each do |method_descriptor|
|
11
|
+
puts build_method(method_descriptor)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def build_method(method_descriptor)
|
18
|
+
name = method_descriptor.name
|
19
|
+
request_klass = modulize(method_descriptor.input_type)
|
20
|
+
response_klass = modulize(method_descriptor.output_type)
|
21
|
+
return "rpc :#{name.underscore}, #{request_klass}, #{response_klass}"
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/lib/protobuf/lifecycle.rb
CHANGED
@@ -9,16 +9,20 @@ module Protobuf
|
|
9
9
|
lifecycle_events[ event_name ] << blk
|
10
10
|
end
|
11
11
|
|
12
|
-
def self.trigger( event_name )
|
12
|
+
def self.trigger( event_name, *args )
|
13
13
|
event_name = normalized_event_name( event_name )
|
14
14
|
|
15
15
|
if lifecycle_events.has_key?( event_name )
|
16
16
|
lifecycle_events[ event_name ].each do |block|
|
17
|
-
block.
|
17
|
+
if ! args.empty? && block.arity != 0
|
18
|
+
block.call(*args)
|
19
|
+
else
|
20
|
+
block.call
|
21
|
+
end
|
18
22
|
end
|
19
23
|
end
|
20
24
|
end
|
21
|
-
|
25
|
+
|
22
26
|
def self.normalized_event_name( event_name )
|
23
27
|
return "#{event_name}".downcase
|
24
28
|
end
|
data/lib/protobuf/message.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'stringio'
|
1
2
|
require 'set'
|
2
3
|
require 'protobuf/field'
|
3
4
|
require 'protobuf/enum'
|
@@ -22,6 +23,10 @@ module Protobuf
|
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
26
|
+
def self.decode(bytes)
|
27
|
+
self.new.decode(bytes)
|
28
|
+
end
|
29
|
+
|
25
30
|
# Define a field. Don't use this method directly.
|
26
31
|
def self.define_field(rule, type, fname, tag, options)
|
27
32
|
field_array = options[:extension] ? extension_fields : fields
|
@@ -46,6 +51,11 @@ module Protobuf
|
|
46
51
|
end
|
47
52
|
end
|
48
53
|
|
54
|
+
# Create a new object with the given values and return the encoded bytes.
|
55
|
+
def self.encode(values = {})
|
56
|
+
self.new(values).encode
|
57
|
+
end
|
58
|
+
|
49
59
|
# Reserve field numbers for extensions. Don't use this method directly.
|
50
60
|
def self.extensions(range)
|
51
61
|
extension_fields.add_range(range)
|
@@ -92,7 +102,7 @@ module Protobuf
|
|
92
102
|
# Find a field object by +tag+ number.
|
93
103
|
def self.get_field_by_tag(tag)
|
94
104
|
fields[tag]
|
95
|
-
rescue TypeError
|
105
|
+
rescue TypeError
|
96
106
|
tag = tag.nil? ? 'nil' : tag.to_s
|
97
107
|
raise FieldNotDefinedError.new("Tag '#{tag}' does not reference a message field for '#{self.name}'")
|
98
108
|
end
|
@@ -144,6 +154,16 @@ module Protobuf
|
|
144
154
|
copy_to(super, :clone)
|
145
155
|
end
|
146
156
|
|
157
|
+
# Decode the given string bytes into this object.
|
158
|
+
def decode(string)
|
159
|
+
decode_from(::StringIO.new(string))
|
160
|
+
end
|
161
|
+
|
162
|
+
# Decode the given stream into this object.
|
163
|
+
def decode_from(stream)
|
164
|
+
Decoder.decode(stream, self)
|
165
|
+
end
|
166
|
+
|
147
167
|
def dup
|
148
168
|
copy_to(super, :dup)
|
149
169
|
end
|
@@ -174,6 +194,28 @@ module Protobuf
|
|
174
194
|
end
|
175
195
|
end
|
176
196
|
|
197
|
+
def encode
|
198
|
+
stream = ""
|
199
|
+
|
200
|
+
each_field_for_serialization do |field, value|
|
201
|
+
if field.repeated?
|
202
|
+
if field.packed?
|
203
|
+
key = (field.tag << 3) | ::Protobuf::WireType::LENGTH_DELIMITED
|
204
|
+
packed_value = value.map { |val| field.encode(val) }.join
|
205
|
+
stream << ::Protobuf::Field::VarintField.encode(key)
|
206
|
+
stream << ::Protobuf::Field::VarintField.encode(packed_value.size)
|
207
|
+
stream << packed_value
|
208
|
+
else
|
209
|
+
value.each { |val| write_pair(stream, field, val) }
|
210
|
+
end
|
211
|
+
else
|
212
|
+
write_pair(stream, field, value)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
return stream
|
217
|
+
end
|
218
|
+
|
177
219
|
# Returns extension fields. See Message#fields method.
|
178
220
|
def extension_fields
|
179
221
|
self.class.extension_fields
|
@@ -209,14 +251,6 @@ module Protobuf
|
|
209
251
|
to_hash.inspect
|
210
252
|
end
|
211
253
|
|
212
|
-
def parse_from(stream)
|
213
|
-
Decoder.decode(stream, self)
|
214
|
-
end
|
215
|
-
|
216
|
-
def parse_from_string(string)
|
217
|
-
parse_from(StringIO.new(string))
|
218
|
-
end
|
219
|
-
|
220
254
|
def respond_to_has?(key)
|
221
255
|
self.respond_to?(key) && self.has_field?(key)
|
222
256
|
end
|
@@ -226,28 +260,6 @@ module Protobuf
|
|
226
260
|
(self.__send__(key).present? || [true, false].include?(self.__send__(key)))
|
227
261
|
end
|
228
262
|
|
229
|
-
def serialize_to_string
|
230
|
-
stream = ""
|
231
|
-
|
232
|
-
each_field_for_serialization do |field, value|
|
233
|
-
if field.repeated?
|
234
|
-
if field.packed?
|
235
|
-
key = (field.tag << 3) | ::Protobuf::WireType::LENGTH_DELIMITED
|
236
|
-
packed_value = value.map { |val| field.encode(val) }.join
|
237
|
-
stream << ::Protobuf::Field::VarintField.encode(key)
|
238
|
-
stream << ::Protobuf::Field::VarintField.encode(packed_value.size)
|
239
|
-
stream << packed_value
|
240
|
-
else
|
241
|
-
value.each { |val| write_pair(stream, field, val) }
|
242
|
-
end
|
243
|
-
else
|
244
|
-
write_pair(stream, field, value)
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
return stream
|
249
|
-
end
|
250
|
-
|
251
263
|
def set_field(tag, bytes)
|
252
264
|
field = (get_field_by_tag(tag) || get_ext_field_by_tag(tag))
|
253
265
|
field.set(self, bytes) if field
|
@@ -297,11 +309,16 @@ module Protobuf
|
|
297
309
|
##
|
298
310
|
# Instance Aliases
|
299
311
|
#
|
312
|
+
alias_method :parse_from_string, :decode
|
313
|
+
alias_method :deserialize, :decode
|
314
|
+
alias_method :parse_from, :decode_from
|
315
|
+
alias_method :deserialize_from, :decode_from
|
316
|
+
alias_method :to_s, :encode
|
317
|
+
alias_method :bytes, :encode
|
318
|
+
alias_method :serialize, :encode
|
319
|
+
alias_method :serialize_to_string, :encode
|
300
320
|
alias_method :to_hash_value, :to_hash
|
301
321
|
alias_method :to_proto_hash, :to_hash
|
302
|
-
alias_method :to_s, :serialize_to_string
|
303
|
-
alias_method :bytes, :serialize_to_string
|
304
|
-
alias_method :serialize, :serialize_to_string
|
305
322
|
alias_method :responds_to_has?, :respond_to_has?
|
306
323
|
alias_method :respond_to_and_has?, :respond_to_has?
|
307
324
|
alias_method :responds_to_and_has?, :respond_to_has?
|