module Google::Cloud::Logging::Convert

@private Conversion to/from Logging GRPC objects.

Public Class Methods

array_to_list(array) click to toggle source

@private Convert an Array to a Google::Protobuf::ListValue.

# File lib/google/cloud/logging/convert.rb, line 32
def self.array_to_list array
  # TODO: ArgumentError if array is not an Array
  Google::Protobuf::ListValue.new \
    values: array.map { |o| object_to_value o }
end
hash_to_struct(hash) click to toggle source

@private Convert a Hash to a Google::Protobuf::Struct.

# File lib/google/cloud/logging/convert.rb, line 24
def self.hash_to_struct hash
  # TODO: ArgumentError if hash is not a Hash
  Google::Protobuf::Struct.new \
    fields: Hash[hash.map { |k, v| [String(k), object_to_value(v)] }]
end
map_to_hash(map) click to toggle source

@private Convert a Google::Protobuf::Map to a Hash

# File lib/google/cloud/logging/convert.rb, line 64
def self.map_to_hash map
  if map.respond_to? :to_h
    map.to_h
  else
    # Enumerable doesn't have to_h on ruby 2.0...
    Hash[map.to_a]
  end
end
object_to_value(obj) click to toggle source

@private Convert an Object to a Google::Protobuf::Value.

# File lib/google/cloud/logging/convert.rb, line 40
def self.object_to_value obj
  case obj
  when NilClass
    Google::Protobuf::Value.new null_value: :NULL_VALUE
  when Numeric
    Google::Protobuf::Value.new number_value: obj
  when String
    Google::Protobuf::Value.new string_value: obj
  when TrueClass
    Google::Protobuf::Value.new bool_value: true
  when FalseClass
    Google::Protobuf::Value.new bool_value: false
  when Hash
    Google::Protobuf::Value.new struct_value: hash_to_struct(obj)
  when Array
    Google::Protobuf::Value.new list_value: array_to_list(obj)
  else
    # TODO: Could raise ArgumentError here, or convert to a string
    Google::Protobuf::Value.new string_value: obj.to_s
  end
end