Module: Google::Cloud::Core::GRPCUtils

Defined in:
lib/google/cloud/datastore/grpc_utils.rb

Overview

This file adds Datastore methods to Core::GRPCUtils.

Constant Summary collapse

PROP_FILTER_OPS =

rubocop:disable all

{ "<"            => :LESS_THAN,
"lt"           => :LESS_THAN,
"<="           => :LESS_THAN_OR_EQUAL,
"lte"          => :LESS_THAN_OR_EQUAL,
">"            => :GREATER_THAN,
"gt"           => :GREATER_THAN,
">="           => :GREATER_THAN_OR_EQUAL,
"gte"          => :GREATER_THAN_OR_EQUAL,
"="            => :EQUAL,
"eq"           => :EQUAL,
"eql"          => :EQUAL,
"~"            => :HAS_ANCESTOR,
"~>"           => :HAS_ANCESTOR,
"ancestor"     => :HAS_ANCESTOR,
"has_ancestor" => :HAS_ANCESTOR,
"has ancestor" => :HAS_ANCESTOR }

Class Method Summary collapse

Class Method Details

.decode_bytes(bytes) ⇒ Object



133
134
135
# File 'lib/google/cloud/datastore/grpc_utils.rb', line 133

def self.decode_bytes bytes
  Base64.decode64(bytes.to_s).force_encoding Encoding::ASCII_8BIT
end

.encode_bytes(bytes) ⇒ Object



129
130
131
# File 'lib/google/cloud/datastore/grpc_utils.rb', line 129

def self.encode_bytes bytes
  Base64.strict_encode64(bytes.to_s).encode("ASCII-8BIT")
end

.from_value(grpc_value) ⇒ Object

Gets an object from a Google::Datastore::V1::Value.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/google/cloud/datastore/grpc_utils.rb', line 55

def self.from_value grpc_value
  if grpc_value.value_type == :null_value
    return nil
  elsif grpc_value.value_type == :key_value
    return Google::Cloud::Datastore::Key.from_grpc(grpc_value.key_value)
  elsif grpc_value.value_type == :entity_value
    return Google::Cloud::Datastore::Entity.from_grpc(
      grpc_value.entity_value)
  elsif grpc_value.value_type == :boolean_value
    return grpc_value.boolean_value
  elsif grpc_value.value_type == :double_value
    return grpc_value.double_value
  elsif grpc_value.value_type == :integer_value
    return grpc_value.integer_value
  elsif grpc_value.value_type == :string_value
    return grpc_value.string_value
  elsif grpc_value.value_type == :array_value
    return Array(grpc_value.array_value.values).map { |v| from_value v }
  elsif grpc_value.value_type == :timestamp_value
    return Time.at grpc_value.timestamp_value.seconds,
                   grpc_value.timestamp_value.nanos/1000.0
  elsif grpc_value.value_type == :geo_point_value
    return grpc_value.geo_point_value.to_hash
  elsif grpc_value.value_type == :blob_value
    return StringIO.new(
      grpc_value.blob_value.dup.force_encoding("ASCII-8BIT"))
  else
    nil
  end
end

.to_prop_filter_op(op) ⇒ Object

Get a property filter operator from op



49
50
51
# File 'lib/google/cloud/datastore/grpc_utils.rb', line 49

def self.to_prop_filter_op op
  PROP_FILTER_OPS[op.to_s.downcase] || :EQUAL
end

.to_value(value) ⇒ Object

Stores an object into a Google::Datastore::V1::Value.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/google/cloud/datastore/grpc_utils.rb', line 88

def self.to_value value
  v = Google::Datastore::V1::Value.new
  if NilClass === value
    v.null_value = :NULL_VALUE
  elsif TrueClass === value
    v.boolean_value = true
  elsif FalseClass === value
    v.boolean_value = false
  elsif Integer === value
    v.integer_value = value
  elsif Float === value
    v.double_value = value
  elsif defined?(BigDecimal) && BigDecimal === value
    v.double_value = value
  elsif Google::Cloud::Datastore::Key === value
    v.key_value = value.to_grpc
  elsif Google::Cloud::Datastore::Entity === value
    value.key = nil # Embedded entities can't have keys
    v.entity_value = value.to_grpc
  elsif String === value
    v.string_value = value
  elsif Array === value
    v.array_value = Google::Datastore::V1::ArrayValue.new(
      values: value.map { |val| to_value val }
    )
  elsif value.respond_to? :to_time
    v.timestamp_value = Google::Protobuf::Timestamp.new(
      seconds: value.to_time.to_i, nanos: value.to_time.nsec)
  elsif value.respond_to?(:to_hash) &&
        value.keys.sort == [:latitude, :longitude]
    v.geo_point_value = Google::Type::LatLng.new(value)
  elsif value.respond_to?(:read) && value.respond_to?(:rewind)
    value.rewind
    v.blob_value = value.read.force_encoding("ASCII-8BIT")
  else
    fail Google::Cloud::Datastore::PropertyError,
         "A property of type #{value.class} is not supported."
  end
  v
end