# Cloud Spanner API - Class Google::Cloud::Spanner::Fields (v2.36.0)

2.36.0 (latest) 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.0 2.24.0 2.23.0 2.22.0 2.21.0 2.20.0 2.19.1 2.18.1 2.17.0 2.16.1 2.15.0 2.14.0 2.13.0 2.12.1 2.11.0 Reference documentation and code samples for the Cloud Spanner API class Google::Cloud::Spanner::Fields.

## Fields

Represents the field names and types of data returned by Cloud Spanner.

See [Data
types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).

## Inherits

- Object

## Example

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

results = db.execute_query "SELECT * FROM users"

results.fields.pairs.each do |name, type|
  puts "Column #{name} is type #{type}"
end
```

## Methods

### #\[\]

    def [](key) -> Symbol, nil

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?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields#Google__Cloud__Spanner__Fields_duplicate_names__instance_ "Google::Cloud::Spanner::Fields#duplicate_names? (method)"))
**Parameter**

- **key** (String, Integer) --- The name (String) or zero-based index position (Integer) of the value.
**Returns**

- (Symbol, nil) --- The type code, or nil if no value is found.
**Raises**

- ([Google::Cloud::Spanner::DuplicateNameError](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-DuplicateNameError)) --- if the data contains duplicate names.

### #data

    def data(data) -> Data

**Alias Of** : [#struct](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields#Google__Cloud__Spanner__Fields_struct_instance_) Creates a new [Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") object given the data values matching the fields.
Can be provided as either an Array of values, or a Hash where the hash
keys match the field name or match the index position of the field.


<br />

For more information, see [Data Types - Constructing a
STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
**Parameter**

- **data** (Array, Hash) --- Accepts an array or hash data values.


  Arrays can contain just the data value, nested arrays will be
  treated as lists of values. Values must be provided in the same
  order as the fields, and there is no way to associate values to the
  field names.

  Hash keys must contain the field name as a `Symbol` or `String`, or
  the field position as an `Integer`. Hash values must contain the
  data value. Hash values will be matched to the fields, so they don't
  need to match the same order as the fields.
**Returns**

- ([Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data)) --- A new Data object.
**Examples**

Create a STRUCT value with named fields using Fields object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

named_type = db.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
named_data = named_type.struct(
  { id: 42, name: nil, active: false }
)
```

Create a STRUCT value with anonymous field names:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

anon_type = db.fields [:INT64, :STRING, :BOOL]
anon_data = anon_type.struct [42, nil, false]
```

Create a STRUCT value with duplicate field names:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

dup_type = db.fields [[:x, :INT64], [:x, :STRING], [:x, :BOOL]]
dup_data = dup_type.struct [42, nil, false]
```

### #duplicate_names?

    def duplicate_names?() -> Boolean

Detects duplicate names in the keys for the fields.
**Returns**

- (Boolean) --- Returns `true` if there are duplicate names.

### #initialize

    def initialize(types) -> Fields

Creates [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") object from types. See [Client#fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client#Google__Cloud__Spanner__Client_fields_instance_ "Google::Cloud::Spanner::Client#fields (method)").


This object can be used to create [Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") objects by providing values
that match the field types. See [#struct](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields#Google__Cloud__Spanner__Fields_struct_instance_ "Google::Cloud::Spanner::Fields#struct (method)").

<br />

See [Data Types - Constructing a
STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
**Parameter**

- **types** (Array, Hash) ---

  Accepts an array or hash types.

  Arrays can contain just the type value, or a sub-array of the
  field's name and type value. Hash keys must contain the field name
  as a `Symbol` or `String`, or the field position as an `Integer`.
  Hash values must contain the type value. If a Hash is used the
  fields will be created using the same order as the Hash keys.

  Supported type values include:
  - `:BOOL`
  - `:BYTES`
  - `:DATE`
  - `:FLOAT64`
  - `:FLOAT32`
  - `:NUMERIC`
  - `:INT64`
  - `:STRING`
  - `:TIMESTAMP`
  - `:UUID`
  - `:PROTO`
  - `Array` - Lists are specified by providing the type code in an array. For example, an array of integers are specified as `[:INT64]`.
  - [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") - Nested Structs are specified by providing a Fields object.
**Returns**

- ([Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields)) --- The fields of the given types.
**Examples**

Create a STRUCT value with named fields using Fields object:

```ruby
require "google/cloud/spanner"

named_type = Google::Cloud::Spanner::Fields.new(
  { id: :INT64, name: :STRING, active: :BOOL }
)
named_data = named_type.struct(
  { id: 42, name: nil, active: false }
)
```

Create a STRUCT value with anonymous field names:

```ruby
require "google/cloud/spanner"

anon_type = Google::Cloud::Spanner::Fields.new(
  [:INT64, :STRING, :BOOL]
)
anon_data = anon_type.struct [42, nil, false]
```

Create a STRUCT value with duplicate field names:

```ruby
require "google/cloud/spanner"

dup_type = Google::Cloud::Spanner::Fields.new(
  [[:x, :INT64], [:x, :STRING], [:x, :BOOL]]
)
dup_data = dup_type.struct [42, nil, false]
```

### #keys

    def 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.
**Returns**

- (Array\<(String,Integer)\>) --- An array containing the names (String) or position (Integer) for the corresponding values of the data.

### #new

    def new(data) -> Data

**Alias Of** : [#struct](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields#Google__Cloud__Spanner__Fields_struct_instance_) Creates a new [Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") object given the data values matching the fields.
Can be provided as either an Array of values, or a Hash where the hash
keys match the field name or match the index position of the field.


<br />

For more information, see [Data Types - Constructing a
STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
**Parameter**

- **data** (Array, Hash) --- Accepts an array or hash data values.


  Arrays can contain just the data value, nested arrays will be
  treated as lists of values. Values must be provided in the same
  order as the fields, and there is no way to associate values to the
  field names.

  Hash keys must contain the field name as a `Symbol` or `String`, or
  the field position as an `Integer`. Hash values must contain the
  data value. Hash values will be matched to the fields, so they don't
  need to match the same order as the fields.
**Returns**

- ([Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data)) --- A new Data object.
**Examples**

Create a STRUCT value with named fields using Fields object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

named_type = db.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
named_data = named_type.struct(
  { id: 42, name: nil, active: false }
)
```

Create a STRUCT value with anonymous field names:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

anon_type = db.fields [:INT64, :STRING, :BOOL]
anon_data = anon_type.struct [42, nil, false]
```

Create a STRUCT value with duplicate field names:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

dup_type = db.fields [[:x, :INT64], [:x, :STRING], [:x, :BOOL]]
dup_data = dup_type.struct [42, nil, false]
```

### #pairs

    def pairs() -> Array<Array>

Returns the names or positions and their corresponding types as an
array of arrays.
**Returns**

- (Array\<Array\>) --- An array containing name/position and types pairs.

### #struct

    def struct(data) -> Data

**Aliases**

- [#data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields#Google__Cloud__Spanner__Fields_data_instance_)
- [#new](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields#Google__Cloud__Spanner__Fields_new_instance_)
Creates a new [Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") object given the data values matching the fields.
Can be provided as either an Array of values, or a Hash where the hash
keys match the field name or match the index position of the field.


<br />

For more information, see [Data Types - Constructing a
STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
**Parameter**

- **data** (Array, Hash) --- Accepts an array or hash data values.


  Arrays can contain just the data value, nested arrays will be
  treated as lists of values. Values must be provided in the same
  order as the fields, and there is no way to associate values to the
  field names.

  Hash keys must contain the field name as a `Symbol` or `String`, or
  the field position as an `Integer`. Hash values must contain the
  data value. Hash values will be matched to the fields, so they don't
  need to match the same order as the fields.
**Returns**

- ([Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data)) --- A new Data object.
**Examples**

Create a STRUCT value with named fields using Fields object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

named_type = db.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
named_data = named_type.struct(
  { id: 42, name: nil, active: false }
)
```

Create a STRUCT value with anonymous field names:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

anon_type = db.fields [:INT64, :STRING, :BOOL]
anon_data = anon_type.struct [42, nil, false]
```

Create a STRUCT value with duplicate field names:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

dup_type = db.fields [[:x, :INT64], [:x, :STRING], [:x, :BOOL]]
dup_data = dup_type.struct [42, nil, false]
```

### #to_a

    def to_a() -> Array<Symbol|Array<Symbol>|Fields|Array<Fields>>

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?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields#Google__Cloud__Spanner__Fields_duplicate_names__instance_ "Google::Cloud::Spanner::Fields#duplicate_names? (method)"))
**Returns**

- (Array\<Symbol\|Array\<Symbol\>\|Fields\|Array\<[Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields)\>\>) --- An array containing the type codes.

### #to_h

    def to_h() -> Hash<(Symbol|Integer) => (Symbol|Array<Symbol>|Fields|Array<Fields>)] A hash containing the names or indexes and corresponding types.

Returns the names or indexes and corresponding type codes as a hash.
**Returns**

- (Hash\<(Symbol\|Integer) =\> (Symbol\|Array\<Symbol\>\|Fields\|Array\<[Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields)\>)\] A hash containing the names or indexes and corresponding types.) --- Hash\<(Symbol\|Integer) =\> (Symbol\|Array
**Raises**

- ([Google::Cloud::Spanner::DuplicateNameError](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-DuplicateNameError)) --- if the data contains duplicate names.

### #types

    def types() -> Array<Symbol>

Returns the types of the data.


<br />

See [Data
types](https://cloud.google.com/spanner/docs/data-definition-language#data_types).
**Returns**

- (Array\<Symbol\>) --- An array containing the types of the data.