Class: ProtocolBuffers::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol_buffers/runtime/field.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: WireFormats Classes: AggregateField, BoolField, BytesField, DoubleField, EnumField, Fixed32Field, Fixed64Field, FloatField, GroupField, Int32Field, Int64Field, MessageField, NumericField, Sfixed32Field, Sfixed64Field, SignedVarintField, Sint32Field, Sint64Field, StringField, Uint32Field, Uint64Field, VarintField

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(otype, name, tag, opts = {}) ⇒ Field

Returns a new instance of Field.



121
122
123
124
125
126
# File 'lib/protocol_buffers/runtime/field.rb', line 121

def initialize(otype, name, tag, opts = {})
  @otype = otype
  @name = name
  @tag = tag
  @opts = opts.dup
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



97
98
99
# File 'lib/protocol_buffers/runtime/field.rb', line 97

def name
  @name
end

#otypeObject (readonly)

Returns the value of attribute otype.



97
98
99
# File 'lib/protocol_buffers/runtime/field.rb', line 97

def otype
  @otype
end

#tagObject (readonly)

Returns the value of attribute tag.



97
98
99
# File 'lib/protocol_buffers/runtime/field.rb', line 97

def tag
  @tag
end

Class Method Details

.create(sender, otype, type, name, tag, opts = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/protocol_buffers/runtime/field.rb', line 102

def self.create(sender, otype, type, name, tag, opts = {})
  if type.is_a?(Symbol)
    klass = Field.const_get("#{type.to_s.capitalize}Field") rescue nil
    raise("Type not found: #{type}") if klass.nil?
    field = klass.new(otype, name, tag, opts)
  elsif type.ancestors.include?(ProtocolBuffers::Enum)
    field = Field::EnumField.new(type, otype, name, tag, opts)
  elsif type.ancestors.include?(ProtocolBuffers::Message)
    if opts[:group]
      field = Field::GroupField.new(type, otype, name, tag, opts)
    else
      field = Field::MessageField.new(type, otype, name, tag, opts)
    end
  else
    raise("Type not found: #{type}")
  end
  return field
end

Instance Method Details

#add_methods_to(klass) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/protocol_buffers/runtime/field.rb', line 184

def add_methods_to(klass)
  add_reader_to(klass)
  add_writer_to(klass)

  if repeated?
    # repeated fields are always "set"
    klass.initial_set_fields[tag] = true

    klass.class_eval "      def has_\#{name}?; true; end\n    EOF\n  else\n    klass.class_eval <<-EOF, __FILE__, __LINE__+1\n      def has_\#{name}?\n        value_for_tag?(\#{tag})\n      end\n    EOF\n  end\nend\n", __FILE__, __LINE__+1

#add_reader_to(klass) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/protocol_buffers/runtime/field.rb', line 128

def add_reader_to(klass)
  if repeated?
    klass.class_eval "    def \#{name}\n      unless @\#{name}\n        @\#{name} = RepeatedField.new(fields[\#{tag}])\n      end\n      @\#{name}\n    end\n    EOF\n  else\n    klass.class_eval <<-EOF, __FILE__, __LINE__+1\n    def \#{name}\n      if @set_fields[\#{tag}] == nil\n        # first access of this field, generate it\n        initialize_field(\#{tag})\n      end\n      @\#{name}\n    end\n    EOF\n  end\nend\n", __FILE__, __LINE__+1

#add_writer_to(klass) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/protocol_buffers/runtime/field.rb', line 151

def add_writer_to(klass)
  if repeated?
    klass.class_eval "      def \#{name}=(value)\n        if value.nil?\n          \#{name}.clear\n        else\n          \#{name}.clear\n          value.each { |i| @\#{name}.push i }\n        end\n      end\n    EOF\n  else\n    klass.class_eval <<-EOF, __FILE__, __LINE__+1\n      def \#{name}=(value)\n        field = fields[\#{tag}]\n        if value.nil?\n          @set_fields[\#{tag}] = false\n          @\#{name} = field.default_value\n        else\n          field.check_valid(value)\n          @set_fields[\#{tag}] = true\n          @\#{name} = value\n          if @parent_for_notify\n            @parent_for_notify.default_changed(@tag_for_notify)\n            @parent_for_notify = @tag_for_notify = nil\n          end\n        end\n      end\n    EOF\n  end\nend\n", __FILE__, __LINE__+1

#check_valid(value) ⇒ Object

Raises:

  • (TypeError)


216
217
218
219
# File 'lib/protocol_buffers/runtime/field.rb', line 216

def check_valid(value)
  raise(TypeError, "can't assign #{value.class.name} to #{self.class.name}") unless valid_type?(value)
  check_value(value)
end

#check_value(value) ⇒ Object



204
205
206
# File 'lib/protocol_buffers/runtime/field.rb', line 204

def check_value(value)
  # pass
end

#deserialize(value) ⇒ Object

the type of value passed in depends on the wire_type of the field: VARINT => Integer (Fixnum or Bignum) FIXED64 => 8-byte string LENGTH_DELIMITED => IO class, make sure to consume all data available FIXED32 => 4-byte string



235
236
237
# File 'lib/protocol_buffers/runtime/field.rb', line 235

def deserialize(value)
  value
end

#inspect_value(value) ⇒ Object



212
213
214
# File 'lib/protocol_buffers/runtime/field.rb', line 212

def inspect_value(value)
  value.inspect
end

#packed?Boolean

Returns:

  • (Boolean)


100
# File 'lib/protocol_buffers/runtime/field.rb', line 100

def packed?; repeated? && @opts[:packed] end

#repeated?Boolean

Returns:

  • (Boolean)


99
# File 'lib/protocol_buffers/runtime/field.rb', line 99

def repeated?; otype == :repeated end

#serialize(value) ⇒ Object

the type of value to return depends on the wire_type of the field: VARINT => Integer FIXED64 => 8-byte string LENGTH_DELIMITED => string FIXED32 => 4-byte string



226
227
228
# File 'lib/protocol_buffers/runtime/field.rb', line 226

def serialize(value)
  value
end

#valid_type?(value) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/protocol_buffers/runtime/field.rb', line 208

def valid_type?(value)
  true
end