Class: Google::Cloud::Spanner::Fields
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Fields
- Defined in:
- lib/google/cloud/spanner/fields.rb
Overview
# Fields
Represents the field names and types of data returned by Cloud Spanner.
See [Data types](cloud.google.com/spanner/docs/data-definition-language#data_types).
Class Method Summary collapse
-
.from_grpc(fields) ⇒ Object
Google::Spanner::V1::Metadata::Row_type::Fields.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](key) ⇒ Symbol?
Returns the type code for the provided name (String) or index (Integer).
- #data(data) ⇒ Object (also: #new)
-
#duplicate_names? ⇒ Boolean
Detects duplicate names in the keys for the fields.
-
#initialize(types) ⇒ Fields
constructor
A new instance of Fields.
- #inspect ⇒ Object
-
#keys ⇒ Array<(String,Integer)>
Returns the names of the data values, or in cases in which values are unnamed, the zero-based index position of values.
-
#pairs ⇒ Array<Array>
Returns the names or positions and their corresponding types as an array of arrays.
-
#to_a ⇒ Array<Symbol>
Returns the type codes as an array.
-
#to_h ⇒ Hash<(String,Integer)=>Symbol>
Returns the names or indexes and corresponding type codes as a hash.
- #to_s ⇒ Object
-
#types ⇒ Array<Symbol>
Returns the types of the data.
Constructor Details
#initialize(types) ⇒ Fields
Returns a new instance of Fields.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/google/cloud/spanner/fields.rb', line 45 def initialize types @fields = [] if types.is_a? Array types.each do |type| @fields << field(type) end elsif types.is_a? Hash types.each do |type| @fields << field(type) end else fail ArgumentError, "can only accept Array or Hash" end end |
Class Method Details
.from_grpc(fields) ⇒ Object
Google::Spanner::V1::Metadata::Row_type::Fields.
220 221 222 223 224 |
# File 'lib/google/cloud/spanner/fields.rb', line 220 def self.from_grpc fields new([]).tap do |f| f.instance_variable_set :@fields, fields end end |
Instance Method Details
#==(other) ⇒ Object
195 196 197 198 |
# File 'lib/google/cloud/spanner/fields.rb', line 195 def == other return false unless other.is_a? Fields pairs == other.pairs end |
#[](key) ⇒ Symbol?
Returns the type code for the provided name (String) or index (Integer). Do not pass a name to this method if the data has more than one member with the same name. (See #duplicate_names?)
135 136 137 138 139 140 141 142 |
# File 'lib/google/cloud/spanner/fields.rb', line 135 def [] key return types[key] if key.is_a? Integer name_count = @fields.find_all { |f| f.name == String(key) }.count return nil if name_count == 0 fail DuplicateNameError if name_count > 1 index = @fields.find_index { |f| f.name == String(key) } types[index] end |
#data(data) ⇒ Object Also known as: new
186 187 188 189 190 191 |
# File 'lib/google/cloud/spanner/fields.rb', line 186 def data data # TODO: match order of types data = data.values if data.is_a?(Hash) values = data.map { |datum| Convert.raw_to_value datum } Data.from_grpc values, @fields end |
#duplicate_names? ⇒ Boolean
Detects duplicate names in the keys for the fields.
107 108 109 |
# File 'lib/google/cloud/spanner/fields.rb', line 107 def duplicate_names? keys.count != keys.uniq.count end |
#inspect ⇒ Object
213 214 215 |
# File 'lib/google/cloud/spanner/fields.rb', line 213 def inspect "#<#{self.class.name} #{self}>" end |
#keys ⇒ Array<(String,Integer)>
Returns the names of the data values, or in cases in which values are unnamed, the zero-based index position of values.
92 93 94 95 96 97 98 99 100 |
# File 'lib/google/cloud/spanner/fields.rb', line 92 def keys @fields.each_with_index.map do |field, index| if field.name.empty? index else field.name.to_sym end end end |
#pairs ⇒ Array<Array>
Returns the names or positions and their corresponding types as an array of arrays.
118 119 120 |
# File 'lib/google/cloud/spanner/fields.rb', line 118 def pairs keys.zip types end |
#to_a ⇒ Array<Symbol>
Returns the type codes as an array. Do not use this method if the data has more than one member with the same name. (See #duplicate_names?)
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/google/cloud/spanner/fields.rb', line 153 def to_a keys.count.times.map { |i| self[i] }.map do |field| if field.is_a? Fields field.to_h elsif field.is_a? Array field.map { |f| f.is_a?(Fields) ? f.to_h : f } else field end end end |
#to_h ⇒ Hash<(String,Integer)=>Symbol>
Returns the names or indexes and corresponding type codes as a hash.
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/google/cloud/spanner/fields.rb', line 171 def to_h fail DuplicateNameError if duplicate_names? hashified_pairs = pairs.map do |key, value| if value.is_a? Fields [key, value.to_h] elsif value.is_a? Array [key, value.map { |v| v.is_a?(Fields) ? v.to_h : v }] else [key, value] end end Hash[hashified_pairs] end |
#to_s ⇒ Object
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/google/cloud/spanner/fields.rb', line 201 def to_s named_types = pairs.map do |key, type| if key.is_a? Integer "#{type.inspect}" else "(#{key})#{type.inspect}" end end "(#{named_types.join ', '})" end |
#types ⇒ Array<Symbol>
Returns the types of the data.
See [Data types](cloud.google.com/spanner/docs/data-definition-language#data_types).
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/google/cloud/spanner/fields.rb', line 68 def types @fields.map(&:type).map do |type| if type.code == :ARRAY if type.array_element_type.code == :STRUCT [Fields.from_grpc(type.array_element_type.struct_type.fields)] else [type.array_element_type.code] end elsif type.code == :STRUCT Fields.from_grpc type.struct_type.fields else type.code end end end |