ruby-protocol-buffers 1.2.2 → 1.2.3.beta1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.2
1
+ 1.2.3.beta1
@@ -128,9 +128,6 @@ HEADER
128
128
  end
129
129
  line fieldline
130
130
  end
131
-
132
- line
133
- line "gen_methods! # new fields ignored after this point"
134
131
  end
135
132
  line
136
133
  end
@@ -120,22 +120,40 @@ module ProtocolBuffers
120
120
  @opts = opts.dup
121
121
  end
122
122
 
123
- def add_methods_to(klass)
124
- klass.class_eval <<-EOF, __FILE__, __LINE__+1
125
- attr_reader :#{name}
126
- EOF
123
+ def add_reader_to(klass)
124
+ if repeated?
125
+ klass.class_eval <<-EOF, __FILE__, __LINE__+1
126
+ def #{name}
127
+ unless @#{name}
128
+ @#{name} = RepeatedField.new(fields[#{tag}])
129
+ end
130
+ @#{name}
131
+ end
132
+ EOF
133
+ else
134
+ klass.class_eval <<-EOF, __FILE__, __LINE__+1
135
+ def #{name}
136
+ if @set_fields[#{tag}] == nil
137
+ # first access of this field, generate it
138
+ initialize_field(#{tag})
139
+ end
140
+ @#{name}
141
+ end
142
+ EOF
143
+ end
144
+ end
145
+
146
+ def add_writer_to(klass)
127
147
  if repeated?
128
148
  klass.class_eval <<-EOF, __FILE__, __LINE__+1
129
149
  def #{name}=(value)
130
150
  if value.nil?
131
- @#{name}.clear
151
+ #{name}.clear
132
152
  else
133
- @#{name}.clear
153
+ #{name}.clear
134
154
  value.each { |i| @#{name}.push i }
135
155
  end
136
156
  end
137
-
138
- def has_#{name}?; true; end
139
157
  EOF
140
158
  else
141
159
  klass.class_eval <<-EOF, __FILE__, __LINE__+1
@@ -154,7 +172,23 @@ module ProtocolBuffers
154
172
  end
155
173
  end
156
174
  end
175
+ EOF
176
+ end
177
+ end
178
+
179
+ def add_methods_to(klass)
180
+ add_reader_to(klass)
181
+ add_writer_to(klass)
157
182
 
183
+ if repeated?
184
+ # repeated fields are always "set"
185
+ klass.initial_set_fields[tag] = true
186
+
187
+ klass.class_eval <<-EOF, __FILE__, __LINE__+1
188
+ def has_#{name}?; true; end
189
+ EOF
190
+ else
191
+ klass.class_eval <<-EOF, __FILE__, __LINE__+1
158
192
  def has_#{name}?
159
193
  value_for_tag?(#{tag})
160
194
  end
@@ -533,6 +567,8 @@ module ProtocolBuffers
533
567
 
534
568
  class MessageField < Field
535
569
  include WireFormats::LENGTH_DELIMITED
570
+
571
+ attr_reader :proxy_class
536
572
 
537
573
  def initialize(proxy_class, otype, name, tag, opts = {})
538
574
  super(otype, name, tag, opts)
@@ -232,22 +232,7 @@ module ProtocolBuffers
232
232
  # message = MyMessageClass.new
233
233
  # message.attributes = attributes
234
234
  def initialize(attributes = {})
235
- @set_fields = []
236
-
237
- fields.each do |tag, field|
238
- if field.repeated?
239
- self.instance_variable_set("@#{field.name}", RepeatedField.new(field))
240
- @set_fields[tag] = true # repeated fields are always "set"
241
- else
242
- value = field.default_value
243
- self.__send__("#{field.name}=", value)
244
- @set_fields[tag] = false
245
- if field.class == Field::MessageField
246
- value.notify_on_change(self, tag)
247
- end
248
- end
249
- end
250
-
235
+ @set_fields = self.class.initial_set_fields.dup
251
236
  self.attributes = attributes
252
237
  end
253
238
 
@@ -354,6 +339,10 @@ module ProtocolBuffers
354
339
  @fields || @fields = {}
355
340
  end
356
341
 
342
+ def self.initial_set_fields
343
+ @set_fields ||= []
344
+ end
345
+
357
346
  # Returns a hash of { tag => ProtocolBuffers::Field }
358
347
  def fields
359
348
  self.class.fields
@@ -418,7 +407,6 @@ module ProtocolBuffers
418
407
  end
419
408
 
420
409
  def self.define_field(otype, type, name, tag, opts = {}) # :NODOC:
421
- raise("gen_methods! already called, cannot add more fields") if @methods_generated
422
410
  type = type.is_a?(Module) ? type : type.to_sym
423
411
  name = name.to_sym
424
412
  tag = tag.to_i
@@ -462,11 +450,13 @@ module ProtocolBuffers
462
450
  return true unless @has_required_field
463
451
 
464
452
  fields.each do |tag, field|
465
- if field.otype == :required && !message.value_for_tag?(tag)
466
- return false unless raise_exception
467
- raise(ProtocolBuffers::EncodeError.new(field), "Required field '#{field.name}' is invalid")
468
- end
453
+ next if field.otype != :required
454
+ next if message.value_for_tag?(tag) && (field.class != Field::MessageField || message.value_for_tag(tag).valid?)
455
+ return false unless raise_exception
456
+ raise(ProtocolBuffers::EncodeError.new(field), "Required field '#{field.name}' is invalid")
469
457
  end
458
+
459
+ true
470
460
  end
471
461
 
472
462
  def validate!
@@ -492,14 +482,21 @@ module ProtocolBuffers
492
482
  (@unknown_fields || []).size
493
483
  end
494
484
 
495
- # Generate the initialize method using reflection, to improve speed. This is
496
- # called by the generated .pb.rb code, it's not necessary to call this
497
- # method directly.
485
+ # left in for compatibility with previously created .pb.rb files -- no longer used
498
486
  def self.gen_methods! # :NODOC:
499
487
  @methods_generated = true
500
- # these generated methods have gone away for now -- the infrastructure has
501
- # been left in place, since they'll probably make their way back in at
502
- # some point.
488
+ end
489
+
490
+ protected
491
+
492
+ def initialize_field(tag)
493
+ field = fields[tag]
494
+ new_value = field.default_value
495
+ self.instance_variable_set("@#{field.name}", new_value)
496
+ if field.class == Field::MessageField
497
+ new_value.notify_on_change(self, tag)
498
+ end
499
+ @set_fields[tag] = false
503
500
  end
504
501
 
505
502
  end
data/spec/fields_spec.rb CHANGED
@@ -46,4 +46,8 @@ describe ProtocolBuffers, "fields" do
46
46
  end
47
47
  end
48
48
 
49
+ it "provides a reader for proxy_class on message fields" do
50
+ ProtocolBuffers::Field::MessageField.new(nil, :optional, :fake_name, 1).should respond_to(:proxy_class)
51
+ ProtocolBuffers::Field::MessageField.new(Class, :optional, :fake_name, 1).proxy_class.should == Class
52
+ end
49
53
  end
data/spec/runtime_spec.rb CHANGED
@@ -81,6 +81,45 @@ describe ProtocolBuffers, "runtime" do
81
81
  a1.sub2.has_subsub1?.should == true
82
82
  end
83
83
 
84
+ it "allows directly recursive sub-messages" do
85
+ ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
86
+ package foo;
87
+ message Foo {
88
+ optional int32 payload = 1;
89
+ optional Foo foo = 2;
90
+ }
91
+ EOS
92
+
93
+ foo = Foo::Foo.new
94
+ foo.has_foo?.should == false
95
+ foo.foo.payload = 17
96
+ foo.has_foo?.should == true
97
+ foo.foo.has_foo?.should == false
98
+ end
99
+
100
+ it "allows indirectly recursive sub-messages" do
101
+ ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
102
+ package foo;
103
+ message Foo {
104
+ optional int32 payload = 1;
105
+ optional Bar bar = 2;
106
+ }
107
+
108
+ message Bar {
109
+ optional Foo foo = 1;
110
+ optional int32 payload = 2;
111
+ }
112
+ EOS
113
+
114
+ foo = Foo::Foo.new
115
+ foo.has_bar?.should == false
116
+ foo.bar.payload = 17
117
+ foo.has_bar?.should == true
118
+ foo.bar.has_foo?.should == false
119
+ foo.bar.foo.payload = 23
120
+ foo.bar.has_foo?.should == true
121
+ end
122
+
84
123
  it "pretends that repeated fields are arrays" do
85
124
  # make sure our RepeatedField class acts like a normal Array
86
125
  ProtocolBuffers::Compiler.compile_and_load_string <<-EOS
@@ -239,10 +278,8 @@ describe ProtocolBuffers, "runtime" do
239
278
  sub.payload.should == ""
240
279
  end
241
280
 
242
- it "doesn't allow defining fields after gen_methods is called" do
243
- proc do
244
- A.define_field(:optional, :string, "newfield", 15)
245
- end.should raise_error()
281
+ it "responds to gen_methods! for backwards compat" do
282
+ A.gen_methods!
246
283
  end
247
284
 
248
285
  def filled_in_bit
@@ -496,16 +533,29 @@ describe ProtocolBuffers, "runtime" do
496
533
  res3 = TehUnknown3::MyResult.parse(serialized2)
497
534
  res3.field_1.should == 2
498
535
  end
499
-
536
+
500
537
  it "can compile and instantiate a message in a package with under_scores" do
501
538
  Object.send(:remove_const, :UnderScore) if defined?(UnderScore)
502
-
539
+
503
540
  ProtocolBuffers::Compiler.compile_and_load(
504
541
  File.join(File.dirname(__FILE__), "proto_files", "under_score_package.proto"))
505
-
542
+
506
543
  proc do
507
544
  under_test = UnderScore::UnderTest.new
508
545
  end.should_not raise_error()
509
546
  end
510
547
 
548
+ describe "Message#valid?" do
549
+ it "should validate sub-messages" do
550
+ f = Featureful::A.new
551
+ f.i3 = 1
552
+ f.sub3 = Featureful::A::Sub.new
553
+ f.valid?.should == false
554
+ f.sub3.valid?.should == false
555
+ f.sub3.payload_type = Featureful::A::Sub::Payloads::P1
556
+ f.valid?.should == true
557
+ f.sub3.valid?.should == true
558
+ end
559
+ end
560
+
511
561
  end
metadata CHANGED
@@ -1,105 +1,82 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ruby-protocol-buffers
3
- version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
6
- segments:
7
- - 1
8
- - 2
9
- - 2
10
- version: 1.2.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.3.beta1
5
+ prerelease: 6
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Brian Palmer
14
9
  - Rob Marable
15
10
  - Paulo Luis Franchini Casaretto
16
11
  autorequire:
17
12
  bindir: bin
18
13
  cert_chain: []
19
-
20
- date: 2012-01-16 00:00:00 Z
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
14
+ date: 2012-04-25 00:00:00.000000000Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
23
17
  name: rspec
24
- prerelease: false
25
- requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirement: &70124285265220 !ruby/object:Gem::Requirement
26
19
  none: false
27
- requirements:
20
+ requirements:
28
21
  - - ~>
29
- - !ruby/object:Gem::Version
30
- hash: 9
31
- segments:
32
- - 2
33
- - 5
34
- version: "2.5"
22
+ - !ruby/object:Gem::Version
23
+ version: '2.5'
35
24
  type: :development
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: autotest-standalone
39
25
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
26
+ version_requirements: *70124285265220
27
+ - !ruby/object:Gem::Dependency
28
+ name: autotest-standalone
29
+ requirement: &70124285264800 !ruby/object:Gem::Requirement
41
30
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 3
46
- segments:
47
- - 0
48
- version: "0"
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
49
35
  type: :development
50
- version_requirements: *id002
51
- - !ruby/object:Gem::Dependency
52
- name: autotest-growl
53
36
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
37
+ version_requirements: *70124285264800
38
+ - !ruby/object:Gem::Dependency
39
+ name: autotest-growl
40
+ requirement: &70124285264340 !ruby/object:Gem::Requirement
55
41
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
- version: "0"
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
63
46
  type: :development
64
- version_requirements: *id003
65
- - !ruby/object:Gem::Dependency
66
- name: rcov
67
47
  prerelease: false
68
- requirement: &id004 !ruby/object:Gem::Requirement
48
+ version_requirements: *70124285264340
49
+ - !ruby/object:Gem::Dependency
50
+ name: rcov
51
+ requirement: &70124285263920 !ruby/object:Gem::Requirement
69
52
  none: false
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- hash: 3
74
- segments:
75
- - 0
76
- version: "0"
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
77
57
  type: :development
78
- version_requirements: *id004
79
- - !ruby/object:Gem::Dependency
80
- name: yard
81
58
  prerelease: false
82
- requirement: &id005 !ruby/object:Gem::Requirement
59
+ version_requirements: *70124285263920
60
+ - !ruby/object:Gem::Dependency
61
+ name: yard
62
+ requirement: &70124285263480 !ruby/object:Gem::Requirement
83
63
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- hash: 3
88
- segments:
89
- - 0
90
- version: "0"
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
91
68
  type: :development
92
- version_requirements: *id005
69
+ prerelease: false
70
+ version_requirements: *70124285263480
93
71
  description:
94
- email:
72
+ email:
95
73
  - brian@codekitchen.net
96
- executables:
74
+ executables:
97
75
  - ruby-protoc
98
76
  extensions: []
99
-
100
- extra_rdoc_files:
77
+ extra_rdoc_files:
101
78
  - Changelog.md
102
- files:
79
+ files:
103
80
  - .document
104
81
  - .gitignore
105
82
  - Changelog.md
@@ -151,38 +128,29 @@ files:
151
128
  - spec/unicode_string_spec.rb
152
129
  homepage: https://github.com/mozy/ruby-protocol-buffers
153
130
  licenses: []
154
-
155
131
  post_install_message:
156
132
  rdoc_options: []
157
-
158
- require_paths:
133
+ require_paths:
159
134
  - lib
160
- required_ruby_version: !ruby/object:Gem::Requirement
135
+ required_ruby_version: !ruby/object:Gem::Requirement
161
136
  none: false
162
- requirements:
163
- - - ">="
164
- - !ruby/object:Gem::Version
165
- hash: 3
166
- segments:
167
- - 0
168
- version: "0"
169
- required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
142
  none: false
171
- requirements:
172
- - - ">="
173
- - !ruby/object:Gem::Version
174
- hash: 3
175
- segments:
176
- - 0
177
- version: "0"
143
+ requirements:
144
+ - - ! '>'
145
+ - !ruby/object:Gem::Version
146
+ version: 1.3.1
178
147
  requirements: []
179
-
180
148
  rubyforge_project:
181
- rubygems_version: 1.8.11
149
+ rubygems_version: 1.8.10
182
150
  signing_key:
183
151
  specification_version: 3
184
152
  summary: Ruby compiler and runtime for the google protocol buffers library.
185
- test_files:
153
+ test_files:
186
154
  - spec/compiler_spec.rb
187
155
  - spec/fields_spec.rb
188
156
  - spec/negative_int32_spec.rb