Module: Gcloud::GRPCUtils
- Defined in:
- lib/gcloud/grpc_utils.rb
Class Method Summary collapse
- .hash_to_struct(hash) ⇒ Object
- .object_to_value(obj) ⇒ Object
- .struct_to_hash(struct) ⇒ Object
- .value_to_object(value) ⇒ Object
Class Method Details
.hash_to_struct(hash) ⇒ Object
25 26 27 28 29 |
# File 'lib/gcloud/grpc_utils.rb', line 25 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 |
.object_to_value(obj) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gcloud/grpc_utils.rb', line 59 def self.object_to_value obj case obj when String then Google::Protobuf::Value.new string_value: obj when Array then Google::Protobuf::ListValue.new(values: obj.map { |o| object_to_value(o) }) when Hash then Google::Protobuf::Value.new struct_value: hash_to_struct(obj) when Numeric then Google::Protobuf::Value.new number_value: obj when TrueClass then Google::Protobuf::Value.new bool_value: true when FalseClass then Google::Protobuf::Value.new bool_value: false when NilClass then Google::Protobuf::Value.new null_value: :NULL_VALUE else # We could raise ArgumentError here, or we could convert to a string... Google::Protobuf::Value.new string_value: obj.to_s end end |
.struct_to_hash(struct) ⇒ Object
33 34 35 36 |
# File 'lib/gcloud/grpc_utils.rb', line 33 def self.struct_to_hash struct # TODO: ArgumentError if struct is not a Google::Protobuf::Struct Hash[struct.fields.map { |k, v| [k, value_to_object(v)] }] end |
.value_to_object(value) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gcloud/grpc_utils.rb', line 40 def self.value_to_object value # TODO: ArgumentError if struct is not a Google::Protobuf::Value if value.null_value nil elsif value.number_value value.number_value elsif value.struct_value struct_to_hash value.struct_value elsif value.list_value value.list_value.values.map { |v| value_to_object(v) } elsif !value.bool_value.nil? # Make sure its a bool, not nil value.bool_value else nil # just in case end end |