module Google::Protobuf

Constants

Any
Api
BoolValue
BytesValue
DoubleValue
Duration
Empty
Enum
EnumValue
Field
FieldMask
FloatValue
Int32Value
Int64Value
ListValue
Method
Mixin
NullValue
Option
SourceContext
StringValue
Struct
Syntax
Timestamp
Type
UInt32Value
UInt64Value
Value

Public Class Methods

decode(klass, proto) click to toggle source
# File lib/google/protobuf.rb, line 67
def self.decode(klass, proto)
  klass.decode(proto)
end
decode_json(klass, json) click to toggle source
# File lib/google/protobuf.rb, line 71
def self.decode_json(klass, json)
  klass.decode_json(json)
end
encode(msg) click to toggle source
# File lib/google/protobuf.rb, line 59
def self.encode(msg)
  msg.to_proto
end
encode_json(msg) click to toggle source
# File lib/google/protobuf.rb, line 63
def self.encode_json(msg)
  msg.to_json
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 168
def self.from_hash(hash)
  ret = Struct.new
  hash.each { |key, val| ret[key] = val }
  ret
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 150
def [](key)
  self.fields[key].to_ruby
end
[]=(key, value) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 154
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 123
def from_ruby(value)
  case value
  when NilClass
    self.null_value = 0
  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
end
from_time(time) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 73
def from_time(time)
  self.seconds = time.to_i
  self.nanos = time.nsec
end
is(klass) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 63
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 42
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 82
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 162
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 78
def to_i
  self.seconds
end
to_ruby(recursive = false) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 96
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 69
def to_time
  Time.at(self.to_f)
end
type_name() click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 59
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 51
def unpack(klass)
  if self.is(klass) then
    klass.decode(self.value)
  else
    nil
  end
end