module Google::Protobuf

Constants

Any
Api
BoolValue
BytesValue
DescriptorProto
DoubleValue
Duration
Empty
Enum
EnumDescriptorProto
EnumOptions
EnumValue
EnumValueDescriptorProto
EnumValueOptions
ExtensionRangeOptions
FeatureSet
Field
FieldDescriptorProto
FieldMask
FieldOptions
FileDescriptorProto
FileDescriptorSet
FileOptions
FloatValue
GeneratedCodeInfo
Int32Value
Int64Value
ListValue
MessageOptions
Method
MethodDescriptorProto
MethodOptions
Mixin
NullValue
OneofDescriptorProto
OneofOptions
Option
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 71
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 75
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 63
def self.encode(msg, options = {})
  msg.to_proto(options)
end
encode_json(msg, options = {}) click to toggle source
# File lib/google/protobuf.rb, line 67
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 227
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 187
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 134
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 79
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 42
def self.pack(msg, type_url_prefix='type.googleapis.com/')
  any = self.new
  any.pack(msg, type_url_prefix)
  any
end

Public Instance Methods

<<(value) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 213
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 167
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 173
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 219
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 138
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 83
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 193
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 69
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 201
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 48
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 223
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 93
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 181
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 89
def to_i
  self.seconds
end
to_ruby(recursive = false) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 107
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 75
def to_time
  Time.at(seconds, nanos, :nanosecond)
end
type_name() click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 65
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 57
def unpack(klass)
  if self.is(klass) then
    klass.decode(self.value)
  else
    nil
  end
end