module Google::Protobuf

Constants

Any
Api
BoolValue
BytesValue
DescriptorProto
DoubleValue
Duration
Edition
Empty
Enum
EnumDescriptorProto
EnumOptions
EnumValue
EnumValueDescriptorProto
EnumValueOptions
ExtensionRangeOptions
FeatureSet
FeatureSetDefaults
Field
FieldDescriptorProto
FieldMask
FieldOptions
FileDescriptorProto
FileDescriptorSet
FileOptions
FloatValue
GeneratedCodeInfo
IMPLEMENTATION
Int32Value
Int64Value
ListValue
MessageOptions
Method
MethodDescriptorProto
MethodOptions
Mixin
NullValue
OBJECT_CACHE
OneofDescriptorProto
OneofOptions
Option
PREFER_FFI
SIZEOF_LONG
SIZEOF_VALUE
ServiceDescriptorProto
ServiceOptions
SourceCodeInfo
SourceContext
StringValue
Struct
Syntax
Timestamp
Type
UInt32Value
UInt64Value
UninterpretedOption
Value

Public Class Methods

decode(klass, proto, options = {}) click to toggle source
# File lib/google/protobuf.rb, line 38
def self.decode(klass, proto, options = {})
  klass.decode(proto, options)
end
decode_json(klass, json, options = {}) click to toggle source
# File lib/google/protobuf.rb, line 42
def self.decode_json(klass, json, options = {})
  klass.decode_json(json, options)
end
Google::Protobuf.deep_copy(obj) → copy_of_obj click to toggle source

Performs a deep copy of a RepeatedField instance, a Map instance, or a message object, recursively copying its members.

VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
  VALUE klass = CLASS_OF(obj);
  if (klass == cRepeatedField) {
    return RepeatedField_deep_copy(obj);
  } else if (klass == cMap) {
    return Map_deep_copy(obj);
  } else {
    VALUE new_arena_rb = Arena_new();
    upb_Arena *new_arena = Arena_get(new_arena_rb);
    const upb_MessageDef *m;
    const upb_Message *msg = Message_Get(obj, &m);
    upb_Message *new_msg = Message_deep_copy(msg, m, new_arena);
    return Message_GetRubyWrapper(new_msg, m, new_arena_rb);
  }
}
Google::Protobuf.discard_unknown(msg) click to toggle source

Discard unknown fields in the given message object and recursively discard unknown fields in submessages.

static VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb) {
  const upb_MessageDef *m;
  upb_Message *msg = Message_GetMutable(msg_rb, &m);
  if (!upb_Message_DiscardUnknown(msg, m, 128)) {
    rb_raise(rb_eRuntimeError, "Messages nested too deeply.");
  }

  return Qnil;
}
encode(msg, options = {}) click to toggle source
# File lib/google/protobuf.rb, line 30
def self.encode(msg, options = {})
  msg.to_proto(options)
end
encode_json(msg, options = {}) click to toggle source
# File lib/google/protobuf.rb, line 34
def self.encode_json(msg, options = {})
  msg.to_json(options)
end
from_a(arr) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 204
def self.from_a(arr)
  ret = ListValue.new
  arr.each { |val| ret << val }
  ret
end
from_hash(hash) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 164
def self.from_hash(hash)
  ret = Struct.new
  hash.each { |key, val| ret[key] = val }
  ret
end
from_ruby(value) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 111
def self.from_ruby(value)
  self.new.from_ruby(value)
end
from_time(time) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 56
def self.from_time(time)
  new.from_time(time)
end
pack(msg, type_url_prefix='type.googleapis.com/') click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 19
def self.pack(msg, type_url_prefix='type.googleapis.com/')
  any = self.new
  any.pack(msg, type_url_prefix)
  any
end

Private Class Methods

cache_implementation() click to toggle source
# File lib/google/protobuf/ffi/object_cache.rb, line 19
def self.cache_implementation
  if interpreter_supports_non_finalized_keys_in_weak_map? and SIZEOF_LONG >= SIZEOF_VALUE
    Google::Protobuf::Internal::ObjectCache
  else
    Google::Protobuf::Internal::LegacyObjectCache
  end
end
interpreter_supports_non_finalized_keys_in_weak_map?() click to toggle source
# File lib/google/protobuf/ffi/object_cache.rb, line 15
def self.interpreter_supports_non_finalized_keys_in_weak_map?
  ! defined? JRUBY_VERSION
end

Public Instance Methods

<<(value) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 190
def <<(value)
  wrapper = Google::Protobuf::Value.new
  wrapper.from_ruby(value)
  self.values << wrapper
end
[](key) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 144
def [](key)
  self.fields[key].to_ruby
rescue NoMethodError
  nil
end
[]=(key, value) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 150
def []=(key, value)
  unless key.is_a?(String)
    raise UnexpectedStructType, "Struct keys must be strings."
  end
  self.fields[key] ||= Google::Protobuf::Value.new
  self.fields[key].from_ruby(value)
end
each() { |to_ruby| ... } click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 196
def each
  self.values.each { |x| yield(x.to_ruby) }
end
from_ruby(value) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 115
def from_ruby(value)
  case value
  when NilClass
    self.null_value = :NULL_VALUE
  when Numeric
    self.number_value = value
  when String
    self.string_value = value
  when TrueClass
    self.bool_value = true
  when FalseClass
    self.bool_value = false
  when Struct
    self.struct_value = value
  when Hash
    self.struct_value = Struct.from_hash(value)
  when ListValue
    self.list_value = value
  when Array
    self.list_value = ListValue.from_a(value)
  else
    raise UnexpectedStructType
  end

  self
end
from_time(time) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 60
def from_time(time)
  self.seconds = time.to_i
  self.nanos = time.nsec
  self
end
has_key?(key) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 170
def has_key?(key)
  self.fields.has_key?(key)
end
is(klass) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 46
def is(klass)
  return self.type_name == klass.descriptor.name
end
length() click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 178
def length
  self.values.length
end
pack(msg, type_url_prefix='type.googleapis.com/') click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 25
def pack(msg, type_url_prefix='type.googleapis.com/')
  if type_url_prefix.empty? or type_url_prefix[-1] != '/' then
    self.type_url = "#{type_url_prefix}/#{msg.class.descriptor.name}"
  else
    self.type_url = "#{type_url_prefix}#{msg.class.descriptor.name}"
  end
  self.value = msg.to_proto
end
to_a() click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 200
def to_a
  self.values.map { |x| x.to_ruby(true) }
end
to_f() click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 70
def to_f
  self.seconds + (self.nanos.quo(1_000_000_000))
end
to_h() click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 158
def to_h
  ret = {}
  self.fields.each { |key, val| ret[key] = val.to_ruby(true) }
  ret
end
to_i() click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 66
def to_i
  self.seconds
end
to_ruby(recursive = false) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 84
def to_ruby(recursive = false)
  case self.kind
  when :struct_value
    if recursive
      self.struct_value.to_h
    else
      self.struct_value
    end
  when :list_value
    if recursive
      self.list_value.to_a
    else
      self.list_value
    end
  when :null_value
    nil
  when :number_value
    self.number_value
  when :string_value
    self.string_value
  when :bool_value
    self.bool_value
  else
    raise UnexpectedStructType
  end
end
to_time() click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 52
def to_time
  Time.at(seconds, nanos, :nanosecond)
end
type_name() click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 42
def type_name
  return self.type_url.split("/")[-1]
end
unpack(klass) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 34
def unpack(klass)
  if self.is(klass) then
    klass.decode(self.value)
  else
    nil
  end
end