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 138
def self.decode(klass, proto)
  klass.decode(proto)
end
decode_json(klass, json, options = {}) click to toggle source
# File lib/google/protobuf.rb, line 142
def self.decode_json(klass, json, options = {})
  klass.decode_json(json, options)
end
encode(msg) click to toggle source
# File lib/google/protobuf.rb, line 130
def self.encode(msg)
  msg.to_proto
end
encode_json(msg, options = {}) click to toggle source
# File lib/google/protobuf.rb, line 134
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 222
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 182
def self.from_hash(hash)
  ret = Struct.new
  hash.each { |key, val| ret[key] = val }
  ret
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 208
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 162
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 168
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 214
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 135
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 85
def from_time(time)
  self.seconds = time.to_i
  self.nanos = time.nsec
end
has_key?(key) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 188
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 196
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 218
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 94
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 176
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 90
def to_i
  self.seconds
end
to_ruby(recursive = false) click to toggle source
# File lib/google/protobuf/well_known_types.rb, line 108
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 76
def to_time
  Time.at(self.to_f)
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