Class: Google::Protobuf::RepeatedField

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Internal::Convert
Includes:
Enumerable, Internal::Convert
Defined in:
lib/google/protobuf/repeated_field.rb,
lib/google/protobuf/ffi/repeated_field.rb

Defined Under Namespace

Classes: ProxyingEnumerator

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Internal::Convert

convert_ruby_to_upb, convert_upb_to_ruby, map_create_hash, message_value_deep_copy, repeated_field_create_array, scalar_create_hash, to_h_internal

Class Method Details

.define_array_wrapper_method(method_name) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/google/protobuf/repeated_field.rb', line 104

def define_array_wrapper_method(method_name)
  define_method(method_name) do |*args, &block|
    arr = self.to_a
    result = arr.send(method_name, *args)
    self.replace(arr)
    return result if result
    return block ? block.call : result
  end
end

.define_array_wrapper_with_result_method(method_name) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/google/protobuf/repeated_field.rb', line 116

def define_array_wrapper_with_result_method(method_name)
  define_method(method_name) do |*args, &block|
    # result can be an Enumerator, Array, or nil
    # Enumerator can sometimes be returned if a block is an optional argument and it is not passed in
    # nil usually specifies that no change was made
    result = self.to_a.send(method_name, *args, &block)
    if result
      new_arr = result.to_a
      self.replace(new_arr)
      if result.is_a?(Enumerator)
        # generate a fresh enum; rewinding the exiting one, in Ruby 2.2, will
        # reset the enum with the same length, but all the #next calls will
        # return nil
        result = new_arr.to_enum
        # generate a wrapper enum so any changes which occur by a chained
        # enum can be captured
        ie = ProxyingEnumerator.new(self, result)
        result = ie.to_enum
      end
    end
    result
  end
end

.new(type, type_class = nil, initial_values = []) ⇒ Object

call-seq:

RepeatedField.new(type, type_class = nil, initial_values = [])

Creates a new repeated field. The provided type must be a Ruby symbol, and an take on the same values as those accepted by FieldDescriptor#type=. If the type is :message or :enum, type_class must be non-nil, and must be the Ruby class or module returned by Descriptor#msgclass or EnumDescriptor#enummodule, respectively. An initial list of elements may also be provided.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 48

def self.new(type, type_class = nil, initial_values = [])
  instance = allocate
  # TODO This argument mangling doesn't agree with the type signature in the comments
  # but is required to make unit tests pass;
  if type_class.is_a?(Enumerable) and initial_values.empty? and ![:enum, :message].include?(type)
    initial_values = type_class
    type_class = nil
  end
  instance.send(:initialize, type, type_class: type_class, initial_values: initial_values)
  instance
end

Instance Method Details

#+(other) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 226

def +(other)
  if other.is_a? RepeatedField
    if type != other.instance_variable_get(:@type) or descriptor != other.instance_variable_get(:@descriptor)
      raise ArgumentError.new "Attempt to append RepeatedField with different element type."
    end
    fuse_arena(other.send(:arena))
    super_set = dup
    other.send(:each_msg_val) do |msg_val|
      super_set.send(:append_msg_val, msg_val)
    end
    super_set
  elsif other.is_a? Enumerable
    super_set = dup
    super_set.push(*other.to_a)
  else
    raise ArgumentError.new "Unknown type appending to RepeatedField"
  end
end

#<<(element) ⇒ Object

Raises:

  • (FrozenError)


155
156
157
158
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 155

def <<(element)
  raise FrozenError if frozen?
  push element
end

#==(other) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 187

def ==(other)
  return true if other.object_id == object_id
  if other.is_a? RepeatedField
    return false unless other.length == length
    each_msg_val_with_index do |msg_val, i|
      other_msg_val = Google::Protobuf::FFI.get_msgval_at(other.send(:array), i)
      unless Google::Protobuf::FFI.message_value_equal(msg_val, other_msg_val, type, descriptor)
        return false
      end
    end
    return true
  elsif other.is_a? Enumerable
    return to_ary == other.to_a
  end
  false
end

#[](*args) ⇒ Object Also known as: at



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 74

def [](*args)
  count = length
  if args.size < 1
    raise ArgumentError.new "Index or range is a required argument."
  end
  if args[0].is_a? Range
    if args.size > 1
      raise ArgumentError.new "Expected 1 when passing Range argument, but got #{args.size}"
    end
    range = args[0]
    # Handle begin-less and/or endless ranges, when supported.
    index_of_first = range.respond_to?(:begin) ? range.begin : range.last
    index_of_first = 0 if index_of_first.nil?
    end_of_range = range.respond_to?(:end) ? range.end : range.last
    index_of_last = end_of_range.nil? ? -1 : end_of_range

    if index_of_last < 0
      index_of_last += count
    end
    unless range.exclude_end? and !end_of_range.nil?
      index_of_last += 1
    end
    index_of_first += count if index_of_first < 0
    length = index_of_last - index_of_first
    return [] if length.zero?
  elsif args[0].is_a? Integer
    index_of_first = args[0]
    index_of_first += count if index_of_first < 0
    if args.size > 2
      raise ArgumentError.new "Expected 1 or 2 arguments, but got #{args.size}"
    end
    if args.size == 1 # No length specified, return one element
      if array.null? or index_of_first < 0 or index_of_first >= count
        return nil
      else
        return convert_upb_to_ruby(Google::Protobuf::FFI.get_msgval_at(array, index_of_first), type, descriptor, arena)
      end
    else
      length = [args[1],count].min
    end
  else
    raise NotImplementedError
  end

  if array.null? or index_of_first < 0 or index_of_first >= count
    nil
  else
    if index_of_first + length > count
      length = count - index_of_first
    end
    if length < 0
      nil
    else
      subarray(index_of_first, length)
    end
  end
end

#[]=(index, value) ⇒ Object

Raises:

  • (FrozenError)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 134

def []=(index, value)
  raise FrozenError if frozen?
  count = length
  index += count if index < 0
  return nil if index < 0
  if index >= count
    resize(index+1)
    empty_message_value = Google::Protobuf::FFI::MessageValue.new # Implicitly clear
    count.upto(index-1) do |i|
      Google::Protobuf::FFI.array_set(array, i, empty_message_value)
    end
  end
  Google::Protobuf::FFI.array_set(array, index, convert_ruby_to_upb(value, arena, type, descriptor))
  nil
end

#clearObject

Raises:

  • (FrozenError)


166
167
168
169
170
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 166

def clear
  raise FrozenError if frozen?
  resize 0
  self
end

#concat(other) ⇒ Object



245
246
247
248
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 245

def concat(other)
  raise ArgumentError.new "Expected Enumerable, but got #{other.class}" unless other.is_a? Enumerable
  push(*other.to_a)
end

#dupObject Also known as: clone



177
178
179
180
181
182
183
184
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 177

def dup
  instance = self.class.allocate
  instance.send(:initialize, type, descriptor: descriptor, arena: arena)
  each_msg_val do |element|
    instance.send(:append_msg_val, element)
  end
  instance
end

#each(&block) ⇒ Object

call-seq:

RepeatedField.each(&block)

Invokes the block once for each element of the repeated field. RepeatedField also includes Enumerable; combined with this method, the repeated field thus acts like an ordinary Ruby sequence.



67
68
69
70
71
72
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 67

def each &block
  each_msg_val do |element|
    yield(convert_upb_to_ruby(element, type, descriptor, arena))
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/google/protobuf/repeated_field.rb', line 92

def empty?
  self.size == 0
end

#first(n = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/google/protobuf/repeated_field.rb', line 58

def first(n=nil)
  if n.nil?
    return self[0]
  elsif n < 0
    raise ArgumentError, "negative array size"
  else
    return self[0...n]
  end
end

#hashObject



218
219
220
221
222
223
224
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 218

def hash
  return_value = 0
  each_msg_val do |msg_val|
    return_value = Google::Protobuf::FFI.message_value_hash(msg_val, type, descriptor, return_value)
  end
  return_value
end

#last(n = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/google/protobuf/repeated_field.rb', line 69

def last(n=nil)
  if n.nil?
    return self[-1]
  elsif n < 0
    raise ArgumentError, "negative array size"
  else
    start = [self.size-n, 0].max
    return self[start...self.size]
  end
end

#lengthObject Also known as: size



172
173
174
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 172

def length
  array.null? ? 0 : Google::Protobuf::FFI.array_size(array)
end

#pop(n = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/google/protobuf/repeated_field.rb', line 81

def pop(n=nil)
  if n
    results = []
    n.times{ results << pop_one }
    return results
  else
    return pop_one
  end
end

#push(*elements) ⇒ Object

Raises:

  • (FrozenError)


150
151
152
153
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 150

def push(*elements)
  raise FrozenError if frozen?
  internal_push(*elements)
end

#replace(replacements) ⇒ Object

Raises:

  • (FrozenError)


160
161
162
163
164
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 160

def replace(replacements)
  raise FrozenError if frozen?
  clear
  push(*replacements)
end

#sliceObject



98
# File 'lib/google/protobuf/repeated_field.rb', line 98

alias_method :slice, :[]

#to_aryObject

call-seq:

RepeatedField.to_ary => array

Used when converted implicitly into array, e.g. compared to an Array. Also called as a fallback of Object#to_a



210
211
212
213
214
215
216
# File 'lib/google/protobuf/ffi/repeated_field.rb', line 210

def to_ary
  return_value = []
  each do |element|
    return_value << element
  end
  return_value
end